To verify whether Keras is using the GPU version of TensorFlow, follow these steps:
-
Check TensorFlow Version
First, confirm that the installed TensorFlow version supports GPU. Use the following code to check the TensorFlow version:
pythonimport tensorflow as tf print(tf.__version__)Ensure the version is TensorFlow 1.x (1.4 or higher) or TensorFlow 2.x, as these versions automatically support GPU when CUDA and cuDNN are correctly installed.
-
Check GPU Availability
Next, use TensorFlow's methods to verify if GPU is detected. You can use the following code snippet:
pythonfrom tensorflow.python.client import device_lib print(device_lib.list_local_devices())Alternatively, use a simpler approach:
pythonprint(tf.config.list_physical_devices('GPU'))If the output includes GPU-related information (e.g., devices with 'GPU' in their name), it confirms TensorFlow is utilizing the GPU.
-
Run a Simple TensorFlow Operation to Observe GPU Utilization
Execute a basic TensorFlow computation and monitor GPU utilization using the system Task Manager (on Windows) or commands (e.g.,
nvidia-smion Linux). Here is a simple TensorFlow computation example:pythontf.random.normal([1000, 1000]).gpu()After running this code, observe GPU utilization. A significant increase typically indicates TensorFlow is using the GPU for computation.
-
Check Keras Backend
Although Keras is a high-level neural network API, it typically uses TensorFlow as its computational backend. Check the current backend library with the following code:
pythonfrom keras import backend as K print(K.backend())If the output is 'tensorflow', Keras is using TensorFlow as the backend. Combined with the previous steps, this confirms Keras is also leveraging the GPU.
By following these steps, you can systematically verify whether Keras is using the GPU version of TensorFlow. These steps ensure your model training process effectively utilizes GPU resources, thereby enhancing training speed and efficiency.