liveBook · Manning

Transformer encoder layer

As explained in Deep Learning with R, Third Edition

An encoder block is a composable sequence-processing module that transforms an input sequence while preserving its shape for stacking.

A Transformer encoder layer transforms a sequence into a representation with the same shape, allowing multiple blocks to be stacked for progressively richer sentence representations. section 15.3.2

A practical encoder block

Feed the sequence into multi-head self-attention, mask padding tokens, add normalization and a feed-forward network, then preserve the hidden dimension at the output. Layer normalization works within each sequence, making it more suitable for sequence data than batch normalization. section 15.3.2

multi_head_attention(key = source, value = source, query = source)

The self-attention call uses the same source sequence as query, key, and value, making the attention explicitly self-attentive. section 15.3.2

Listing 15.14 A Transformer encoder block
layer_transformer_encoder <- new_layer_class(
  "TransformerEncoder",
  initialize = function(hidden_dim, intermediate_dim, num_heads) {
    super$initialize()
    key_dim <- hidden_dim %/% num_heads
    self$self_attention <- layer_multi_head_attention(  #1
      num_heads = num_heads,
      key_dim = key_dim
    )
    self$self_attention_layernorm <- layer_layer_normalization()   #1
    self$feed_forward_1 <- layer_dense(, intermediate_dim,  #2
                                       activation = "relu")  #2
    self$feed_forward_2 <- layer_dense(, hidden_dim)  #2
    self$feed_forward_layernorm <- layer_layer_normalization()  #2
  },
  call = function(source, source_mask) {
    residual <- x <- source  #3
    mask <- source_mask@r[, newaxis, ]  #3
    x <- self$self_attention(  #3
      query = x,  #3
      key = x,  #3
      value = x,  #3
      attention_mask = mask  #3
    )  #3
    x <- x + residual  #3
    x <- x |> self$self_attention_layernorm()  #3

    residual <- x  #4
    x <- x |>  #4
      self$feed_forward_1() |>  #4
      self$feed_forward_2()  #4
    x <- x + residual  #4
    x <- x |> self$feed_forward_layernorm()  #4
    x
  }
)

The implementation listing shows the block’s main components: multi-head attention, layer normalization, two dense feed-forward layers, and residual state. section 15.3.2

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains attention masking, sequence-wise normalization, and stackable shape preservation, while leaving internal attention calculations implicit.
Examples
Uses R/Keras layer code, self-attention wiring, source masks, and contrasting normalization snippets for sequences and images.
What you can do after
Builds an encoder block from multi-head attention, normalization, dense layers, masking, and residual structure.
How it connects
Connects self-attention with attention masks, layer normalization, feed-forward networks, residual connections, and stacked representations.

Also explained in

Compare how 3 books explain Transformer encoder layer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.