mac 训练模型MultiProcess报错问题(freeze_support)

欢迎关注我们组的微信公众号,更多好文章在等你呦!
微信公众号名:碳硅数据
公众号二维码:
在这里插入图片描述

mac上现在主要一跑模型,马上报错

Epochs:   0%|                                                                 | 0/125 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/spawn.py", line 122, in spawn_main
    exitcode = _main(fd, parent_sentinel)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/spawn.py", line 131, in _main
    prepare(preparation_data)
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/spawn.py", line 246, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/spawn.py", line 297, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen runpy>", line 291, in run_path
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/yxk/Desktop/scaleX/SCALEX-main/pbmc_scaleX.py", line 18, in <module>
    adata=SCALEX('/Users/yxk/Desktop/test_dataset/pbmc/pbmc.h5ad', batch_name='batch',gpu=0,outdir='./pbmc_output_AAA/',num_workers=1)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yxk/Desktop/scaleX/SCALEX-main/scalex/function.py", line 171, in SCALEX
    model.fit(
  File "/Users/yxk/Desktop/scaleX/SCALEX-main/scalex/net/vae.py", line 160, in fit
    tk0 = tenumerate(dataloader, total=len(dataloader), leave=False, desc='Iterations', disable=(not verbose))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/site-packages/tqdm/contrib/__init__.py", line 66, in tenumerate
    return enumerate(tqdm_class(iterable, total=total, **tqdm_kwargs), start)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/site-packages/tqdm/asyncio.py", line 33, in __init__
    self.iterable_iterator = iter(iterable)
                             ^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/site-packages/torch/utils/data/dataloader.py", line 438, in __iter__
    return self._get_iterator()
           ^^^^^^^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/site-packages/torch/utils/data/dataloader.py", line 386, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/site-packages/torch/utils/data/dataloader.py", line 1039, in __init__
    w.start()
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
                  ^^^^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/context.py", line 288, in _Popen
    return Popen(process_obj)
           ^^^^^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/spawn.py", line 164, in get_preparation_data
    _check_not_importing_main()
  File "/Users/yxk/anaconda3/lib/python3.11/multiprocessing/spawn.py", line 140, in _check_not_importing_main
    raise RuntimeError('''
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.

        To fix this issue, refer to the "Safe importing of main module"
        section in https://docs.python.org/3/library/multiprocessing.html
        
Epochs:   0%|                                                                 | 0/125 [00:11<?, ?it/s]
Traceback (most recent call last):

问题解决:
将dataloader的num_workers设置为0,记住这里是设置为0,不是1,设置为1还是会报错的

num_workers=0