[pytorch][持续更新]pytorch踩坑汇总

Posted wildkid1024

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[pytorch][持续更新]pytorch踩坑汇总相关的知识,希望对你有一定的参考价值。

  1. BN层不能少于1张图片
File "/home/user02/wildkid1024/haq/models/mobilenet.py", line 71, in forward
    x = self.features(x)
  File "/home/user02/anaconda2/envs/py3_dl/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/user02/anaconda2/envs/py3_dl/lib/python3.6/site-packages/torch/nn/modules/container.py", line 92, in forward
    input = module(input)
  File "/home/user02/anaconda2/envs/py3_dl/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/user02/anaconda2/envs/py3_dl/lib/python3.6/site-packages/torch/nn/modules/container.py", line 92, in forward
    input = module(input)
  File "/home/user02/anaconda2/envs/py3_dl/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/user02/wildkid1024/haq/lib/utils/utils.py", line 244, in lambda_forward
    return m.old_forward(x)
  File "/home/user02/anaconda2/envs/py3_dl/lib/python3.6/site-packages/torch/nn/modules/batchnorm.py", line 76, in forward
    exponential_average_factor, self.eps)
  File "/home/user02/anaconda2/envs/py3_dl/lib/python3.6/site-packages/torch/nn/functional.py", line 1619, in batch_norm
    raise ValueError(‘Expected more than 1 value per channel when training, got input size {}‘.format(size))
ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 512, 1, 1])

问题分析: 模型中用了batchnomolization,训练中用batch训练的时候,应该是有单数,比如dataset的总样本数为17,batch_size为8,就会报这样的错误。
解决方案: 1. 将dataloader的一个丢弃参数设置为true 2. 手动舍弃小于1的样本数量 3. 如果是验证过程,通过设置model.eval()改变BN层的行为。 4. 如果训练过程中只能使用1个sample,替换BN为InstanceNorm.


以上是关于[pytorch][持续更新]pytorch踩坑汇总的主要内容,如果未能解决你的问题,请参考以下文章

pytorch 中遇到的问题(持续更新中)

Pytorch可视化--Visdom使用(持续更新)

pytorch踩坑之model.eval()和model.train()输出差距很大

踩坑笔记(pytorch-bert,dataframe,交叉熵)

PyTorch 半精度训练踩坑

Pytorch安装踩坑