<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/framework/ops.py#L1093-L1691) |

Represents a graph node that performs computation on tensors.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.Operation`](https://www.tensorflow.org/api_docs/python/tf/Operation)

<br />

    tf.Operation(
        *args, **kwargs
    )

An `Operation` is a node in a [`tf.Graph`](https://www.tensorflow.org/api_docs/python/tf/Graph) that takes zero or more `Tensor`
objects as input, and produces zero or more `Tensor` objects as output.
Objects of type `Operation` are created by calling a Python op constructor
(such as [`tf.matmul`](https://www.tensorflow.org/api_docs/python/tf/linalg/matmul)) within a [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) or under a [`tf.Graph.as_default`](https://www.tensorflow.org/api_docs/python/tf/Graph#as_default)
context manager.

For example, within a [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function), `c = tf.matmul(a, b)` creates an
`Operation` of type "MatMul" that takes tensors `a` and `b` as input, and
produces `c` as output.

If a [`tf.compat.v1.Session`](https://www.tensorflow.org/api_docs/python/tf/compat/v1/Session) is used, an `Operation` of a [`tf.Graph`](https://www.tensorflow.org/api_docs/python/tf/Graph) 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)`.

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `control_inputs` | The `Operation` objects on which this op has a control dependency. <br /> 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. |
| `device` | The name of the device to which this op has been assigned, if any. |
| `graph` | <br /> <br /> |
| `inputs` | The sequence of `Tensor` objects representing the data inputs of this op. |
| `name` | <br /> <br /> |
| `node_def` | <br /> <br /> |
| `op_def` | <br /> <br /> |
| `outputs` | <br /> <br /> |
| `traceback` | Returns the call stack from when this operation was constructed. |
| `type` | <br /> <br /> |

<br />

## Methods

### `colocation_groups`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/framework/ops.py#L1295-L1312)

    colocation_groups() -> list[bytes]

Returns the list of colocation groups of the op.

### `experimental_set_type`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/framework/ops.py#L1656-L1673)

    experimental_set_type(
        type_proto
    ) -> None

Sets the corresponding node's `experimental_type` field.

See the description of `NodeDef.experimental_type` for more info.

<br />

<br />

<br />

| Args ||
|---|---|
| `type_proto` | A FullTypeDef proto message. The root type_if of this object must be `TFT_PRODUCT`, even for ops which only have a singlre return value. |

<br />

### `from_node_def`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/framework/ops.py#L1112-L1230)

    @classmethod
    from_node_def(
        node_def,
        g,
        inputs=None,
        output_types=None,
        control_inputs=None,
        input_types=None,
        original_op=None,
        op_def=None
    ) -> OperationType

Creates an `Operation`.

> [!NOTE]
> **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_.\\-/]*

<br />

<br />

<br />

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

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `TypeError` | if control inputs are not Operations or Tensors, or if `node_def` is not a `NodeDef`, or if `g` is not a `Graph`, or if `inputs` are not tensors, or if `inputs` and `input_types` are incompatible. |
| `ValueError` | if the `node_def` name is not valid. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| Operation object. ||

<br />

### `get_attr`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/framework/ops.py#L1592-L1629)

    get_attr(
        name
    )

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

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | The name of the attr to fetch. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The value of the attr, as a Python object. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If this op does not have an attr with the given `name`. |

<br />

### `run`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/framework/ops.py#L1675-L1691)

    run(
        feed_dict=None, session=None
    ) -> None

Runs this operation in a `Session`.

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

> [!NOTE]
> **Note:** Before invoking [`Operation.run()`](https://www.tensorflow.org/api_docs/python/tf/Operation#run), its graph must have been launched in a session, and either a default session must be available, or `session` must be specified explicitly.

<br />

<br />

<br />

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

<br />

### `values`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/framework/ops.py#L1314-L1316)

    values() -> tuple[Any, ...]

> [!WARNING]
> **Deprecated:** Use outputs.