TensorFlow 2.X训练时GPU显存分配
TensorFlow 2.X训练时,默认状态下GPU显存只能分配82%左右(笔者电脑为RTX 2070 Max-Q 8GB,Windows 10,CUDA 10.1,cuDNN 7.6,TensorFlow 2.1.0)。
很多博客给出如下的代码:
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
但是并不起作用(不知是否是配置问题,在笔者电脑上没有效果)。
以下代码可以解决:
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_virtual_device_configuration(
gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=8192)])
直接将GPU显存设置为最大(MB为单位),设置后便可解除显存限制。