liveBook · Manning

Transformer architecture

As explained in Machine Learning Algorithms in Depth

A transformer model is a neural network architecture that uses attention-based mechanisms to model contextual relationships in structured sequences or other data.

A transformer is an encoder–decoder sequence-to-sequence model that uses attention in both branches instead of relying on recurrent neural networks. Self-attention is its central component: it lets the model weight information from different parts of a sequence. section 11.3 section 11.7

Attention relates queries Q to key–value pairs K and V. A normalized similarity score determines how strongly each query weights the values; softmax makes those weights sum to one. In neural machine translation, target-sequence queries attend to source-sequence keys and values. section 11.3

The architecture combines attention with components that preserve and process sequence information:

  • Multihead attention runs attention in parallel, then concatenates and transforms the resulting heads. section 11.3
  • Positional encodings are added to input embeddings so token representations reflect sequence position. section 11.3
  • Dense and normalization layers accompany residual connections, which ease gradient backpropagation. section 11.3
Figure 11.10 Transformer architecture
figure

The architecture diagram locates the encoder and decoder branches and their attention and positional-encoding components. section 11.3

Text classification

A transformer can be implemented for text classification with Keras/TensorFlow. The accompanying training and validation plots track classifier loss and accuracy; this application is distinct from the encoder–decoder translation example used to explain attention. section 11.0 section 11.3

How Machine Learning Algorithms in Depth covers it

How deep it goes
Explains query–key–value weighting and major architectural components, then provides a classifier implementation using Keras/TensorFlow layers.
Examples
Uses a translation attention heatmap, a transformer architecture diagram, and text-classifier training and validation plots.
What it assumes
Builds on fundamental deep-learning approaches to numerical, image, and text data before developing attention and transformers.
What you can do after
Apply it via Keras/TensorFlow to text classification and inspect training and validation loss and accuracy.
How it connects
Connects self-attention to sequence-to-sequence models, multihead attention, positional encodings, residual connections, and neural machine translation.

Also explained in

Compare how 28 books explain transformer architecture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Algorithms in Depth. The section links point to the parts of the book it draws on.