【发布时间】:2020-09-11 09:07:39
【问题描述】:
我正在训练一个 CNN 架构来使用 PyTorch 解决回归问题,其中我的输出是 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()。
【问题讨论】:
标签: python deep-learning pytorch artificial-intelligence loss-function