TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. Error occured using Pytorch
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. Error occured using Pytorch
我在 Whosebug 上阅读过有关相同错误的问题,但不幸的是它们不起作用。
我有一个定义的函数:
def plot_loss(train_loss, validation_loss, title):
plt.grid(True)
plt.xlabel("subsequent epochs")
plt.ylabel('average loss')
plt.plot(range(1, len(train_loss)+1), train_loss, 'o-', label='training')
plt.plot(range(1, len(validation_loss)+1), validation_loss, 'o-', label='validation')
plt.legend()
plt.title(title)
plt.show()
问题是在行
plt.plot(range(1, len(validation_loss)+1), validation_loss, 'o-', label='validation')
发生此错误。然后流经 pyplot.py
中的 gca().plot(...)
,最终流向 tensor.py
中的 return self.numpy()
在测试阶段我定义了以下函数:
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
# calculate and sum up batch loss
test_loss += F.nll_loss(output, target, reduction='mean')
# get the index of class with the max log-probability
prediction = output.argmax(dim=1)
# item() returns value of the given tensor
correct += prediction.eq(target).sum().item()
test_loss /= len(test_loader)
return test_loss
我试过换行
prediction = output.argmax(dim=1)
正如在另一个关于相同错误的问题中所描述的那样,但不幸的是它没有帮助。
我已经尝试 运行 在 Google Colab 以及我的带有 GPU 的本地机器上 运行 这段代码(cuda 可用),但不幸的是发生了同样的错误。
编辑 我设法在这个 link 中找到了解决方案。它似乎与 CUDA 和 CPU 之间的移动数据有关。我调用了 .cpu()
并解决了:
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
# calculate and sum up batch loss
test_loss += F.nll_loss(output, target, reduction='mean')
# get the index of class with the max log-probability
prediction = output.argmax(dim=1)
# item() returns value of the given tensor
correct += prediction.eq(target).sum().item()
test_loss /= len(test_loader)
return test_loss.cpu()
我设法在这个 link 中找到了解决方案。它似乎与 CUDA 和 CPU 之间的移动数据有关。我调用了 .cpu()
并解决了:
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
# calculate and sum up batch loss
test_loss += F.nll_loss(output, target, reduction='mean')
# get the index of class with the max log-probability
prediction = output.argmax(dim=1)
# item() returns value of the given tensor
correct += prediction.eq(target).sum().item()
test_loss /= len(test_loader)
return test_loss.cpu()
我在 Whosebug 上阅读过有关相同错误的问题,但不幸的是它们不起作用。 我有一个定义的函数:
def plot_loss(train_loss, validation_loss, title):
plt.grid(True)
plt.xlabel("subsequent epochs")
plt.ylabel('average loss')
plt.plot(range(1, len(train_loss)+1), train_loss, 'o-', label='training')
plt.plot(range(1, len(validation_loss)+1), validation_loss, 'o-', label='validation')
plt.legend()
plt.title(title)
plt.show()
问题是在行
plt.plot(range(1, len(validation_loss)+1), validation_loss, 'o-', label='validation')
发生此错误。然后流经 pyplot.py
中的 gca().plot(...)
,最终流向 tensor.py
return self.numpy()
在测试阶段我定义了以下函数:
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
# calculate and sum up batch loss
test_loss += F.nll_loss(output, target, reduction='mean')
# get the index of class with the max log-probability
prediction = output.argmax(dim=1)
# item() returns value of the given tensor
correct += prediction.eq(target).sum().item()
test_loss /= len(test_loader)
return test_loss
我试过换行
prediction = output.argmax(dim=1)
正如在另一个关于相同错误的问题中所描述的那样,但不幸的是它没有帮助。
我已经尝试 运行 在 Google Colab 以及我的带有 GPU 的本地机器上 运行 这段代码(cuda 可用),但不幸的是发生了同样的错误。
编辑 我设法在这个 link 中找到了解决方案。它似乎与 CUDA 和 CPU 之间的移动数据有关。我调用了 .cpu()
并解决了:
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
# calculate and sum up batch loss
test_loss += F.nll_loss(output, target, reduction='mean')
# get the index of class with the max log-probability
prediction = output.argmax(dim=1)
# item() returns value of the given tensor
correct += prediction.eq(target).sum().item()
test_loss /= len(test_loader)
return test_loss.cpu()
我设法在这个 link 中找到了解决方案。它似乎与 CUDA 和 CPU 之间的移动数据有关。我调用了 .cpu()
并解决了:
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
# calculate and sum up batch loss
test_loss += F.nll_loss(output, target, reduction='mean')
# get the index of class with the max log-probability
prediction = output.argmax(dim=1)
# item() returns value of the given tensor
correct += prediction.eq(target).sum().item()
test_loss /= len(test_loader)
return test_loss.cpu()