torch.nn.MSELoss

均方损失函数

loss(xi,yi)=(xi−yi)2

有三个可选参数:reduce、size_average、reduction

(1)如果 reduce = False,那么 size_average 参数失效,直接返回向量形式的 loss
(2)如果 reduce = True,那么 loss 返回的是标量

a)如果 size_average = True,返回 loss.mean(),即loss的平均值
b)如果 size_average = False,返回 loss.sum(),loss的和

注意:默认情况下, reduce = True,size_average = True

(3)reduction = ‘none’,直接返回向量形式的 loss
(4) reduction = ‘sum’,返回loss之和
(5) reduction = ''elementwise_mean,返回loss的平均值
(6) reduction = ''mean,返回loss的平均值

验证代码
1.

import torch
import numpy as np
loss_fn = torch.nn.MSELoss(reduce=False, size_average=False)
a=np.array([[1,2],[3,8]])
b=np.array([[5,4],[6,2]])
input = torch.autograd.Variable(torch.from_numpy(a))
target = torch.autograd.Variable(torch.from_numpy(b))
loss = loss_fn(input.float(), target.float())
print(loss)

结果
在这里插入图片描述
2.

import torch
import numpy as np
loss_fn = torch.nn.MSELoss(reduce=False, size_average=True)
a=np.array([[1,2],[3,8]])
b=np.array([[5,4],[6,2]])
input = torch.autograd.Variable(torch.from_numpy(a))
target = torch.autograd.Variable(torch.from_numpy(b))
loss = loss_fn(input.float(), target.float())
print(loss)

结果
在这里插入图片描述
3.

import torch
import numpy as np
a=np.array([[1,2],[3,8]])
b=np.array([[5,4],[6,2]])
loss_fn = torch.nn.MSELoss(reduce=True, size_average=False)
input = torch.autograd.Variable(torch.from_numpy(a))
target = torch.autograd.Variable(torch.from_numpy(b))
loss = loss_fn(input.float(), target.float())
print(loss)

结果
在这里插入图片描述
4.

import torch
import numpy as np
a=np.array([[1,2],[3,8]])
b=np.array([[5,4],[6,2]])
loss_fn = torch.nn.MSELoss(reduce=True, size_average=True)
input = torch.autograd.Variable(torch.from_numpy(a))
target = torch.autograd.Variable(torch.from_numpy(b))
loss = loss_fn(input.float(), target.float())
print(loss)

结果
在这里插入图片描述
5.

import torch
import numpy as np
a=np.array([[1,2],[3,8]])
b=np.array([[5,4],[6,2]])
loss_fn = torch.nn.MSELoss()##reduce=True, size_average=True
input = torch.autograd.Variable(torch.from_numpy(a))
target = torch.autograd.Variable(torch.from_numpy(b))
loss = loss_fn(input.float(), target.float())
print(loss)

结果
在这里插入图片描述
6.

import torch
import numpy as np
loss_fn = torch.nn.MSELoss(reduction = 'none')
a=np.array([[1,2],[3,8]])
b=np.array([[5,4],[6,2]])
input = torch.autograd.Variable(torch.from_numpy(a))
target = torch.autograd.Variable(torch.from_numpy(b))
loss = loss_fn(input.float(), target.float())
# [[16, 4],[9, 36]]
print(loss)

结果
在这里插入图片描述
7.

import torch
import numpy as np
loss_fn = torch.nn.MSELoss(reduction = 'sum')
a=np.array([[1,2],[3,8]])
b=np.array([[5,4],[6,2]])
input = torch.autograd.Variable(torch.from_numpy(a))
target = torch.autograd.Variable(torch.from_numpy(b))
loss = loss_fn(input.float(), target.float())
print(loss) # 65

结果
在这里插入图片描述
8.

import torch
import numpy as np
loss_fn = torch.nn.MSELoss(reduction = 'elementwise_mean')
a=np.array([[1,2],[3,8]])
b=np.array([[5,4],[6,2]])
input = torch.autograd.Variable(torch.from_numpy(a))
target = torch.autograd.Variable(torch.from_numpy(b))
loss = loss_fn(input.float(), target.float())
print(loss) # 16.25

结果
在这里插入图片描述
9.

import torch
import numpy as np
loss_fn = torch.nn.MSELoss(reduction = 'mean')
a=np.array([[1,2],[3,8]])
b=np.array([[5,4],[6,2]])
input = torch.autograd.Variable(torch.from_numpy(a))
target = torch.autograd.Variable(torch.from_numpy(b))
loss = loss_fn(input.float(), target.float())
print(loss) # 16.25

结果
在这里插入图片描述

参考
https://blog.csdn.net/hao5335156/article/details/81029791

你可能感兴趣的:(python)