【问题标题】:PyTorch Deconvolution Layer input dimension mismatch with Linear LayerPyTorch 反卷积层输入维度与线性层不匹配
【发布时间】:2020-06-11 09:00:54
【问题描述】:

我构建了一个自动编码器,其中包含一些卷积层、中间的全连接层和一些反卷积层。我使用 Atari Frames 作为输入(210x160x3 尺寸)。我目前的问题是我只能处理平方图像(所有边的边长相同)。当我使用原始边长时,会发生此错误:

UserWarning: Using a target size (torch.Size([30, 3, 210, 160])) that is different to the input size (torch.Size([30, 3, 158, 158])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)

我认为问题出在从 linear 到 deconv Layer 的变化,因为张量大小总是变成二次的:

线性:torch.Size([30, 1000]) 反卷积:torch.Size([30, 1000, 1, 1])

但是,我想要一个torch.Size,其形状类似于:反卷积层中的torch.Size([30,1000,11,8])。

我很确定有一个简单的解决方案,但我还没有找到。

forward函数的代码sn-p:

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x = F.relu(self.conv3(x))
        x = F.relu(self.conv4(x))

        x = x.view(x.size(0), -1)
        x = torch.sigmoid(self.lv(x))
        x = x.unsqueeze(-1).unsqueeze(-1)

        x = F.relu(self.deconv1(x))
        x = F.relu(self.deconv2(x))
        x = F.relu(self.deconv3(x))
        x = F.relu(self.deconv4(x))
        x = F.relu(self.deconv5(x))
        return x

非常感谢您!

【问题讨论】:

    标签: python pytorch dimensions deconvolution


    【解决方案1】:

    我解决了! 这一行:

    x = x.view(x.size(0), -1, 11, 8)
    

    代替:

    x = x.unsqueeze(-1).unsqueeze(-1)
    

    会做的。

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 1970-01-01
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多