Embedded source
×tensorinputs
Scaled token vectors of the source positions.
- shape
- [B batch, S source positions, 512 d]
Sources
Contextual evidence from Token embedding
Embedding: gather rows of the table, multiply by √depth, zero the padding id 0; with shared weights, inputs and targets read the same table.
symbol-bottom · tensorflow/tensor2tensor / tensor2tensor/models/modalities.py
Pinned revision 0c66117d4297 ↗ · lines 75–96
Read code excerpt
def bottom_simple(self, x, name, reuse):
with tf.variable_scope(name, reuse=reuse):
# Squeeze out the channels dimension.
x = tf.squeeze(x, axis=3)
var = self._get_weights()
ret = tf.gather(var, x)
if self._model_hparams.multiply_embedding_mode == "sqrt_depth":
ret *= self._body_input_depth**0.5
ret *= tf.expand_dims(tf.to_float(tf.not_equal(x, 0)), -1)
return ret
def bottom(self, x):
if self._model_hparams.shared_embedding_and_softmax_weights:
return self.bottom_simple(x, "shared", reuse=None)
else:
return self.bottom_simple(x, "input_emb", reuse=None)
def targets_bottom(self, x):
if self._model_hparams.shared_embedding_and_softmax_weights:
return self.bottom_simple(x, "shared", reuse=True)
else:
return self.bottom_simple(x, "target_emb", reuse=None)Contextual evidence from Token embedding
The symbol table: vocab_size × depth, built from num_shards shards and concatenated.
symbol-weights · tensorflow/tensor2tensor / tensor2tensor/models/modalities.py
Pinned revision 0c66117d4297 ↗ · lines 51–73
Read code excerpt
def _get_weights(self):
"""Create or get concatenated embedding or softmax variable.
Returns:
a list of self._num_shards Tensors.
"""
num_shards = self._model_hparams.symbol_modality_num_shards
shards = []
for i in xrange(num_shards):
shard_size = (self._vocab_size // num_shards) + (
1 if i < self._vocab_size % num_shards else 0)
var_name = "weights_%d" % i
shards.append(
tf.get_variable(
var_name, [shard_size, self._body_input_depth],
initializer=tf.random_normal_initializer(
0.0, self._body_input_depth**-0.5)))
if num_shards == 1:
ret = shards[0]
else:
ret = tf.concat(shards, 0)
ret = eu.ConvertGradientToTensor(ret)
return retContextual evidence from Token embedding
Defaults the models inherit: embeddings are multiplied by √depth; the symbol table is stored in 16 shards.
hparams-common · tensorflow/tensor2tensor / tensor2tensor/models/common_hparams.py
Pinned revision 0c66117d4297 ↗ · lines 68–71
Read code excerpt
multiply_embedding_mode="sqrt_depth",
norm_type="none", # "batch", layer", "noam", "none".
layer_norm_epsilon=1e-6,
symbol_modality_num_shards=16,Contextual evidence from Padding mask
Padding positions are those whose embedding is all zeros; they get an attention bias of −1e9.
padding · tensorflow/tensor2tensor / tensor2tensor/models/common_attention.py
Pinned revision 0c66117d4297 ↗ · lines 166–205
Read code excerpt
def embedding_to_padding(emb):
"""Input embeddings -> is_padding.
We have hacked symbol_modality to return all-zero embeddings for padding.
Args:
emb: a Tensor with shape [..., depth].
Returns:
a boolean Tensor with shape [...].
"""
emb_sum = tf.reduce_sum(tf.abs(emb), axis=-1)
return tf.equal(emb_sum, 0.0)
… lines 179–193 omitted …
def attention_bias_ignore_padding(memory_padding):
"""Create an bias tensor to be added to attention logits.
Args:
memory_padding: a boolean `Tensor` with shape [batch, memory_length].
Returns:
a `Tensor` with shape [batch, 1, 1, memory_length].
"""
ret = tf.to_float(memory_padding) * -1e9
return tf.expand_dims(tf.expand_dims(ret, 1), 1)
Contextual evidence from Padding mask
The encoder input is the embedded source plus a learned target-space embedding of 32 rows, plus the timing signal; the padding positions become a −1e9 attention bias.
prepare-encoder · tensorflow/tensor2tensor / tensor2tensor/models/transformer.py
Pinned revision 0c66117d4297 ↗ · lines 78–106
Read code excerpt
def transformer_prepare_encoder(inputs, target_space, hparams):
"""Prepare one shard of the model for the encoder.
Args:
inputs: a Tensor.
target_space: a Tensor.
hparams: run hyperparameters
Returns:
encoder_input: a Tensor, bottom of encoder stack
encoder_self_attention_bias: a Tensor, containing large negative values
to implement masked attention and possibly baises for diagonal
alignments
encoder_padding: a Tensor
"""
# Flatten inputs.
ishape_static = inputs.shape.as_list()
encoder_input = inputs
encoder_padding = common_attention.embedding_to_padding(encoder_input)
encoder_self_attention_bias = common_attention.attention_bias_ignore_padding(
encoder_padding)
# Append target_space_id embedding to inputs.
emb_target_space = common_layers.embedding(
target_space, 32, ishape_static[-1], name="target_space_embedding")
emb_target_space = tf.reshape(emb_target_space, [1, 1, -1])
encoder_input += emb_target_space
if hparams.pos == "timing":
encoder_input = common_attention.add_timing_signal_1d(encoder_input)
return (encoder_input, encoder_self_attention_bias, encoder_padding)Contextual evidence from Add target-space embedding
A learned embedding of the target space id, 32 rows of the model width, is added to every source position.
target-space · tensorflow/tensor2tensor / tensor2tensor/models/transformer.py
Pinned revision 0c66117d4297 ↗ · lines 99–103
Read code excerpt
# Append target_space_id embedding to inputs.
emb_target_space = common_layers.embedding(
target_space, 32, ishape_static[-1], name="target_space_embedding")
emb_target_space = tf.reshape(emb_target_space, [1, 1, -1])
encoder_input += emb_target_space