9951 explained code solutions for 126 technologies


python-kerasHow do I use a GPU with Keras in Python?


Using a GPU with Keras in Python is a great way to speed up the training time of deep learning models. Here’s an example of how to do it:

# Import the necessary packages
import tensorflow as tf
from keras import backend as K

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

# Build your model
model = ...

# Compile your model
model.compile(...)

# Fit your model on the GPU
model.fit(..., use_multiprocessing=True)

The code above will set up a TensorFlow session that allows the GPU to grow as needed, and then compile and fit the model on the GPU.

Code explanation

  1. import tensorflow as tf: imports the TensorFlow library
  2. from keras import backend as K: imports the Keras backend library
  3. config = tf.ConfigProto(): configures the GPU
  4. config.gpu_options.allow_growth = True: allows the GPU to grow as needed
  5. sess = tf.Session(config=config): creates a TensorFlow session
  6. K.set_session(sess): sets the TensorFlow session
  7. model = ...: builds the model
  8. model.compile(...): compiles the model
  9. model.fit(..., use_multiprocessing=True): fits the model on the GPU

For more information, see the following links:

Edit this code on GitHub