Tensorflow-gpu不能使用GPU问题解决

相关资源链接:关于TensorFlow | TensorFlow中文官网

https://pypi.org/project/tensorflow-gpu/#files

安装的前提是一定要安装好cuda和cudnn,并且版本要和tensorflow一一对应上,否则免谈。

Tensorflow和pytorch的使用GPU的方式不同:

pytorch只需要安装好显卡驱动且不必安装CUDA,pytorch会自动检测到并使用GPU来进行计算(基本使用够了,但需要进一步优化性能,还是需要安装cuda)。

Tensorflow需要显式地安装CUDA,而且版本必须对应上才可以使用GPU。

一般的步骤(安装有问题):

1、创建环境:conda create -n tf260 python=3.8

2、安装:pip install tensorflow-gpu==2.6.0

问题:ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
wandb 0.14.0 requires Click!=8.0.0,>=7.0, which is not installed.
tensorflow 2.11.0 requires packaging, which is not installed.
tensorflow 2.11.0 requires keras<2.12,>=2.11.0, but you have keras 2.7.0 which is incompatible.
tensorflow 2.11.0 requires tensorflow-estimator<2.12,>=2.11.0, but you have tensorflow-estimator 2.7.0 which is incompatible

这样操作的话会出现各种依赖错误问题因此

 解决

在经过多次探索后得到一个最优的版本搭配方案:

ubuntu20.04+cuda11.2+cudnn8.1.1+tensorflow2.5/2.6/2.7

根据上述的错误提示,可以分为一下两种解决方案:

方案一:在该虚拟环境中先安装对应版本的pytorch,安装pytorch时会自动安装上述错误依赖

1、创建环境:

conda create -n tf260 python=3.8

2、安装pytorch:

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch

3、安装tensorflow-gpu:

pip install tensorflow-gpu==2.6.0

4、可能会出现下面的问题

ModuleNotFoundError: No module named 'google.protobuf'

直接升级一下即可:

pip install protobuf==3.20.0

5、测试代码:

import tensorflow as tf
print(tf.test.is_gpu_available())

 方案二:直接手动补充依赖项

主要不符合条件的依赖项有:Click basl-py numpy  安装好对应版本的依赖,其他依赖问题也会得到解决

1、创建环境

conda create -n tf260 python=3.8

2、安装依赖项

pip install absl-py==0.15.0
pip install numpy==1.19.5
pip install click==8.1.3

 3、安装tensorflow-gpu:

pip install tensorflow-gpu==2.6.0

4、测试代码:

import tensorflow as tf
print(tf.test.is_gpu_available())
物联沃分享整理
物联沃-IOTWORD物联网 » Tensorflow-gpu不能使用GPU问题解决

发表评论