How to plot a list of torch.tensors? "RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead."
I am learning Pytorch and was following a tutorial when I came accross this error:
&quot;RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.&quot;
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.(&quot;RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.&quot;)
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=&lt;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!

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.
