Home > Enterprise >  How to plot a list of torch.tensors? "RuntimeError: Can't call numpy() on Tensor that requ
How to plot a list of torch.tensors? "RuntimeError: Can't call numpy() on Tensor that requ

Time:10-13

I am learning Pytorch and was following a tutorial when I came accross this error: "RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead."

I am adding my losses to a list called final_losses

for i in range(epochs):
    y_pred=model.forward(X_train)
    loss=loss_function(y_pred,y_train)
    final_losses.append(loss)

This is a simple ANN module having 2 fully connected layers and I use Relu function in them. I am trying to print a graph of epochs vs loss:

plt.plot(range(epochs),final_losses)
plt.show()

When I execute this I am getting the above error.("RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.")

I have printed the these variables for your reference:

Epochs is 150, length of final_losses is 150 and final_losses is [tensor(1.5851, grad_fn=<NllLossBackward0),...

I also tried doing this :

plt.plot(range(epochs),torch.detach(final_losses).numpy())
plt.show()

I am getting the following error: TypeError: detach(): argument 'input' (position 1) must be Tensor, not list

Please let me know how to solve this. Thank you!

CodePudding user response:

You have a list of tensors, rather than a tensor. Modify your initial code to store your losses as numpy arrays (or singular floats by taking the mean):

for i in range(epochs):
    y_pred=model.forward(X_train)
    loss=loss_function(y_pred,y_train)
    final_losses.append(loss.detach().numpy())

If you are on gpu you might also need to convert to cpu in that last line.

  • Related