Last active
February 2, 2023 13:42
-
-
Save raven4752/5bb381219ff87c4d9597702b3104349d to your computer and use it in GitHub Desktop.
limit gpu memory usage of keras
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#put the these lines before importing any module from keras. | |
import tensorflow as tf | |
from keras.backend.tensorflow_backend import set_session | |
config = tf.ConfigProto() | |
config.gpu_options.allow_growth = True | |
config.gpu_options.visible_device_list = "0" #only the gpu 0 is allowed | |
set_session(tf.Session(config=config)) |
For TensorFlow v2 and Keras this call can be updated to
import tf
from keras.backend import set_session
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = "0" # only the gpu 0 is allowed
set_session(tf.compat.v1.Session(config=config))
or
import tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank You!