我尝试运行这个仓库中的代码,并且需要使用 Pytorch 1.4.0。我已经安装了仅 CPU 版本的 pytorch,安装命令为 pip install torch==1.4.0+cpu torchvision==0.5.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
。
我通过运行 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()
操作。