首页 > 解决方案 > Pytorch、INPUT(正常张量)和 WEIGHT(cuda 张量)不匹配

问题描述

免责声明我知道,这个问题已经问过很多次了,但是我尝试了他们的解决方案,但没有一个对我有用,所以经过所有这些努力,我找不到其他任何东西,最终我不得不再次问。

我正在使用 cnns (PYTORCH) 进行图像分类,我不想在 GPU 上训练它(nvidia gpu,与安装的 cuda/cuda 兼容),我成功地将网络放在上面,但问题出在数据上。

if torch.cuda.is_available():
    device = torch.device("cuda:0") 
    print("Running on the GPU")
    print("Available GPU", torch.cuda.device_count())

Running on the GPU
Available GPU 1
net = Net()
net.to(device)
for epoch in range(2):
    running_loss=0.0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data[0].to(device), data[1].to(device) # putting data on gpu
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        
        running_loss += loss.item()
        if i % 2000 == 1999:
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0
dataiter = iter(testloader)
images, labels = dataiter.next()

print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))

out = net(images)

ERROR
----------------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-81-76c52eabb174> in <module>
----> 1 out = net(images)

~/anaconda3/envs/home/lib/python3.8/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

<ipython-input-57-c74f8361a10b> in forward(self, x)
     11 
     12     def forward(self, x):
---> 13         x = self.pool(F.relu(self.conv1(x)))
     14         x = self.pool(F.relu(self.conv2(x)))
     15         x = x.view(-1, 16*5*5)

~/anaconda3/envs/home/lib/python3.8/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

~/anaconda3/envs/home/lib/python3.8/site-packages/torch/nn/modules/conv.py in forward(self, input)
    351 
    352     def forward(self, input):
--> 353         return self._conv_forward(input, self.weight)
    354 
    355 class Conv3d(_ConvNd):

~/anaconda3/envs/home/lib/python3.8/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
    347                             weight, self.bias, self.stride,
    348                             _pair(0), self.dilation, self.groups)
--> 349         return F.conv2d(input, weight, self.bias, self.stride,
    350                         self.padding, self.dilation, self.groups)
    351 

RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same
inputs.is_cuda

True

和标签相同。

我试过的:

https://stackoverflow.com/q/59013109/13287412 
https://github.com/sksq96/pytorch-summary/issues/57
https://blog.csdn.net/qq_27261889/article/details/86575033
https://blog.csdn.net/public669/article/details/96510293

但到目前为止没有任何效果......

标签: python-3.xpytorchconv-neural-network

解决方案


您的images张量位于 CPU 上,而您的张量位于netGPU 上。即使在评估时,您也希望确保输入张量和模型位于同一设备上,否则您将收到张量数据类型错误。

out = net(images.to(device))

推荐阅读