如何利用 Conda 安裝 Pytorch 教程 ?
總共分為六步走:
(1)第一步:驗證conda 環境是否安裝好?
1) conda -V2) 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)