Class Operation
Represents a graph node that performs computation on tensors.
Aliases:
- Class
tf.compat.v1.Operation
- Class
tf.compat.v2.Operation
tf.matmulAn 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 ) or tf.Graph.create_op.
For exa
mple = tf.m`a`tmul(`a`, `b`)
rea
tes a
n Operation
of type "Ma
tMul" tha
t ta
kes tensors a
a
nd b
a
s input, a
nd produes
a
s output.
After the graph has been launched in a session, an Operation
can be executed by passing it to tf.Session.run
. op.run
() is a shortcut for calling tf.compat.v1.get_default_session().run(op).
init
__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 theOperation
. Used for attributes ofnode_def
_pb2.NodeDef
, typicallyname
,op
, anddevice
. Theinput
attribute is irrelevant here as it will be computed when generating the model.g
:Graph
. The parentg
raph.input
s: list ofTensor
objects. Theinput
s to thisOperation
.output_types
: list ofDType
objects. List of the types of theTensor
s computed by thisop
eration. The leng
th of this list indicates the number of output endpoints of theOperation
.control_inputs
: list ofop
erations or tensors from which to have a control dependency.input
_types: List ofDType
objects representing
the types of the tensors accepted by theOperation
. By default uses[x.dtype.base_dtype for x in inputs]
.Operation
s that expect reference-typedinput
s must specify these explicitly.original_op
: Optional. Used to associate the newOperation
with an existing
Operation
(for example, a replica with theop
that was replicated).op
_def: Optional. Theop
_def_pb2.OpDef proto that describes theop
type that thisOperation
represents.
Raises:
TypeError
: if controlinputs
are not Operations or Tensors, or ifnode_def
is not aNodeDef
, or ifg
is not aGraph
, or ifinputs
are not tensors, or ifinputs
andinput_types
are incompatible.ValueError
: if thenode_def
name is not valid.
Properties
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_inputs
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.
Returns:
A list of Operation
objects.
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.
graph
The Graph
that contains this operation.
inputs
The list of Tensor
objects representing the data inputs of this op.
name
The full name of this operation.
node_def
Returns the NodeDef
representation of this operation.
Returns:
NodeDefA protocol buffer.
op_def
Returns the OpDef
proto that represents the type of this op.
Returns:
OpDefAn protocol buffer.
outputs
The list of Tensor
objects representing the outputs of this op.
traceback
Returns the call stack from when this operation was constructed.
traceback_with_start_lines
Same as traceback but includes start line of function definition.
Returns:
A list of 5-tuples (filename, lineno, name, code, func_start_lineno).
type
The type of the op (e.g. "MatMul"
).
Methods
colocation_groups
colocation_groups()
Returns the list of colocation groups of the op.
get_attr
get_attr(name)
Returns the value of the attr of this op with the given name
.
Args:
name
: Thename
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 givenname
.
run
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.
Operation.run()N.B. Before invoking , 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 mapsTensor
objects to feed values. Seetf.Session.run
for a description of the valid feed values.session
: (Optional.) TheSession
to be used to run to this operation. If none, the defaultsession
will be used.
values
values()
DEPRECATED: Use outputs.