Decoder 6 layers
Normalized sum
×tensorx · 212
The residual features after the attention update, feeding the MLP branch and its bypass.
- shape
- [B batch, T target positions, 512 d]
Sources
Contextual evidence from LayerNorm
LayerNorm over the last axis with a learned scale and bias and ε = 1e-6.
layer-norm · tensorflow/tensor2tensor / tensor2tensor/models/common_layers.py
Pinned revision 0c66117d4297 ↗ · lines 406–443
Read code excerpt
def layer_norm_compute_python(x, epsilon, scale, bias):
"""Layer norm raw computation."""
mean = tf.reduce_mean(x, axis=[-1], keep_dims=True)
variance = tf.reduce_mean(tf.square(x - mean), axis=[-1], keep_dims=True)
norm_x = (x - mean) * tf.rsqrt(variance + epsilon)
return norm_x * scale + bias
@function.Defun(compiled=True)
def layer_norm_compute_grad(x, epsilon, scale, bias, dy):
y = layer_norm_compute_python(x, epsilon, scale, bias)
dx = tf.gradients(ys=[y], xs=[x, epsilon, scale, bias], grad_ys=[dy])
return dx
@function.Defun(
compiled=True,
separate_compiled_gradients=True,
grad_func=layer_norm_compute_grad)
def layer_norm_compute(x, epsilon, scale, bias):
return layer_norm_compute_python(x, epsilon, scale, bias)
def layer_norm(x, filters=None, epsilon=1e-6, name=None, reuse=None):
"""Layer normalize the tensor x, averaging over the last dimension."""
if filters is None:
filters = x.get_shape()[-1]
with tf.variable_scope(
name, default_name="layer_norm", values=[x], reuse=reuse):
scale = tf.get_variable(
"layer_norm_scale", [filters], initializer=tf.ones_initializer())
bias = tf.get_variable(
"layer_norm_bias", [filters], initializer=tf.zeros_initializer())
if allow_defun:
result = layer_norm_compute(x, tf.constant(epsilon), scale, bias)
result.set_shape(x.get_shape())
else:
result = layer_norm_compute_python(x, epsilon, scale, bias)Contextual evidence from LayerNorm
Post-norm: each sublayer output is dropped out, added to its input and layer-normalized.
residual-fn · tensorflow/tensor2tensor / tensor2tensor/models/transformer.py
Pinned revision 0c66117d4297 ↗ · lines 59–61
Read code excerpt
def residual_fn(x, y):
return common_layers.layer_norm(x + tf.nn.dropout(
y, 1.0 - hparams.residual_dropout))Contextual evidence from Position-wise feed-forward network
The feed-forward layer is conv_hidden_relu with filter_size hidden units and hidden_size outputs.
ffn · tensorflow/tensor2tensor / tensor2tensor/models/transformer.py
Pinned revision 0c66117d4297 ↗ · lines 229–244
Read code excerpt
def transformer_ffn_layer(x, hparams):
"""Feed-forward layer in the transformer.
Args:
x: a Tensor of shape [batch_size, length, hparams.hidden_size]
hparams: hyperparmeters for model
Returns:
a Tensor of shape [batch_size, length, hparams.hidden_size]
"""
if hparams.ffn_layer == "conv_hidden_relu":
return common_layers.conv_hidden_relu(
x,
hparams.filter_size,
hparams.hidden_size,
dropout=hparams.relu_dropout)Contextual evidence from Position-wise feed-forward network
conv_hidden_relu: a 1×1 convolution to the hidden size with ReLU, optional dropout, then a 1×1 convolution to the output size.
conv-relu · tensorflow/tensor2tensor / tensor2tensor/models/common_layers.py
Pinned revision 0c66117d4297 ↗ · lines 1160–1194
Read code excerpt
def conv_hidden_relu(inputs,
hidden_size,
output_size,
kernel_size=(1, 1),
second_kernel_size=(1, 1),
summaries=True,
dropout=0.0,
**kwargs):
"""Hidden layer with RELU activation followed by linear projection."""
name = kwargs.pop("name") if "name" in kwargs else None
with tf.variable_scope(name, "conv_hidden_relu", [inputs]):
if inputs.get_shape().ndims == 3:
is_3d = True
inputs = tf.expand_dims(inputs, 2)
else:
is_3d = False
conv_f1 = conv if kernel_size == (1, 1) else separable_conv
h = conv_f1(
inputs,
hidden_size,
kernel_size,
activation=tf.nn.relu,
name="conv1",
**kwargs)
if dropout != 0.0:
h = tf.nn.dropout(h, 1.0 - dropout)
if summaries and not tf.get_variable_scope().reuse:
tf.summary.histogram("hidden_density_logit",
relu_density_logit(
h, list(range(inputs.shape.ndims - 1))))
conv_f2 = conv if second_kernel_size == (1, 1) else separable_conv
ret = conv_f2(h, output_size, second_kernel_size, name="conv2", **kwargs)
if is_3d:
ret = tf.squeeze(ret, 2)
return retContextual evidence from Position-wise feed-forward network
conv1d is a 1×1 tf.layers.conv2d, which owns a kernel and a bias.
conv1d · tensorflow/tensor2tensor / tensor2tensor/models/common_layers.py
Pinned revision 0c66117d4297 ↗ · lines 354–360
Read code excerpt
def conv(inputs, filters, kernel_size, **kwargs):
return conv_internal(tf.layers.conv2d, inputs, filters, kernel_size, **kwargs)
def conv1d(inputs, filters, kernel_size, **kwargs):
return tf.squeeze(
conv(tf.expand_dims(inputs, 2), filters, (kernel_size, 1), **kwargs), 2)