【问题标题】:Error: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same错误:输入类型(torch.cuda.FloatTensor)和权重类型(torch.FloatTensor)应该相同
【发布时间】:2021-12-06 08:24:56
【问题描述】:

所以我正在尝试制作一个用于众数统计的 CNN 模型,这就是结构。

    self.base = nn.Sequential(Conv2d( 1, 64 ,3, same_padding=True, bn=bn),
                              Conv2d(64, 64 ,3, same_padding=True, bn=bn),
                              nn.MaxPool2d(2),
                              Conv2d( 64, 128 ,3, same_padding=True, bn=bn),
                              Conv2d(128, 128 ,3, same_padding=True, bn=bn))
    
    self.layer1_1 = nn.Sequential(nn.MaxPool2d(2),
                                  Conv2d(128, 256 ,3, same_padding=True, bn=bn))
    self.layer1_2 = nn.Sequential(Conv2d(256, 256 ,3, same_padding=True, bn=bn))
    self.layer1_3 = nn.Sequential(Conv2d(256, 256 ,3, same_padding=True, bn=bn))
    
    self.fuse_layer1 = nn.Sequential(nn.MaxPool2d(2),
                                     Conv2d(256, 256 ,3, same_padding=True, bn=bn))
    
    
    self.layer2_1 = nn.Sequential(nn.MaxPool2d(2),
                                  Conv2d(256, 512 ,3, same_padding=True, bn=bn))
    self.layer2_2 = nn.Sequential(Conv2d(512, 512 ,3, same_padding=True, bn=bn))
    self.layer2_3 = nn.Sequential(Conv2d(512, 512 ,3, same_padding=True, bn=bn))
                                
    self.fuse_layer2 = nn.Sequential(Conv2d(512, 256 ,3, same_padding=True, bn=bn))
    
    
    self.layer3_1 = nn.Sequential(nn.MaxPool2d(2),
                                  Conv2d(512, 512 ,3, same_padding=True, bn=bn))
    self.layer3_2 = nn.Sequential(Conv2d(512, 512 ,3, same_padding=True, bn=bn))
    self.layer3_3 = nn.Sequential(Conv2d(512, 512 ,3, same_padding=True, bn=bn))
    
    self.fuse_layer3 = nn.Sequential(Conv2d(512, 256 ,3, same_padding=True, bn=bn))
    
    
    self.fuse_layers = nn.Sequential(Conv2d(768, 1 ,1, same_padding=True, bn=bn))

在 forward 方法中加入层时:

def forward(self, im_data):
   
    base_layer = self.base(im_data)    
    l1_1 = self.layer1_1(base_layer)
    l1_2 = self.layer1_2(l1_1)
    l1_3 = self.layer1_3(l1_2)
    fuse_l1 = self.fuse_layer1(l1_1.add(l1_2).add(l1_3))
    print(fuse_l1.size())
    
    l2_1 = self.layer2_1(l1_3)
    l2_2 = self.layer2_2(l2_1)
    l2_3 = self.layer2_3(l2_2)
    fuse_l2 = self.fuse_layer2(l2_1.add(l2_2).add(l2_3))
    print(fuse_l2.size())
    
    l3_1 = self.layer3_1(l2_3)
    l3_2 = self.layer3_2(l3_1)
    l3_3 = self.layer3_3(l3_2)
    fuse_l3 = self.fuse_layer3(l3_1.add(l3_2).add(l3_3))
    upsample = nn.ConvTranspose2d(256, 256, 3, stride=2, padding=1)
    fuse_l3 = upsample(fuse_l3, output_size = fuse_l2.size())
    print(fuse_l3.size())
    
    fuse_all = self.fuse_layers(torch.cat((fuse_l1,fuse_l2,fuse_l3),1))
    
    
    return fuse_all

The important this of all of this is fuse_l1, fuse_l2 and fuse_l3, these 3 are torch tensors and here are their sizes:

我一直在寻找放大这个张量的方法,发现 ConvTranspose2d 可以完成这项工作。

upsample = nn.ConvTranspose2d(256, 256, 3, stride=2, padding=1)
fuse_l3 = upsample(fuse_l3, output_size = fuse_l2.size())

我已经使用虚拟张量对此进行了测试并且工作正常,但是使用这些真实张量会发现错误: “输入类型(torch.cuda.FloatTensor)和权重类型(torch.FloatTensor)应该相同”

任何想法为什么会发生这种情况?

【问题讨论】:

    标签: python tensorflow machine-learning conv-neural-network


    【解决方案1】:

    当您将张量发送到 gpu 而不是模型时,通常会出现此错误。试试

    device = torch.device('cuda')`
    model.to(device)
    

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 2020-09-29
      • 2021-01-03
      • 2023-03-27
      • 2022-11-11
      • 2021-07-19
      • 1970-01-01
      • 2023-04-10
      • 2020-08-05
      相关资源
      最近更新 更多