安装pytorch 方法一

运行以下命令,可将清华镜像添加至Anaconda仓库中

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

进入pytorch官网,选择你需要的OS、Package、Language和CUDA,得到下载命令。
在这里插入图片描述

pytorch官网给出的下载命令为(使用conda下载):

conda install pytorch torchvision torchaudio cudatoolkit=9.2 -c pytorch

实际操作发现,添加镜像源之后,安装pytorch依然花费很长的时间,这是因为“ -c pytorch ”。
-c pytorch 指定了conda获取pytorch的channel,指定为conda自带的pytorch仓库,下载会比较慢。
因此,只需将 -c pytorch 去掉,就可以使用清华镜像源快速安装pytorch了。

pip install torch==1.7.0+cu92 torchvision==0.8.1+cu92 torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

同理,如果使用pip下载,去掉pip命令中的 -f https://download.pytorch.org/whl/torch_stable.html

安装pytorch 方法二(推荐)

https://download.pytorch.org/whl/torch/ 中找到对应版本,下载下来;然后到whl文件下载的位置,使用 pip install xxx.whl 进行安装即可。

安装torchvision

注意:torchvision需要安装与pytorch对应的版本,否则会报错!!!
例如,pytorch0.4.1 与 torchvision0.2.1 对应
torch- torchvision- python 版本对应关系,可查看博客 https://blog.csdn.net/qq_41542989/article/details/118974550
命令:

pip install torchvision==0.2.1

查看当前pytorch版本

import torch
print(torch.__version__)

卸载pytorch

# 使用conda卸载Pytorch 
conda uninstall pytorch
conda uninstall libtorch

# 使用pip卸载Pytorch
pip uninstall torch
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐