【pytorch函数笔记(四)】torch.nn.MSELoss()

import torch.nn as nn
nn.MSELoss(reduction = 'none')

一、torch.nn.MSELoss()介绍
  torch.nn.MSELoss()是一种均方误差损失函数。其公式如下: l n = ( x n − y n ) 2 l_n={(x_n-y_n)}^2 ln=(xnyn)2
  其中, x n x_n xn表示预测值张量, y n y_n yn表示真实值张量。

二、torch.nn.MSELoss()应用
代码:

import torch
import torch.nn as nn

loss = nn.MSELoss(reduction = 'none')
input = torch.tensor([[-0.1514,  0.0744, -1.5716],
        [-0.3198, -1.2424, -1.4921],
        [ 0.5548,  0.8131,  1.0369]], requires_grad=True)
target = torch.tensor([[0., 1., 0.],
        [0., 1., 1.],
        [0., 0., 0.]])
output = loss(input, target)
print(input)					#预测值张量
print(target)					#真实值张量
print(output)					#损失值张量

运行结果:

tensor([[-0.1514,  0.0744, -1.5716],
        [-0.3198, -1.2424, -1.4921],
        [ 0.5548,  0.8131,  1.0369]], requires_grad=True)
tensor([[0., 1., 0.],
        [0., 1., 1.],
        [0., 0., 0.]])
tensor([[0.0229, 0.8567, 2.4699],
        [0.1023, 5.0284, 6.2106],
        [0.3078, 0.6611, 1.0752]], grad_fn=<MseLossBackward>)

你可能感兴趣的:(pytorch,深度学习,pytorch,torch,MSELoss,损失函数)