【Pytorch基础】torch.nn.MSELoss损失函数

MSE: Mean Squared Error(均方误差
含义:均方误差,是预测值与真实值之差的平方和的平均值,即:
M S E = 1 N ∑ i = 1 n ( x i − y i ) 2 \begin{aligned} MSE =\cfrac {1}{N}\sum_{i=1}^n(x_i-y_i)^2 \end{aligned} MSE=N1i=1n(xiyi)2
  但是,在具体的应用中跟定义稍有不同。主要差别是参数的设置,在torch.nn.MSELoss中有一个reduction参数。reduction是维度要不要缩减以及如何缩减主要有三个选项:

  • ‘none’:no reduction will be applied.
  • ‘mean’: the sum of the output will be divided by the number of elements in the output.
  • ‘sum’: the output will be summed.

  如果不设置reduction参数,默认是’mean’
下面看个例子:

import torch
import torch.nn as nn
 
a = torch.tensor([[1, 2], 
				  [3, 4]], dtype=torch.float)
				  
b = torch.tensor([[3, 5], 
				  [8, 6]], dtype=torch.float)
 
loss_fn1 = torch.nn.MSELoss(reduction='none')
loss1 = loss_fn1(a.float(), b.float())
print(loss1)   # 输出结果:tensor([[ 4.,  9.],
               #                 [25.,  4.]])
 
loss_fn2 = torch.nn.MSELoss(reduction='sum')
loss2 = loss_fn2(a.float(), b.float())
print(loss2)   # 输出结果:tensor(42.)
 
 
loss_fn3 = torch.nn.MSELoss(reduction='mean')
loss3 = loss_fn3(a.float(), b.float())
print(loss3)   # 输出结果:tensor(10.5000)

  在loss1中是按照原始维度输出,即对应位置的元素相减然后求平方;loss2中是对应位置求和;loss3中是对应位置求和后取平均。
  除此之外,torch.nn.MSELoss还有一个妙用,求矩阵的F范数(F范数详解)当然对于所求出来的结果还需要开方。

参考文献

[1]pytorch的nn.MSELoss损失函数
[2]状态估计的基本概念(3)最小均方估计和最小均方误差估计

猜你喜欢

转载自blog.csdn.net/zfhsfdhdfajhsr/article/details/115637954
今日推荐