我非常困惑,为什么我有一个形状错误后,验证了层的输出形状。有人能帮我找出我哪里错了吗?
根据我所包含的层的总结,在第6层和第7层之间似乎出现了错误,但是第6层的输出显示与第7层的输入相同的维度,应该注意的是,误差维度6272与第3/4层的输出相对应。
我收到了这个错误:
回溯(最近一次调用):
File "C:\Users\logan\Spyder_ProjectCode.py", line 215, in <module>
training_loss[t] = train_loop(trainloader, model, loss_fn, opt)/len(trainloader)
File "C:\Users\logan\Spyder_ProjectCode.py", line 175, in train_loop
pred = model(X)
File "C:\Users\logan\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\logan\anaconda3\lib\site-packages\torch\nn\modules\container.py", line 141, in forward
input = module(input)
File "C:\Users\logan\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1110, in _call_impl
return forward_call(*input, **kwargs)
File "C:\Users\logan\anaconda3\lib\site-packages\torch\nn\modules\linear.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!")发布于 2022-04-08 19:09:54
这个错误是输入的数学错误。在运行火炬总结时,当训练期间的实际输入为(3,64,64)时,我给出的输入为(3,32,32)。因此,当我检查打印输出中各层之间的输出形状时,为什么没有检测到错误。我增加了一个额外的conv2d层,使输出形状达到所需的123,3,3,3进入完全连接的层。
https://stackoverflow.com/questions/71759758
复制相似问题