A trap of parameter ‘size_average’ in pytorch 详解

简介: pytorch的入门教程中有这样的一句: 12345loss = torch.nn.

pytorch的入门教程中有这样的一句:

上面的程序很简单,设定一个loss函数,然后设定一个input和target进行loss计算,然后再backward。

这里我们不看反向过程,只看output的值是多少:

这个4是output的值,可以看待这个值是一个scalar而并不是一个向量。

我们再来看一段程序:

同样一段程序,怎么就变成16了,原因是在设置loss函数中我们把参数size_average设为了False。

当size_average为Ture的时候,我们得到4,反之我们得到16,结合size_average参数的官方解释:



也就是说True的loss除以了loss的数量(此处n=4),False的时候loss为summed的也就是全部loss的总和。

size_average参数默认为True,也就是说我们平时计算的时候得到的loss是平均loss。

这样会出现怎么问题呢:

这个参数默认是True,我们平时的训练集都是规整的,也就是说训练集中的每个类数量几乎差不多的训练集,然后我们算出了损失,比如这个损失是个[10]的Tensor,这里的10就是你之前说的N 也就是分类的数量,这个参数设为True则在得出的所有loss中除以N 如果为Flase则不处理直接把所有类的loss直接加起来给你。这个平时加起来和除以N在实际计算中影响不大,但是如果是别的任务 比如RNN 这时候分类loss就需要权重了。每个分类的loss的权重系数应该不一样,或者说训练集中某些类的数据没有另一类多,得出的损失有可能也有缺陷也需要参数,这时在pytorch中我们就无能为力了,因为其只能输出平均或者总和的loss 不能输出a batch of losses 也就是一个类一个损失,这种输出后期我们可以自由处理 。所以说这个是pytorch的一个缺陷 ,在github中的issue中有人提出来了。pytorch的编写人员正在修改底层。

该问题相关的链接:

如何自定义不同权重loss:https://github.com/pytorch/pytorch/issues/563

loss中奇怪的问题:https://discuss.pytorch.org/t/the-default-value-of-size-average-true-in-loss-function-is-a-trap/4251

How to combine multiple criterions to a loss function?
Get loss for individual samples
losses per-batch-element:https://github.com/pytorch/pytorch/issues/264

 

目录
相关文章
RuntimeError: Given groups=1, weight of size 64 128 1 7, expected input[16,
RuntimeError: Given groups=1, weight of size 64 128 1 7, expected input[16,
2347 0
|
11月前
Expected more than 1 value per channel when training, got input size torch.Size
因为模型中用了batchnomolization,训练中用batch训练的时候当前batch恰好只含一个sample,而由于BatchNorm操作需要多于一个数据计算平均值,因此造成该错误。
694 0
|
机器学习/深度学习 PyTorch 算法框架/工具
解决Pytorch中RuntimeError: expected scalar type Double but found Float
解决Pytorch中RuntimeError: expected scalar type Double but found Float
2417 0
|
TensorFlow 算法框架/工具
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
229 0
解决TypeError: tf__update_state() got an unexpected keyword argument ‘sample_weight‘
error: passing ‘const AppJniCommand’ as ‘this’ argument discards qualifiers [-fpermissive]
error: passing ‘const AppJniCommand’ as ‘this’ argument discards qualifiers [-fpermissive]
68 0
|
Python
解决办法:RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96
解决办法:RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96
136 0
解决办法:RuntimeError: dictionary changed size during iteration
解决办法:RuntimeError: dictionary changed size during iteration
86 0
解决办法: error: passing ‘const VideoFrame’ as ‘this’ argument discards qualifiers [-fpermissive]
解决办法: error: passing ‘const VideoFrame’ as ‘this’ argument discards qualifiers [-fpermissive]
172 0
|
PyTorch 算法框架/工具
【Pytorch问题】RuntimeError: “max_pool2d“ not implemented for ‘Long‘
【Pytorch问题】RuntimeError: “max_pool2d“ not implemented for ‘Long‘
279 0
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“