Align decoder queries with encoder values
Sequence-to-sequence models
Keras · Class
Keras Attention is a Luong-style dot-product attention layer. It converts query-key scores into weights and uses them to combine value vectors.
Attention compares each query timestep with the key sequence, normalizes those scores into a distribution, and uses it to combine value vectors. With no separate key, the value tensor serves as both key and value; optional masking and score return are supported by the call.
keras.layers.Attention(use_scale=False, score_mode="dot", dropout=0.0, seed=None, **kwargs)| Name | Type | Description |
|---|---|---|
use_scale | bool | Whether to multiply attention scores by a trainable scalar. |
score_mode | str | Attention scoring method: dot or concat. |
dropout | float | Fraction of attention-score units to drop, between 0 and 1. |
seed | int or None | Random seed for dropout. |
**kwargs | dict | Additional base-layer keyword arguments. |
Attention outputs with shape (batch_size, Tq, dim); optionally, attention scores with shape (batch_size, Tq, Tv) when requested by the call.
import keras
import numpy as np
query = keras.Input(shape=(3, 4))
value = keras.Input(shape=(5, 4))
output = keras.layers.Attention()([query, value])
model = keras.Model([query, value], output)
result = model([np.random.random((2, 3, 4)), np.random.random((2, 5, 4))])
print(result.shape)Builds and runs dot-product attention with separate query and value sequences; the value tensor also supplies the keys.
Sequence-to-sequence models
Self-attention blocks
Cross-modal or cross-sequence fusion
keras.layers.Attention produce its output?The layer computes query-key scores with shape (batch_size, Tq, Tv), applies softmax, and uses the resulting distribution to form weighted combinations of value vectors. The output has shape (batch_size, Tq, dim).
Follow-up: What happens when key is omitted? The layer reuses value as the key, producing the common self-attention-style query/value configuration.
The layer accepts a list containing query and value, plus an optional key. Query and key have sequence lengths Tq and Tv, while the feature dimension is dim; the output preserves Tq and dim.
Follow-up: What shape do attention scores have? They are (batch_size, Tq, Tv), representing every query-to-key timestep pairing.
use_scale enables a trainable scalar scale for the attention scores, and score_mode selects either dot-product scoring or concatenation-based scoring. The constructor also exposes score dropout and a seed.
Follow-up: Which constructor option controls score regularization? dropout specifies the fraction of attention-score units dropped, while seed controls the random stream for that operation.
keras.layers.AdditiveAttentionkeras.layers.MultiHeadAttentionkeras.layers.Softmax