Linux中tensorflow2使用GPU训练模型

查看ubuntu的Tensorflow2是否可以使用GPU

import tensorflow as tf
tf.test.is_gpu_available()

在这里插入图片描述
上图说tf.test.is_gpu_available()这个指令在未来版本中将被移除,所以它建议你用tf.config.list_physical_devices('GPU')来检测tf2是否可以用GPU

import tensorflow as tf
tf.config.list_physical_devices('GPU')

在这里插入图片描述
tensorflow版本和cuda版本的对应–官网

Linux查看当前Cuda(CUDA Toolkit )版本

区分两个基本的命令
命令1:
nvcc -V或者cat /usr/local/cuda/version.txt
在这里插入图片描述
这条命令是查看当前Cuda的版本,即实际安装的Cuda版本,由上图可知服务器实际安装cuda的版本为11.0.207

命令2:

nvidia-smi

在这里插入图片描述
这条命令不仅可以查看当前NVIDIA驱动的版本(即470.86),还可以查询与此驱动相匹配的Cuda版本(即11.4),虽是匹配,但是Cuda的版本可以略低于此时驱动匹配的Cuda版本,因此,我们可以安装版本高一点的驱动,来兼容不同版本的Cuda!
但是实际安装的cuda的版本为11.0.207, 而不是11.4!!!

参考博客

去查看官网可知,11.0版本的cuda应该安装tensorflow-2.4.0版本的,python版本要在3.6~3.8之间的

在这里插入图片描述

conda虚拟环境安装GPU版本tensorflow2版本的

# 1.创建虚拟环境
conda create --name tfgpu python=3.8 -y #虚拟环境的名字叫tfgpu, python版本为3.8
# 2.激活创建的虚拟环境
conda activate tfgpu
# 3.安装GPU版本的tensorflow
# 先在终端查看服务器上实际安装的cuda的版本
cat /usr/local/cuda/version.txt
# 再去官网查看和自己cuda版本匹配的tensorflow2对应的版本
#  官网https://tensorflow.google.cn/install/source#linux
# 安装cudatoolkit
conda install cudatoolkit=11.0  #因为我这里服务器上的cuda=11.0
# 安装cudnn
conda install cudnn
# 安装tf
pip install tensorflow==2.4.0  #因为我这里的cuda=11.0对应的是tensorflow-2.4.0

注意:

  1. tensorflow2.0+已经默认安装GPU版本了
  2. 先安装cudatoolkit和cudnn,再安装tensorflow
  3. 不要使用conda install tensorflow-gpu, 此方法安装后GPU可能无法使用
  4. 注意tensorflow与cudatoolkit和cudnn版本的对应关系
    测试安装情况:
$ python
>>> import tensorflow as tf
>>> tf.config.list_physical_devices('GPU')
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

在这里插入图片描述
在这里插入图片描述
可以看到它说是有两张显卡可以使用的!!!我这里的服务器上确实是有两张显卡!!!

参考博客
在ipynb文件上运行程序时报错如下:

Running cells with 'Python 3.8.13 ('tfgpu': conda)' requires ipykernel package.
Run the following command to install 'ipykernel' into the Python environment. 
Command: 'conda install -n tfgpu ipykernel --update-deps --force-reinstall'

所以要安装ipykernel

# 安装ipykernel
pip install ipykernel
# 判断有无GPU可用
import tensorflow as tf
print(tf.test.is_gpu_available())   # True,说明有GPU可以使用
# 指定使用0号GPU
physical_device = tf.config.experimental.list_physical_devices("GPU")
tf.config.experimental.set_memory_growth(physical_device[0], True)

参考博客

ValueError: Memory growth cannot differ between GPU devices

报错:ValueError: Memory growth cannot differ between GPU devices
报错原因:
默认情况下,TensorFlow会占用所有GPUs的所有GPU内存(取决于CUDA_VISIBLE_DEVICES这个系统变量),这样做可以减少内存碎片,更有效地利用设备上相对宝贵的GPU内存资源。

参考博客

tensorflow2.0使用单GPU训练模型

查看服务器显卡型号

猜你喜欢

转载自blog.csdn.net/weixin_43845922/article/details/127599413