Bipin Maharjan
Bipin Maharjan

Reputation: 523

How to Know if Keras is using GPU or CPU

Is there any way to check if the Keras framework is using the GPU or CPU for training the model?

I am training my model on GPU using keras but its so slow that I'm unsure if it's using CPU or GPU for training.

Upvotes: 1

Views: 7808

Answers (3)

peng liu
peng liu

Reputation: 1

here is the ref demo :

import tensorflow as tf
physical_device = tf.config.experimental.list_physical_devices('GPU')
print(f'Device found : {physical_device}') 

Upvotes: 0

Gerry P
Gerry P

Reputation: 8092

First lets make sure tensorflow is detecting your GPU. Run the code below. If number of GPUs=0 it is not detecting your GPU. For tensorflow to use the GPU you need to have the Cuda toolkit and Cudnn installed. If no GPU is detected and you are using Anaconda reinstall tensorflow with Conda. It automatically installs the toolkit and Cudnn. Pip does not install these when you use it to install tensorflow.

import tensorflow as tf
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
print(tf.__version__)
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
tf.test.is_gpu_available()
!python --version

Upvotes: 1

Bipin Maharjan
Bipin Maharjan

Reputation: 523

First, you need to find the GPU device:

physical_device = tf.config.experimental.list_physical_devices('GPU')
print(f'Device found : {physical_device}')

then you can check if your GPU device is on Used for training or not with this code:

tf.config.experimental.get_memory_growth(physical_device[0])

if this code returns False or nothing then you can run this code below to set GPU for training

tf.config.experimental.set_memory_growth(physical_device[0],True)

Upvotes: 2

Related Questions