pytorch+torchvision+python版本對應及環(huán)境安裝
一、版本對應
下表來自 pytorch 的 github 官方文檔:pytorch/vision: Datasets, Transforms and Models specific to Computer Vision
- pytorch 安裝官網:Start Locally | PyTorch
- pytorch 之前版本的安裝命令:Previous PyTorch Versions | PyTorch
- torch、torchvision 等相關庫:download.pytorch.org/whl/torch_stable.html
其中,命令中 "-c pytorch" 表示官方源,自己換源可以去掉。
| torch 版本 | torchvision 版本 | torchaudio 版本 | 支持的 Python 版本(示例) | Cuda 版本 |
|---|---|---|---|---|
| 2.5.1 | 0.20.1 | 2.5.1 | >=3.9, <3.13(3.12)[9/10/11/12] | 12.4/12.1/11.8 |
| 2.5.0 | 0.20.0 | 2.5.0 | >=3.9, <3.13(3.12) | 12.4/12.1/11.8 |
| 2.4.1 | 0.19.1 | 2.4.1 | >=3.8, <3.13(3.12)[8/9/10/11/12] | 12.4/12.1/11.8 |
| 2.4.0 | 0.19.0 | 2.4.0 | >=3.8, <3.13(3.12) | 12.4/12.1/11.8 |
| 2.3.1 | 0.18.1 | 2.3.1 | >=3.8, <3.13(3.12)8/9/10/11/12 | 12.1/11.8 |
| 2.3.0 | 0.18.0 | 2.3.0 | >=3.8, <3.13(3.12) | 12.1/11.8 |
| 2.2.2 | 0.17.2 | 2.2.2 | >=3.8, <3.12 [8/9/10/11] | 12.1/11.8 |
| 2.2.1 | 0.17.1 | 2.2.1 | >=3.8, <3.12 | 12.1/11.8 |
| 2.2.0 | 0.17.0 | 2.2.0 | >=3.8, <3.12 | 12.1/11.8 |
| 2.1.2 | 0.16.2 | 2.1.2 | >=3.8, <3.12(3.10)8/9/10/11 | 12.1/11.8 |
| 2.1.1 | 0.16.1 | 2.1.1 | >=3.8, <3.12(3.10) | 12.1/11.8 |
| 2.1.0 | 0.16.0 | 2.1.0 | >=3.8, <3.12(3.10) | 12.1/11.8 |
| 2.0.0 | 0.15.0 | 2.0.0 | >=3.8, <3.12(3.8)[8/9/10/11] | 11.8/11.7 |
| 1.13.1 | 0.14.1 | 0.13.1 | >=3.7.2, <=3.10(3.8)[7/8/9/10] | 11.7/11.6 |
| 1.13.0 | 0.14.0 | 0.13.0 | >=3.7.2, <=3.10(3.8) | 11.7/11.6 |
| 1.12.1 | 0.13.1 | 1.12.1 | >=3.7, <=3.10(3.8)[7/8/9/10] | 11.6/11.3/10.2 |
| 1.12.0 | 0.13.0 | 1.12.0 | >=3.7, <=3.10(3.8) | 11.6/11.3/10.2 |
| 1.11.0 | 0.12.0 | 1.11.0 | >=3.7, <=3.10(3.8) | 11.3/10.2 |
| 1.10.1 | 0.11.2 | 0.10.1 | >=3.6, <=3.9(3.8)[6/7/8/9] | 11.3/10.2 |
| 1.10.0 | 0.11.0 | 0.10.0 | >=3.6, <=3.9(3.8) | 11.3/10.2 |
| 1.9.1 | 0.10.1 | 0.9.1 | >=3.6, <=3.9(3.8)[6/7/8/9] | 11.1/10.2 |
| 1.9.0 | 0.10.0 | 0.9.0 | >=3.6, <=3.9(3.8) | 11.1/10.2 |
| 1.8.1 | 0.9.1 | 0.8.1 | >=3.6, <=3.9(3.8)[6/7/8/9] | 11.1/10.2 |
| 1.8.0 | 0.9.0 | 0.8.0 | >=3.6, <=3.9(3.8) | 11.1/10.2 |
| 1.7.1 | 0.8.2 | 0.7.2 | >=3.6(3.6) | 11.0/10.2/10.1 |
| 1.7.0 | 0.8.0 | 0.7.0 | >=3.6(3.6) | 11.0/10.2/10.1 |
二、安裝命令(pip)
1. 版本
(1)v2.5.1 ~ v2.0.0
# v2.5.1 # CUDA 12.4 pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu124 -i https://pypi.tuna.tsinghua.edu.cn/simple/ # CPU only pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cpu -i https://pypi.tuna.tsinghua.edu.cn/simple/
(2)v1.13.1 ~ v1.11.0
# v1.13.1 # CUDA 11.7 pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117 -i https://pypi.tuna.tsinghua.edu.cn/simple/ # CPU only pip install torch==1.13.1+cpu torchvision==0.14.1+cpu torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cpu -i https://pypi.tuna.tsinghua.edu.cn/simple/
(3)v1.10.1 ~ v1.7.0
# v1.10.1 # CUDA 10.2 pip install torch==1.10.1+cu102 torchvision==0.11.2+cu102 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu102/torch_stable.html -i https://pypi.tuna.tsinghua.edu.cn/simple/ # CPU only pip install torch==1.10.1+cpu torchvision==0.11.2+cpu torchaudio==0.10.1 -f https://download.pytorch.org/whl/cpu/torch_stable.html -i https://pypi.tuna.tsinghua.edu.cn/simple/
2. 安裝全過程
(1)選擇版本
| torch 版本 | torchvision 版本 | torchaudio 版本 | 支持的 Python 版本(示例) | Cuda 版本 |
|---|---|---|---|---|
| 2.1.0 | 0.16.0 | 2.1.0 | >=3.8, <3.12(3.10) | 12.1/11.8 |
這里選擇的框架和環(huán)境如下:torch2.1.0 | torchvision0.16.0 | torchaudio2.1.0 | python3.10 | Cuda12.1,若需要將創(chuàng)建的虛擬環(huán)境添加到 Jupyter Lab / Jupyter Notebook 中使用,則需要第 3-6 步,否則不用。
打開 WIN + R,輸入 “cmd”,進入命令行窗口,其他步驟如下:
# 1. Anaconda 創(chuàng)建虛擬環(huán)境 conda create -n torch python=3.10 # 2. 激活并進入虛擬環(huán)境 activate torch # 3. 安裝 ipykernel pip install ipykernel -i https://pypi.tuna.tsinghua.edu.cn/simple/ # 4. 安裝ipykernel,將虛擬環(huán)境加入 jupyter 內核中 python -m ipykernel install --name torch --display-name torch # 5. 檢查新虛擬環(huán)境是否成功加入內核 jupyter kernelspec list # 6. 從指定文件夾里進入 jupyter jupyter lab # 7. 安裝 torch 等軟件包 pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://download.pytorch.org/whl/cpu -i https://pypi.tuna.tsinghua.edu.cn/simple/
注意:此時,因為 Numpy 2.x 等其他的相關庫 與 Pytorch 2.1.0 的不兼容(以 Numpy 庫為例),所以需要將 numpy 庫的版本降級至1.x.x(如 1.24.x 或 1.23.x 版本)才能與 Pytorch 2.1.0 是兼容,從而適配以上版本。具體示例步驟如下:
- pip uninstall numpy
- pip install numpy==1.24.3
注意:若在安裝途中不小心關閉了命令窗或者發(fā)現(xiàn)沒有關閉梯子,可先將 Anaconda 的環(huán)境中刪除未完全安裝好的虛擬環(huán)境(如在 E:\Anaconda\envs\torch 此目錄下),即可重新進行上述安裝步驟。
(2)安裝結果


3. 命令相關解釋
(1)-i / --index-url
第一條命令:pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cpu -i https://pypi.tuna.tsinghua.edu.cn/simple/
--index-url https://download.pytorch.org/whl/cpu:這個參數(shù)指定了主要的包索引源 URL,即使用https://download.pytorch.org/whl/cpu作為 PyTorch 相關庫的包來源。這是 PyTorch 官方的 CPU 版本的二進制文件源。-i https://pypi.tuna.tsinghua.edu.cn/simple/:這個參數(shù)指定了鏡像源,指向清華大學的 PyPI 鏡像源,以加速從 PyPI 安裝包的速度。- 當同時使用
--index-url和-i時,--index-url會設置 主索引源(比如官方 PyTorch 或其他自定義源),而-i用來指定 一個額外的索引源 / 備用源(如清華鏡像)。此時,pip會先從--index-url指定的源查找包,如果找不到,才會去-i指定的額外源查找。
(2)--extra-index-url
第二條命令:pip install torch==1.13.1+cpu torchvision==0.14.1+cpu torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cpu -i https://pypi.tuna.tsinghua.edu.cn/simple/
--extra-index-url https://download.pytorch.org/whl/cpu:這個參數(shù)指定了額外的索引源 URL。與--index-url不同,--extra-index-url只是添加額外的索引源,而不是替換原有源。此處指定的是 PyTorch 官方 CPU 版本的源。-i https://pypi.tuna.tsinghua.edu.cn/simple/:同樣指定了 PyPI 鏡像源,指向清華大學的 PyPI 鏡像源。
(3)-f / --find-links
第三條命令:pip install torch==1.10.1+cpu torchvision==0.11.2+cpu torchaudio==0.10.1 -f https://download.pytorch.org/whl/cpu/torch_stable.html -i https://pypi.tuna.tsinghua.edu.cn/simple/
-f https://download.pytorch.org/whl/cpu/torch_stable.html:這個參數(shù)用于指定一個包的 URL 文件列表(一個.html或.xml文件),其中列出了穩(wěn)定版本的 PyTorch 安裝包。-i https://pypi.tuna.tsinghua.edu.cn/simple/:同樣指定了 PyPI 鏡像源,指向清華大學的 PyPI 鏡像源。
參考文章
[1] pytorch,torchvision與python版本對應關系及安裝命令_pytorch python版本-CSDN博客
[2] Python安裝torch(含torch和torchvision對應版本)-CSDN博客
到此這篇關于pytorch+torchvision+python版本對應及環(huán)境安裝的文章就介紹到這了,更多相關pytorch torchvision python版本對應內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python加密解密庫cryptography使用openSSL生成的密匙加密解密
這篇文章主要介紹了python加密解密庫cryptography使用openSSL生成的密匙加密解密,需要的朋友可以參考下2020-02-02
python中Pycharm 輸出中文或打印中文亂碼現(xiàn)象的解決辦法
本篇文章主要介紹了python中Pycharm 輸出中文或打印中文亂碼現(xiàn)象的解決辦法 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
pandas實現(xiàn)對一列/多列進行數(shù)據(jù)區(qū)間篩選
這篇文章主要介紹了pandas實現(xiàn)對一列/多列進行數(shù)據(jù)區(qū)間篩選方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Python實現(xiàn)過濾單個Android程序日志腳本分享
這篇文章主要介紹了Python實現(xiàn)過濾單個Android程序日志腳本分享,本文講解了原理、實現(xiàn)代碼、使用方法、最新代碼等內容,需要的朋友可以參考下2015-01-01

