PyTorch中多输出回归问题的均方根误差(RMSE)损失

7

我正在使用PyTorch训练卷积神经网络(CNN)来解决一个回归问题,其中输出是包含20个值的张量。我计划使用均方根误差(RMSE)作为模型的损失函数,并尝试使用PyTorch的nn.MSELoss()函数进行实现,随后使用torch.sqrt()对其进行开方操作,但在获取结果后感到困惑。我将尽力解释原因。显然,对于批次大小bs,我的输出张量的维度将是[bs,20]。我尝试自己实现一个RMSE函数:

   def loss_function (predicted_x , target ):
        loss = torch.sum(torch.square(predicted_x - target) , axis= 1)/(predicted_x.size()[1]) #Taking the mean of all the squares by dividing it with the number of outputs i.e 20 in my case
        loss = torch.sqrt(loss)
        loss = torch.sum(loss)/predicted_x.size()[0]  #averaging out by batch-size
        return loss

但是我的loss_function()输出结果与PyTorch使用nn.MSELoss()实现的不同。我不确定是我的实现有误还是我错误地使用了nn.MSELoss()

2个回答

4

MSE损失是错误平方的平均值。在计算MSE之后,您需要进行平方根运算,因此无法将您的损失函数输出与PyTorch nn.MSELoss()函数的输出进行比较,它们计算的是不同的值。

但是,您可以使用nn.MSELoss()函数创建自己的RMSE损失函数,如下所示:

loss_fn = nn.MSELoss()
RMSE_loss = torch.sqrt(loss_fn(prediction, target))
RMSE_loss.backward()

希望这可以帮到你。

3

为了复制默认的PyTorch均方误差(MSE)损失函数,您需要将loss_function方法更改为以下内容:

def loss_function (predicted_x , target ):
    loss = torch.sum(torch.square(predicted_x - target) , axis= 1)/(predicted_x.size()[1])
    loss = torch.sum(loss)/loss.shape[0]
    return loss

以下是为什么上述方法有效的原因 - MSE Loss 意味着均方误差损失。因此,您无需在代码中实现平方根 (torch.sqrt)。默认情况下,PyTorch 中的损失对于计算损失会对批处理中的所有示例进行平均。因此,该方法中的第二行。

要实现 RMSELoss 并将其集成到您的训练中,您可以这样做:

class RMSELoss(torch.nn.Module):
    def __init__(self):
        super(RMSELoss,self).__init__()

    def forward(self,x,y):
        criterion = nn.MSELoss()
        loss = torch.sqrt(criterion(x, y))
        return loss

你可以像在PyTorch中调用任何损失函数一样调用这个类。


我使用了 torch.sqrt()nn.MSELoss() 来获取 RMSE。我已经在我的问题中更新了这个。然而问题是,在我的原始损失函数中,如果我在批量大小平均化之后再取平方根,它符合 PyTorch 的版本,但如果我像在我的帖子中所做的那样在之前应用它,结果就会不同。 - cronin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接