drop_last=True
当你使用了batch normalization的时候,如果batch_size设置得不合适,又没有使用 drop_last = true,那么可能会遇到以下错误
Expected more than 1 value per channel when training, got input size torch.Size([1, 1024])
解决方案:
https://discuss.pytorch.org/t/about-the-relation-between-batch-size-and-length-of-data-loader/10510/4
train_iter = torch.utils.data.DataLoader(train_dataset, batch_size = batch_size, shuffle = True, num_workers = num_workers, drop_last=True)
test_iter = torch.utils.data.DataLoader(test_dataset, batch_size = batch_size, shuffle = True, num_workers = num_workers, drop_last=True)
使用了drop_last = True, 可能会有以下后果:
比如 test set中有229个数据, 如果batch size = 40, 那么就会有29个数据不会被使用。在每一个epoch中,就只有200个数据。229 - 40*5 = 29
再比如,batch size = 20, 那么就会有9个数据不会被使用。在每一个epoch中,就只有220个数据。 229 - 20*11 = 9
当使用Batch Normalization时,若batch_size设置不当且未启用drop_last=True,可能会导致训练过程中出现错误。例如,在PyTorch中,如果批次大小无法整除数据集大小,则最后一批可能包含较少的数据点,从而引发Expected more than 1 value per channel when training的错误。通过设置drop_last=True,可以避免此问题,但代价是每个epoch中部分数据将不被使用。
3131

被折叠的 条评论
为什么被折叠?



