一 . gpu转至cpu处理
问题描述
RuntimeError: expected device cpu but got device cuda:0
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
期待cpu 型数据,但传入了 cuda类型数据
出错代码
def sleep(self,x):
self.conv = nn.Conv2d(in_channels=output, out_channels=input, kernel_size=4, stride=2, padding=1)
self.bn = nn.BatchNorm1d(out_channels)
self.relu = nn.PReLU()
c1 = self.conv(x) # 报错位置
c1 = self.bn(c1)
c1 = self.relu(c1)
return c1
解决办法
c1 = self.conv(x.cpu()) # 更改为 名称.cpu()
同理,类似的有
x.numpy() 更改为 x.cpu().numpy()
查看变量运行是否在gpu或cpu的命令: print(x.device)