Advertisement

PyTorch:The “freeze_support()” line can be omitted if the program is not going to be frozen

阅读量:

在Windows系统中进行pytorch程序运行时,默认情况下可能会导致程序触发了freeze_support函数返回异常。为了避免这种情况的发生,请确保所有脚本操作均放置于if name == “main”: 的执行范围内即可。
为了深入分析问题根源和解决方案,请关注后续提供的报错信息。

复制代码
    line 105, in spawn_main
    images, labels = next(iter(train_loader))
    
    line 278, in __iter__
    exitcode = _main(fd)
    
    line 114, in _main
        return _MultiProcessingDataLoaderIter(self)prepare(preparation_data)
    
    in prepare
    _fixup_main_from_path(data['init_main_from_path'])
    
    。。。。。。。。一堆
    line 278, in __iter__
    ForkingPickler(file, protocol).dump(obj)
    BrokenPipeError: [Errno 32] Broken pipe
    return _MultiProcessingDataLoaderIter(self)
    
    
    line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
    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.

以下是其他博客的原文,我觉得说的不够详细

这个问题涉及Windows环境下多进程实现的技术探讨。
当运行在Windows系统时,在导入该文件时,默认情况下会启动相关子进程中执行相应的代码。
如果未指定"main"关键字进行限制,则会导致无限递归地生成子进程中执行该文件中的指令。
因此,在导入操作中使用name=="""""""""""""""""""" "" main" 作为判断条件来进行保护是解决问题的有效方法。

需要弄明白的是,为什么有时候不会出现这种问题,有些时候会

改写说明

首先我们要知道计算机的结构

复制代码
    数据存在硬盘或者磁带(?别笑,真的有人用磁带),然后读到内存里面
    供进程调用,而如何读,在多进程/多线程模型下又怎么读/发生页错误的
    时候又怎么读?

在Windows与Linux系统中处理方式存在差异,在Windows系统中该操作通常涉及启动多个线程以完成类似功能。具体而言,在Windows环境中该过程通常会先启动多个子进程以分担计算任务,在此过程中如果出现错误则需重新执行操作以恢复一致的状态。这一做法本身并无大碍。然而,在实际运行中发现某些场景会自动启用多进程机制以提升性能表现。为此,
考虑到大多数用户可能并未深入关注多进程问题,
因此在设计时选择了较为保守的方式进行处理。

好吧说了这么多也像是给自己讲故事,记住放在if name == ‘main ’: 里面就好了

全部评论 (0)

还没有任何评论哟~