运行时错误:mat1 和 mat2 形状无法相乘(25x340 和 360x1)

发布于 2025-01-13 05:00:42 字数 1824 浏览 1 评论 0原文

我收到此错误消息,但不知道为什么。我的输入是来自表格数据的 (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

I get this error message and I'm not sure why. My input is (batch, 1, 312) from tabular data and this CNN is constructed for a regression prediction. I worked out the shapes for each step with the formula (input + 2*padding - filter size)/stride + 1 as in the comment below. The problem appears to occur at x = self.fc(x) and I can't figure out why. Your help is greatly appreciated. Thank you.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2025-01-20 05:00:42

该问题似乎与 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 个元素。

The problem seems to be related to the input size of your FC layer:

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

The output of the previous layer is 340, so you must use in_features=340.

These are the shapes of the output for the third and fourth layers.

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

Notice that out of the "pool4" layer come 20x17, meaning 340 elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文