How to list physical devices in TensorFlow

Physical devices are hardware devices present on the host machine. By default all discovered CPU and GPU devices are considered visible. tf.config.list_physical_devices allows querying the physical hardware resources prior to runtime initialization.

List the number of visible CPUs on the host



import tensorflow as tf
physical_devices = tf.config.list_physical_devices('CPU')
for dev in physical_devices:
  print(dev)


#### Output ####
PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')


List the number of visible GPUs on the host



import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
for dev in physical_devices:
  print(dev)

#### Output ####
PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')

List all visible physical devices on the host



import tensorflow as tf
physical_devices = tf.config.list_physical_devices()
for dev in physical_devices:
  print(dev)


### Output ####
PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')
PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')


Follow US on Twitter: