今天头一次使用TensorFlow2.0的GPU版本进行训练,训练中遇到【CUDA_ERROR_OUT_OF_MEMORY out of memory】,于是百度各种解决办法,基本能查到的都是TensorFlow1.0的用法,但此方法在TensorFlow2.0中不适用。于是经过多方查找,终于找到设置方法,在此进行记录,一是方便日后回看,二是弥补百度中各种查不到。
- TensorFlow1.0用法
from keras import backend as K
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
K.set_session(sess)
- TensorFlow2.0用法
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
for k in range(len(physical_devices)):
tf.config.experimental.set_memory_growth(physical_devices[k], True)
tf.config.experimental.
print('memory growth:', tf.config.experimental.get_memory_growth(physical_devices[k]))
else:
print("Not enough GPU hardware devices available")