Open In App

Python – tensorflow.DeviceSpec.from_string()

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

from_string is used to generate DeviceSpec from string.



Syntax: tensorflow.DeviceSpec.from_string( spec )

Parameters:



  • spec: It is a string of the form  /job:/replica:/task:/device:CPU: or /job:/replica:/task:/device:GPU with all entries being optional.

Returns: It returns a DeviceSpec object generated by the string.

Example 1:




# Importing the library
import tensorflow as tf
  
# Formatting the string
spec = '/job:gfg / replica:1 / task:2'
  
# Initializing Device Specification
device_spec = tf.DeviceSpec.from_string(spec)
  
# Printing the DeviceSpec object
print('Device Spec: ', device_spec)

Output:


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

Example 2:




# Importing the library
import tensorflow as tf
  
# Formatting the string
spec = '/job:gfg / replica:2 / task:3'
  
# Initializing Device Specification
device_spec = tf.DeviceSpec.from_string(spec)
  
# Printing the DeviceSpec object
print('Device Spec: ', device_spec)

Output:


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

Article Tags :