Skip to content

Instantly share code, notes, and snippets.

@raven4752
Last active February 2, 2023 13:42
Show Gist options
  • Save raven4752/5bb381219ff87c4d9597702b3104349d to your computer and use it in GitHub Desktop.
Save raven4752/5bb381219ff87c4d9597702b3104349d to your computer and use it in GitHub Desktop.
limit gpu memory usage of keras
#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))
@yustiks
Copy link

yustiks commented Sep 18, 2019

Thank You!

@mexicantexan
Copy link

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