在Ubuntu上安裝PyTorch的步驟如下:
nvidia-smi
來查看你的顯卡支持的CUDA版本。conda create -n pytorch_env python=3.9
conda activate pytorch_env
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
(請將 11.8
替換為你的CUDA版本)
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
(請將 cu118
替換為你的CUDA版本)
在Python中輸入以下代碼來驗證PyTorch是否成功安裝:
import torch
print(f"PyTorch版本: {torch.__version__}")
print(f"CUDA可用: {torch.cuda.is_available()}")
print(f"當前設備: {torch.device('cuda' if torch.cuda.is_available() else 'cpu')}")
以上步驟應該可以幫助你在Ubuntu上成功安裝PyTorch。如果在安裝過程中遇到問題,可以參考PyTorch的官方文檔或相關社區論壇尋求幫助。