9951 explained code solutions for 126 technologies


python-kerasHow can I use a GPU to run Keras models in Python?


Using a GPU to run Keras models in Python requires a few steps. First, you must have a GPU-enabled machine with the appropriate drivers installed. Secondly, you will need to install the appropriate version of the CUDA Toolkit and cuDNN library for your GPU. Finally, you will need to install the GPU version of TensorFlow and Keras.

Once these prerequisites have been met, you can use the following code to run a Keras model on a GPU:

import tensorflow as tf
from keras import backend as K

# Set up GPU
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
K.set_session(sess)

# Load model
model = load_model('my_model.h5')

# Run model on GPU
model.fit(X_train, y_train, batch_size=32, epochs=10)

The code above sets up the GPU, loads a Keras model, and then runs it on the GPU.

Parts of the code:

  1. import tensorflow as tf: imports the TensorFlow library.
  2. from keras import backend as K: imports the Keras backend.
  3. config = tf.ConfigProto(): creates a configuration object for the GPU.
  4. config.gpu_options.allow_growth = True: sets the GPU memory to grow as needed.
  5. sess = tf.Session(config=config): creates a TensorFlow session with the GPU configuration.
  6. K.set_session(sess): sets the Keras backend to use the TensorFlow session.
  7. model = load_model('my_model.h5'): loads the Keras model.
  8. model.fit(X_train, y_train, batch_size=32, epochs=10): runs the model on the GPU.

Helpful links

Edit this code on GitHub