Pytorch nn.线性运行时错误:mat1尺寸1必须与mat2尺寸0匹配

2022-05-21 06:48:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

以上就是模型。输入图像的形状为1×28×28,批量大小为16。我尝试在展平后打印x.shape,即(161568),完全对应于self.fc1()的输入大小。有人知道吗


Tags: selfnetinitdefnnmaxclasslinearrelupoolpaddingconv2dfc1conv1conv2