<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/models/blob/v2.20.0/official/vision/modeling/layers/nn_layers.py#L482-L647) |

Creates a network layer that adds a sinusoidal positional encoding.

    tfm.vision.layers.PositionalEncoding(
        initializer: tf_keras.initializers.Initializer = 'zeros',
        cache_encoding: bool = False,
        state_prefix: Optional[str] = None,
        **kwargs
    )

Positional encoding is incremented across frames, and is added to the input.
The positional encoding is first weighted at 0 so that the network can choose
to ignore it. This implements:

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones,
Aidan N. Gomez, Lukasz Kaiser, Illia Polosukhin.
Attention Is All You Need.
(<https://arxiv.org/pdf/1706.03762.pdf>).

<br />

<br />

<br />

| ## Args ||
|---|---|
| `initializer` | A `str` of initializer for weighting the positional encoding. |
| `cache_encoding` | A `bool`. If True, cache the positional encoding tensor after calling build. Otherwise, rebuild the tensor for every call. Setting this to False can be useful when we want to input a variable number of frames, so the positional encoding tensor can change shape. |
| `state_prefix` | a prefix string to identify states. |
| `**kwargs` | Additional keyword arguments to be passed to this layer. |

<br />

## Methods

### `call`

[View source](https://github.com/tensorflow/models/blob/v2.20.0/official/vision/modeling/layers/nn_layers.py#L608-L647)

    call(
        inputs: tf.Tensor,
        states: Optional[States] = None,
        output_states: bool = True
    ) -> Union[tf.Tensor, Tuple[tf.Tensor, States]]

Calls the layer with the given inputs.

<br />

<br />

<br />

| Args ||
|---|---|
| `inputs` | An input [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor). |
| `states` | A `dict` of states such that, if any of the keys match for this layer, will overwrite the contents of the buffer(s). Expected keys include `state_prefix + '_pos_enc_frame_count'`. |
| `output_states` | A `bool`. If True, returns the output tensor and output states. Returns just the output tensor otherwise. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An output [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) (and optionally the states if `output_states=True`). ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If using 'channels_first' data format. |

<br />