关于Pytorch多卡训练中遇到的问题解决
具体问题报错:
Dataloader BUG ValueError: sampler option is mutually exclusive with shuffle
问题截图
![]()
问题产生的代码
from torch.utils.data.dataloader import DataLoader
dataset_train = Dataset(opt.images_dir)
train_sampler = torch.utils.data.distributed.DistributedSampler(dataset_train)
dataloader = DataLoader(dataset=dataset_train,batch_size=opt.batch_size,shuffle = True,num_workers=opt.workers,pin_memory=True,drop_last=True,sampler=train_sampler)
- 1
- 2
- 3
- 4
问题产生的原因:
在设置sampler为torch.utils.data.distributed.DistributedSampler类之后,依然设置Dataloader的shuffle属性为True
在下面的源码解释中可以看出,如果sampler不为默认的None的时候,不用设置shuffle属性了
源码解释:
pytorch的Dataloader源码 参考链接
![]()
if sampler is not None and shuffle:
raise ValueError('sampler option is mutually exclusive with shuffle')
- 1
- 2
源码补充
当sampler为None的时候会根据shuffle属性设置不一样的采样器(代码想要达到的功能就是在sampler
设置为默认值的时候根据shuffle属性初始化sampler,在sampler不为None的时候,就直接已经初始化完毕了,有了具体的采样器属性,是否随机等属性是根据你定义的sampler里面定义的(正如这里的RandomSampler一样))
![]()
参考文献
python - ValueError: sampler option is mutually exclusive with shuffle pytorch - Stack Overflow
评论记录:
回复评论: