Torch中gpu的使用

113 阅读1分钟

查看 gpu

cmd 输入 nvidia-smi 查看。

image.png

这里是 RTX 3050, 4GB 显存, 有点小了。

安装 cuda

用 conda 安装 官方推荐的 PyTorch 版本(带 CUDA 12.1):

torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

安装完成后,在 Anaconda Prompt 里运行:

import torch
print(torch.cuda.is_available())  # True = 识别到 GPU
print(torch.cuda.device_count())  # 你的 GPU 数量
print(torch.cuda.get_device_name(0))  # 你的 GPU 名称

image.png

相关使用

  • 查询可用 gpu 数量

image.png

  • 在 gpu 上运算

image.png

image.png