在Ubuntu系統上集成PyTorch可以通過多種方式實現,主要包括使用pip或Anaconda進行安裝,并選擇是否支持GPU加速。以下是詳細的步驟:
sudo apt update
sudo apt upgrade
sudo apt install -y python3 python3-pip
python3 -m venv pytorch_env
source pytorch_env/bin/activate
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
請將118替換為你的CUDA版本。
python -c "import torch; print(torch.__version__)"
wget https://repo.anaconda.com/archive/Anaconda3-2023.07-2-Linux-x86_64.sh
bash Anaconda3-2023.07-2-Linux-x86_64.sh
source ~/.bashrc
conda create -n pytorch_env python=3.8
conda activate pytorch_env
conda install pytorch torchvision torchaudio cpuonly -c pytorch
conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
請將11.7替換為你的CUDA版本。
python -c "import torch; print(torch.cuda.is_available())"
通過以上步驟,你應該能夠在Ubuntu系統上成功集成PyTorch,無論是用于CPU還是GPU加速的深度學習任務。