文章目录
关于pytorch在训练模型时常见错误
问题1
【Pytorch】Expected hidden[0] size (2, 32, 256), got [2, 14, 256]
错误原因:
改正方法:
- 修改batchsize,让数据集大小能整除batchsize
- 如果使用Dataloader,设置一个参数drop_last=True,会自动舍弃最后不足batchsize的batch
问题2
1D target tensor expected, multi-target not supported
错误原因:
改正方法:
- 查看label和pred的shape形状
pred = rnn(data) #预测
label.shape #标签
pred.shape
label.shape | pred.shape |
---|---|
(batch,) | (batch,classes) |
(batch,seq_len) | (batch,classes,seq_len) |
问题3
too many values to unpack (expected 2,3......)
错误原因:
改正方法:
- 对应每一个值
问题4 np转int
IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed解决方案
错误原因:
如
当你取没有维度的np时会报错
改正方法:
问题5
unhashable type: 'numpy.ndarray'
错误原因:
如
改正方法: