pytorch中将tensor转换为numpy时报错

阅读 197

2022-03-12

测试集中,对x预测,想把预测结果装进y这个ndarray中

y = rnn(x).to(device)

出错

TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

参考网上,原因大概是无法直接将cuda tensor转换为ndarray,得经过cpu

解决方法:

改为

y = rnn(x).to(device).data.cpu().numpy()

精彩评论(0)

0 0 举报