pytorch,tensorflow等因版本问题导致重新无法调用GPU问题的常规解决解决方法
常见包安装
首先根据获取的代码先查看requirements.txt通过一下命令进行安装
pip install -r requirements.txt
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
或者根据.yml文件进行下载
其中.yml在国内下载pytorch会引入官方源,需要自行更改一下
如例:
name: xxx
channels:
- pytorch
- conda-forge
- defaults
dependencies:
- cudatoolkit=10.2.89
...
- pytorch=1.10.1
...
- torchvision=0.11.2
- pip:
...
其中就应该删去- pytorch用换源进行下载。
安装命令为
activate base
conda env create -f xxx.yml
换源
首先这种依赖用国外源下载很容易出现下载失败,甚至使用了魔法都会报错httperror,所以下载的第一步就是换源。
conda换源
首先生成源,通过一下命令生成如下.condarc的文件
conda config --set show_channel_urls yes
然后编辑成如下
channels:
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64/
- http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/
show_channel_urls: true
default_channels:
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/main
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/r
- https://mirrors.bfsu.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.bfsu.edu.cn/anaconda/cloud
msys2: https://mirrors.bfsu.edu.cn/anaconda/cloud
bioconda: https://mirrors.bfsu.edu.cn/anaconda/cloud
menpo: https://mirrors.bfsu.edu.cn/anaconda/cloud
pytorch: https://mirrors.bfsu.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.bfsu.edu.cn/anaconda/cloud
simpleitk: https://mirrors.bfsu.edu.cn/anaconda/cloud
ssl_verify: true
如果之后依然出现下载问题使用以下链接自己diy.condarc文件
pip换源
对于单个命令可以用以下方式进行换源
pip install xxx -i https://pypi.tuna.tsinghua.edu.cn/simple
或者使用全局换源
自己看链接
换源后下载依然下载失败原因
一般下载失败有以下三种可能:
1.有些包可能只有linux上才有
2.官方源下载太慢数据包超时
3.清华或者其他源没有这个包
结局方法解决方法:
1.换平台
2.换源或者用命令加长超时时间慢慢下
在安装命令后加上–default-timeout=1000,修改下载超时判定的条件为1s(这么干不仅降低了超时的概率,似乎还提高了下载速度(?玄学))
3.多加几个源,实在不行去找包下
CUDA以及CUDNN版本选择
首先根据代码或者需求定下pytorch以及tensorflow的版本。
以上面.yml为例,pytorch以及torchvision版本在里面也写了,就不用查表了,否则可以根据需求通过电脑cuda以及cudnn版本反选torch。或者根据官网链接进行查询(注:此yml文件里规定了cuda版本cudatoolkit=10.2.89即是10.2,如果使用其他版本的cuda需要先下载对应版本的cudatoolkit然后重装pytorch),tensorflow对应版本安装同理。
pytorch对应CUDA版本匹配
TensorFlow、Python、CUDA、cuDNN版本匹配
官网:TensorFlow
查询版本链接:TensorFlow
首先记得将cuda以及cudnn的目录加入path
export PATH=“c:Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.0/bin:$PATH”
export PATH=“c:Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.0/extras/CUPTI/libx64:$PATH”
export PATH="/c/tools/cuda/bin:$PATH"
对应表格
查表安装对应的tensorflow_gpu版本。
keras对应版本
如下表
安装好后的测试方法
CUDA测试
1.命令测试
nvcc -V
2.文件测试
进入cuda安装地址中的demo_suite目录
命令
cd /d xxxxxx\demo_suite
deviceQuery
如上图则安装成功
pytorch GPU测试
import torch
from torch import nn
import torchvision
print(torch.__version__) # 输出pyTorch版本
print(torch.version.cuda) # 输出CUDA版本
print(torch.backends.cudnn.version) # 输出cuDNN的版本
print(torch.cuda.get_device_name(0)) # 输出显卡版本
print(torch.cuda.is_available()) # 测试GPU是否生效
Tensoflow GPU测试
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
print(tf.test.is_gpu_available())
print(tf.__version__)