Open In App
Related Articles

Python – tensorflow.DeviceSpec.device_index Attribute

Improve Article
Improve
Save Article
Save
Like Article
Like

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. 

device_index is used to get the device index of DeviceSpec.

Syntax: tensorflow.DeviceSpec.device_index

Returns: It returns the device index.

Example 1:

Python3




# Importing the library
import tensorflow as tf
  
# Initializing Device Specification
device_spec = tf.DeviceSpec(job ="gfg", device_type ="CPU", device_index = 0)
  
# Printing the DeviceSpec object
print('Device Spec: ', device_spec)
  
# Getting device index
device_index = device_spec.device_index
  
# Printing the result
print('Device Index: ', device_index)


Output:


Device Spec:  <tensorflow.python.framework.device_spec.DeviceSpecV2 object at 0x7fe5ba96aee8>
Device Index:  0

Example 2:

Python3




# Importing the library
import tensorflow as tf
  
# Initializing Device Specification
device_spec = tf.DeviceSpec(job ="gfg", device_type ="CPU", device_index = 1)
  
# Printing the DeviceSpec object
print('Device Spec: ', device_spec)
  
# Getting device index
device_index = device_spec.device_index
  
# Printing the result
print('Device Index: ', device_index)


Output:


Device Spec:  <tensorflow.python.framework.device_spec.DeviceSpecV2 object at 0x7fe5ba97f048>
Device Index:  1


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jul, 2020
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials