CPU only pytorch is crashing with error AssertionError: Torch not compiled with CUDA enabled

CPU only pytorch is crashing with error AssertionError: Torch not compiled with CUDA enabled

我正在尝试 运行 来自 this repository 的代码,我需要使用 Pytorch 1.4.0。我已经使用 pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html.

安装了 CPU 唯一版本的 pytorch

我通过 py -m train_Kfold_CV --device 0 --fold_id 10 --np_data_dir "C:\Users\username\OneDrive\Desktop\emadeldeen\AttnSleep\prepare_datasets\edf_20_npz" 运行 程序,但出现此错误:

  File "C:\Users\username\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\username\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\username\OneDrive\Desktop\emadeldeen\AttnSleep\train_Kfold_CV.py", line 94, in <module>
    main(config, fold_id)
  File "C:\Users\username\OneDrive\Desktop\emadeldeen\AttnSleep\train_Kfold_CV.py", line 65, in main
    trainer.train()
  File "C:\Users\username\OneDrive\Desktop\emadeldeen\AttnSleep\base\base_trainer.py", line 66, in train
    result, epoch_outs, epoch_trgs = self._train_epoch(epoch, self.epochs)
  File "C:\Users\username\OneDrive\Desktop\emadeldeen\AttnSleep\trainer\trainer.py", line 49, in _train_epoch
    loss = self.criterion(output, target, self.class_weights)
  File "C:\Users\username\OneDrive\Desktop\emadeldeen\AttnSleep\model\loss.py", line 6, in weighted_CrossEntropyLoss
    cr = nn.CrossEntropyLoss(weight=torch.tensor(classes_weights).cuda())
  File "C:\Users\username\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\cuda\__init__.py", line 196, in _lazy_init
    _check_driver()
  File "C:\Users\username\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\cuda\__init__.py", line 94, in _check_driver
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled

我已将配置中的 GPU 数量更改为 0,并尝试在程序开头添加 device = torch.device('cpu'),但它没有任何作用。我该如何解决这个错误?如果有帮助,我将 windows 10 与 python 3.7.9 一起使用

谢谢

您只使用 CPU pytorch,但您的代码有类似 cr = nn.CrossEntropyLoss(weight=torch.tensor(classes_weights).cuda()) 的语句,它试图将张量移动到 GPU。

要修复它, 删除所有 .cuda() 操作。