How to Check if Tensorflow is Using GPU?


GPU is abbreviated as Graphics Processing Unit. It is a specialized processor designed to handle the complex and repetitive calculations required for video encoding or decoding, graphics rendering and other computational intensive tasks.

It is mainly suited to perform the large scale parallel computations, which makes ideal for the machine learning and other data based applications.

The GPU in machine learning has become more popular as it reduces the time required to train complex neural networks. Tensorflow, Pytorch, keras are the built-in frameworks of machine learning which supports the GPU acceleration.

The following are the steps to check if Tensorflow is using GPU.

Installing the Tensorflow

First we have to install the Tensorflow in the python environment by using the below code.

pip install tensorflow
If you see the following output, then Tensorflow is installed.
Collecting tensorflow
  Downloading tensorflow-2.12.0-cp310-cp310-win_amd64.whl (1.9 kB)
Collecting tensorflow-intel==2.12.0
  Downloading tensorflow_intel-2.12.0-cp310-cp310-win_amd64.whl (272.8 MB)
     ---------------------------------------- 272.8/272.8 MB 948.3 kB/s eta 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 

Installing collected packages: tensorflow
Successfully installed tensorflow-2.12.0

Importing the Tensorflow

Now we have to import the Tensorflow package in the python environment.

import tensorflow as tf

Checking the available devices

Next we have to check all the available devices on our system, including CPU and GPU.

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices()) 
All the available devices are displayed.
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 11826112642512424455
xla_global_id: -1
]

Tensorflow Access

Next we will check whether the tensorflow will access the GPU or not. The output will be defined in the Boolean format, i.e. True or False where True is has access and False has no access. The following is the code.

tf.test.is_gpu_available()

The below is the output of the above code.

False

Updated on: 09-Aug-2023

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements