我是PyTorch的初学者,通常会构建NN,而且有点卡住。
我有这个CNN架构:
class ConvNet(nn.Module):
def __init__(self, num_classes=10):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(
in_channels=3,
out_channels=16,
kernel_size=3)
self.conv2 = nn.Conv2d(
in_channels=16,
out_channels=24,
kernel_size=4)
self.conv3 = nn.Conv2d(
in_channels=24,
out_channels=32,
kernel_size=4)
self.dropout = nn.Dropout2d(p=0.3)
self.pool = nn.MaxPool2d(2)
self.fc1 = nn.Linear(600, 120)
self.fc2 = nn.Linear(512, 10)
self.final = nn.Softmax(dim=1)
def forward(self, x):
# conv 3 layers
x = F.max_pool2d(F.relu(self.conv1(x)), 2) # output of conv layers
x = self.dropout(x)
x = F.max_pool2d(F.relu(self.conv2(x)), 2) # output of conv layers
x = self.dropout(x)
x = F.max_pool2d(F.relu(self.conv3(x)), 2) # output of conv layers
x = self.dropout(x)
# linear layer
x = F.interpolate(x, size=(600, 120))
x = x.view(x.size(0), -1)
x = self.fc1(x)
return x
但是当我尝试训练图像时,它不起作用,并且出现此错误:
RuntimeError: size mismatch, m1: [16 x 2304000], m2: [600 x 120]
我想添加第二个线性层(self.fc2
)和最后一个SoftMax层(self.final
),但是由于我卡在第一个线性层中,因此无法取得任何进展。
答案 0 :(得分:1)
self.fc1
的输入尺寸必须与展平张量的特征(第二)尺寸匹配。因此,您可以用self.fc1 = nn.Linear(600, 120)
代替self.fc1 = nn.Linear(2304000, 120)
。
请记住,由于您使用的是完全连接的图层,因此模型的输入大小不能不变(与完全卷积网络不同)。如果您在x = x.view(x.size(0), -1)
之前更改通道的大小或空间尺寸(就像您从上一个问题移到了这个问题一样),则self.fc1
的输入尺寸将必须进行相应的更改。