繁体   English   中英

Pytorch nn.Linear RuntimeError: mat1 dim 1 必须匹配 mat2 dim 0

[英]Pytorch nn.Linear RuntimeError: mat1 dim 1 must match mat2 dim 0

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 16, 3, padding=1)
        self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
        self.max_pool = nn.MaxPool2d(2)
        self.fc1 = nn.Linear(7*7*32, 128)
        self.fc1 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.max_pool(F.relu(self.conv1(x)))
        x = self.max_pool(F.relu(self.conv2(x)))
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.fc2(x)
        x = F.softmax(x, dim=1)
        return x

以上是model。 输入图像的形状是1×28×28,batch_size 16。我尝试在flatten后打印x.shape,即(16, 1568),完全对应self.fc1()的输入大小。 有人有什么主意吗?

您已将此 function nn.Linear(128, 10)命名为self.fc1
self.fc1 = nn.Linear(128, 10)
请将其更改为self.fc2 = nn.Linear(128, 10)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM