问题描述:
device = "cuda:0"
y_hat,hidden = my_LSTM(test_x.to(device))#这里加to(device)的原因在于为在GPU上,test_x是在CPU上的,所以需要统一到GPU上面
y_hat = y_hat[:, -1, :].squeeze(-1)
# print(len(predict_result))
print("--------------------------")
print(y_hat.device)
visualize(len(test_x)-1, y_hat.detach(), test_y,y_label='Loss')#y_hat.cpu()是因为经过核查,这个结果是在GPU上,为了保证统一在一起,需要
# 将其换到CPU
TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
解决方案:
一句话:使用变量挨个试,看看是在GPU还是CPU上,查到哪个就转换哪个
device = "cuda:0"
y_hat,hidden = my_LSTM(test_x.to(device))#这里加to(device)的原因在于为在GPU上,test_x是在CPU上的,所以需要统一到GPU上面
y_hat = y_hat[:, -1, :].squeeze(-1)
# print(len(predict_result))
print("--------------------------")
print(y_hat.device)
visualize(len(test_x)-1, y_hat.cpu().detach(), test_y,y_label='Loss')#y_hat.cpu()是因为经过核查,这个结果是在GPU上,为了保证统一在一起,需要
# 将其换到CPU
- 详细说明: