训练、测试Tensorflow、Keras代码时,出现could not create cudnn handle: CUDNN_STATUS_NOT_INITIALIZED、error retrieving driver version: Unimplemented: kernel reported driver version not implemented on Windows、could not destroy cudnn handle: CUDNN_STATUS_BAD_PARAM等错误。
错误主要指向cudnn,但是CUDA版本和cudnn版本是符合当前tensorflow要求的,因此只能是GPU占用问题导致的。解决方法如下:
tensorflow 框架下设置GPU按需分配:
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
keras框架(Tensorflow backend) 设置GPU按需分配:
import tensorflow as tf
from keras import backend as K
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
K.set_session(sess)
Tensorflow 2.0 设置GPU按需分配方式(没有session):
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)