Cuda:0 device type tensor to numpy problem for plotting graph
as mentioned in the title, I am facing the problem of
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
I found out that that need to be a .cpu() method to overcome the problem, but tried various ways and still unable to solve the problem
def plot(val_loss,train_loss,typ):
    plt.title(&quot;{} after epoch: {}&quot;.format(typ,len(train_loss)))
    plt.xlabel(&quot;Epoch&quot;)
    plt.ylabel(typ)
    plt.plot(list(range(len(train_loss))),train_loss,color=&quot;r&quot;,label=&quot;Train &quot;+typ)
    plt.plot(list(range(len(val_loss))),val_loss,color=&quot;b&quot;,label=&quot;Validation &quot;+typ)
    plt.legend()
    plt.savefig(os.path.join(data_dir,typ+&quot;.png&quot;))
    plt.close()


I guess during loss calculation, when you try to save the loss, instead of
train_loss.append(loss)

it should be
train_loss.append(loss.item())

item() returns the value of the tensor as a standard Python number, therefore, train_loss will be a list of numbers and you will be able to plot it.
You can read more about item() here
https://pytorch.org/docs/stable/generated/torch.Tensor.item.html
