因此,我将定义CNN模型如下:
一个卷积+一个隐藏层:在 网络的开始,然后是最大池化层和具有100个神经元的完全连接层。使用5x5本地接受域,跨度为1和2 [0内核。最大池层 应该使用2x2池窗口来组合功能。
输入张量X的大小为:torch.Size [X.size(0),1,28,28]。卷积+池化层的输出的形状为[X.size(0),20,12,12]。对于X的每个示例,此输出必须转换为一维张量,以用作全连接层的输入。
以下是我的模型代码。我将X转换为[X.size(0),20 * 12 * 12]的形状,然后将其馈送到完全连接的层:
def model_2(self, X, hidden_size, class_num):
model = NN.Sequential()
model.add_module("conv1",NN.Conv2d(1,20,5,stride=1))
model.add_module("mp",NN.MaxPool2d(2))
X = model(X)
X = X.view(X.size(0), -1)
print(X.size())
n_x = X.shape[1]
model.add_module("linear1", NN.Linear(n_x, hidden_size))
model.add_module("sigmoid", NN.Sigmoid())
model.add_module("linear2", NN.Linear(hidden_size,class_num))
model.add_module("softmax", NN.LogSoftmax())
return model
但是,运行模型时出现以下错误(F.nll_loss(model(X),Y)错误:
Traceback (most recent call last):
File "pytorch/cnnExercise.py", line 113, in <module>
accuracy = cnn.train_and_evaluate(FLAGS, train_set, test_set)
File "/home/taiman/DL/hw04/pytorch/train_cnn.py", line 210, in train_and_evaluate
loss_func = F.nll_loss(model(X), Y)
File "/home/taiman/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
result = self.forward(*input, **kwargs)
File "/home/taiman/anaconda3/lib/python3.7/site-packages/torch/nn/modules/container.py", line 100, in forward
input = module(input)
File "/home/taiman/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 532, in __call__
result = self.forward(*input, **kwargs)
File "/home/taiman/anaconda3/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 87, in forward
return F.linear(input, self.weight, self.bias)
File "/home/taiman/anaconda3/lib/python3.7/site-packages/torch/nn/functional.py", line 1372, in linear
output = input.matmul(weight.t())
RuntimeError: size mismatch, m1: [2400 x 12], m2: [2880 x 100] at /opt/conda/conda-bld/pytorch_1579022060824/work/aten/src/TH/generic/THTensorMath.cpp:136
如果您查看错误消息的最后一行,则input.matmul(weight.t())中存在张量大小不匹配,其中m1:[2400 x 12],m2:[2880 x 100]。知道我在做什么错吗?预先感谢。