pytorch环境配置

前言
我的显卡 GeForce RTX 2060

安装cuda

cuda_11.7.0_516.01_windows 配合cudnn-windows-x86_64-8.7.0.84_cuda11-archive 下载链接我懒得找了,需要的根据我给的版本号找吧

下载好之后

cuda直接管理员身份安装就好,这里不展示图片了

cudnn要解压

pytorch 和CUDA对应 pytorch1.6 cuda_pytorch 和CUDA对应


复制上图所有文件

粘贴到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7这个目录下

pytorch 和CUDA对应 pytorch1.6 cuda_python_02


cuda就安装完成了

检验cuda

win+r 输入cmd打开命令行

输入nvcc -V

pytorch 和CUDA对应 pytorch1.6 cuda_命令行_03

安装conda

其实这里有很多要注意的地方,首先我要说明,我将文件放到C盘主要是我的C盘足够大,有足够的空间存放,任性

但是这样放会有一个缺点,那就是要给C盘文件夹权限

如果想在C盘以外的盘安装conda,可以跳过给权限这一步

给权限

右键要安装的文件夹,找到安全

pytorch 和CUDA对应 pytorch1.6 cuda_python_04


点击更改

pytorch 和CUDA对应 pytorch1.6 cuda_深度学习_05


顺次点击

pytorch 和CUDA对应 pytorch1.6 cuda_pytorch_06


确定

pytorch 和CUDA对应 pytorch1.6 cuda_深度学习_07


根据需要,点编辑,给指定用户完全控制权限

pytorch 和CUDA对应 pytorch1.6 cuda_pytorch 和CUDA对应_08

安装conda

下载最新conda

https://www.anaconda.com/products/distribution

pytorch 和CUDA对应 pytorch1.6 cuda_深度学习_09


下载后安装,注意管理员身份运行

pytorch 和CUDA对应 pytorch1.6 cuda_pytorch 和CUDA对应_10


改成自己安装的目录,安装就行

换源,更改conda包以及环境目录

pytorch 和CUDA对应 pytorch1.6 cuda_pytorch 和CUDA对应_11


点开.condarc文件,记事本打开

全选,粘贴以下内容

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - defaults
show_channel_urls: true

以下是我的配置

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
  - defaults
show_channel_urls: true
envs_dirs:
  - C://software//anaconda3//dir//envs
pkgs_dirs:
  - C://software//anaconda3//dir//pkgs

其中
可以直接看到上面是清华源,
envs_dirs是你的环境存放目录,
pkgs_dirs是你的包存放目录,
修改你的环境以及包存放目录,注意双斜杠!

修改jupyter目录

win+r输入cmd打开命令行

输入activate激活conda

pytorch 和CUDA对应 pytorch1.6 cuda_深度学习_12

输入jupyter notebook --generate-config 找到输出的地址

pytorch 和CUDA对应 pytorch1.6 cuda_python_13


pytorch 和CUDA对应 pytorch1.6 cuda_pytorch 和CUDA对应_14


第一个文件,有编译器的就编译器打开,没有就记事本打开

pytorch 和CUDA对应 pytorch1.6 cuda_命令行_15

ctrl+f 搜索c.NotebookApp.notebook_dir 找到上面的
删除#,添加你要存放的目录地址

注意,这里地址应该是双斜杠*

pytorch 和CUDA对应 pytorch1.6 cuda_命令行_16


如果用了单斜杠,打开jupyter的时候会提示

pytorch 和CUDA对应 pytorch1.6 cuda_深度学习_17

保存退出

右键jupyter快捷方式

pytorch 和CUDA对应 pytorch1.6 cuda_pytorch_18

pytorch 和CUDA对应 pytorch1.6 cuda_pytorch_19


删除后面的 “%USERPROFILE%/”

pytorch 和CUDA对应 pytorch1.6 cuda_pytorch 和CUDA对应_20

创建环境

命令行输入

conda create -n py37 python

根据提示按y
这里的py37是环境名,可以根据自己需要更改
我这里就不演示了

安装pytorch

在py37环境下
输入命令

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117

等安装就行

如果结束提示WARNING: There was an error checking the latest version of pip. 可以升级pip

pip install --upgrade pip

添加jupyter内核py37

在py37环境下运行

conda install -c anaconda ipykernel
python -m ipykernel install --user --name=py37

启动jupyter,点开new

pytorch 和CUDA对应 pytorch1.6 cuda_深度学习_21

测试pytorch

输入以下命令

import torch
torch.cuda.is_available()
torch.cuda.current_device()
torch.cuda.device(0)
torch.cuda.device_count()
torch.cuda.get_device_name(0)

pytorch 和CUDA对应 pytorch1.6 cuda_pytorch 和CUDA对应_22


结束!