【发布时间】:2020-06-11 23:09:21
【问题描述】:
我是 PyTorch 的初学者,一般都在构建神经网络,但我有点卡住了。
我有这个 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),但由于我被困在第一个线性层,我无法取得任何进展。
【问题讨论】:
-
在我的 DataLoader 中写成 batch_size=16
-
我不确定,你能告诉我如何找到这个吗?
-
好的。我在第一层之前和每一层之后添加了一行
print(x.shape)。所以我总共有 5 条打印线,输出如下:torch.Size([16, 3, 256, 256])、torch.Size([16, 16, 127, 127])、torch.Size([16, 24, 62, 62])、torch.Size([16, 32, 29, 29])、torch.Size([16, 2304000]) -
好的,我刚改了。以下是新的输出:
torch.Size([16, 3, 256, 256])、torch.Size([16, 16, 127, 127])、torch.Size([16, 24, 62, 62])、torch.Size([16, 32, 30, 30])、torch.Size([16, 2304000])
标签: python tensorflow neural-network artificial-intelligence pytorch