# tf.contrib.seq2seq.Decoder

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/seq2seq/python/ops/decoder.py#L50-L150) |

An RNN Decoder abstract interface object.

Concepts used by this interface:

- `inputs`: (structure of) tensors and TensorArrays that is passed as input to the RNNCell composing the decoder, at each time step.
- `state`: (structure of) tensors and TensorArrays that is passed to the RNNCell instance as the state.
- `finished`: boolean tensor telling whether each sequence in the batch is finished.
- `outputs`: Instance of BasicDecoderOutput. Result of the decoding, at each time step.

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `batch_size` | The batch size of input values. |
| `output_dtype` | A (possibly nested tuple of...) dtype\[s\]. |
| `output_size` | A (possibly nested tuple of...) integer\[s\] or `TensorShape` object\[s\]. |
| `tracks_own_finished` | Describes whether the Decoder keeps track of finished states. <br /> Most decoders will emit a true/false `finished` value independently at each time step. In this case, the `dynamic_decode` function keeps track of which batch entries are already finished, and performs a logical OR to insert new batches to the finished set. Some decoders, however, shuffle batches / beams between time steps and `dynamic_decode` will mix up the finished state across these entries because it does not track the reshuffle across time steps. In this case, it is up to the decoder to declare that it will keep track of its own finished state by setting this property to `True`. |

<br />

## Methods

### `finalize`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/seq2seq/python/ops/decoder.py#L115-L130)

    finalize(
        outputs, final_state, sequence_lengths
    )

Called after decoding iterations complete.

<br />

<br />

<br />

| Args ||
|---|---|
| `outputs` | RNNCell outputs (possibly nested tuple of) tensor\[s\] for all time steps. |
| `final_state` | RNNCell final state (possibly nested tuple of) tensor\[s\] for last time step. |
| `sequence_lengths` | 1-D `int32` tensor containing lengths of each sequence. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `(final_outputs, final_state)`: `final_outputs` is an object containing the final decoder output, `final_state` is a (structure of) state tensors and TensorArrays. ||

<br />

### `initialize`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/seq2seq/python/ops/decoder.py#L79-L92)

    @abc.abstractmethod
    initialize(
        name=None
    )

Called before any decoding iterations.

This methods must compute initial input values and initial state.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | Name scope for any created operations. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `(finished, initial_inputs, initial_state)`: initial values of 'finished' flags, inputs and state. ||

<br />

### `step`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/seq2seq/python/ops/decoder.py#L94-L113)

    @abc.abstractmethod
    step(
        time, inputs, state, name=None
    )

Called per step of decoding (but only once for dynamic decoding).

<br />

<br />

<br />

| Args ||
|---|---|
| `time` | Scalar `int32` tensor. Current step number. |
| `inputs` | RNNCell input (possibly nested tuple of) tensor\[s\] for this time step. |
| `state` | RNNCell state (possibly nested tuple of) tensor\[s\] from previous time step. |
| `name` | Name scope for any created operations. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `(outputs, next_state, next_inputs, finished)`: `outputs` is an object containing the decoder output, `next_state` is a (structure of) state tensors and TensorArrays, `next_inputs` is the tensor that should be used as input for the next step, `finished` is a boolean tensor telling whether the sequence is complete, for each sequence in the batch. ||

<br />