RuntimeError: An attempt has been made to start a new process before the current process解决(亲测有效)

今天用pytorch测试代码的时候忽然发现了一个报错

RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
    ForkingPickler(file, protocol).dump(obj)
BrokenPipeError: [Errno 32] Broken pipe

查了一波资料发现导致这个报错的原因是因为:
linux系统中可以使用多个子进程加载数据,windows系统里是不可以的,可以发现报错时产生在DataLoader文件中的。我们找到自己调用DataLoader的文件中num_workers的设置,设置为0或者采用默认为0的设置。类似下图:
在这里插入图片描述
修改之后就会发现可以正常运行啦~

你可能感兴趣的:(pytorch,pytorch,linux)