当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tensorflow.DeviceSpec.parse_from_string()用法及代码示例


TensorFlow是Google设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

parse_from_string()用于将DeviceSpec名称解析为其组件。

用法:tensorflow.DeviceSpec.parse_from_string( spec )

参数:

  • spec:它是格式为/job:/副本:/task:/device:CPU:或/job:/副本:/task:/device:GPU的字符串,所有字段均为可选。

返回值:它返回一个DeviceSpec对象。



范例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())

输出:

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

范例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())

输出:

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






注:本文由纯净天空筛选整理自 Python – tensorflow.DeviceSpec.parse_from_string()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。