HPZ001
HPZ001

Reputation: 21

How to Use Build a Keras (TF) model using GPU?

The question is pretty straightforward but nothing has really been answered.

Pretty simple, how do I know that when I build a Sequential() model in tensorflow via Keras it's going to use my GPU?

Upvotes: 0

Views: 375

Answers (1)

Laplace Ricky
Laplace Ricky

Reputation: 1687

Tensorflow uses GPU for most of the operations by default when

  1. It detects at least one GPU
  2. Its GPU support is installed and configured properly. For information regarding how to install and configure it properly for GPU support: https://www.tensorflow.org/install/gpu

One of the requirements to emphasize is that specific version of CUDA library has to be installed. e.g. Tensorflow 2.5 requires CUDA 11.2. Check here for the CUDA version required for each version of TF.

To know whether it detects GPU devices:

print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))

It will also print out debug messages by default to stderr to indicate whether the GPU support is configured properly and whether it detects GPU devices.

To validate using nvidia-smi that it is really using GPU:

You have to define a sufficiently deep and complex neural network model such that the bottleneck is in the GPU side. This can be achieved by increasing the number of layers and the number of channels in each of the layers.

When doing training or inference of the model like model.fit() and model.evaluate(), the GPU utilization in the logging from nvidia-smi should be high.

To know exactly where each operation will be executed, you can add the following line in the beginning of your codes

tf.debugging.set_log_device_placement(True)

For more information: https://www.tensorflow.org/guide/gpu

Upvotes: 1

Related Questions