PyTorch快速安装并验证GPU是否可用

步骤一:PyTorch官网安装

PyTorch官网:https://pytorch.org/

根据PyTorch官网的安装提示,选择合适版本后会生成安装语句,拷贝安装语句到Anaconda相应虚拟环境中,即可一键安装。 

步骤二:检查GPU是否可用

  • 进入环境:

        ipython

  • 导入pytorch包:

        import torch

  • 查看cuda是否可用:

        torch.cuda.is_available()

  • 查看cuda设备的数量:

        torch.cuda.device_count()

  •  查看当前使用的cuda编号:

        torch.cuda.current_device()

  •  查看GPU设备名:

        torch.cuda.get_device_name()

  •  查看设备容量:

        torch.cuda.get_device_capability(0)

在PyCharm中检查GPU是否可用

  • 代码:
import torch

def gpu_is_available():
    print('\nGPU details:')
    print(f'    gpu_is_available      : ', torch.cuda.is_available())
    print(f'    cuda_device_count     : ', torch.cuda.device_count())
    print(f'    cuda_device_name      : ', torch.cuda.get_device_name())
    print(f'    cuda_device_capability: ', torch.cuda.get_device_capability(0))

gpu_is_available()
  • 效果: