當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。