Limiting GPU memory usage by Keras / TF 2019?


I have read answers like:

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.2

But it just doesn’t work. There seems to be so much update in both keras and TF that almost anything written in 2017 doesn’t work!
So, how to limit memory usage?


One way to restrict reserving all GPU RAM in tensorflow is to grow the amount of reservation. This method will allow you to train multiple NN using same GPU but you cannot set a threshold on the amount of memory you want to reserve.

Using the following snippet before importing keras or just use tf.keras instead.

import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)

    except RuntimeError as e:

Answered By – Coderji

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published