在Linux環境下安裝PyTorch可以分為幾個主要步驟,具體取決于你是否需要使用GPU支持。以下是在Linux環境下安裝PyTorch的詳細指南:
更新系統包
sudo apt update && sudo apt upgrade -y
安裝Anaconda
bash Anaconda3-2022.10-Linux-x86_64.sh
.bashrc文件中添加Anaconda的路徑。創建虛擬環境
conda create --name pytorch_env python=3.8
conda activate pytorch_env
安裝PyTorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
驗證安裝
python -c "import torch; print(torch.__version__)"
更新包管理器
sudo apt-get update
安裝Python和基本庫
sudo apt-get install python3-pip
pip3 install torch torchvision
檢查CUDA和cuDNN支持(如果使用GPU)
pip3 install torch torchvision -f https://download.pytorch.org/whl/cu118/torch_stable.html
驗證安裝
python -c "import torch; print(torch.__version__)"
請根據你的系統配置和需求選擇合適的安裝方法。如果你使用的是GPU版本,請確保你的系統已經正確安裝了NVIDIA驅動和相應的CUDA版本。