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