Sequence-to-sequence models
Use encoder context to construct representations aligned with decoder query positions.
TensorFlow · Class
A Keras layer implementing Luong-style attention. It scores query-key pairs, normalizes them with softmax, and returns weighted value combinations.
The layer compares each query vector with every key vector, converts those scores into a softmax distribution across the value sequence, and uses the distribution to form weighted value combinations. If no key is supplied, the value tensor also serves as the key; optional score dropout applies only during training.
tf.keras.layers.Attention(use_scale=False, score_mode='dot', dropout=0.0, seed=None, **kwargs)| Name | Type | Description |
|---|---|---|
use_scale | bool, default False | Whether to learn a scalar scale applied to attention scores. |
score_mode | string, default "dot" | Score function: "dot" or "concat". |
dropout | float, default 0.0 | Fraction of attention-score units to drop; must be between 0 and 1. |
seed | int or None | Python integer random seed used when dropout is enabled. |
**kwargs | keyword arguments | Additional keyword arguments forwarded to the Keras layer. |
Attention outputs of shape (batch_size, Tq, dim).
tf.keras.layers.Attention implements single-head Luong-style attention.[query, value] or [query, value, key] as the layer input.import tensorflow as tf
query = tf.random.normal([2, 3, 4])
value = tf.random.normal([2, 5, 4])
layer = tf.keras.layers.Attention(use_scale=True)
output = layer([query, value])
print(output.shape) # (2, 3, 4)Creates attention context vectors for each query position from a batch of query and value sequences.
Use encoder context to construct representations aligned with decoder query positions.
Compute weighted context vectors when query and context sequences have different lengths.
Attend over a sequence using the same tensor as both key and value.
Attention perform?It accepts query and value tensors, plus an optional key tensor. Scores have shape (batch_size, Tq, Tv), are normalized with softmax, and weight the values to produce (batch_size, Tq, dim) outputs.
Follow-up: What happens when key is omitted? The value tensor is reused as the key, so the layer performs attention against the value sequence itself.
With score_mode="dot", the layer compares query and key vectors by dot product. With score_mode="concat", it uses the hyperbolic tangent of their concatenation.
Follow-up: How would you choose between the modes? Use dot mode for the standard Luong-style formulation; choose concat when the documented additive-style scoring behavior is desired.
The query shape is (batch_size, Tq, dim), while value and optional key shapes are (batch_size, Tv, dim). The resulting output keeps the query length and feature dimension: (batch_size, Tq, dim).
Follow-up: What dimension represents the attended positions? Tv indexes the value/key sequence, and softmax distributes each query position's weights across that dimension.
tf.keras.layers.MultiHeadAttentiontf.keras.layers.Layer