出现该错误时,提示错误位置在
File "network_pytorch.py", line 99, in forward
x = self.conv(x) # wronged
表示输入的是CPU类型的变量,但是权重是CUDA类型的,即网络和输入不在同一设备上
但是,在输入网络之前,输入已经按照
input = torch.randn([20, 16, 8, 1, 81920])
input.cuda()
转到GPU上了
在https://www.jianshu.com/p/b6e4bbe5b16c上查到,要对tensor使用
t3 = torch.Tensor([2.5], device=torch.device('cuda'))
将变量转到GPU上。
.cuda()和device='cuda'对模型来说作用是一样的,但是变量.cuda()生成的只是变量的副本,不会对其自身改变,要使用变量 = 变量.cuda()或者直接在构造变量的时候使用device=torch.device('cuda')
参考:
https://blog.csdn.net/hyk_1996/article/details/80824747