pytorch中正确使用损失函数nn.MSELoss

函数参数有reduce和size_average,类型为布尔类型。因为损失函数一般都是计算一个batch的数据,所以返回的结果都是维度为(batchsize, )的向量。

1.如果reduce=false,size_average参数失效,直接返回向量形式的loss。

2.如果reduce=true,那么loss返回的是标量

size_average=true,返回的是loss.mean()

size_average=false,返回的是loss.sum()

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

import torch
import numpy as np

# 返回向量
mse_loss = torch.nn.MSELoss(reduce=False, size_average=False)

v1 = np.array([[1, 2], [3, 4]])
v2 = np.array([[2, 3], [4, 5]])

input1 = torch.autograd.Variable(torch.from_numpy(v1))
target1 = torch.autograd.Variable(torch.from_numpy(v2))

loss = mse_loss(input1.float(), target1.float())
print(loss)

# 返回平均值
v3 = np.array([[1, 2], [3, 4]])
v4 = np.array([[2, 3], [4, 4]])

mse_loss = torch.nn.MSELoss(reduce=True, size_average=False)

input2 = torch.autograd.Variable(torch.from_numpy(v3))
target2 = torch.autograd.Variable(torch.from_numpy(v4))

loss = mse_loss(input2.float(), target2.float())
print(loss)
tensor([[1., 1.],
        [1., 1.]])

tensor(3.)

 

你可能感兴趣的:(pytorch)