PyTorch 中多输出回归问题的 RMSE 损失
RMSE loss for multi output regression problem in PyTorch
我正在使用 PyTorch 训练 CNN 架构来解决回归问题,其中我的输出是一个包含 20 个值的张量。我计划使用 RMSE 作为模型的损失函数,并尝试使用 PyTorch 的 nn.MSELoss()
并使用 torch.sqrt()
对其求平方根,但在获得 results.I 后感到困惑尽力解释原因。很明显,对于批量大小 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()
。
MSE 损失是误差 平方 的 均值 。您在计算 MSE 后取平方根,因此无法将损失函数的输出与 PyTorch nn.MSELoss()
函数的输出进行比较——它们计算的是不同的值。
但是,您可以使用 nn.MSELoss()
创建自己的 RMSE 损失函数,如下所示:
loss_fn = nn.MSELoss()
RMSE_loss = torch.sqrt(loss_fn(prediction, target))
RMSE_loss.backward()
希望对您有所帮助。
要复制默认的 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 损失意味着均方误差损失。因此,您不必在代码中实现平方根 (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
您可以将其称为 class,类似于 PyTorch 中的任何损失函数。
我正在使用 PyTorch 训练 CNN 架构来解决回归问题,其中我的输出是一个包含 20 个值的张量。我计划使用 RMSE 作为模型的损失函数,并尝试使用 PyTorch 的 nn.MSELoss()
并使用 torch.sqrt()
对其求平方根,但在获得 results.I 后感到困惑尽力解释原因。很明显,对于批量大小 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()
。
MSE 损失是误差 平方 的 均值 。您在计算 MSE 后取平方根,因此无法将损失函数的输出与 PyTorch nn.MSELoss()
函数的输出进行比较——它们计算的是不同的值。
但是,您可以使用 nn.MSELoss()
创建自己的 RMSE 损失函数,如下所示:
loss_fn = nn.MSELoss()
RMSE_loss = torch.sqrt(loss_fn(prediction, target))
RMSE_loss.backward()
希望对您有所帮助。
要复制默认的 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 损失意味着均方误差损失。因此,您不必在代码中实现平方根 (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
您可以将其称为 class,类似于 PyTorch 中的任何损失函数。