class tf.DeviceSpec
Represents a (possibly partial) specification for a TensorFlow device.
DeviceSpec
s are used throughout TensorFlow to describe where state is stored and computations occur. Using DeviceSpec
allows you to parse device spec strings to verify their validity, merge them or compose them programmatically.
Example:
# 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)
If a DeviceSpec
is partially specified, it will be merged with other DeviceSpec
s according to the scope in which it is defined. DeviceSpec
components defined in inner scopes take precedence over those defined in outer scopes.
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.
A DeviceSpec
consists of 5 components -- each of which is optionally specified:
job
replica
task
__init__(job=None, replica=None, task=None, device_type=None, device_index=None)
Create a new DeviceSpec
object.
job
: string. Optional job name.replica
: int. Optional replica index.task
: int. Optional task index.device_type
: Optional device type string (e.g. "CPU" or "GPU")device_index
: int. Optional device index. If left unspecified, device represents 'any' device_index.from_string(spec)
Construct a DeviceSpec
from a string.
spec
: a string of the form /job:A DeviceSpec.
merge_from(dev)
Merge the properties of "dev" into this DeviceSpec
.
dev
: a DeviceSpec
.parse_from_string(spec)
Parse a DeviceSpec
name into its components.
spec
: a string of the form /job:The DeviceSpec
.
ValueError
: if the spec was not valid.to_string()
Return a string representation of this DeviceSpec
.
a string of the form /job:
Defined in tensorflow/python/framework/device.py
.
© 2017 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/api_docs/python/tf/DeviceSpec