# 构建计算图 API

315人阅读 评论(0)

## 类：tf.Graph

1. 一系列的操作（Operation）对象，代表一个计算单元；
2. 还有张量（Tensor）对象，代表了在操作对象之间流动的数据单元


c = tf.constant(4.0)
assert c.graph is tf.get_default_graph()

g = tf.Graph()
with g.as_default():
# Define operations and tensors in g.
c = tf.constant(30.0)
assert c.graph is g

#### tf.Graph.as_default()

# 1. Using Graph.as_default():
g = tf.Graph()
with g.as_default():
c = tf.constant(5.0)
assert c.graph is g

# 2. Constructing and making default:
with tf.Graph().as_default() as g:
c = tf.constant(5.0)
assert c.graph is g

#### tf.Graph.as_graph_def(from_version=None, add_shapes=False)

• from_version ：可选。如果这个被设置了，将返回一个只包含被添加到这个图且version属性含有这个给定值的结点的GraphDef
• from_version: Optional. If this is set, returns a GraphDef containing only the nodes that were added to this graph since its version property had the given value.
• add_shapes: If true, adds an “_output_shapes” list attr to each node with the inferred shapes of each of its outputs.

A GraphDef protocol buffer.

• ValueError: 如果这个 graph_def 太大了。

#### tf.Graph.control_dependencies(control_inputs)

with 关键字一起用于指定上下文中所有的操作结构的 control_inputs 属性的控制依赖。例如：

with g.control_dependencies([a, b, c]):
# d 和 e 将只会在 a, b 和 c 运行完毕后才运行。
d = ...
e = ...

with g.control_dependencies([a, b]):
# Ops constructed here run after a and b.
with g.control_dependencies([c, d]):
# Ops constructed here run after a, b, c, and d.

with g.control_dependencies([a, b]):
# 在这里创建的操作将在 a 和 b运行之后才运行
with g.control_dependencies(None):
# 在这里创建的操作将正常运行，不会等待 a 或 b
with g.control_dependencies([c, d]):
# 在这里创建的操作将在 c 和 d运行之后才运行，不会等待 a 或 b

# 错误
def my_func(pred, tensor):
t = tf.matmul(tensor, tensor)
with tf.control_dependencies([pred]):
# 这个 matmul 操作是在上下文之外创建的，所以不会添加任何控制依赖
return t

# 正确
def my_func(pred, tensor):
with tf.control_dependencies([pred]):
# 这个 matmul 操作是在上下文中创建的，所以控制依赖会被添加
return tf.matmul(tensor, tensor)

• control_inputs：一个操作和张量对象的列表，它们将在运行在上下文中定义的操作之前运行。当然也可以设置为 None 来清空控制依赖。

• TypeError ：如果 control_inputs 不是一个 Operation 或 Tensor 对象的列表。

#### tf.Graph.device(device_name_or_function)

device_name_or_function 这个参数可以是一个设备的名称，一个 device function ，或者是 None：

• 如果它是一个设备的名称，则素有在这个上下文中创建的操作将会被分配给那个名称的设备，除非在上下文中被一个嵌套的 device() 重写。
• If it is a function, it will be treated as a function from Operation objects to device name strings, and invoked each time a new Operation is created. The Operation will be assigned to the device with the returned name.
• If it is None, all device() invocations from the enclosing context will be ignored.

For information about the valid syntax of device name strings, see the documentation inDeviceNameUtils.

with g.device('/gpu:0'):
# 所有在此上下文中创建的操作将被放置在 GPU 0 中。
with g.device(None):
# 所有在此上下文中创建的操作将不会被分配给任何设备。

# Defines a function from Operation to device string.
def matmul_on_gpu(n):
if n.type == "MatMul":
return "/gpu:0"
else:
return "/cpu:0"

with g.device(matmul_on_gpu):
# All operations of type "MatMul" constructed in this context
# will be placed on GPU 0; all other operations will be placed
# on CPU 0.

• device_name_or_function ：在上下文中使用的设备的名称或方法。

A context manager that specifies the default device to use for newly created ops.

#### tf.Graph.name_scope(name)

A graph maintains a stack of name scopes. A with name_scope(...): statement pushes a new name onto the stack for the lifetime of the context.

The name argument will be interpreted as follows:

• A string (not ending with ‘/’) will create a new name scope, in which name is appended to the prefix of all operations created in the context. If name has been used before, it will be made unique by calling self.unique_name(name).
• A scope previously captured from a with g.name_scope(...) as scope: statement will be treated as an “absolute” name scope, which makes it possible to re-enter existing scopes.
• A value of None or the empty string will reset the current name scope to the top-level (empty) name scope.

with tf.Graph().as_default() as g:
c = tf.constant(5.0, name="c")
assert c.op.name == "c"
c_1 = tf.constant(6.0, name="c")
assert c_1.op.name == "c_1"

# Creates a scope called "nested"
with g.name_scope("nested") as scope:
nested_c = tf.constant(10.0, name="c")
assert nested_c.op.name == "nested/c"

# Creates a nested scope called "inner".
with g.name_scope("inner"):
nested_inner_c = tf.constant(20.0, name="c")
assert nested_inner_c.op.name == "nested/inner/c"

# Create a nested scope called "inner_1".
with g.name_scope("inner"):
nested_inner_1_c = tf.constant(30.0, name="c")
assert nested_inner_1_c.op.name == "nested/inner_1/c"

# Treats scope as an absolute name scope, and
# switches to the "nested/" scope.
with g.name_scope(scope):
nested_d = tf.constant(40.0, name="d")
assert nested_d.op.name == "nested/d"

with g.name_scope(""):
e = tf.constant(50.0, name="e")
assert e.op.name == "e"

The name of the scope itself can be captured by with g.name_scope(...) as scope:, which stores the name of the scope in the variable scope. This value can be used to name an operation that represents the overall result of executing the ops in a scope. For example:

inputs = tf.constant(...)
with g.name_scope('my_layer') as scope:
weights = tf.Variable(..., name="weights")
biases = tf.Variable(..., name="biases")
affine = tf.matmul(inputs, weights) + biases
output = tf.nn.relu(affine, name=scope)

[A-Za-z0-9.][A-Za-z0-9_.\\-/]* (for scopes at the root)
[A-Za-z0-9_.\\-/]* (for other scopes)

• name: A name for the scope.

• A context manager that installs name as a new name scope.

• ValueError: If name is not a valid scope name.

A Graph instance supports an arbitrary number of “collections” that are identified by name. For convenience when building a large graph, collections can store groups of related objects: for example, the tf.Variable uses a collection (named tf.GraphKeys.GLOBAL_VARIABLES) for all variables that are created during the construction of a graph. The caller may define additional collections by specifying a new name.

#### tf.Graph.add_to_collection(name, value)

names can be any iterable, but if names is a string, it is treated as a single collection name.

##### Args:
• names: The keys for the collections to add to. The GraphKeys class contains many standard names for collections.
• value: The value to add to the collections.

#### tf.Graph.get_collection(name, scope=None)

Returns a list of values in the collection with the given name.

This is different from get_collection_ref() which always returns the actual collection list if it exists in that it returns a new list each time it is called.

##### Args:
• name: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
• scope: (Optional.) If supplied, the resulting list is filtered to include only items whose nameattribute matches using re.match. Items without a name attribute are never returned if a scope is supplied and the choice or re.match means that a scope without special tokens filters by prefix.
##### Returns:

The list of values in the collection with the given name, or an empty list if no value has been added to that collection. The list contains the values in the order under which they were collected.

#### tf.Graph.get_collection_ref(name)

Returns a list of values in the collection with the given name.

If the collection exists, this returns the list itself, which can be modified in place to change the collection. If the collection does not exist, it is created as an empty list and the list is returned.

This is different from get_collection() which always returns a copy of the collection list if it exists and never creates an empty collection.

##### Args:
• name: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
##### Returns:

The list of values in the collection with the given name, or an empty list if no value has been added to that collection.

#### tf.Graph.as_graph_element(obj, allow_tensor=True, allow_operation=True)

Returns the object referred to by obj, as an Operation or Tensor.

This function validates that obj represents an element of this graph, and gives an informative error message if it is not.

This function is the canonical way to get/validate an object of one of the allowed types from an external argument reference in the Session API.

This method may be called concurrently from multiple threads.

##### Args:
• obj: A Tensor, an Operation, or the name of a tensor or operation. Can also be any object with an _as_graph_element() method that returns a value of one of these types.
• allow_tensor: If true, obj may refer to a Tensor.
• allow_operation: If true, obj may refer to an Operation.
##### Returns:

The Tensor or Operation in the Graph corresponding to obj.

##### Raises:
• TypeError: If obj is not a type we support attempting to convert to types.
• ValueError: If obj is of an appropriate type but invalid. For example, an invalid string.
• KeyError: If obj is not an object in the graph.

#### tf.Graph.get_operation_by_name(name)

Returns the Operation with the given name.

This method may be called concurrently from multiple threads.

##### Args:
• name: The name of the Operation to return.
##### Returns:

The Operation with the given name.

##### Raises:
• TypeError: If name is not a string.
• KeyError: If name does not correspond to an operation in this graph.

#### tf.Graph.get_tensor_by_name(name)

Returns the Tensor with the given name.

This method may be called concurrently from multiple threads.

##### Args:
• name: The name of the Tensor to return.
##### Returns:

The Tensor with the given name.

##### Raises:
• TypeError: If name is not a string.
• KeyError: If name does not correspond to a tensor in this graph.

#### tf.Graph.get_operations()

Return the list of operations in the graph.

You can modify the operations in place, but modifications to the list such as inserts/delete have no effect on the list of operations known to the graph.

This method may be called concurrently from multiple threads.

##### Returns:

A list of Operations.

#### tf.Graph.seed

The graph-level random seed of this graph.

#### tf.Graph.unique_name(name, mark_as_used=True)

Return a unique operation name for name.

Note: You rarely need to call unique_name() directly. Most of the time you just need to create with g.name_scope() blocks to generate structured names.

unique_name is used to generate structured names, separated by "/", to help identify operations when debugging a graph. Operation names are displayed in error messages reported by the TensorFlow runtime, and in various visualization tools such as TensorBoard.

If mark_as_used is set to True, which is the default, a new unique name is created and marked as in use. If it’s set to False, the unique name is returned without actually being marked as used. This is useful when the caller simply wants to know what the name to be created will be.

##### Args:
• name: The name for an operation.
• mark_as_used: Whether to mark this name as being used.
##### Returns:

A string to be passed to create_op() that will be used to name the operation being created.

#### tf.Graph.version

Returns a version number that increases as ops are added to the graph.

Note that this is unrelated to the GraphDef version.

#### tf.Graph.graph_def_versions

The GraphDef version information of this graph.

For details on the meaning of each version, see GraphDef.

##### Returns:

A VersionDef.

#### tf.Graph.create_op(op_type, inputs, dtypes, input_types=None, name=None, attrs=None, op_def=None, compute_shapes=True, compute_device=True)

Creates an Operation in this graph.

This is a low-level interface for creating an Operation. Most programs will not call this method directly, and instead use the Python op constructors, such as tf.constant(), which add ops to the default graph.

##### Args:
• op_type: The Operation type to create. This corresponds to the OpDef.name field for the proto that defines the operation.
• inputs: A list of Tensor objects that will be inputs to the Operation.
• dtypes: A list of DType objects that will be the types of the tensors that the operation produces.
• input_types: (Optional.) A list of DTypes that will be the types of the tensors that the operation consumes. By default, uses the base DType of each input in inputs. Operations that expect reference-typed inputs must specify input_types explicitly.
• name: (Optional.) A string name for the operation. If not specified, a name is generated based on op_type.
• attrs: (Optional.) A dictionary where the key is the attribute name (a string) and the value is the respective attr attribute of the NodeDef proto that will represent the operation (an AttrValueproto).
• op_def: (Optional.) The OpDef proto that describes the op_type that the operation will have.
• compute_shapes: (Optional.) If True, shape inference will be performed to compute the shapes of the outputs.
• compute_device: (Optional.) If True, device functions will be executed to compute the device property of the Operation.
##### Raises:
• TypeError: if any of the inputs is not a Tensor.
• ValueError: if colocation conflicts with existing device assignment.
##### Returns:

An Operation object.

#### tf.Graph.gradient_override_map(op_type_map)

EXPERIMENTAL: A context manager for overriding gradient functions.

This context manager can be used to override the gradient function that will be used for ops within the scope of the context.

For example:

@tf.RegisterGradient("CustomSquare")
# ...

with tf.Graph().as_default() as g:
c = tf.constant(5.0)
s_1 = tf.square(c)  # Uses the default gradient for tf.square.
s_2 = tf.square(s_2)  # Uses _custom_square_grad to compute the

##### Args:
• op_type_map: A dictionary mapping op type strings to alternative op type strings.
##### Returns:

A context manager that sets the alternative op type to be used for one or more ops created in that context.

##### Raises:
• TypeError: If op_type_map is not a dictionary mapping strings to strings.

#### tf.Graph.building_function

Returns True iff this graph represents a function.

#### tf.Graph.clear_collection(name)

Clears all values in a collection.

##### Args:
• name: The key for the collection. The GraphKeys class contains many standard names for collections.

#### tf.Graph.colocate_with(op, ignore_existing=False)

Returns a context manager that specifies an op to colocate with.

Note: this function is not for public use, only for internal libraries.

For example:

a = tf.Variable([1.0])
with g.colocate_with(a):
b = tf.constant(1.0)


b and c will always be colocated with a, no matter where a is eventually placed.

##### Args:
• op: The op to colocate all created ops with.
• ignore_existing: If true, only applies colocation of this op within the context, rather than applying all colocation properties on the stack.
##### Raises:
• ValueError: if op is None.
##### Yields:

A context manager that specifies the op with which to colocate newly created ops.

#### tf.Graph.container(container_name)

Returns a context manager that specifies the resource container to use.

Stateful operations, such as variables and queues, can maintain their states on devices so that they can be shared by multiple processes. A resource container is a string name under which these stateful operations are tracked. These resources can be released or cleared with tf.Session.reset().

For example:

with g.container('experiment0'):
# All stateful Operations constructed in this context will be placed
# in resource container "experiment0".
v1 = tf.Variable([1.0])
v2 = tf.Variable([2.0])
with g.container("experiment1"):
# All stateful Operations constructed in this context will be
# placed in resource container "experiment1".
v3 = tf.Variable([3.0])
q1 = tf.FIFOQueue(10, tf.float32)
# All stateful Operations constructed in this context will be
# be created in the "experiment0".
v4 = tf.Variable([4.0])
q1 = tf.FIFOQueue(20, tf.float32)
with g.container(""):
# All stateful Operations constructed in this context will be
# be placed in the default resource container.
v5 = tf.Variable([5.0])
q3 = tf.FIFOQueue(30, tf.float32)

# Resets container "experiment0", after which the state of v1, v2, v4, q1
# will become undefined (such as uninitialized).
tf.Session.reset(target, ["experiment0"])

##### Args:
• container_name: container name string.
##### Returns:

A context manager for defining resource containers for stateful ops, yields the container name.

#### tf.Graph.get_all_collection_keys()

Returns a list of collections used in this graph.

#### tf.Graph.is_feedable(tensor)

Returns True if and only if tensor is feedable.

#### tf.Graph.is_fetchable(tensor_or_op)

Returns True if and only if tensor_or_op is fetchable.

#### tf.Graph.prevent_feeding(tensor)

Marks the given tensor as unfeedable in this graph.

#### tf.Graph.prevent_fetching(op)

Marks the given op as unfetchable in this graph.

## class tf.Operation

Represents a graph node that performs computation on tensors.

An Operation is a node in a TensorFlow Graph that takes zero or more Tensor objects as input, and produces zero or more Tensor objects as output. Objects of type Operation are created by calling a Python op constructor (such as tf.matmul()) or Graph.create_op().

For example c = tf.matmul(a, b) creates an Operation of type “MatMul” that takes tensors aand b as input, and produces c as output.

After the graph has been launched in a session, an Operation can be executed by passing it toSession.run(). op.run() is a shortcut for calling tf.get_default_session().run(op).

#### tf.Operation.name

The full name of this operation.

#### tf.Operation.type

The type of the op (e.g. "MatMul").

#### tf.Operation.inputs

The list of Tensor objects representing the data inputs of this op.

#### tf.Operation.control_inputs

The Operation objects on which this op has a control dependency.

Before this op is executed, TensorFlow will ensure that the operations in self.control_inputshave finished executing. This mechanism can be used to run ops sequentially for performance reasons, or to ensure that the side effects of an op are observed in the correct order.

##### Returns:

A list of Operation objects.

#### tf.Operation.outputs

The list of Tensor objects representing the outputs of this op.

#### tf.Operation.device

The name of the device to which this op has been assigned, if any.

##### Returns:

The string name of the device to which this op has been assigned, or an empty string if it has not been assigned to a device.

#### tf.Operation.graph

The Graph that contains this operation.

#### tf.Operation.run(feed_dict=None, session=None)

Runs this operation in a Session.

Calling this method will execute all preceding operations that produce the inputs needed for this operation.

N.B. Before invoking Operation.run(), its graph must have been launched in a session, and either a default session must be available, or session must be specified explicitly.

##### Args:
• feed_dict: A dictionary that maps Tensor objects to feed values. See Session.run() for a description of the valid feed values.
• session: (Optional.) The Session to be used to run to this operation. If none, the default session will be used.

#### tf.Operation.get_attr(name)

Returns the value of the attr of this op with the given name.

##### Args:
• name: The name of the attr to fetch.
##### Returns:

The value of the attr, as a Python object.

##### Raises:
• ValueError: If this op does not have an attr with the given name.

#### tf.Operation.traceback

Returns the call stack from when this operation was constructed.

#### tf.Operation.__init__(node_def, g, inputs=None, output_types=None, control_inputs=None, input_types=None, original_op=None, op_def=None)

Creates an Operation.

NOTE: This constructor validates the name of the Operation (passed as node_def.name). Valid Operation names match the following regular expression:

[A-Za-z0-9.][A-Za-z0-9_.\\-/]*

##### Args:
• node_def: node_def_pb2.NodeDef. NodeDef for the Operation. Used for attributes of node_def_pb2.NodeDef, typically name, op, and device. The input attribute is irrelevant here as it will be computed when generating the model.
• g: Graph. The parent graph.
• inputs: list of Tensor objects. The inputs to this Operation.
• output_types: list of DType objects. List of the types of the Tensors computed by this operation. The length of this list indicates the number of output endpoints of the Operation.
• control_inputs: list of operations or tensors from which to have a control dependency.
• input_types: List of DType objects representing the types of the tensors accepted by the Operation. By default uses [x.dtype.base_dtype for x in inputs]. Operations that expect reference-typed inputs must specify these explicitly.
• original_op: Optional. Used to associate the new Operation with an existing Operation(for example, a replica with the op that was replicated).
• op_def: Optional. The op_def_pb2.OpDef proto that describes the op type that this Operation represents.
##### Raises:
• TypeError: if control inputs are not Operations or Tensors, or if node_def is not a NodeDef, or if g is not a Graph, or if inputs are not tensors, or if inputs and input_types are incompatible.
• ValueError: if the node_def name is not valid.

#### tf.Operation.colocation_groups()

Returns the list of colocation groups of the op.

#### tf.Operation.node_def

Returns a serialized NodeDef representation of this operation.

##### Returns:

A NodeDef protocol buffer.

#### tf.Operation.op_def

Returns the OpDef proto that represents the type of this op.

##### Returns:

An OpDef protocol buffer.

#### tf.Operation.values()

DEPRECATED: Use outputs.

## class tf.Tensor

1. 一个张量（ Tensor ）能够作为输入传递到另一个操作（ Operation）中。 This builds a dataflow connection between operations, which enables TensorFlow to execute an entire Graph that represents a large, multi-step computation.
2. After the graph has been launched in a session, the value of the Tensor can be computed by passing it to Session.run(). t.eval() is a shortcut for callingtf.get_default_session().run(t).

In the following example, c, d, and e are symbolic Tensor objects, whereas result is a numpy array that stores a concrete value:

# 构建一个数据流图
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)

# 构建一个 Session 来执行这个图
sess = tf.Session()

# Execute the graph and store the value that e represents in result.
result = sess.run(e)


#### tf.Tensor.dtype

The DType of elements in this tensor.

#### tf.Tensor.name

The string name of this tensor.

#### tf.Tensor.value_index

The index of this tensor in the outputs of its Operation.

#### tf.Tensor.graph

The Graph that contains this tensor.

#### tf.Tensor.op

The Operation that produces this tensor as an output.

#### tf.Tensor.consumers()

Returns a list of Operations that consume this tensor.

##### Returns:

A list of Operations.

#### tf.Tensor.eval(feed_dict=None, session=None)

Evaluates this tensor in a Session.

Calling this method will execute all preceding operations that produce the inputs needed for the operation that produces this tensor.

N.B. Before invoking Tensor.eval(), its graph must have been launched in a session, and either a default session must be available, or session must be specified explicitly.

##### Args:
• feed_dict: A dictionary that maps Tensor objects to feed values. See Session.run() for a description of the valid feed values.
• session: (Optional.) The Session to be used to evaluate this tensor. If none, the default session will be used.
##### Returns:

A numpy array corresponding to the value of this tensor.

#### tf.Tensor.get_shape()

Returns the TensorShape that represents the shape of this tensor.

The shape is computed using shape inference functions that are registered in the Op for each Operation. See TensorShape for more details of what a shape represents.

The inferred shape of a tensor is used to provide shape information without having to launch the graph in a session. This can be used for debugging, and providing early error messages. For example:

c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])

print(c.get_shape())
==> TensorShape([Dimension(2), Dimension(3)])

d = tf.constant([[1.0, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]])

print(d.get_shape())
==> TensorShape([Dimension(4), Dimension(2)])

# Raises a ValueError, because c and d do not have compatible
# inner dimensions.
e = tf.matmul(c, d)

f = tf.matmul(c, d, transpose_a=True, transpose_b=True)

print(f.get_shape())
==> TensorShape([Dimension(3), Dimension(4)])


In some cases, the inferred shape may have unknown dimensions. If the caller has additional information about the values of these dimensions, Tensor.set_shape() can be used to augment the inferred shape.

##### Returns:

A TensorShape representing the shape of this tensor.

#### tf.Tensor.set_shape(shape)

Updates the shape of this tensor.

This method can be called multiple times, and will merge the given shape with the current shape of this tensor. It can be used to provide additional information about the shape of this tensor that cannot be inferred from the graph alone. For example, this can be used to provide additional information about the shapes of images:

_, image_data = tf.TFRecordReader(...).read(...)
image = tf.image.decode_png(image_data, channels=3)

# The height and width dimensions of image are data dependent, and
# cannot be computed without executing the op.
print(image.get_shape())
==> TensorShape([Dimension(None), Dimension(None), Dimension(3)])

# We know that each image in this dataset is 28 x 28 pixels.
image.set_shape([28, 28, 3])
print(image.get_shape())
==> TensorShape([Dimension(28), Dimension(28), Dimension(3)])

##### Args:
• shape: A TensorShape representing the shape of this tensor.
##### Raises:
• ValueError: If shape is not compatible with the current shape of this tensor.

#### tf.Tensor.__abs__(x, name=None)

Computes the absolute value of a tensor.

Given a tensor of real numbers x, this operation returns a tensor containing the absolute value of each element in x. For example, if x is an input element and y is an output element, this operation computes y=|x|.

See tf.complex_abs() to compute the absolute value of a complex number.

##### Args:
• x: A Tensor or SparseTensor of type float32, float64, int32, or int64.
• name: A name for the operation (optional).
##### Returns:

A Tensor or SparseTensor the same size and type as x with absolute values.

#### tf.Tensor.__add__(x, y)

Returns x + y element-wise.

NOTE: Add supports broadcasting. AddN does not. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x.

#### tf.Tensor.__and__(x, y)

Returns the truth value of x AND y element-wise.

NOTE: LogicalAnd supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor of type bool.
• y: A Tensor of type bool.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type bool.

#### tf.Tensor.__bool__()

Dummy method to prevent a tensor from being used as a Python bool.

This overload raises a TypeError when the user inadvertently treats a Tensor as a boolean (e.g. in an if statement). For example:

if tf.constant(True):  # Will raise.
# ...

if tf.constant(5) < tf.constant(7):  # Will raise.
# ...


This disallows ambiguities between testing the Python value vs testing the dynamic condition of the Tensor.

##### Raises:

TypeError.

#### tf.Tensor.__div__(x, y)

Returns x / y element-wise.

NOTE: Div supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x.

#### tf.Tensor.__floordiv__(x, y)

Divides x / y elementwise, rounding toward the most negative integer.

The same as tf.div(x,y) for integers, but uses tf.floor(tf.div(x,y)) for floating point arguments so that the result is always an integer (though possibly an integer represented as floating point). This op is generated by x // y floor division in Python 3 and in Python 2.7 with from __future__ import division.

Note that for efficiency, floordiv uses C semantics for negative numbers (unlike Python and Numpy).

x and y must have the same type, and the result will have the same type as well.

##### Args:
• x: Tensor numerator of real numeric type.
• y: Tensor denominator of real numeric type.
• name: A name for the operation (optional).
##### Returns:

x / y rounded down (except possibly towards zero for negative integers).

##### Raises:
• TypeError: If the inputs are complex.

#### tf.Tensor.__ge__(x, y, name=None)

Returns the truth value of (x >= y) element-wise.

NOTE: GreaterEqual supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8, uint16, half.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type bool.

#### tf.Tensor.__getitem__(tensor, slice_spec, var=None)

This operation extracts the specified region from the tensor. The notation is similar to NumPy with the restriction that currently only support basic indexing. That means that using a tensor as input is not currently allowed

Some useful examples:

# strip leading and trailing 2 elements
foo = tf.constant([1,2,3,4,5,6])
print(foo[2:-2].eval()) # => [3,4]

# skip every row and reverse every column
foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
print(foo[::2,::-1].eval()) # => [[3,2,1], [9,8,7]]

# Insert another dimension
foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
print(foo[tf.newaxis, :, :].eval()) # => [[[3,2,1], [9,8,7]]]
print(foo[:, tf.newaxis, :].eval()) # => [[[3,2,1]], [[9,8,7]]]
print(foo[:, :, tf.newaxis].eval()) # => [[[3],[2],[1]], [[9],[8],[7]]]

# Ellipses (3 equivalent operations)
print(foo[tf.newaxis, :, :].eval()) # => [[[3,2,1], [9,8,7]]]
print(foo[tf.newaxis, ...].eval()) # => [[[3,2,1], [9,8,7]]]
print(foo[tf.newaxis].eval()) # => [[[3,2,1], [9,8,7]]]

##### Notes:
• tf.newaxis is None as in NumPy.
• An implicit ellipsis is placed at the end of the slice_spec
• NumPy advanced indexing is currently not supported.
##### Args:
• tensor: An ops.Tensor object.
• slice_spec: The arguments to Tensor.getitem.
• var: In the case of variable slice assignment, the Variable object to slice (i.e. tensor is the read-only view of this variable).
##### Returns:

The appropriate slice of “tensor”, based on “slice_spec”.

##### Raises:
• ValueError: If a slice range is negative size.
• TypeError: If the slice indices aren’t int, slice, or Ellipsis.

#### tf.Tensor.__gt__(x, y, name=None)

Returns the truth value of (x > y) element-wise.

NOTE: Greater supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8, uint16, half.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type bool.

#### tf.Tensor.__init__(op, value_index, dtype)

Creates a new Tensor.

##### Args:
• op: An Operation. Operation that computes this tensor.
• value_index: An int. Index of the operation’s endpoint that produces this tensor.
• dtype: A DType. Type of elements stored in this tensor.
##### Raises:
• TypeError: If the op is not an Operation.

#### tf.Tensor.__invert__(x, name=None)

Returns the truth value of NOT x element-wise.

##### Args:
• x: A Tensor of type bool.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type bool.

#### tf.Tensor.__iter__()

Dummy method to prevent iteration. Do not call.

NOTE(mrry): If we register getitem as an overloaded operator, Python will valiantly attempt to iterate over the Tensor from 0 to infinity. Declaring this method prevents this unintended behavior.

##### Raises:
• TypeError: when invoked.

#### tf.Tensor.__le__(x, y, name=None)

Returns the truth value of (x <= y) element-wise.

NOTE: LessEqual supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8, uint16, half.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type bool.

#### tf.Tensor.__lt__(x, y, name=None)

Returns the truth value of (x < y) element-wise.

NOTE: Less supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8, uint16, half.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type bool.

#### tf.Tensor.__mod__(x, y)

Returns element-wise remainder of division.

NOTE: Mod supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: int32, int64, float32, float64.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x.

#### tf.Tensor.__mul__(x, y)

Dispatches cwise mul for “Dense*Dense” and “Dense*Sparse”.

#### tf.Tensor.__neg__(x, name=None)

Computes numerical negative value element-wise.

I.e., y=−x.

##### Args:
• x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x.

#### tf.Tensor.__nonzero__()

Dummy method to prevent a tensor from being used as a Python bool.

This is the Python 2.x counterpart to __bool__() above.

##### Raises:

TypeError.

#### tf.Tensor.__or__(x, y)

Returns the truth value of x OR y element-wise.

NOTE: LogicalOr supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor of type bool.
• y: A Tensor of type bool.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type bool.

#### tf.Tensor.__pow__(x, y)

Computes the power of one value to another.

Given a tensor x and a tensor y, this operation computes xy for corresponding elements in x and y. For example:

# tensor 'x' is [[2, 2], [3, 3]]
# tensor 'y' is [[8, 16], [2, 3]]
tf.pow(x, y) ==> [[256, 65536], [9, 27]]

##### Args:
• x: A Tensor of type float32, float64, int32, int64, complex64, or complex128.
• y: A Tensor of type float32, float64, int32, int64, complex64, or complex128.
• name: A name for the operation (optional).
##### Returns:

A Tensor.

#### tf.Tensor.__radd__(y, x)

Returns x + y element-wise.

NOTE: Add supports broadcasting. AddN does not. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x.

#### tf.Tensor.__rand__(y, x)

Returns the truth value of x AND y element-wise.

NOTE: LogicalAnd supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor of type bool.
• y: A Tensor of type bool.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type bool.

#### tf.Tensor.__rdiv__(y, x)

Returns x / y element-wise.

NOTE: Div supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x.

#### tf.Tensor.__rfloordiv__(y, x)

Divides x / y elementwise, rounding toward the most negative integer.

The same as tf.div(x,y) for integers, but uses tf.floor(tf.div(x,y)) for floating point arguments so that the result is always an integer (though possibly an integer represented as floating point). This op is generated by x // y floor division in Python 3 and in Python 2.7 with from __future__ import division.

Note that for efficiency, floordiv uses C semantics for negative numbers (unlike Python and Numpy).

x and y must have the same type, and the result will have the same type as well.

##### Args:
• x: Tensor numerator of real numeric type.
• y: Tensor denominator of real numeric type.
• name: A name for the operation (optional).
##### Returns:

x / y rounded down (except possibly towards zero for negative integers).

##### Raises:
• TypeError: If the inputs are complex.

#### tf.Tensor.__rmod__(y, x)

Returns element-wise remainder of division.

NOTE: Mod supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: int32, int64, float32, float64.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x.

#### tf.Tensor.__rmul__(y, x)

Dispatches cwise mul for “Dense*Dense” and “Dense*Sparse”.

#### tf.Tensor.__ror__(y, x)

Returns the truth value of x OR y element-wise.

NOTE: LogicalOr supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor of type bool.
• y: A Tensor of type bool.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type bool.

#### tf.Tensor.__rpow__(y, x)

Computes the power of one value to another.

Given a tensor x and a tensor y, this operation computes xy for corresponding elements in x and y. For example:

# tensor 'x' is [[2, 2], [3, 3]]
# tensor 'y' is [[8, 16], [2, 3]]
tf.pow(x, y) ==> [[256, 65536], [9, 27]]

##### Args:
• x: A Tensor of type float32, float64, int32, int64, complex64, or complex128.
• y: A Tensor of type float32, float64, int32, int64, complex64, or complex128.
• name: A name for the operation (optional).
##### Returns:

A Tensor.

#### tf.Tensor.__rsub__(y, x)

Returns x - y element-wise.

NOTE: Sub supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x.

#### tf.Tensor.__rtruediv__(y, x)

Divides x / y elementwise, always producing floating point results.

The same as tf.div for floating point arguments, but casts integer arguments to floating point before dividing so that the result is always floating point. This op is generated by normal x / ydivision in Python 3 and in Python 2.7 with from __future__ import division. If you want integer division that rounds down, use x // y or tf.floordiv.

x and y must have the same numeric type. If the inputs are floating point, the output will have the same type. If the inputs are integral, the inputs are cast to float32 for int8 and int16 and float64 for int32 and int64 (matching the behavior of Numpy).

##### Args:
• x: Tensor numerator of numeric type.
• y: Tensor denominator of numeric type.
• name: A name for the operation (optional).
##### Returns:

x / y evaluated in floating point.

##### Raises:
• TypeError: If x and y have different dtypes.

#### tf.Tensor.__rxor__(y, x)

x ^ y = (x | y) & ~(x & y).

#### tf.Tensor.__sub__(x, y)

Returns x - y element-wise.

NOTE: Sub supports broadcasting. More about broadcasting here

##### Args:
• x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
• y: A Tensor. Must have the same type as x.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x.

#### tf.Tensor.__truediv__(x, y)

Divides x / y elementwise, always producing floating point results.

The same as tf.div for floating point arguments, but casts integer arguments to floating point before dividing so that the result is always floating point. This op is generated by normal x / ydivision in Python 3 and in Python 2.7 with from __future__ import division. If you want integer division that rounds down, use x // y or tf.floordiv.

x and y must have the same numeric type. If the inputs are floating point, the output will have the same type. If the inputs are integral, the inputs are cast to float32 for int8 and int16 and float64 for int32 and int64 (matching the behavior of Numpy).

##### Args:
• x: Tensor numerator of numeric type.
• y: Tensor denominator of numeric type.
• name: A name for the operation (optional).
##### Returns:

x / y evaluated in floating point.

##### Raises:
• TypeError: If x and y have different dtypes.

#### tf.Tensor.__xor__(x, y)

x ^ y = (x | y) & ~(x & y).

#### tf.Tensor.device

The name of the device on which this tensor will be produced, or None.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：90458次
• 积分：2310
• 等级：
• 排名：第17503名
• 原创：136篇
• 转载：56篇
• 译文：0篇
• 评论：26条
博客专栏
 Spring Boot Demos 文章：0篇 阅读：0
阅读排行