在训练模型时,numpy需要在CPU上进行计算,如果数据在GPU就会碰到如下错误:
TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor
有时直接加.cpu()会报错:
AttributeError: 'list' object has no attribute 'cpu'
遇到这个问题的解决办法是:
加下面这个句子,可以解决这个问题。
其中,yourdata是你需要转到cpu上的变量。
yourdata = torch.tensor(yourdata).detach().cpu().numpy()