天天看點

TensorFlow教程之API DOC 6.3.5. FRAMEWORKBuilding Graphs

本文檔為tensorflow參考文檔,本轉載已得到tensorflow中文社群授權。

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#autogenerated-core-graph-data-structures">core graph data structures</a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#graph"><code>class tf.graph</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#operation"><code>class tf.operation</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#tensor"><code>class tf.tensor</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#autogenerated-tensor-types">tensor types</a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#dtype"><code>class tf.dtype</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#as_dtype"><code>tf.as_dtype(type_value)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#autogenerated-utility-functions">utility functions</a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#device"><code>tf.device(dev)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#name_scope"><code>tf.name_scope(name)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#control_dependencies"><code>tf.control_dependencies(control_inputs)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#convert_to_tensor"><code>tf.convert_to_tensor(value, dtype=none, name=none)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#get_default_graph"><code>tf.get_default_graph()</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#import_graph_def"><code>tf.import_graph_def(graph_def, input_map=none, return_elements=none, name=none, op_dict=none)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#autogenerated-graph-collections">graph collections</a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#add_to_collection"><code>tf.add_to_collection(name, value)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#get_collection"><code>tf.get_collection(key, scope=none)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#graphkeys"><code>class tf.graphkeys</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#autogenerated-defining-new-operations">defining new operations</a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#registergradient"><code>class tf.registergradient</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#nogradient"><code>tf.nogradient(op_type)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#registershape"><code>class tf.registershape</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#tensorshape"><code>class tf.tensorshape</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#dimension"><code>class tf.dimension</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#op_scope"><code>tf.op_scope(values, name, default_name)</code></a>

<a href="http://www.tensorfly.cn/tfdoc/api_docs/python/framework.html#get_seed"><code>tf.get_seed(op_seed)</code></a>

classes and functions for building tensorflow graphs.

a tensorflow computation, represented as a dataflow graph.

important note: this class is not thread-safe for graph construction. all operations should be created from a single thread, or external synchronization must be provided. unless otherwise specified, all methods are not thread-safe.

creates a new, empty graph.

returns a context manager that makes this <code>graph</code> the default graph.

this method should be used if you want to create multiple graphs in the same process. for convenience, a global default graph is provided, and all ops will be added to this graph if you do not create a new graph explicitly. use this method the <code>with</code> keyword to specify that ops created within the scope of a block should be added to this graph.

the default graph is a property of the current thread. if you create a new thread, and wish to use the default graph in that thread, you must explicitly add a <code>with g.as_default():</code> in that thread's function.

the following code examples are equivalent:

a context manager for using this graph as the default graph.

returns a serialized <code>graphdef</code> representation of this graph.

this method is thread-safe.

<code>from_version</code>: optional. if this is set, returns a <code>graphdef</code> containing only the nodes that were added to this graph since its <code>version</code> property had the given value.

finalizes this graph, making it read-only.

true if this graph has been finalized.

returns a context manager that specifies control dependencies.

use with the <code>with</code> keyword to specify that all operations constructed within the context should have control dependencies on <code>control_inputs</code>. for example:

multiple calls to <code>control_dependencies()</code> can be nested, and in that case a new <code>operation</code> will have control dependencies on the union of <code>control_inputs</code> from all active contexts.

n.b. the control dependencies context applies only to ops that are constructed within the context. merely using an op or tensor in the context does not add a control dependency. the following example illustrates this point:

<code>control_inputs</code>: a list of <code>operation</code> or <code>tensor</code> objects, which must be executed or computed before running the operations defined in the context.

a context manager that specifies control dependencies for all operations constructed within the context.

<code>typeerror</code>: if <code>control_inputs</code> is not a list of <code>operation</code> or <code>tensor</code> objects.

returns a context manager that specifies the default device to use.

the <code>device_name_or_function</code> argument may either be a device name string, a device function, or none:

if it is a device name string, all operations constructed in this context will be assigned to the device with that name.

if it is a function, it will be treated as 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, the default device will be cleared.

for example:

<code>device_name_or_function</code>: the device name or function to use in the context.

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

returns a context manager that creates hierarchical names for operations.

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

the <code>name</code> argument will be interpreted as follows:

a string (not ending with '/') will create a new name scope, in which <code>name</code> is appended to the prefix of all operations created in the context. if <code>name</code> has been used before, it will be made unique by calling <code>self.unique_name(name)</code>.

a scope previously captured from a <code>with g.name_scope(...) as scope:</code> statement will be treated as an "absolute" name scope, which makes it possible to re-enter existing scopes.

a value of <code>none</code> or the empty string will reset the current name scope to the top-level (empty) name scope.

the name of the scope itself can be captured by <code>with g.name_scope(...) as scope:</code>, which stores the name of the scope in the variable <code>scope</code>. this value can be used to name an operation that represents the overall result of executing the ops in a scope. for example:

<code>name</code>: a name for the scope.

a context manager that installs <code>name</code> as a new name scope.

stores <code>value</code> in the collection with the given <code>name</code>.

<code>name</code>: the key for the collection. for example, the <code>graphkeys</code> class contains many standard names for collections.

<code>value</code>: the value to add to the collection.

returns a list of values in the collection with the given <code>name</code>.

<code>key</code>: the key for the collection. for example, the <code>graphkeys</code> class contains many standard names for collections.

<code>scope</code>: (optional.) if supplied, the resulting list is filtered to include only items whose name begins with this string.

the list of values in the collection with the given <code>name</code>, 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.

returns the object referred to by <code>obj</code>, as an <code>operation</code> or <code>tensor</code>.

this function validates that <code>obj</code> 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.

<code>obj</code>: a <code>tensor</code>, an <code>operation</code>, or the name of a tensor or operation. can also be any object with an <code>_as_graph_element()</code> method that returns a value of one of these types.

<code>allow_tensor</code>: if true, <code>obj</code> may refer to a <code>tensor</code>.

<code>allow_operation</code>: if true, <code>obj</code> may refer to an <code>operation</code>.

the <code>tensor</code> or <code>operation</code> in the graph corresponding to <code>obj</code>.

<code>typeerror</code>: if <code>obj</code> is not a type we support attempting to convert to types.

<code>valueerror</code>: if <code>obj</code> is of an appropriate type but invalid. for example, an invalid string.

<code>keyerror</code>: if <code>obj</code> is not an object in the graph.

returns the <code>operation</code> with the given <code>name</code>.

<code>name</code>: the name of the <code>operation</code> to return.

the <code>operation</code> with the given <code>name</code>.

<code>typeerror</code>: if <code>name</code> is not a string.

<code>keyerror</code>: if <code>name</code> does not correspond to an operation in this graph.

returns the <code>tensor</code> with the given <code>name</code>.

<code>name</code>: the name of the <code>tensor</code> to return.

the <code>tensor</code> with the given <code>name</code>.

<code>keyerror</code>: if <code>name</code> does not correspond to a tensor in this graph.

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.

a list of operations.

returns the default device.

a string.

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.

<code>unique_name</code> 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.

<code>name</code>: the name for an <code>operation</code>.

a string to be passed to <code>create_op()</code> that will be used to name the operation being created.

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

creates an <code>operation</code> in this graph.

this is a low-level interface for creating an <code>operation</code>. most programs will not call this method directly, and instead use the python op constructors, such as <code>tf.constant()</code>, which add ops to the default graph.

<code>op_type</code>: the <code>operation</code> type to create. this corresponds to the <code>opdef.name</code> field for the proto that defines the operation.

<code>inputs</code>: a list of <code>tensor</code> objects that will be inputs to the <code>operation</code>.

<code>dtypes</code>: a list of <code>dtype</code> objects that will be the types of the tensors that the operation produces.

<code>input_types</code>: (optional.) a list of <code>dtype</code>s that will be the types of the tensors that the operation consumes. by default, uses the base <code>dtype</code> of each input in <code>inputs</code>. operations that expect reference-typed inputs must specify <code>input_types</code> explicitly.

<code>name</code>: (optional.) a string name for the operation. if not specified, a name is generated based on <code>op_type</code>.

<code>attrs</code>: (optional.) a list of <code>attrvalue</code> protos for the <code>attr</code> field of the <code>nodedef</code> proto that will represent the operation.

<code>op_def</code>: (optional.) the <code>opdef</code> proto that describes the <code>op_type</code> that the operation will have.

<code>compute_shapes</code>: (optional.) if true, shape inference will be performed to compute the shapes of the outputs.

<code>typeerror</code>: if any of the inputs is not a <code>tensor</code>.

an <code>operation</code> object.

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.

<code>op_type_map</code>: a dictionary mapping op type strings to alternative op type strings.

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

<code>typeerror</code>: if <code>op_type_map</code> is not a dictionary mapping strings to strings.

represents a graph node that performs computation on tensors.

for example <code>c = tf.matmul(a, b)</code> creates an <code>operation</code> of type "matmul" that takes tensors <code>a</code> and <code>b</code> as input, and produces <code>c</code> as output.

the full name of this operation.

the type of the op (e.g. <code>"matmul"</code>).

the list of <code>tensor</code> objects representing the data inputs of this op.

the <code>operation</code> objects on which this op has a control dependency.

before this op is executed, tensorflow will ensure that the operations in <code>self.control_inputs</code> have 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.

a list of <code>operation</code> objects.

the list of <code>tensor</code> objects representing the outputs of this op.

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

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

the <code>graph</code> that contains this operation.

runs this operation in a <code>session</code>.

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

n.b. before invoking <code>operation.run()</code>, its graph must have been launched in a session, and either a default session must be available, or <code>session</code> must be specified explicitly.

<code>session</code>: (optional.) the <code>session</code> to be used to run to this operation. if none, the default session will be used.

returns the value of the attr of this op with the given <code>name</code>.

<code>name</code>: the name of the attr to fetch.

the value of the attr, as a python object.

<code>valueerror</code>: if this op does not have an attr with the given <code>name</code>.

returns the call stack from when this operation was constructed.

creates an <code>operation</code>.

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_.-/]*

<code>node_def</code>: graph_pb2.nodedef. nodedef for the operation. used for attributes of graph_pb2.nodedef, typically "name", "op", and "device". the "input" attribute is irrelevant here as it will be computed when generating the model.

<code>g</code>: graph. the parent graph.

<code>inputs</code>: list of tensor objects. the inputs to this operation.

<code>output_types</code>: list of types_pb2.datatype. 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.

<code>control_inputs</code>: list of operations or tensors from which to have a control dependency.

<code>input_types</code>: list of types_pb2.datatype 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.

<code>original_op</code>: optional. used to associate the new operation with an existing operation (for example, a replica with the op that was replicated).

<code>op_def</code>: optional. the op_def_pb2.opdef proto that describes the op type that this operation represents.

<code>typeerror</code>: 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.

<code>valueerror</code>: if the node_def name is not valid.

returns a serialized <code>nodedef</code> representation of this operation.

returns the <code>opdef</code> proto that represents the type of this op.

deprecated: use outputs.

represents a value produced by an <code>operation</code>.

this class has two primary purposes:

a <code>tensor</code> can be passed as an input to another <code>operation</code>. this builds a dataflow connection between operations, which enables tensorflow to execute an entire <code>graph</code> that represents a large, multi-step computation.

in the following example, <code>c</code>, <code>d</code>, and <code>e</code> are symbolic <code>tensor</code> objects, whereas <code>result</code> is a numpy array that stores a concrete value:

the <code>dtype</code> of elements in this tensor.

the string name of this tensor.

the index of this tensor in the outputs of its <code>operation</code>.

the <code>graph</code> that contains this tensor.

the <code>operation</code> that produces this tensor as an output.

returns a list of <code>operation</code>s that consume this tensor.

a list of <code>operation</code>s.

evaluates this tensor in a <code>session</code>.

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

n.b. before invoking <code>tensor.eval()</code>, its graph must have been launched in a session, and either a default session must be available, or <code>session</code> must be specified explicitly.

<code>session</code>: (optional.) the <code>session</code> to be used to evaluate this tensor. if none, the default session will be used.

a numpy array corresponding to the value of this tensor.

returns the <code>tensorshape</code> that represents the shape of this tensor.

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:

in some cases, the inferred shape may have unknown dimensions. if the caller has additional information about the values of these dimensions, <code>tensor.set_shape()</code> can be used to augment the inferred shape.

a <code>tensorshape</code> representing the shape of this tensor.

updates the shape of this tensor.

this method can be called multiple times, and will merge the given <code>shape</code> 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:

<code>shape</code>: a <code>tensorshape</code> representing the shape of this tensor.

<code>valueerror</code>: if <code>shape</code> is not compatible with the current shape of this tensor.

creates a new <code>tensor</code>.

<code>op</code>: an <code>operation</code>. <code>operation</code> that computes this tensor.

<code>value_index</code>: an <code>int</code>. index of the operation's endpoint that produces this tensor.

<code>dtype</code>: a <code>types.dtype</code>. type of data stored in this tensor.

<code>typeerror</code>: if the op is not an <code>operation</code>.

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

represents the type of the elements in a <code>tensor</code>.

the following <code>dtype</code> objects are defined:

<code>tf.float32</code>: 32-bit single-precision floating-point.

<code>tf.float64</code>: 64-bit double-precision floating-point.

<code>tf.bfloat16</code>: 16-bit truncated floating-point.

<code>tf.complex64</code>: 64-bit single-precision complex.

<code>tf.int8</code>: 8-bit signed integer.

<code>tf.uint8</code>: 8-bit unsigned integer.

<code>tf.int32</code>: 32-bit signed integer.

<code>tf.int64</code>: 64-bit signed integer.

<code>tf.bool</code>: boolean.

<code>tf.string</code>: string.

<code>tf.qint8</code>: quantized 8-bit signed integer.

<code>tf.quint8</code>: quantized 8-bit unsigned integer.

<code>tf.qint32</code>: quantized 32-bit signed integer.

in addition, variants of these types with the <code>_ref</code> suffix are defined for reference-typed tensors.

the <code>tf.as_dtype()</code> function converts numpy types and string type names to a <code>dtype</code> object.

returns true if the <code>other</code> dtype will be converted to this dtype.

the conversion rules are as follows:

<code>other</code>: a <code>dtype</code> (or object that may be converted to a <code>dtype</code>).

true if a tensor of the <code>other</code> <code>dtype</code> will be implicitly converted to this <code>dtype</code>.

returns the string name for this <code>dtype</code>.

returns a non-reference <code>dtype</code> based on this <code>dtype</code>.

returns <code>true</code> if this <code>dtype</code> represents a reference type.

returns a reference <code>dtype</code> based on this <code>dtype</code>.

returns whether this is a (non-quantized) integer type.

returns whether this is a quantized data type.

returns a <code>numpy.dtype</code> based on this <code>dtype</code>.

returns a <code>types_pb2.datatype</code> enum value based on this <code>dtype</code>.

creates a new <code>datatype</code>.

note(mrry): in normal circumstances, you should not need to construct a datatype object directly. instead, use the types.as_dtype() function.

<code>type_enum</code>: a <code>types_pb2.datatype</code> enum value.

<code>typeerror</code>: if <code>type_enum</code> is not a value <code>types_pb2.datatype</code>.

returns the maximum representable value in this data type.

<code>typeerror</code>: if this is a non-numeric, unordered, or quantized type.

returns the minimum representable value in this data type.

converts the given <code>type_value</code> to a <code>dtype</code>.

a <code>dtype</code> corresponding to <code>type_value</code>.

<code>typeerror</code>: if <code>type_value</code> cannot be converted to a <code>dtype</code>.

wrapper for <code>graph.device()</code> using the default graph.

wrapper for <code>graph.name_scope()</code> using the default graph.

a context manager that installs <code>name</code> as a new name scope in the default graph.

wrapper for <code>graph.control_dependencies()</code> using the default graph.

converts the given <code>value</code> to a <code>tensor</code>.

this function converts python objects of various types to <code>tensor</code> objects. it accepts <code>tensor</code> objects, numpy arrays, python lists, and python scalars. for example:

this function can be useful when composing a new operation in python (such as <code>my_func</code> in the example above). all standard python op constructors apply this function to each of their tensor-valued inputs, which allows those ops to accept numpy arrays, python lists, and scalars in addition to <code>tensor</code> objects.

<code>value</code>: an object whose type has a registered <code>tensor</code> conversion function.

<code>dtype</code>: optional element type for the returned tensor. if missing, the type is inferred from the type of <code>value</code>.

<code>name</code>: optional name to use if a new <code>tensor</code> is created.

a <code>tensor</code> based on <code>value</code>.

<code>typeerror</code>: if no conversion function is registered for <code>value</code>.

<code>runtimeerror</code>: if a registered conversion function returns an invalid value.

returns the default graph for the current thread.

the returned graph will be the innermost graph on which a <code>graph.as_default()</code> context has been entered, or a global default graph if none has been explicitly created.

n.b. the default graph is a property of the current thread. if you create a new thread, and wish to use the default graph in that thread, you must explicitly add a <code>with g.as_default():</code> in that thread's function.

the default <code>graph</code> being used in the current thread.

imports the tensorflow graph in <code>graph_def</code> into the python <code>graph</code>.

<code>graph_def</code>: a <code>graphdef</code> proto containing operations to be imported into the default graph.

<code>input_map</code>: a dictionary mapping input names (as strings) in <code>graph_def</code> to <code>tensor</code> objects. the values of the named input tensors in the imported graph will be re-mapped to the respective <code>tensor</code>values.

<code>return_elements</code>: a list of strings containing operation names in <code>graph_def</code> that will be returned as <code>operation</code> objects; and/or tensor names in <code>graph_def</code> that will be returned as <code>tensor</code> objects.

<code>name</code>: (optional.) a prefix that will be prepended to the names in <code>graph_def</code>. defaults to <code>"import"</code>.

<code>op_dict</code>: (optional.) a dictionary mapping op type names to <code>opdef</code> protos. must contain an <code>opdef</code>proto for each op type named in <code>graph_def</code>. if omitted, uses the <code>opdef</code> protos registered in the global registry.

a list of <code>operation</code> and/or <code>tensor</code> objects from the imported graph, corresponding to the names in `return_elements'.

<code>typeerror</code>: if <code>graph_def</code> is not a <code>graphdef</code> proto, <code>input_map' is not a dictionary mapping strings to</code>tensor<code>objects, or</code>return_elements` is not a list of strings.

<code>valueerror</code>: if <code>input_map</code>, or <code>return_elements</code> contains names that do not appear in <code>graph_def</code>, or <code>graph_def</code> is not well-formed (e.g. it refers to an unknown tensor).

wrapper for <code>graph.add_to_collection()</code> using the default graph.

wrapper for <code>graph.get_collection()</code> using the default graph.

standard names to use for graph collections.

the standard library uses various well-known names to collect and retrieve values associated with a graph. for example, the <code>tf.optimizer</code> subclasses default to optimizing the variables collected under <code>tf.graphkeys.trainable_variables</code> if none is specified, but it is also possible to pass an explicit list of variables.

the following standard keys are defined:

a decorator for registering the gradient function for an op type.

this decorator is only used when defining a new op type. for an op with <code>m</code> inputs and <code>n</code> inputs, the gradient function is a function that takes the original <code>operation</code> and <code>n</code> <code>tensor</code> objects (representing the gradients with respect to each output of the op), and returns <code>m</code> <code>tensor</code> objects (representing the partial gradients with respect to each input of the op).

for example, assuming that operations of type <code>"sub"</code> take two inputs <code>x</code> and <code>y</code>, and return a single output <code>x - y</code>, the following gradient function would be registered:

the decorator argument <code>op_type</code> is the string type of an operation. this corresponds to the <code>opdef.name</code>field for the proto that defines the operation.

creates a new decorator with <code>op_type</code> as the operation type.

<code>op_type</code>: the string type of an operation. this corresponds to the <code>opdef.name</code> field for the proto that defines the operation.

specifies that ops of type <code>op_type</code> do not have a defined gradient.

this function is only used when defining a new op type. it may be used for ops such as <code>tf.size()</code> that are not differentiable. for example:

<code>typeerror</code>: if <code>op_type</code> is not a string.

a decorator for registering the shape function for an op type.

this decorator is only used when defining a new op type. a shape function is a function from an <code>operation</code> object to a list of <code>tensorshape</code> objects, with one <code>tensorshape</code> for each output of the operation.

for example, assuming that operations of type <code>"sub"</code> take two inputs <code>x</code> and <code>y</code>, and return a single output <code>x - y</code>, all with the same shape, the following shape function would be registered:

saves the "op_type" as the operation type.

represents the shape of a <code>tensor</code>.

a <code>tensorshape</code> represents a possibly-partial shape specification for a <code>tensor</code>. it may be one of the following:

fully-known shape: has a known number of dimensions and a known size for each dimension.

partially-known shape: has a known number of dimensions, and an unknown size for one or more dimension.

unknown shape: has an unknown number of dimensions, and an unknown size in all dimensions.

returns a <code>tensorshape</code> combining the information in <code>self</code> and <code>other</code>.

the dimensions in <code>self</code> and <code>other</code> are merged elementwise, according to the rules defined for <code>dimension.merge_with()</code>.

<code>other</code>: another <code>tensorshape</code>.

a <code>tensorshape</code> containing the combined information of <code>self</code> and <code>other</code>.

<code>valueerror</code>: if <code>self</code> and <code>other</code> are not compatible.

returns the concatenation of the dimension in <code>self</code> and <code>other</code>.

n.b. if either <code>self</code> or <code>other</code> is completely unknown, concatenation will discard information about the other shape. in future, we might support concatenation that preserves this information for use with slicing.

a <code>tensorshape</code> whose dimensions are the concatenation of the dimensions in <code>self</code> and <code>other</code>.

returns the rank of this shape, or none if it is unspecified.

returns a list of dimensions, or none if the shape is unspecified.

returns a list of integers or none for each dimension.

returns true iff <code>self</code> is compatible with <code>other</code>.

two possibly-partially-defined shapes are compatible if there exists a fully-defined shape that both shapes can represent. thus, compatibility allows the shape inference code to reason about partially-defined shapes. for example:

tensorshape(none) is compatible with all shapes.

tensorshape([none, none]) is compatible with all two-dimensional shapes, such as tensorshape([32, 784]), and also tensorshape(none). it is not compatible with, for example, tensorshape([none]) or tensorshape([none, none, none]).

tensorshape([32, none]) is compatible with all two-dimensional shapes with size 32 in the 0th dimension, and also tensorshape([none, none]) and tensorshape(none). it is not compatible with, for example, tensorshape([32]), tensorshape([32, none, 1]) or tensorshape([64, none]).

tensorshape([32, 784]) is compatible with itself, and also tensorshape([32, none]), tensorshape([none, 784]), tensorshape([none, none]) and tensorshape(none). it is not compatible with, for example, tensorshape([32, 1, 784]) or tensorshape([none]).

the compatibility relation is reflexive and symmetric, but not transitive. for example, tensorshape([32, 784]) is compatible with tensorshape(none), and tensorshape(none) is compatible with tensorshape([4, 4]), but tensorshape([32, 784]) is not compatible with tensorshape([4, 4]).

<code>other</code>: another tensorshape.

true iff <code>self</code> is compatible with <code>other</code>.

returns true iff <code>self</code> is fully defined in every dimension.

returns a shape based on <code>self</code> with the given rank.

this method promotes a completely unknown shape to one with a known rank.

<code>rank</code>: an integer.

a shape that is at least as specific as <code>self</code> with the given rank.

<code>valueerror</code>: if <code>self</code> does not represent a shape with the given <code>rank</code>.

returns a shape based on <code>self</code> with at least the given rank.

a shape that is at least as specific as <code>self</code> with at least the given rank.

<code>valueerror</code>: if <code>self</code> does not represent a shape with at least the given <code>rank</code>.

returns a shape based on <code>self</code> with at most the given rank.

a shape that is at least as specific as <code>self</code> with at most the given rank.

<code>valueerror</code>: if <code>self</code> does not represent a shape with at most the given <code>rank</code>.

raises an exception if <code>self</code> is not compatible with the given <code>rank</code>.

raises an exception if <code>self</code> and <code>other</code> do not have compatible ranks.

<code>valueerror</code>: if <code>self</code> and <code>other</code> do not represent shapes with the same rank.

raises exception if <code>self</code> and <code>other</code> do not represent the same shape.

this method can be used to assert that there exists a shape that both <code>self</code> and <code>other</code> represent.

<code>valueerror</code>: if <code>self</code> and <code>other</code> do not represent the same shape.

raises an exception if <code>self</code> is not fully defined in every dimension.

<code>valueerror</code>: if <code>self</code> does not have a known value for every dimension.

creates a new tensorshape with the given dimensions.

<code>dims</code>: a list of dimensions, or none if the shape is unspecified.

<code>deprecated</code>: a single integer is treated as a singleton list.

deprecated: use as_list().

returns the total number of elements, or none for incomplete shapes.

represents the value of one dimension in a tensorshape.

creates a new dimension with the given value.

raises an exception if <code>other</code> is not compatible with this dimension.

<code>other</code>: another dimension.

<code>valueerror</code>: if <code>self</code> and <code>other</code> are not compatible (see is_compatible_with).

returns true if <code>other</code> is compatible with this dimension.

two known dimensions are compatible if they have the same value. an unknown dimension is compatible with all other dimensions.

true if this dimension and <code>other</code> are compatible.

returns a dimension that combines the information in <code>self</code> and <code>other</code>.

dimensions are combined as follows:

dimension(n) .merge_with(dimension(n)) == dimension(n) dimension(n) .merge_with(dimension(none)) == dimension(n) dimension(none).merge_with(dimension(n)) == dimension(n) dimension(none).merge_with(dimension(none)) == dimension(none) dimension(n) .merge_with(dimension(m)) raises valueerror for n != m

a dimension containing the combined information of <code>self</code> and <code>other</code>.

the value of this dimension, or none if it is unknown.

returns a context manager for use when defining a python op.

this context manager validates that the given <code>values</code> are from the same graph, ensures that that graph is the default graph, and pushes a name scope.

for example, to define a new python op called <code>my_op</code>:

<code>values</code>: the list of <code>tensor</code> arguments that are passed to the op function.

<code>name</code>: the name argument that is passed to the op function.

<code>default_name</code>: the default name to use if the <code>name</code> argument is <code>none</code>.

a context manager for use in defining a python op.

returns the local seeds an operation should use given an op-specific seed.

given operation-specific seed, <code>op_seed</code>, this helper function returns two seeds derived from graph-level and op-level seeds. many random operations internally use the two seeds to allow user to change the seed globally for a graph, or for only specific operations.

<code>op_seed</code>: integer.

a tuple of two integers that should be used for the local seed of this operation.