Python – tensorflow.DeviceSpec.device_index 属性
TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。
device_index用于获取 DeviceSpec 的设备索引。
Syntax: tensorflow.DeviceSpec.device_index
Returns: It returns the device index.
示例 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)
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)
输出:
Device Spec:
Device Index: 0
示例 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)
输出:
Device Spec:
Device Index: 1