Pytorch RuntimeError:Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor

**

Pytorch遇到的bug:
**

RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for argument #4 'mat2'
1
RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #4 'mat1
1
TraceBack

RuntimeError                              Traceback (most recent call last)
<ipython-input-1-a2020579d495> in <module>
    297 for epoch in range(START,EPOCHS):
    298     # train for one epoch
--> 299     train(train_loader, model, criterion, optimizer, epoch, scheduler)
    300     # evaluate on validation set
    301     F1 = validate(val_loader, model, criterion,epoch)

<ipython-input-1-a2020579d495> in train(train_loader, model, criterion, optimizer, epoch, scheduler)
     90         #print(input_var.type())
     91         # compute output
---> 92         output = model(input_var)
     93         loss = criterion(output, target_var)
     94         #print(output.data)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

~/model/BALL/t_cnn.py in forward(self, x)
     91         #print(e.size())
     92 
---> 93         x = self.fc1_layer(e)
     94         #print(x.size())
     95         x = self.classifier(x)

~/model/BALL/t_cnn.py in fc1_layer(self, x)
     79         fc = nn.Linear(x_size[1],4096)
     80         #print(x.type())
---> 81         out = fc(x)
     82         return out
     83     def forward(self,x):

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    475             result = self._slow_forward(*input, **kwargs)
    476         else:
--> 477             result = self.forward(*input, **kwargs)
    478         for hook in self._forward_hooks.values():
    479             hook_result = hook(self, input, result)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
     53 
     54     def forward(self, input):
---> 55         return F.linear(input, self.weight, self.bias)
     56 
     57     def extra_repr(self):

~/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1022     if input.dim() == 2 and bias is not None:
   1023         # fused op is marginally faster
-> 1024         return torch.addmm(bias, input, weight.t())
   1025 
   1026     output = input.matmul(weight.t())

RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #4 'mat1'


以上两个bug,都是因为输入和模型参数不在一块导致的,输入在GPU,但是模型参数在CPU,因为mat1表示input,mat2表示weight。所以解决方案当然是把他们都放在GPU上。(第一个是没解决时的错误,第二个是解决了想复现错误时报的错误,有点区别但是本质一样)

这个错误网上很多博客都说类似的问题是因为输入没有在GPU,所有只要给输入的变量input加上.cuda()就可以了。但是在这里输出input的类型其实它早就是torch.cuda.FloatTensor类型了,所以问题可能不是这里。

所以很可能就是模型没有加载到GPU,但是代码中又写了model = model.cuda(),而且从trace back看出只有一层出现了问题,那是不是因为这一层没有加载呢。

class TCNN_Alex(AlexNet):
    def __init__(self,num_classes = 1000):
        super(TCNN_Alex,self).__init__()
    
    def energy_layer(self,x):
        x_size = x.size()
        avgpool = nn.AvgPool2d(kernel_size = x_size[2],stride = 1)
        out = avgpool(x)
        return out
    def fc1_layer(self,x):
        x_size = x.size()
        x = x.view(x.size(0), x_size[1])
        fc = nn.Linear(x_size[1],4096)
        #print(x.type())
        out = fc(x)
        return out
    def forward(self,x):
        x = self.conv1(x)
        #print(x.size())
        x = self.conv2(x)
        #print(x.size())
        x = self.conv3(x)
        #print(x.size())
        e = self.energy_layer(x)
        #print(e.size())     
        x = self.fc1_layer(e)
        #print(x.size())
        x = self.classifier(x)
        #print(x.size())

        return x

从上面的代码可以看出,Linear层是我自己定义的,并不是模型的属性,所以那就是说明它还在CPU中,于是尝试加上cuda(),还有上面energy_layer中的AvgPool2d为了保险起见也加上(实际上这里没有报错,也是迷醉了)。

class TCNN_Alex(AlexNet):
    def __init__(self,num_classes = 1000):
        super(TCNN_Alex,self).__init__()
    
    def energy_layer(self,x):
        x_size = x.size()
        avgpool = nn.AvgPool2d(kernel_size = x_size[2],stride = 1).cuda()
        out = avgpool(x)
        return out
    def fc1_layer(self,x):
        x_size = x.size()
        x = x.view(x.size(0), x_size[1])
        fc = nn.Linear(x_size[1],4096).cuda()
        #print(x.type())
        out = fc(x)
        return out
    def forward(self,x):
        x = self.conv1(x)
        #print(x.size())
        x = self.conv2(x)
        #print(x.size())
        x = self.conv3(x)
        #print(x.size())
        e = self.energy_layer(x)
        #print(e.size())     
        x = self.fc1_layer(e)
        #print(x.size())
        x = self.classifier(x)
        #print(x.size())

        return x


再次运行果然可以了!
--------------------- 
作者:Rayfong_Kwong 
来源:CSDN 
原文:https://blog.csdn.net/BUPT_Kwong/article/details/84927456 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/qq_22194315/article/details/88541595