Reputation: 523
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
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
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
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