Position-wise feed-forward network

First linear map

×

Map 512 features to 2,048.

h=xW1+b1{\htmlData{light=h up}{h}}={\htmlData{light=x}{x}}W_1+b_1
in
xx [B batch, S source positions, 512 d]
out
hh [B batch, S source positions, 2048 d_ff]

The code applies ReLU inside the same 1×1 convolution (activation=tf.nn.relu); it is drawn as its own step.

Parameters1,050,624 × 6 layers

1,050,624 parameters per instance.

ComponentCalculationParameters
Weight matrix512 × 2,0481,048,576
Output bias2,048
Per-instance total1,050,624

One in each of the 6 layers, each with its own weights.

No trained weights were published to check against.

Sources

In codeconv_f1(inputs, hidden_size, kernel_size, activation=tf.nn.relu, name="conv1") · 1177–1183

Open pinned code ↗

Direct evidence

conv_hidden_relu: a 1×1 convolution to the hidden size with ReLU, optional dropout, then a 1×1 convolution to the output size.

common_layers.py 1160–1194 ↗ · tensorflow/tensor2tensor @ 0c66117d4297

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 ret

conv1d is a 1×1 tf.layers.conv2d, which owns a kernel and a bias.

common_layers.py 354–360 ↗ · tensorflow/tensor2tensor @ 0c66117d4297

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)