TensorFlow2.1.0 报错解决:failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected

今天在jupyter notebook中使用keras-gpu时报错,一开始没注意控制台输出,只从jupyter notebook看到了错误信息。于是,查解决方案,大概分为两种,一版本退回,二指定运行设备。因为感觉不是版本的问题,没有退回,仍然用最新的版本,所以尝试第二种方法解决问题,即程序制定运行设备,程序不报错可以跑,无意间注意到控制台输出,发现用的是CPU跑的?然后发现指定设备的名称错误,导致系统找不到设备,于是先查询设备名称再指定设备,解决问题。


本机环境:

  • cudatoolkit = 10.1.243
  • cudnn = 7.6.5
  • tensorflow-gpu = 2.1.0
  • keras-gpu = 2.3.1

Jupyter notebook 打印错误:

...
UnknownError:  Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
	 [[node time_distributed_1/convolution (defined at C:\anaconda3\envs\keras\lib\site-packages\keras\backend\tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_1967]

Function call stack:
keras_scratch_graph

Jupyter notebook 控制台打印错误:failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
在这里插入图片描述


解决方案:

  1. 查看本机GPU/CPU信息
from tensorflow.python.client import device_lib
device_lib.list_local_devices()

输出:

[name: "/device:CPU:0"
 device_type: "CPU"
 memory_limit: 268435456
 locality {
 }
 incarnation: 16677593686354176255,
 name: "/device:GPU:0"
 device_type: "GPU"
 memory_limit: 1440405913
 locality {
   bus_id: 1
   links {
   }
 }
  incarnation: 787265797177696422
 physical_device_desc: "device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0"]
  1. 可以看到本机设备有GPU,所以用如下语句指定运行设备名称:
    注意!= 后边一定是设备名称,每个人的设备名称可能存在差异,不要照搬,一定要先用命令查询后,再指定!
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '/device:GPU:0'
  1. 查看当前加载的设备:
import tensorflow as tf
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))

输出:

Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0
  1. 控制台输出类似如下信息说明代码已经在GPU运行:
    在这里插入图片描述
发布了122 篇原创文章 · 获赞 94 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_39653948/article/details/105001145