'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("{} after epoch: {}".format(typ,len(train_loss)))
    plt.xlabel("Epoch")
    plt.ylabel(typ)
    plt.plot(list(range(len(train_loss))),train_loss,color="r",label="Train "+typ)
    plt.plot(list(range(len(val_loss))),val_loss,color="b",label="Validation "+typ)
    plt.legend()
    plt.savefig(os.path.join(data_dir,typ+".png"))
    plt.close()


Solution 1:[1]

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

Solution 2:[2]

If you are using the GPU (CUDA) you need to get the data from the GPU to the CPU, doing e.g. :

plt.plot(list(range(len(train_loss))),train_loss.cpu().detach().numpy(),color="r",label="Train "+typ)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anhster
Solution 2 Kilian