RuntimeError: mat1 and mat2 shapes cannot be multiplied (25x340 and 360x1)

RuntimeError: mat1 and mat2 shapes cannot be multiplied (25x340 and 360x1)

我收到此错误消息,但不确定原因。我的输入是 (batch, 1, 312) 来自表格数据,这个 CNN 是为回归预测而构建的。我使用公式 (input + 2*padding - filter size)/stride + 1 计算出每一步的形状,如下面的评论所示。问题似乎出现在 x = self.fc(x),我不明白为什么。非常感谢您的帮助。谢谢。

class CNNWeather(nn.Module):
    # input (batch, 1, 312)
    def __init__(self):
        super(CNNWeather, self).__init__()
        self.conv1 = nn.Conv1d(in_channels=1, out_channels=8, kernel_size=9, stride=1, padding='valid')         # (312+2*0-9)/1 + 1 = 304
        self.pool1 = nn.AvgPool1d(kernel_size=2, stride=2)                                                      # 304/2 = 302
        self.conv2 = nn.Conv1d(in_channels=8, out_channels=12, kernel_size=3, stride=1, padding='valid')        # (302-3)/1+1 = 300
        self.pool2 = nn.AvgPool1d(kernel_size=2, stride=2)                                                      # 300/2 = 150
        self.conv3 = nn.Conv1d(in_channels=12, out_channels=16, kernel_size=3, stride=1, padding='valid')       # (150-3)/1+1 = 76
        self.pool3 = nn.AvgPool1d(kernel_size=2, stride=2)                                                      # 76/2 = 38
        self.conv4 = nn.Conv1d(in_channels=16, out_channels=20, kernel_size=3, stride=1, padding='valid')       # (38-3)/1+1 = 36
        self.pool4 = nn.AvgPool1d(kernel_size=2, stride=2)                                                      # 36/2 = 18  (batch, 20, 18)
        self.fc = nn.Linear(in_features=20*18, out_features=1)

    def forward(self, x):
        x = self.pool1(F.relu(self.conv1(x)))
        x = self.pool2(F.relu(self.conv2(x)))
        x = self.pool3(F.relu(self.conv3(x)))
        x = self.pool4(F.relu(self.conv4(x)))
        print(x.size())
        x = x.view(x.size(0), -1)               # flatten   (batch, 20*18)
        x = self.fc(x)
        return x

问题好像和你的FC层的输入大小有关:

self.fc = nn.Linear(in_features=20*18, out_features=1)

上一层的输出是340,所以必须用in_features=340

这些是第三层和第四层的输出形状。

torch.Size([5, 16, 73]) conv3 out
torch.Size([5, 16, 36]) pool3 out
torch.Size([5, 20, 34]) conv4 out
torch.Size([5, 20, 17]) pool4 out

请注意,从“pool4”层出来的是 20x17,这意味着 340 个元素。