欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED的一种解决方案

程序员文章站 2022-06-06 08:45:53
...

最近在使用tensorflow-gpu2.0的时候老是报这个错误:

Could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED

BaseCollectiveExecutor::StartAbort Unknown: 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.


错误和cudnn有关,排除了cuda和cudnn的版本问题,在网上查了一下,应该是GPU被占用,所以无法使用,需要在代码里面添加几行语句,设置GPU的按需分配

tf2.0下,添加以下代码:

import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0
tf.config.experimental.set_memory_growth(physical_devices[0], True)
assert tf.config.experimental.get_memory_growth(physical_devices[0]) == True

如果用的是tf1.n,则可以参考这篇博客,下面是来自这位大佬的内容:

keras(后端为tf1.n)添加以下代码:

import tensorflow as tf
from keras import backend as K
config1 = tf.ConfigProto()
config1.gpu_options.allow_growth=True
sess = tf.Session(config=config1)
K.set_session(sess)