[Pytorch] ValueError: sampler option is mutually exclusive with shuffle

踩坑记录:

pytoroch利用DDP(DistributedDataParallel)进行单机多卡的训练时,
提示:
ValueError: sampler option is mutually exclusive with shuffle

原因:

使用了DistributedSampler,
而在dataloader中,sampler和 shuffle不能同时为真.

但由于模型确实需要使用shuffle,不能因为加快速度不管准确性了啊

查阅文档,得知
DistributedSampler中shuffle默认为真,
但是有一个隐藏的小坑就是随机种子默认为0.

解决方案:

官方代码给了这么一段话:
In distributed mode, calling the :meth:set_epoch method at the beginning of each epoch before creating the :class:DataLoader iterator is necessary to make shuffling work properly across multiple epochs. Otherwise, the same ordering will be always used.

调用一下DS.set_epoch(*)再将dataloader的shuffle改为false即可

猜你喜欢

转载自blog.csdn.net/ftimes/article/details/120462626