1. 程式人生 > >pytorch報錯:ValueError: Expected more than 1 value per channel when training, got input size [1, 768,1

pytorch報錯:ValueError: Expected more than 1 value per channel when training, got input size [1, 768,1

Traceback (most recent call last):
  File "train_ammeter_twoclass.py", line 189, in <module>
    train(epoch)
  File "train_ammeter_twoclass.py", line 133, in train
    outputs = net(inputs)
  File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477,
in __call__ result = self.forward(*input, **kwargs) File "/home/iot/chenjun/1_program/classifer/src/model.py", line 79, in forward x = self.net(x) # inception會返回兩個矩陣 File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__ result =
self.forward(*input, **kwargs) File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torchvision/models/inception.py", line 109, in forward aux = self.AuxLogits(x) File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__ result = self.
forward(*input, **kwargs) File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torchvision/models/inception.py", line 308, in forward x = self.conv1(x) File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__ result = self.forward(*input, **kwargs) File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torchvision/models/inception.py", line 326, in forward x = self.bn(x) File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__ result = self.forward(*input, **kwargs) File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torch/nn/modules/batchnorm.py", line 66, in forward exponential_average_factor, self.eps) File "/home/iot/miniconda2/envs/pytorch3/lib/python3.6/site-packages/torch/nn/functional.py", line 1251, 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 [1, 768, 1, 1]

問題分析: 模型中用了batchnomolization,訓練中用batch訓練的時候,應該是有單數,比如dataset的總樣本數為17,你的batch_size為8,就會報這樣的錯誤。
解決方案: 從dataset中刪掉一個sample。