以下是在Ubuntu下安裝PyTorch的步驟:
更新系統包列表:sudo apt update
。若需GPU支持,安裝NVIDIA顯卡驅動,可通過sudo apt install nvidia-driver-xxx
(xxx為驅動版本號)安裝。同時,根據顯卡型號從NVIDIA官網下載安裝對應版本的CUDA工具包和cuDNN庫。
創建并激活虛擬環境:conda create -n pytorch_env python=3.8
,conda activate pytorch_env
。然后根據CUDA版本安裝PyTorch,如conda install pytorch torchvision torchaudio cudatoolkit=12.0 -c pytorch
(12.0替換為實際CUDA版本)。
若不使用Conda,可先安裝pip:sudo apt install python3-pip
。再根據CUDA版本選擇安裝命令,如pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu120
(12.0替換為實際CUDA版本),不使用GPU則用pip install torch torchvision torchaudio
。
在Python解釋器中輸入import torch
,print(torch.__version__)
查看版本號,print(torch.cuda.is_available())
檢查GPU是否可用。