如何解决RuntimeError: No CUDA GPUs are available

如何解决RuntimeError: No CUDA GPUs are available

问题:

Traceback (most recent call last):
  File "test.py", line 65, in <module>
    model = model.cuda()
  File "C:\Users\upuplions\Anaconda3\envs\base1\lib\site-packages\torch\nn\modules\module.py", line 747, in cuda
    return self._apply(lambda t: t.cuda(device))
  File "C:\Users\upuplions\Anaconda3\envs\base1\lib\site-packages\torch\nn\modules\module.py", line 639, in _apply
    module._apply(fn)
  File "C:\Users\upuplions\Anaconda3\envs\base1\lib\site-packages\torch\nn\modules\module.py", line 639, in _apply
    module._apply(fn)
  File "C:\Users\upuplions\Anaconda3\envs\base1\lib\site-packages\torch\nn\modules\module.py", line 662, in _apply
    param_applied = fn(param)
  File "C:\Users\upuplions\Anaconda3\envs\base1\lib\site-packages\torch\nn\modules\module.py", line 747, in <lambda>
    return self._apply(lambda t: t.cuda(device))
  File "C:\Users\upuplions\Anaconda3\envs\base1\lib\site-packages\torch\cuda\__init__.py", line 229, in _lazy_init
    torch._C._cuda_init()
RuntimeError: No CUDA GPUs are available

解决方法两个

  • 检查GPU是否可用

    在终端(python环境且导入了torch)输入以下代码

    >>> import torch
    >>> print(torch.cuda.device_count())
    1
    >>> print(torch.cuda.is_available())
    True
    >>> exit()
    

    如果以上没用则可以采用以下方法

  • 使用xxx.cuda前输入

    torch.cuda.empty_cache()
    

    例如

    torch.cuda.empty_cache()
    model = model.cuda()
    

你可能感兴趣的:(python,深度学习,pytorch)