1.安装
创建虚拟环境pytorch,pip 安装gpu版本,没有cuda就选安装cpu
conda create -n pytorh python=3.7
pip install torch==1.7.0+cu101 torchvision==0.8.1+cu101 torchaudio===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
2.测试
import torch # 如正常则静默
a = torch.Tensor([1.]) # 如正常则静默
a.cuda() # 如正常则返回"tensor([ 1.], device='cuda:0')"
from torch.backends import cudnn # 如正常则静默
cudnn.is_acceptable(a.cuda()) # 如正常则返回 "True"

本文介绍了如何在Python环境中安装PyTorch,并提供了简单的测试代码来验证安装是否成功。包括创建虚拟环境、使用pip安装特定版本的PyTorch及其依赖项,以及运行测试代码检查CUDA支持。

1342





