Tensorflow with GPU, how to see tensorflow is using the GPU?

Question:

Trying to install tensorflow to work with the GPU. Some documentation I see says tensorflow comes out of box with gpu support when detected. If so, what command can I use to see tensorflow is using my GPU? I have seen other documentation saying you need tensorflow-gpu installed. I have tried both but do not see how my GPU is being used?

Asked By: lceans

||

Answers:

Tensorflow 2 GPU checking can be found here,

Deprecated, https://www.tensorflow.org/api_docs/python/tf/test/is_gpu_available

https://www.tensorflow.org/api_docs/python/tf/config/list_physical_devices

Check if Tensorflow was built with CUDA (GPU) support,

https://www.tensorflow.org/api_docs/python/tf/test/is_built_with_cuda

Tensorflow GPU guide,

https://www.tensorflow.org/guide/gpu

Code

import tensorflow as tf

print(tf.config.list_physical_devices('GPU'))
# [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

print(tf.test.is_built_with_cuda)
# <function is_built_with_cuda at 0x7f4f5730fbf8>

print(tf.test.gpu_device_name())
# /device:GPU:0

print(tf.config.get_visible_devices())
# [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
Answered By: B200011011

Go to command line and run Python

The following example lists the number of visible GPUs on the host.
Docs

import tensorflow as tf
devices = tf.config.list_physical_devices('GPU')
print(len(devices)) 

For CUDA
Docs

import tensorflow as tf
tf.test.is_built_with_cuda()

Returns whether TensorFlow was built with CUDA (GPU) support.
Docs

You can check with following function too but it’s deprecated**

import tensorflow as tf

tf.test.is_gpu_available()

Both returns True if your GPU is available

Answered By: Sekomer
print(tf.config.list_physical_devices('GPU'))

[PhysicalDevice(name=’/physical_device:GPU:0′, device_type=’GPU’)]

Answered By: Laurie King
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.