提问人:Amir 提问时间:11/17/2023 最后编辑:Amir 更新时间:11/17/2023 访问量:41
RuntimeError:mat1 和 mat2 形状无法相乘(64x1400 和 200x100)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x1400 and 200x100)
问:
我正在研究一个多变量时间序列预测问题,使用 household_power_consumption.txt 数据集并实现 1D CNN 模型。加载数据的批处理大小为 64,序列长度为 50,特征为 7。预处理完成。下面提供了型号代码,请帮我出了什么问题?与模型的尺寸不匹配。
class CNN_ForecastNet(nn.Module):
def __init__(self):
super(CNN_ForecastNet,self).__init__()
self.conv1d = nn.Conv1d(50,200,kernel_size=1)
self.relu = nn.ReLU(inplace=True)
self.drop_out = nn.Dropout(0.5)
self.max_pooling = nn.MaxPool1d(1)
self.fc1 = nn.Linear(200,100)
self.fc2 = nn.Linear(100,1)
def forward(self,x):
x = self.conv1d(x)
x = self.relu(x)
x = self.drop_out(x)
x = self.max_pooling(x)
x = x.view(x.size(0),-1)
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
model = CNN_ForecastNet()
train_losses = []
valid_losses = []
def Train():
running_loss = .0
model.train()
for idx, (inputs,labels) in enumerate(train_loader):
optimizer.zero_grad()
preds = model(inputs.float())
loss = criterion(preds,labels)
loss.backward()
optimizer.step()
running_loss += loss
train_loss = running_loss/len(train_loader)
train_losses.append(train_loss.detach().numpy())
print(f'train_loss {train_loss}')
def Valid():
running_loss = .0
model.eval()
with torch.no_grad():
for idx, (inputs, labels) in enumerate(test_loader):
optimizer.zero_grad()
preds = model(inputs.float())
loss = criterion(preds,labels)
running_loss += loss
valid_loss = running_loss/len(test_loader)
valid_losses.append(valid_loss.detach().numpy())
print(f'valid_loss {valid_loss}')
epochs = 10
for epoch in range(epochs):
if epoch % 2==0:
print('epochs {}/{}'.format(epoch+1,epochs))
Train()
Valid()
gc.collect()
错误如下所示
Traceback (most recent call last):
Cell In[15], line 73
Train()
Cell In[15], line 41 in Train
preds = model(inputs.float())
File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1501 in _call_impl
return forward_call(*args, **kwargs)
Cell In[15], line 17 in forward
x = self.fc1(x)
File ~\anaconda3\Lib\site-packages\torch\nn\modules\module.py:1501 in _call_impl
return forward_call(*args, **kwargs)
File ~\anaconda3\Lib\site-packages\torch\nn\modules\linear.py:114 in forward
return F.linear(input, self.weight, self.bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x1400 and 200x100)
答:
0赞
khushi-411
11/17/2023
#1
看起来 RuntimeError 是由于维度不匹配造成的。要解决此错误,您必须将输入要素 (in_features) 更改为 1400。self.fc1
希望它能解决您的问题。谢谢!
评论
0赞
Amir
11/17/2023
谢谢你,先生,问题解决了。但是,我有点困惑。在卷积层中,我们的输出大小为 200。但是根据你提到的,我用1400作为全连接层,调整后它就工作了。我想知道为什么我们没有直接将卷积层(即 200)的输出输入到第一个全连接层中?
0赞
khushi-411
11/18/2023
这是因为在调用 之前,您正在使用 size [batch size, 1400] 创建张量,其中 1400 是训练数据的特征数。您可以在创建张量时更改 self.fc1 的in_features或更改输入特征。self.fc1
x.view(x.size(0),-1)
评论