pytorch TypeError:无法将cuda:0设备类型Tensor转换为numpy,使用Tensor.cpu()复制

n3h0vuf2  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(121)
plt.plot(train_accuracy, label='training accuracy')
plt.plot(test_accuracy, label='validation accuracy')
plt.title('Accuracy at the end of each epoch')
plt.legend();

错误代码:
TypeError:无法将cuda:0设备类型Tensor转换为numpy。首先使用Tensor.cpu()将Tensor复制到主机内存中。
例如,test_accuracy是[tensor(0.6943,device ='cuda:0',dtype = torch.float64),Tensor(0.6891,device ='cuda:0',dtype = torch.float64),Tensor(0.7409,device ='cuda:0',dtype = torch.float64),Tensor(0.7202,device ='cuda:0',dtype = torch.float64),Tensor(0.7565,device ='cuda:0',dtype = torch.float64),Tensor(0.6891,device ='cuda:0',dtype = torch.float64),Tensor(0.7047,device ='cuda:0',dtype = torch.float64),Tensor(0.7409,device ='cuda:0',dtype = torch.float64),Tensor(0.6943,device ='cuda:0',dtype = torch.float64)]

ifmq2ha2

ifmq2ha21#

如错误所示,您需要首先将Tensor复制到主机内存。
试试看:

train_accuracy = [x.cpu().numpy() for x in train_accuracy]

test_accuracy = [x.cpu().numpy() for x in test_accuracy]

plt.plot(train_accuracy, label='training accuracy')
plt.plot(test_accuracy, label='validation accuracy')
plt.title('Accuracy at the end of each epoch')
plt.legend();

相关问题