这里 loss, x, y 的维度是一样的,可以是向量或者矩阵,i 是下标。
很多的 loss 函数都有 size_average 和 reduce 两个布尔类型的参数。
因为一般损失函数都是直接计算 batch 的数据,因此返回的 loss 结果都是维度为 (batch_size, ) 的向量。
a)如果 reduce = False,那么 size_average 参数失效,直接返回向量形式的 loss
b)如果 reduce = True,那么 loss 返回的是标量
a)reduction='mean',返回均值;
b)reduction='mean',返回sum;
默认情况下, reduce = True,size_average = True
import torch
import numpy as np
loss_fn = torch.nn.MSELoss(reduction='mean')
# loss_fn = torch.nn.MSELoss(reduction='sum')
a = np.array([[1, 2], [3, 4]])
b = np.array([[2, 3], [4, 5]])
print(a)
print(torch.from_numpy(a))
# 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)
UserWarning: size_average and reduce args will be deprecated, please use reduction=‘sum’ instead. warnings.warn(warning.format(ret))
criterion = torch.nn.BCELoss(size_average=True)
改为:
criterion = torch.nn.BCELoss(reduction='mean')
criterion = torch.nn.BCELoss(size_average=False)
改为:
criterion = torch.nn.BCELoss(reduction='sum')