It’s pretty hard to help you here since I can’t reproduce your error. I would recommend you to use a debugger and look at your loss variable and figure out how to access the loss from what is likely some sort of tensor. The error message indicates that you might want to use a .item() somewhere.
looks like you didn’t cast your val_loss properly. I assume val_loss is a torch tensor - you can try loss = torch.mean(val_loss).numpy() if you get errors related to using a gpu try loss = torch.mean(val_loss).detach().cpu().numpy()
Ok so val_loss is a list so it is not the loss you got directly from PyTorch since it would be a tensor then.
It is impossible to give you a good answer without you sharing minimal code necessary to reproduce this error. If you don’t do that we’re stuck with guessing and that’s quite frankly not satisfying for either of us.
As I see it your code should work if you replace val_loss.append(net.loss.data) with val_loss.append(net.loss.data.numpy()) that way you should get a list of floats instead of a list of tensors which seems to be your issue.
Alrighty - so you still need to get the tensor back from your GPU - Try net.loss.data.cpu().numpy() and if that doesn’t work net.loss.data.detach().cpu().numpy()
Still reported the error in the same place.When I try net.loss.data.cpu().numpy() ,it reports AttributeError:‘builtin_function_or_method’ object has no attribute ‘numpy’ .When I try net.loss.data.detach().cpu().numpy() ,it reports AttributeError:‘builtin_function_or_method’ object has no attribute ‘cpu’ .
Thank you, I finally solved this problem. My method is loss = np.mean(val_loss) .Now I will share this joy with you, thank you again for your constant help.