在运行PyTorch NN的forward()方法时输出的不同形状



你好,我想理解以下事情:

我使用PyTorch创建了以下神经网络模型来运行回归任务。

class Model(nn.Module):
def __init__(self, in_features, h1, h2, out_features=0):
super(Model, self).__init__()
self.fc1 = nn.Linear(in_features,h1)    # input layer
self.fc2 = nn.Linear(h1, h2)            # hidden layer
self.out = nn.Linear(h2, out_features)  # output layer

def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.out(x)
return x
model = Model(in_features=59, h1=64, h2=32, out_features=1)

然后我们进入训练,我运行以下代码:

epochs = 300
losses = []
for i in range(epochs):

y_pred = model(X_train)

loss = criterion(y_pred, y_train)
losses.append(loss.detach().numpy())

optimizer.zero_grad()
loss.backward()
optimizer.step()

一切都很好,但通过模型的forward()方法,我的y_pred得到形状[1359, 1](我猜它应该是[1359],因为我的y_train匹配这个形状,我得到以下警告:

C:Usershpanaconda3libsite-packagestorchnnmodulesloss.py:528: UserWarning: Using a target size (torch.Size([1359])) that is different to the input size (torch.Size([1359, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.mse_loss(input, target, reduction=self.reduction)

当我尝试评估我的模型

时也会发生这种情况
with torch.no_grad():

y_val = model(X_test)

loss = criterion(y_val.flatten(), y_test)

print(loss)

确实你有一个形状不匹配,你的模型将输出形状为(batch_size, 1)的张量,而你的目标形状为(batch_size,)。你必须明确地传播你的张量,这样你的准则的输入才有形状。

通过重塑预测y_val本身:

>>> loss = criterion(y_val[:,0], y_test)
或者目标张量y_test:
>>> loss = criterion(y_val, y_test[:,None])

最新更新