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()的输入大小。有人知道吗
您已将此函数
nn.Linear(128, 10)
命名为self.fc1
i、 e
self.fc1 = nn.Linear(128, 10)
请将其更改为
self.fc2 = nn.Linear(128, 10)
编程相关推荐