我很困惑为什么在验证层的输出形状后出现形状错误。有人能帮我找出哪里出了问题吗?
根据我所包括的层的总结,错误似乎发生在第6层和第7层之间。但是层6的输出显示出与层7的输入相同的维度。应当注意,误差尺寸6272对应于层3/4的输出。
我收到这个错误:
追踪(最近一次通话(:
File "C:UsersloganSpyder_ProjectCode.py", line 215, in <module>
training_loss[t] = train_loop(trainloader, model, loss_fn, opt)/len(trainloader)
File "C:UsersloganSpyder_ProjectCode.py", line 175, in train_loop
pred = model(X)
File "C:Userslogananaconda3libsite-packagestorchnnmodulesmodule.py", line 1110, in _call_impl
return forward_call(*input, **kwargs)
File "C:Userslogananaconda3libsite-packagestorchnnmodulescontainer.py", line 141, in forward
input = module(input)
File "C:Userslogananaconda3libsite-packagestorchnnmodulesmodule.py", line 1110, in _call_impl
return forward_call(*input, **kwargs)
File "C:Userslogananaconda3libsite-packagestorchnnmoduleslinear.py", line 103, in forward
return F.linear(input, self.weight, self.bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x6272 and 1152x512)
#层摘要
Conv2d-1 [-1, 64, 15, 15] 1,792
ReLU-2 [-1, 64, 15, 15] 0
Conv2d-3 [-1, 128, 7, 7] 73,856
ReLU-4 [-1, 128, 7, 7] 0
MaxPool2d-5 [-1, 128, 3, 3] 0
Flatten-6 [-1, 1152] 0
Linear-7 [-1, 512] 590,336
ReLU-8 [-1, 512] 0
Linear-9 [-1, 340] 174,420
ReLU-10 [-1, 340] 0
Linear-11 [-1, 47] 16,027
=============================================
这是我的代码:
model = nn.Sequential(
Conv2d(3, 64, kernel_size=3, stride=2),
ReLU(),
Conv2d(64, 128, kernel_size=3, stride=2),
ReLU(),
MaxPool2d((2,2), stride=(2,2)),
Flatten(),
Linear(3*3*128, 512),
ReLU(),
Linear(512, 340),
ReLU(),
Linear(340, 47)
)
loss_fn = nn.CrossEntropyLoss()
learning_rate = 0.1
epochs = 15
momen = 0.8
model = model.to(device) #choose one or the other
opt = optim.SGD(model.parameters(), lr=learning_rate, momentum=momen)
def train_loop(dataloader, model, loss_fn, optimizer):
size = len(dataloader.dataset)
training_loss = 0
model.train()
for batch, (X, y) in enumerate(dataloader):
X, y = X.to(device), y.to(device)
pred = model(X)
loss = loss_fn(pred, y)
opt.zero_grad()
loss.backward()
opt.step()
training_loss += loss.item()
return training_loss
training_loss = np.zeros(epochs)
for t in range(epochs):
print(f"Epoch {t+1}\n-------------------------------")
training_loss[t] = train_loop(trainloader, model, loss_fn, opt)/len(trainloader)
print("Done!")
该错误是输入的数学错误。当运行torchsummary时,我给出了(3,32,32(的输入,而训练期间的实际输入是(3,64,64(。因此,当我检查打印输出中各层之间的输出形状时,为什么没有检测到错误。我添加了一个额外的conv2d层,使输出形状达到所需的123,3,3,进入完全连接的层。