当前位置: 代码网 > it编程>前端脚本>Python > pytorch+torchvision+python版本对应及环境安装

pytorch+torchvision+python版本对应及环境安装

2025年03月16日 Python 我要评论
一、版本对应下表来自pytorch 的 github 官方文档:pytorch/vision: datasets, transforms and models specific to computer

一、版本对应

下表来自 pytorch 的 github 官方文档:pytorch/vision: datasets, transforms and models specific to computer vision

其中,命令中 "-c pytorch" 表示官方源,自己换源可以去掉。

torch 版本torchvision 版本torchaudio 版本支持的 python 版本(示例)cuda 版本
2.5.10.20.12.5.1>=3.9, <3.13(3.12)[9/10/11/12]12.4/12.1/11.8
2.5.00.20.02.5.0>=3.9, <3.13(3.12)12.4/12.1/11.8
2.4.10.19.12.4.1>=3.8, <3.13(3.12)[8/9/10/11/12]12.4/12.1/11.8
2.4.00.19.02.4.0>=3.8, <3.13(3.12)12.4/12.1/11.8
2.3.10.18.12.3.1>=3.8, <3.13(3.12)8/9/10/11/1212.1/11.8
2.3.00.18.02.3.0>=3.8, <3.13(3.12)12.1/11.8
2.2.20.17.22.2.2>=3.8, <3.12 [8/9/10/11]12.1/11.8
2.2.10.17.12.2.1>=3.8, <3.1212.1/11.8
2.2.00.17.02.2.0>=3.8, <3.1212.1/11.8
2.1.20.16.22.1.2>=3.8, <3.12(3.10)8/9/10/1112.1/11.8
2.1.10.16.12.1.1>=3.8, <3.12(3.10)12.1/11.8
2.1.00.16.02.1.0>=3.8, <3.12(3.10)12.1/11.8
2.0.00.15.02.0.0>=3.8, <3.12(3.8)[8/9/10/11]11.8/11.7
1.13.10.14.10.13.1>=3.7.2, <=3.10(3.8)[7/8/9/10]11.7/11.6
1.13.00.14.00.13.0>=3.7.2, <=3.10(3.8)11.7/11.6
1.12.10.13.11.12.1>=3.7, <=3.10(3.8)[7/8/9/10]11.6/11.3/10.2
1.12.00.13.01.12.0>=3.7, <=3.10(3.8)11.6/11.3/10.2
1.11.00.12.01.11.0>=3.7, <=3.10(3.8)11.3/10.2
1.10.10.11.20.10.1>=3.6, <=3.9(3.8)[6/7/8/9]11.3/10.2
1.10.00.11.00.10.0>=3.6, <=3.9(3.8)11.3/10.2
1.9.10.10.10.9.1>=3.6, <=3.9(3.8)[6/7/8/9]11.1/10.2
1.9.00.10.00.9.0>=3.6, <=3.9(3.8)11.1/10.2
1.8.10.9.10.8.1>=3.6, <=3.9(3.8)[6/7/8/9]11.1/10.2
1.8.00.9.00.8.0>=3.6, <=3.9(3.8)11.1/10.2
1.7.10.8.20.7.2>=3.6(3.6)11.0/10.2/10.1
1.7.00.8.00.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.00.16.02.1.0>=3.8, <3.12(3.10)12.1/11.8

这里选择的框架和环境如下:torch2.1.0 | torchvision0.16.0 | torchaudio2.1.0 | python3.10 | cuda12.1,若需要将创建的虚拟环境添加到 jupyter lab / jupyter notebook 中使用,则需要第 3-6 步,否则不用。

打开 win + r,输入 “cmd”,进入命令行窗口,其他步骤如下:

# 1. anaconda 创建虚拟环境
conda create -n torch python=3.10
# 2. 激活并进入虚拟环境
activate torch
# 3. 安装 ipykernel 
pip install ipykernel -i https://pypi.tuna.tsinghua.edu.cn/simple/
# 4. 安装ipykernel,将虚拟环境加入 jupyter 内核中
python -m ipykernel install --name torch --display-name torch
# 5. 检查新虚拟环境是否成功加入内核
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

注意:若在安装途中不小心关闭了命令窗或者发现没有关闭梯子,可先将 anaconda 的环境中删除未完全安装好的虚拟环境(如在  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:这个参数指定了主要的包索引源 url,即使用 https://download.pytorch.org/whl/cpu 作为 pytorch 相关库的包来源。这是 pytorch 官方的 cpu 版本的二进制文件源。
  • -i https://pypi.tuna.tsinghua.edu.cn/simple/:这个参数指定了镜像源,指向清华大学的 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:这个参数指定了额外的索引源 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:这个参数用于指定一个包的 url 文件列表(一个 .html 或 .xml 文件),其中列出了稳定版本的 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版本对应及环境安装的文章就介绍到这了,更多相关pytorch torchvision python版本对应内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com