Windows系统 pytorch dataloader num_workers大于0 broken pipe 问题 spawn

问题:

windows系统中,当pytorch的dataloader的num_workers>0时,报错broken pipe;但是num_workers=0则无问题。

解决方案:

参考DataLoader with num_workers>0 fails when running with "spawn" and using joblib · Issue #44687 · pytorch/pytorch · GitHub

在实例化DataLoader时,指定‘loky’作为multiprocessing_context。

from joblib.externals.loky.backend.context import get_context
a_random_loader = data.DataLoader(train_dataset,
                               batch_size=batch_size,
                               num_workers=config.num_workers,
                               drop_last=True,
                               shuffle=is_shuffle,
                               pin_memory=True,
                               sampler=train_sampler,
                               multiprocessing_context=get_context('loky'))

Use fork instead of spawn if available by vanpelt · Pull Request #1637 · wandb/client · GitHub介绍 “spawn was chosen as that is the default for windows and mac in modern pythons”。

你可能感兴趣的:(深度学习,python,pytorch)