在Linux系統(tǒng)下安裝pytorch的步驟如下:
方法一:使用pip安裝
-
更新pip:
pip install --upgrade pip
-
安裝PyTorch: 根據(jù)你的CUDA版本選擇合適的命令。你可以在PyTorch官網(wǎng)找到最新的安裝命令。
-
無(wú)GPU(CPU版本):
pip install torch torchvision torchaudio
-
有GPU(CUDA 11.3):
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
-
其他CUDA版本: 請(qǐng)參考PyTorch官網(wǎng)上的表格,選擇對(duì)應(yīng)的CUDA版本進(jìn)行安裝。
-
-
驗(yàn)證安裝: 打開Python解釋器并嘗試導(dǎo)入PyTorch:
import torch print(torch.__version__)
方法二:使用conda安裝
如果你使用的是Anaconda或Miniconda,可以使用conda來(lái)安裝PyTorch。
-
更新conda:
conda update conda
-
創(chuàng)建新的conda環(huán)境(可選):
conda create -n pytorch_env Python=3.8 conda activate pytorch_env
-
安裝PyTorch: 根據(jù)你的CUDA版本選擇合適的命令。你可以在PyTorch官網(wǎng)找到最新的安裝命令。
-
無(wú)GPU(CPU版本):
conda install pytorch torchvision torchaudio cpuonly -c pytorch
-
有GPU(CUDA 11.3):
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
-
其他CUDA版本: 請(qǐng)參考PyTorch官網(wǎng)上的表格,選擇對(duì)應(yīng)的CUDA版本進(jìn)行安裝。
-
-
驗(yàn)證安裝: 打開Python解釋器并嘗試導(dǎo)入PyTorch:
import torch print(torch.__version__)
注意事項(xiàng)
- CUDA版本:確保你的GPU驅(qū)動(dòng)和CUDA版本與PyTorch支持的版本匹配。
- 網(wǎng)絡(luò)連接:安裝過(guò)程中可能需要下載較大的文件,請(qǐng)確保網(wǎng)絡(luò)連接穩(wěn)定。
- 權(quán)限問(wèn)題:如果遇到權(quán)限問(wèn)題,可以在命令前加上sudo(僅限r(nóng)oot用戶)。
通過(guò)以上步驟,你應(yīng)該能夠在Linux系統(tǒng)下成功安裝PyTorch。