Open In App

Python – tensorflow.DeviceSpec.parse_from_string()

Improve
Improve
Like Article
Like
Save
Share
Report

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

parse_from_string() is used to parse a DeviceSpec name into its components. 

Syntax: tensorflow.DeviceSpec.parse_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 fields being optional.

Returns: It returns a DeviceSpec object.

Example 1:

Python3




# Importing the library
import tensorflow as tf
  
# Initializing Device Specification
device_spec = tf.DeviceSpec(job ="gfg", replica = 5)
  
# Printing the DeviceSpec 
print('Device Spec: ', device_spec.to_string())
  
# Getting new DeviceSpec object
new_device_spec = device_spec.parse_from_string("/GPU:0")
  
# Printing the result
print('New Device Spec: ', new_device_spec.to_string())


Output:

Device Spec:  /job:gfg/replica:5
New Device Spec:  /device:GPU:0

Example 2:

Python3




# Importing the library
import tensorflow as tf
  
# Initializing Device Specification
device_spec = tf.DeviceSpec(job ="gfg", replica = 5)
  
# Printing the DeviceSpec 
print('Device Spec: ', device_spec.to_string())
  
# Getting new DeviceSpec object
new_device_spec = device_spec.parse_from_string("replica:2 / GPU:0")
  
# Printing the result
print('New Device Spec: ', new_device_spec.to_string())


Output:

Device Spec:  /job:gfg/replica:5
New Device Spec:  /replica:2/device:GPU:0




Last Updated : 01 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads