Add positional encoding
×Add the same sinusoids to the target positions.
Why
Order. The model has no recurrence and no convolution, so the authors inject information about the relative or absolute position of the tokens to let it use the order of the sequence. The encoding has the width of the embeddings, 512, so that the two can be summed. Transformer paper §3.5, p. 6
Sinusoids. The authors chose sinusoids because they hypothesized the model could then easily learn to attend by relative position: for any fixed offset k, the encoding at position pos + k is a linear function of the encoding at pos. Transformer paper §3.5, p. 6
Learned position embeddings gave nearly identical results. The authors kept the sinusoids because they may let the model extrapolate to sequences longer than those seen in training. Transformer paper §3.5 and Table 3 (E), pp. 6–9
- in
- [B batch, T target positions, 512 d]
- out
- [B batch, T target positions, 512 d]
Explanation
Sources
In codecommon_attention.add_timing_signal_1d(decoder_input) · transformer.py 124–125
Direct evidence
The timing signal: channels/2 geometric timescales from 1 to 10,000; the sines and the cosines of position/timescale are concatenated along the channels and added to x.
common_attention.py 29–70 ↗ · tensorflow/tensor2tensor @ 0c66117d4297
Excerpt
def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
"""Adds a bunch of sinusoids of different frequencies to a Tensor.
Each channel of the input Tensor is incremented by a sinusoid of a different
frequency and phase.
This allows attention to learn to use absolute and relative positions.
Timing signals should be added to some precursors of both the query and the
memory inputs to attention.
The use of relative position is possible because sin(x+y) and cos(x+y) can be
experessed in terms of y, sin(x) and cos(x).
In particular, we use a geometric sequence of timescales starting with
min_timescale and ending with max_timescale. The number of different
timescales is equal to channels / 2. For each timescale, we
generate the two sinusoidal signals sin(timestep/timescale) and
cos(timestep/timescale). All of these sinusoids are concatenated in
the channels dimension.
Args:
x: a Tensor with shape [batch, length, channels]
min_timescale: a float
max_timescale: a float
Returns:
a Tensor the same shape as x.
"""
length = tf.shape(x)[1]
channels = tf.shape(x)[2]
position = tf.to_float(tf.range(length))
num_timescales = channels // 2
log_timescale_increment = (
math.log(float(max_timescale) / float(min_timescale)) /
(tf.to_float(num_timescales) - 1))
inv_timescales = min_timescale * tf.exp(
tf.to_float(tf.range(num_timescales)) * -log_timescale_increment)
scaled_time = tf.expand_dims(position, 1) * tf.expand_dims(inv_timescales, 0)
signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=1)
signal = tf.pad(signal, [[0, 0], [0, tf.mod(channels, 2)]])
signal = tf.reshape(signal, [1, length, channels])
return x + signalThe decoder input is the embedded targets shifted right by one position, plus the timing signal; its self-attention bias is lower-triangular.
transformer.py 109–126 ↗ · tensorflow/tensor2tensor @ 0c66117d4297
Excerpt
def transformer_prepare_decoder(targets, hparams):
"""Prepare one shard of the model for the decoder.
Args:
targets: a Tensor.
hparams: run hyperparameters
Returns:
decoder_input: a Tensor, bottom of decoder stack
decoder_self_attention_bias: a Tensor, containing large negative values
to implement masked attention and possibly baises for diagonal alignments
"""
decoder_self_attention_bias = (
common_attention.attention_bias_lower_triangle(tf.shape(targets)[1]))
decoder_input = common_layers.shift_left_3d(targets)
if hparams.pos == "timing":
decoder_input = common_attention.add_timing_signal_1d(decoder_input)
return (decoder_input, decoder_self_attention_bias)