Type Graph
Namespace tensorflow
Parent PythonObjectContainer
Interfaces IGraph
A TensorFlow computation, represented as a dataflow graph. A `Graph` contains a set of
tf.Operation
objects,
which represent units of computation; and
tf.Tensor
objects, which represent
the units of data that flow between operations. A default `Graph` is always registered, and accessible by calling
`tf.compat.v1.get_default_graph`.
To add an operation to the default graph, simply call one of the functions
that defines a new `Operation`:
Another typical usage involves the
tf.Graph.as_default
context manager, which overrides the current default graph for the
lifetime of the context:
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. 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.
Show Example
c = tf.constant(4.0) assert c.graph is tf.compat.v1.get_default_graph()
Methods
- add_to_collection
- add_to_collection
- add_to_collection_dyn
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections
- add_to_collections_dyn
- as_graph_def
- as_graph_def_dyn
- as_graph_element
- as_graph_element_dyn
- clear_collection
- clear_collection_dyn
- colocate_with
- colocate_with
- colocate_with
- colocate_with
- colocate_with_dyn
- create_op
- create_op
- create_op_dyn
- finalize
- finalize_dyn
- get_all_collection_keys
- get_all_collection_keys_dyn
- get_collection
- get_collection_dyn
- get_collection_ref
- get_collection_ref
- get_collection_ref_dyn
- get_name_scope_dyn
- get_operation_by_name
- get_operation_by_name
- get_operation_by_name_dyn
- get_operations
- get_operations_dyn
- get_tensor_by_name
- get_tensor_by_name_dyn
- gradient_override_map
- gradient_override_map_dyn
- is_feedable
- is_feedable_dyn
- is_fetchable
- is_fetchable_dyn
- name_scope
- name_scope
- name_scope_dyn
- prevent_feeding
- prevent_feeding_dyn
- prevent_fetching
- prevent_fetching
- prevent_fetching_dyn
- switch_to_thread_local
- switch_to_thread_local_dyn
- unique_name
- unique_name_dyn
Properties
Public instance methods
void add_to_collection(string name, IEnumerable<object> value)
Stores `value` in the collection with the given `name`. Note that collections are not sets, so it is possible to add a value to
a collection several times.
Parameters
-
string
name - The key for the collection. The `GraphKeys` class contains many standard names for collections.
-
IEnumerable<object>
value - The value to add to the collection.
void add_to_collection(string name, object value)
Stores `value` in the collection with the given `name`. Note that collections are not sets, so it is possible to add a value to
a collection several times.
Parameters
-
string
name - The key for the collection. The `GraphKeys` class contains many standard names for collections.
-
object
value - The value to add to the collection.
object add_to_collection_dyn(object name, object value)
Stores `value` in the collection with the given `name`. Note that collections are not sets, so it is possible to add a value to
a collection several times.
Parameters
-
object
name - The key for the collection. The `GraphKeys` class contains many standard names for collections.
-
object
value - The value to add to the collection.
void add_to_collections(IEnumerable<string> names, DistributedDelegate value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerable<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
DistributedDelegate
value - The value to add to the collections.
void add_to_collections(string names, AggregatingVariable value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
string
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
AggregatingVariable
value - The value to add to the collections.
void add_to_collections(string names, string value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
string
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
string
value - The value to add to the collections.
void add_to_collections(IEnumerable<string> names, AggregatingVariable value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerable<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
AggregatingVariable
value - The value to add to the collections.
void add_to_collections(string names, ReplicatedVariable value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
string
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
ReplicatedVariable
value - The value to add to the collections.
void add_to_collections(string names, IEnumerable<object> value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
string
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
IEnumerable<object>
value - The value to add to the collections.
void add_to_collections(IEnumerator<string> names, string value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerator<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
string
value - The value to add to the collections.
void add_to_collections(string names, object value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
string
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
object
value - The value to add to the collections.
void add_to_collections(IEnumerator<string> names, DistributedDelegate value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerator<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
DistributedDelegate
value - The value to add to the collections.
void add_to_collections(IEnumerator<string> names, object value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerator<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
object
value - The value to add to the collections.
void add_to_collections(IEnumerator<string> names, ReplicatedVariable value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerator<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
ReplicatedVariable
value - The value to add to the collections.
void add_to_collections(IEnumerator<string> names, IEnumerable<object> value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerator<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
IEnumerable<object>
value - The value to add to the collections.
void add_to_collections(IEnumerable<string> names, string value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerable<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
string
value - The value to add to the collections.
void add_to_collections(IEnumerable<string> names, object value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerable<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
object
value - The value to add to the collections.
void add_to_collections(IEnumerable<string> names, ReplicatedVariable value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerable<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
ReplicatedVariable
value - The value to add to the collections.
void add_to_collections(IEnumerable<string> names, IEnumerable<object> value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerable<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
IEnumerable<object>
value - The value to add to the collections.
void add_to_collections(IEnumerator<string> names, AggregatingVariable value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
IEnumerator<string>
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
AggregatingVariable
value - The value to add to the collections.
void add_to_collections(string names, DistributedDelegate value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
string
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
DistributedDelegate
value - The value to add to the collections.
object add_to_collections_dyn(object names, object value)
Stores `value` in the collections given by `names`. Note that collections are not sets, so it is possible to add a value to
a collection several times. This function makes sure that duplicates in
`names` are ignored, but it will not check for pre-existing membership of
`value` in any of the collections in `names`. `names` can be any iterable, but if `names` is a string, it is treated as a
single collection name.
Parameters
-
object
names - The keys for the collections to add to. The `GraphKeys` class contains many standard names for collections.
-
object
value - The value to add to the collections.
object as_graph_def(object from_version, bool add_shapes)
Returns a serialized `GraphDef` representation of this graph. The serialized `GraphDef` can be imported into another `Graph`
(using
tf.import_graph_def
) or used with the
[C++ Session API](../../api_docs/cc/index.md). This method is thread-safe.
Parameters
-
object
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.
-
bool
add_shapes - If true, adds an "_output_shapes" list attr to each node with the inferred shapes of each of its outputs.
Returns
-
object
- A [`GraphDef`](https://www.tensorflow.org/code/tensorflow/core/framework/graph.proto) protocol buffer.
object as_graph_def_dyn(object from_version, ImplicitContainer<T> add_shapes)
Returns a serialized `GraphDef` representation of this graph. The serialized `GraphDef` can be imported into another `Graph`
(using
tf.import_graph_def
) or used with the
[C++ Session API](../../api_docs/cc/index.md). This method is thread-safe.
Parameters
-
object
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.
-
ImplicitContainer<T>
add_shapes - If true, adds an "_output_shapes" list attr to each node with the inferred shapes of each of its outputs.
Returns
-
object
- A [`GraphDef`](https://www.tensorflow.org/code/tensorflow/core/framework/graph.proto) protocol buffer.
object as_graph_element(object obj, bool allow_tensor, bool allow_operation)
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.
Parameters
-
object
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. Note: `_as_graph_element` will be called inside the graph's lock and so may not modify the graph.
-
bool
allow_tensor - If true, `obj` may refer to a `Tensor`.
-
bool
allow_operation - If true, `obj` may refer to an `Operation`.
Returns
-
object
- The `Tensor` or `Operation` in the Graph corresponding to `obj`.
object as_graph_element_dyn(object obj, ImplicitContainer<T> allow_tensor, ImplicitContainer<T> allow_operation)
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.
Parameters
-
object
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. Note: `_as_graph_element` will be called inside the graph's lock and so may not modify the graph.
-
ImplicitContainer<T>
allow_tensor - If true, `obj` may refer to a `Tensor`.
-
ImplicitContainer<T>
allow_operation - If true, `obj` may refer to an `Operation`.
Returns
-
object
- The `Tensor` or `Operation` in the Graph corresponding to `obj`.
void clear_collection(string name)
Clears all values in a collection.
Parameters
-
string
name - The key for the collection. The `GraphKeys` class contains many standard names for collections.
object clear_collection_dyn(object name)
Clears all values in a collection.
Parameters
-
object
name - The key for the collection. The `GraphKeys` class contains many standard names for collections.
IContextManager<T> colocate_with(IGraphNodeBase op, bool ignore_existing)
Returns a context manager that specifies an op to colocate with. Note: this function is not for public use, only for internal libraries.
`b` and `c` will always be colocated with `a`, no matter where `a`
is eventually placed. **NOTE** Using a colocation scope resets any existing device constraints. If `op` is `None` then `ignore_existing` must be `True` and the new
scope resets all colocation and device constraints.
Parameters
-
IGraphNodeBase
op - The op to colocate all created ops with, or `None`.
-
bool
ignore_existing - If true, only applies colocation of this op within the context, rather than applying all colocation properties on the stack. If `op` is `None`, this value must be `True`.
Show Example
a = tf.Variable([1.0]) with g.colocate_with(a): b = tf.constant(1.0) c = tf.add(a, b)
IContextManager<T> colocate_with(Operation op, bool ignore_existing)
Returns a context manager that specifies an op to colocate with. Note: this function is not for public use, only for internal libraries.
`b` and `c` will always be colocated with `a`, no matter where `a`
is eventually placed. **NOTE** Using a colocation scope resets any existing device constraints. If `op` is `None` then `ignore_existing` must be `True` and the new
scope resets all colocation and device constraints.
Parameters
-
Operation
op - The op to colocate all created ops with, or `None`.
-
bool
ignore_existing - If true, only applies colocation of this op within the context, rather than applying all colocation properties on the stack. If `op` is `None`, this value must be `True`.
Show Example
a = tf.Variable([1.0]) with g.colocate_with(a): b = tf.constant(1.0) c = tf.add(a, b)
IContextManager<T> colocate_with(object op, bool ignore_existing)
Returns a context manager that specifies an op to colocate with. Note: this function is not for public use, only for internal libraries.
`b` and `c` will always be colocated with `a`, no matter where `a`
is eventually placed. **NOTE** Using a colocation scope resets any existing device constraints. If `op` is `None` then `ignore_existing` must be `True` and the new
scope resets all colocation and device constraints.
Parameters
-
object
op - The op to colocate all created ops with, or `None`.
-
bool
ignore_existing - If true, only applies colocation of this op within the context, rather than applying all colocation properties on the stack. If `op` is `None`, this value must be `True`.
Show Example
a = tf.Variable([1.0]) with g.colocate_with(a): b = tf.constant(1.0) c = tf.add(a, b)
IContextManager<T> colocate_with(PythonClassContainer op, bool ignore_existing)
Returns a context manager that specifies an op to colocate with. Note: this function is not for public use, only for internal libraries.
`b` and `c` will always be colocated with `a`, no matter where `a`
is eventually placed. **NOTE** Using a colocation scope resets any existing device constraints. If `op` is `None` then `ignore_existing` must be `True` and the new
scope resets all colocation and device constraints.
Parameters
-
PythonClassContainer
op - The op to colocate all created ops with, or `None`.
-
bool
ignore_existing - If true, only applies colocation of this op within the context, rather than applying all colocation properties on the stack. If `op` is `None`, this value must be `True`.
Show Example
a = tf.Variable([1.0]) with g.colocate_with(a): b = tf.constant(1.0) c = tf.add(a, b)
object colocate_with_dyn(object op, ImplicitContainer<T> ignore_existing)
Returns a context manager that specifies an op to colocate with. Note: this function is not for public use, only for internal libraries.
`b` and `c` will always be colocated with `a`, no matter where `a`
is eventually placed. **NOTE** Using a colocation scope resets any existing device constraints. If `op` is `None` then `ignore_existing` must be `True` and the new
scope resets all colocation and device constraints.
Parameters
-
object
op - The op to colocate all created ops with, or `None`.
-
ImplicitContainer<T>
ignore_existing - If true, only applies colocation of this op within the context, rather than applying all colocation properties on the stack. If `op` is `None`, this value must be `True`.
Show Example
a = tf.Variable([1.0]) with g.colocate_with(a): b = tf.constant(1.0) c = tf.add(a, b)
Operation create_op(string op_type, IEnumerable<object> inputs, IEnumerable<object> dtypes, IEnumerable<DType> input_types, string name, IDictionary<object, object> attrs, object op_def, bool compute_shapes, bool compute_device)
Creates an `Operation` in this graph. (deprecated arguments) Warning: SOME ARGUMENTS ARE DEPRECATED: `(compute_shapes)`. They will be removed in a future version.
Instructions for updating:
Shapes are always computed; don't use the compute_shapes as it has no effect. 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.
Parameters
-
string
op_type - The `Operation` type to create. This corresponds to the `OpDef.name` field for the proto that defines the operation.
-
IEnumerable<object>
inputs - A list of `Tensor` objects that will be inputs to the `Operation`.
-
IEnumerable<object>
dtypes - (Optional) A list of `DType` objects that will be the types of the tensors that the operation produces.
-
IEnumerable<DType>
input_types - (Optional.) A list of `DType`s 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.
-
string
name - (Optional.) A string name for the operation. If not specified, a name is generated based on `op_type`.
-
IDictionary<object, object>
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 `AttrValue` proto).
-
object
op_def - (Optional.) The `OpDef` proto that describes the `op_type` that the operation will have.
-
bool
compute_shapes - (Optional.) Deprecated. Has no effect (shapes are always computed).
-
bool
compute_device - (Optional.) If True, device functions will be executed to compute the device property of the Operation.
Returns
-
Operation
- An `Operation` object.
Operation create_op(string op_type, IEnumerable<object> inputs, IEnumerable<object> dtypes, IEnumerable<DType> input_types, int name, IDictionary<object, object> attrs, object op_def, bool compute_shapes, bool compute_device)
Creates an `Operation` in this graph. (deprecated arguments) Warning: SOME ARGUMENTS ARE DEPRECATED: `(compute_shapes)`. They will be removed in a future version.
Instructions for updating:
Shapes are always computed; don't use the compute_shapes as it has no effect. 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.
Parameters
-
string
op_type - The `Operation` type to create. This corresponds to the `OpDef.name` field for the proto that defines the operation.
-
IEnumerable<object>
inputs - A list of `Tensor` objects that will be inputs to the `Operation`.
-
IEnumerable<object>
dtypes - (Optional) A list of `DType` objects that will be the types of the tensors that the operation produces.
-
IEnumerable<DType>
input_types - (Optional.) A list of `DType`s 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.
-
int
name - (Optional.) A string name for the operation. If not specified, a name is generated based on `op_type`.
-
IDictionary<object, object>
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 `AttrValue` proto).
-
object
op_def - (Optional.) The `OpDef` proto that describes the `op_type` that the operation will have.
-
bool
compute_shapes - (Optional.) Deprecated. Has no effect (shapes are always computed).
-
bool
compute_device - (Optional.) If True, device functions will be executed to compute the device property of the Operation.
Returns
-
Operation
- An `Operation` object.
object create_op_dyn(object op_type, object inputs, object dtypes, object input_types, object name, object attrs, object op_def, ImplicitContainer<T> compute_shapes, ImplicitContainer<T> compute_device)
Creates an `Operation` in this graph. (deprecated arguments) Warning: SOME ARGUMENTS ARE DEPRECATED: `(compute_shapes)`. They will be removed in a future version.
Instructions for updating:
Shapes are always computed; don't use the compute_shapes as it has no effect. 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.
Parameters
-
object
op_type - The `Operation` type to create. This corresponds to the `OpDef.name` field for the proto that defines the operation.
-
object
inputs - A list of `Tensor` objects that will be inputs to the `Operation`.
-
object
dtypes - (Optional) A list of `DType` objects that will be the types of the tensors that the operation produces.
-
object
input_types - (Optional.) A list of `DType`s 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.
-
object
name - (Optional.) A string name for the operation. If not specified, a name is generated based on `op_type`.
-
object
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 `AttrValue` proto).
-
object
op_def - (Optional.) The `OpDef` proto that describes the `op_type` that the operation will have.
-
ImplicitContainer<T>
compute_shapes - (Optional.) Deprecated. Has no effect (shapes are always computed).
-
ImplicitContainer<T>
compute_device - (Optional.) If True, device functions will be executed to compute the device property of the Operation.
Returns
-
object
- An `Operation` object.
void finalize()
Creates operations if needed and finalizes the graph.
object finalize_dyn()
Creates operations if needed and finalizes the graph.
IList<object> get_all_collection_keys()
Returns a list of collections used in this graph.
object get_all_collection_keys_dyn()
Returns a list of collections used in this graph.
IList<object> get_collection(string name, object scope)
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.
Parameters
-
string
name - The key for the collection. For example, the `GraphKeys` class contains many standard names for collections.
-
object
scope - (Optional.) A string. If supplied, the resulting list is filtered to include only items whose `name` attribute matches `scope` using `re.match`. Items without a `name` attribute are never returned if a scope is supplied. The choice of `re.match` means that a `scope` without special tokens filters by prefix.
Returns
-
IList<object>
- 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.
object get_collection_dyn(object name, object scope)
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.
Parameters
-
object
name - The key for the collection. For example, the `GraphKeys` class contains many standard names for collections.
-
object
scope - (Optional.) A string. If supplied, the resulting list is filtered to include only items whose `name` attribute matches `scope` using `re.match`. Items without a `name` attribute are never returned if a scope is supplied. The choice of `re.match` means that a `scope` without special tokens filters by prefix.
Returns
-
object
- 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.
IList<object> get_collection_ref(IEnumerable<string> 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.
Parameters
-
IEnumerable<string>
name - The key for the collection. For example, the `GraphKeys` class contains many standard names for collections.
Returns
-
IList<object>
- The list of values in the collection with the given `name`, or an empty list if no value has been added to that collection.
IList<object> get_collection_ref(string 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.
Parameters
-
string
name - The key for the collection. For example, the `GraphKeys` class contains many standard names for collections.
Returns
-
IList<object>
- The list of values in the collection with the given `name`, or an empty list if no value has been added to that collection.
object get_collection_ref_dyn(object 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.
Parameters
-
object
name - The key for the collection. For example, the `GraphKeys` class contains many standard names for collections.
Returns
-
object
- The list of values in the collection with the given `name`, or an empty list if no value has been added to that collection.
object get_name_scope_dyn()
Returns the current name scope.
would print the string `scope1/scope2`.
Returns
-
object
- A string representing the current name scope.
Show Example
with tf.name_scope('scope1'): with tf.name_scope('scope2'): print(tf.compat.v1.get_default_graph().get_name_scope())
Operation get_operation_by_name(string name)
Returns the `Operation` with the given `name`. This method may be called concurrently from multiple threads.
Parameters
-
string
name - The name of the `Operation` to return.
Returns
-
Operation
- The `Operation` with the given `name`.
Operation get_operation_by_name(IEnumerable<string> name)
Returns the `Operation` with the given `name`. This method may be called concurrently from multiple threads.
Parameters
-
IEnumerable<string>
name - The name of the `Operation` to return.
Returns
-
Operation
- The `Operation` with the given `name`.
object get_operation_by_name_dyn(object name)
Returns the `Operation` with the given `name`. This method may be called concurrently from multiple threads.
Parameters
-
object
name - The name of the `Operation` to return.
Returns
-
object
- The `Operation` with the given `name`.
IList<object> 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
-
IList<object>
- A list of Operations.
object get_operations_dyn()
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
-
object
- A list of Operations.
Tensor get_tensor_by_name(string name)
Returns the `Tensor` with the given `name`. This method may be called concurrently from multiple threads.
Parameters
-
string
name - The name of the `Tensor` to return.
Returns
-
Tensor
- The `Tensor` with the given `name`.
object get_tensor_by_name_dyn(object name)
Returns the `Tensor` with the given `name`. This method may be called concurrently from multiple threads.
Parameters
-
object
name - The name of the `Tensor` to return.
Returns
-
object
- The `Tensor` with the given `name`.
IContextManager<T> gradient_override_map(IDictionary<string, string> 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.
Parameters
-
IDictionary<string, string>
op_type_map - A dictionary mapping op type strings to alternative op type strings.
Returns
-
IContextManager<T>
- A context manager that sets the alternative op type to be used for one or more ops created in that context.
Show Example
@tf.RegisterGradient("CustomSquare") def _custom_square_grad(op, grad): #... with tf.Graph().as_default() as g: c = tf.constant(5.0) s_1 = tf.square(c) # Uses the default gradient for tf.square. with g.gradient_override_map({"Square": "CustomSquare"}): s_2 = tf.square(s_2) # Uses _custom_square_grad to compute the # gradient of s_2.
object gradient_override_map_dyn(object 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.
Parameters
-
object
op_type_map - A dictionary mapping op type strings to alternative op type strings.
Returns
-
object
- A context manager that sets the alternative op type to be used for one or more ops created in that context.
Show Example
@tf.RegisterGradient("CustomSquare") def _custom_square_grad(op, grad): #... with tf.Graph().as_default() as g: c = tf.constant(5.0) s_1 = tf.square(c) # Uses the default gradient for tf.square. with g.gradient_override_map({"Square": "CustomSquare"}): s_2 = tf.square(s_2) # Uses _custom_square_grad to compute the # gradient of s_2.
bool is_feedable(IGraphNodeBase tensor)
Returns `True` if and only if `tensor` is feedable.
object is_feedable_dyn(object tensor)
Returns `True` if and only if `tensor` is feedable.
bool is_fetchable(IGraphNodeBase tensor_or_op)
Returns `True` if and only if `tensor_or_op` is fetchable.
object is_fetchable_dyn(object tensor_or_op)
Returns `True` if and only if `tensor_or_op` is fetchable.
IContextManager<T> name_scope(PythonFunctionContainer name)
Returns a context manager that creates hierarchical names for operations. 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.
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.
NOTE: This constructor validates the given `name`. Valid scope
names match one of the following regular expressions: [A-Za-z0-9.][A-Za-z0-9_.\-/]* (for scopes at the root)
[A-Za-z0-9_.\-/]* (for other scopes)
Parameters
-
PythonFunctionContainer
name - A name for the scope.
Returns
-
IContextManager<T>
- A context manager that installs `name` as a new name scope.
Show Example
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"
IContextManager<T> name_scope(string name)
Returns a context manager that creates hierarchical names for operations. 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.
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.
NOTE: This constructor validates the given `name`. Valid scope
names match one of the following regular expressions: [A-Za-z0-9.][A-Za-z0-9_.\-/]* (for scopes at the root)
[A-Za-z0-9_.\-/]* (for other scopes)
Parameters
-
string
name - A name for the scope.
Returns
-
IContextManager<T>
- A context manager that installs `name` as a new name scope.
Show Example
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"
object name_scope_dyn(object name)
Returns a context manager that creates hierarchical names for operations. 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.
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.
NOTE: This constructor validates the given `name`. Valid scope
names match one of the following regular expressions: [A-Za-z0-9.][A-Za-z0-9_.\-/]* (for scopes at the root)
[A-Za-z0-9_.\-/]* (for other scopes)
Parameters
-
object
name - A name for the scope.
Returns
-
object
- A context manager that installs `name` as a new name scope.
Show Example
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"
void prevent_feeding(IGraphNodeBase tensor)
Marks the given `tensor` as unfeedable in this graph.
object prevent_feeding_dyn(object tensor)
Marks the given `tensor` as unfeedable in this graph.
void prevent_fetching(object op)
Marks the given `op` as unfetchable in this graph.
void prevent_fetching(Operation op)
Marks the given `op` as unfetchable in this graph.
object prevent_fetching_dyn(object op)
Marks the given `op` as unfetchable in this graph.
void switch_to_thread_local()
Make device, colocation and dependencies stacks thread-local. Device, colocation and dependencies stacks are not thread-local be default.
If multiple threads access them, then the state is shared. This means that
one thread may affect the behavior of another thread. After this method is called, the stacks become thread-local. If multiple
threads access them, then the state is not shared. Each thread uses its own
value; a thread doesn't affect other threads by mutating such a stack. The initial value for every thread's stack is set to the current value
of the stack when `switch_to_thread_local()` was first called.
object switch_to_thread_local_dyn()
Make device, colocation and dependencies stacks thread-local. Device, colocation and dependencies stacks are not thread-local be default.
If multiple threads access them, then the state is shared. This means that
one thread may affect the behavior of another thread. After this method is called, the stacks become thread-local. If multiple
threads access them, then the state is not shared. Each thread uses its own
value; a thread doesn't affect other threads by mutating such a stack. The initial value for every thread's stack is set to the current value
of the stack when `switch_to_thread_local()` was first called.
string unique_name(string name, bool mark_as_used)
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.
Parameters
-
string
name - The name for an operation.
-
bool
mark_as_used - Whether to mark this name as being used.
Returns
-
string
- A string to be passed to `create_op()` that will be used to name the operation being created.
object unique_name_dyn(object name, ImplicitContainer<T> mark_as_used)
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.
Parameters
-
object
name - The name for an operation.
-
ImplicitContainer<T>
mark_as_used - Whether to mark this name as being used.
Returns
-
object
- A string to be passed to `create_op()` that will be used to name the operation being created.
Public properties
bool building_function get;
Returns True iff this graph represents a function.
object building_function_dyn get;
Returns True iff this graph represents a function.
IList<object> collections get;
Returns the names of the collections known to this graph.
object collections_dyn get;
Returns the names of the collections known to this graph.
bool finalized get;
True if this graph has been finalized.
object finalized_dyn get;
True if this graph has been finalized.
object graph_def_versions get;
The GraphDef version information of this graph. For details on the meaning of each version, see
[`GraphDef`](https://www.tensorflow.org/code/tensorflow/core/framework/graph.proto).
object graph_def_versions_dyn get;
The GraphDef version information of this graph. For details on the meaning of each version, see
[`GraphDef`](https://www.tensorflow.org/code/tensorflow/core/framework/graph.proto).
object PythonObject get;
object seed get; set;
The graph-level random seed of this graph.
object seed_dyn get; set;
The graph-level random seed of this graph.
int version get;
Returns a version number that increases as ops are added to the graph. Note that this is unrelated to the
tf.Graph.graph_def_versions
.
object version_dyn get;
Returns a version number that increases as ops are added to the graph. Note that this is unrelated to the
tf.Graph.graph_def_versions
.