Search code examples
pythonpytorchtorch

How can I overcome PyTorch Tensor plotting problem?


I am a new PyTorch user and here is the code I am playing with.

epochs=20  # train for this number of epochs
losses=[] #to keep track on losses
for i in range(epochs):
    i+=1 #counter
    
    y_pred=model(cat_train,con_train) 
    loss=torch.sqrt(criterion(y_pred,y_train)) 
    losses.append(loss)  # append loss values
    
    if i%10==1: # print out our progress 
        print(f'epoch: {i} loss is {loss}')
    # back propagation
    optimizer.zero_grad() # find the zero gradient 
    loss.backward() #move backward 
    optimizer.step()
plt.plot(range(epochs),losses)

and it gives me the following error:

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

I know the problem is related to the type of the losses with the following kind of rows:

tensor(3.6168, grad_fn=<SqrtBackward0>)

Can you suggest how I can grab the first column (numeric values of this tensor) and make it plottable e.i. an array not a Tensor.


Solution

  • You can use torch.Tensor.item.

    So, replace the statement

    losses.append(loss)
    

    with

    losses.append(loss.item())