安装PyTorch 1.13
pytorch1.13安装,个人参考
情况交代
- 显卡3060,cuda版本12.0
- 已有一个虚拟环境安装了cuda11.2和cudnn8.1.0以及对应的tensorflow
- 现在需要创建一个可以使用GPU加速的pytorch环境
安装流程
注意事项
pytorch自身是带了cuda环境的,所以不需要强制要求和之前tensorflow那个环境一致
torch1.13.0不支持cuda10.2和11.3版本了
显卡配置查看
nvidia-smi
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 526.47 Driver Version: 526.47 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... WDDM | 00000000:01:00.0 On | N/A |
| 0% 44C P8 22W / 170W | 1486MiB / 12288MiB | 31% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
创建环境
conda create -name torch python=3.9
激活环境
activate torch
安装对应的torch版本
torch官网
提供了几个示例代码:
# CUDA 10.2
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=10.2 -c pytorch
# CUDA 11.3
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch
# CUDA 11.6
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.6 -c pytorch -c conda-forge
# CPU Only
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cpuonly -c pytorch
删掉最后的-c pytorch
然后把cudatoolkit的版本修改成11.2(这句话的意思就是下载11.2版本的cuda,并且这个cuda可以和系统的cuda版本不一致,但是一定得低于系统本身的,也就是nidia-smi指令查询得到的那个版本(12.0))
即在torch的环境中使用以下命令:(注意这个命令是错误的,后面会修改)
conda install pytorch torchvision torchaudio cudatoolkit=11.2
检查
使用pip list
Package Version
----------------- -------
numpy 1.21.6
Pillow 9.2.0
pip 22.3.1
setuptools 65.5.1
six 1.16.0
torch 1.13.0
torchaudio 0.13.0
torchvision 0.2.2
typing_extensions 4.4.0
wheel 0.38.2
导入查看
import torch
torch.cuda.is_available()
返现返回false
使用conda list查看,发现下载的是cpu版本
pytorch 1.13.0 py3.7_cpu_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
pytorch-mutex 1.0 cpu https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
setuptools 65.5.1 pyhd8ed1ab_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
six 1.16.0 pyh6c4a22f_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
sqlite 3.39.4 hcfcfb64_0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
卸载
conda uninstall pytorch
下载gpu版本的
查看官网的pip形式的安装命令:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
验证
可行
把这个内核加到jupyter
- 查看是否有ipykernel
python -m ipykernel –version - 没有的话安装
python -m pip install ipykernel - 安装完添加
python -m ipykernel install –user –name=torch - 查看内核
jupyter kernelspec list
- 不想用了删除
jupyter kernelspec remove kernelname