codecamp

TensorFlow设备规范

tf.DeviceSpec

DeviceSpec 类

定义在:tensorflow/python/framework/device.py.

表示 TensorFlow 设备的(可能只是部分)规范.

在整个 TensorFlow 中使用 DeviceSpecs 来描述存储状态和计算发生的位置.使用 DeviceSpec 可以允许您分析设备规范字符串以验证其有效性、合并或以编程方式组合它们.

例:

# Place the operations on device "GPU:0" in the "ps" job.
device_spec = DeviceSpec(job="ps", device_type="GPU", device_index=0)
with tf.device(device_spec):
  # Both my_var and squared_var will be placed on /job:ps/device:GPU:0.
  my_var = tf.Variable(..., name="my_variable")
  squared_var = tf.square(my_var)

如果 DeviceSpec 是部分指定的,它将根据它定义的范围与其他 DeviceSpecs 合并.在内部范围内定义的 DeviceSpec 组件优先于外部作用域中定义的组件.

with tf.device(DeviceSpec(job="train", )):
  with tf.device(DeviceSpec(job="ps", device_type="GPU", device_index=0):
    # Nodes created here will be assigned to /job:ps/device:GPU:0.
  with tf.device(DeviceSpec(device_type="GPU", device_index=1):
    # Nodes created here will be assigned to /job:train/device:GPU:1.

DeviceSpec 由5个组件组成,每个组件都是可选指定的:

  • job:job 名称.
  • Replica:副本索引.
  • Task:任务索引.
  • Device type:设备类型字符串(例如 “CPU” 或 “GPU”).
  • Device index:设备索引.

属性

  • job
  • replica
  • task

方法

__init__

__init__(
    job=None,
    replica=None,
    task=None,
    device_type=None,
    device_index=None
)

创建一个新的 DeviceSpec 对象.

ARGS:

  • job:字符串,可选的 job 名称.
  • replica:int,可选的副本索引.
  • task:int,可选的任务索引.
  • device_type:可选的设备类型字符串(例如 “CPU” 或 “GPU”)
  • device_index:int,可选的设备索引.如果未指定,则 device 表示 “任意的” device_index.

from_string

from_string ( spec )

从字符串构造 DeviceSpec.

ARGS:

  • spec:一个字符串形式:/job:/replica:/task:/device:CPU:  或者 /job:/replica:/task:/device:GPU: 因为 cpu 和 gpu 是互斥的.所有条目都是可选的.

返回:

返回一个 DeviceSpec(设备规范).

merge_from

merge_from ( dev )

将 “dev” 的属性合并到此 DeviceSpec.

ARGS:

  • dev:一个 DeviceSpec.

parse_from_string

parse_from_string ( spec )

将 DeviceSpec 名称解析为其组件.

ARGS:

  • spec:一个字符串形式:/job:/replica:/task:/device:CPU:  或者 /job:/replica:/task:/device:GPU: 因为 cpu 和 gpu 是互斥的.所有条目都是可选的.

返回:

返回 DeviceSpec(设备规范).

注意:

  • ValueError:如果规范无效.

to_string

to_string ()

返回此 DeviceSpec 的字符串表示形式.

返回:

返回一个字符串的形式:/job:/replica:/task:/device::.


反量化TensorFlow张量
使用diag返回对角张量
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

TensorFlow 函数介绍

TensorFlow 函数模块:tf

TensorFlow的image模块

TensorFlow使用之tf.io

TensorFlow使用之tf.keras

TensorFlow函数教程:tf.keras.applications

TensorFlow函数教程:tf.keras.backend

TensorFlow使用之tf.metrics

TensorFlow使用之tf.nn

TensorFlow使用之tf.python_io

TensorFlow 功能函数

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }