这个错误通常发生在使用PyTorch训练神经网络时,输入数据维度不符合预期,不能batch_size整除。可能是输入的数据有剩余但不足以达到batch_size所导致的
查阅资料发现:在torch.utils.data这个包中, DataLoader类下有一参数为:
drop_last – set to True to drop the last incomplete batch, if the dataset size is not divisible by the batch size. If False and the size of dataset is not divisible by the batch size, then the last batch will be smaller. (default: False)
drop_last-如果数据集大小不能被批次大小整除,则设置为True可删除最后一个不完整的批次。如果为False,并且数据集的大小不能被批次大小整除,则最后一个批次将更小。(默认值:False)
解决办法:只需要将这里的drop_last=True,问题便可解决。