如何利用 Conda 安装 Pytorch 教程 ?
总共分为六步走:
(1)第一步:验证conda 环境是否安装好?
1) conda -V
2) conda --version
(2)第二步:查看现有环境
conda env list
如果是第一次创建的话,应该只有一个base环境。
(3)第三步:创建python环境
conda create -n py310 python=3.10
这时,会提示你安装一些包,输入Y回车即可。
(4)第四步:激活python虚拟环境
conda activate py310
(5)第五步:在Pytorch官网选择支持当前系统、当前cuda版本的Pytorch命令.
## 1.查看当前显卡驱动支持的CUDA版本
nvidia-smi
这里以cuda 12.6为例:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
安装过程中,出现如下错误:ERROR: Could not find a version that satisfies the requirement typing-extensions>=4.10.0 (from torch) (from versions: 4.4.0, 4.8.0, 4.9.0, 4.12.2)
ERROR: No matching distribution found for typing-extensions>=4.10.0, 尝试执行如下命令,然后再执行上面 torch安装命令。
python -m pip install --upgrade pip
(6)第六步:验证Pytorch 是否安装成功
import torch
# 检查PyTorch是否可用
print("PyTorch版本:", torch.__version__)
# 检查CUDA是否可用
print("CUDA是否可用:", torch.cuda.is_available())
# 创建一个简单的张量测试
x = torch.rand(5, 3)
print("测试张量:\n", x)