很多的 loss 函数都有 size_average 和 reduce 两个布尔类型的参数,需要解释一下。因为一般损失函数都是直接计算 batch 的数据,因此返回的 loss 结果都是维度为 (batch_size, ) 的向量。
如果 reduce = False,返回维度为 (batch_size, ) 的向量,input和output的element wise求loss,此时size_average失效
如果 reduce = True,那么 loss 返回的是标量 tensor.shape = ()
如果size_average = True,那么返回loss.mean();
如果size_average = False,那么返回loss.sum();
查看官网这两个flag都Deprecated了,而且默认都是进行了降维与平均的
https://pytorch.org/docs/stable/nn.html?highlight=crossentropyloss#torch.nn.CrossEntropyLoss
nn.L1Loss
两个向量的差值
loss_fn = torch.nn.SmoothL1Loss(reduce=True, size_average=False)
input = torch.autograd.Variable(torch.randn(3,4))
target = torch.autograd.Variable(torch.randn(3,4))
loss = loss_fn(input, target)
print(input); print(target); print(loss)
print(input.size(), target.size(), loss.size())
#print(input.shape, target.shape, loss.shape)
#tensor.shape和tensor.size()都可以表示形状
>>>tensor([[-0.8321, -1.5957, -0.7866, 0.1292],
[-0.1069, 0.3355, -2.4420, -2.0299],
[ 0.0513, 0.1084, -0.7796, -0.8375]])
>>>tensor([[-0.6004, -0.6185, -1.1132, 1.0856],
[ 0.3787, -0.9000, 1.7433, -1.6910],
[ 0.6767, 0.3840, -1.3867, -0.6717]])
>>>tensor(6.0426)
#这种一维的tensor是可以直接当做一个数来计算的,例如loss/12可直接求平均
#但是在计算除法时“/”只取结果整数部分,“%”只取结果余数部分,“float(loss)/12”才是精确除法
>>>((3, 4), (3, 4), ())
nn.SmoothL1Loss
也叫作 Huber Loss,误差在 (-1,1) 上是平方损失,其他情况是 L1 损失。
nn.MSELoss
均方损失函数,用法和上面类似,这里 loss, x, y 的维度是一样的,可以是向量或者矩阵,ii 是下标。
nn.CrossEntropyLoss
weight = torch.Tensor([1,2,1,1,10])
loss_fn = torch.nn.CrossEntropyLoss(reduce=False, size_average=False, weight=weight)
input = Variable(torch.randn(3, 5)) # (batch_size, C)
target = Variable(torch.LongTensor(3).random_(5))
loss = loss_fn(input, target)
print(input); print(target); print(loss)
输入为三个数据,输出的loss为一个长度为3的一维tensor,这里reduce, size_average也可以使用
参考链接:https://blog.csdn.net/zhangxb35/article/details/72464152