Creating a Transformer from Scratch A developer built a transformer from scratch and computed a full forward pass by hand to understand the architecture introduced in the 2017 paper 'Attention is All You Need' by Vaswani et al. The transformer, similar to the original, is used to translate the English sentence 'The dog ran fast.' into German, with 4 initial tokens, 4-dimensional embeddings, 2 attention heads, 2 encoder and 2 decoder layers, and a hidden layer of 8 neurons in each feed-forward block. Originally introduced in the 2017 paper “Attention is All You Need” Vaswani et al., 2017 , transformers form the structure for most modern large language models, including ChatGPT and Claude. In fact, the GPT in ChatGPT stands for Generative Pre-trained Transformer . Transformer architecture blends small feed-forward MLPs with attention mechanisms and has been shown “to be superior in quality while being more parallelizable and requiring significantly less time to train” Vaswani et al., 2017 when compared to previous model designs. In an attempt to grasp these mechanisms more thoroughly, I decided to build my own transformer and compute an entire forward pass by hand. This builds directly on the feed-forward network I worked through by hand in my MLP Walkthrough https://github.com/clippie/Walkthrough MLP from Scratch/tree/main , adding attention, positional encoding, and the encoder-decoder structure. Working through every matrix multiplication myself gave me a much more confident understanding of how these pieces actually fit together, and made a technology that can otherwise feel like a black box a lot less mysterious. The transformer that I will be creating is very similar to the one described in “Attention is All You Need” Vaswani et al., 2017 . It will be used for translating a short English sentence, “The dog ran fast.” into German. Computers do not understand written language the way humans do, but they do understand numbers. Therefore, we need to translate the initial sentence into a numeric representation. We can do this by breaking up the sentence into different pieces or tokens. In this case, I am just setting everything to lowercase, removing the punctuation, and using the whitespace to determine where to start the next token. This tokenization method results in each token being a word, but there are other methods like subword tokenization that may work better at scale. The tokens are then turned into vector embeddings to complete the transformation from language to numbers. A vector represents magnitude and direction. In this case, the vectors that describe each token are a series of numbers that position the token in an embedding space. This space could have anywhere from 1 to infinite dimensions, where each dimension represents a different part of context, though in practice the dimensions are learned and not always human-interpretable. An example of this vector space is shown in Figure 1. You can also use these vectors in equations. For example, Dog — Bark + Meow = Cat. Looking at the Hyperparameters/Structure, there are 4 initial tokens and 4 dimensions that make up each vector embedding dmodel . As we will see later, each attention block will have 2 heads, and each Feed Forward block will have a hidden layer with 8 neurons. I decided to use 2 encoder and 2 decoder layers to show how these layers stack and interact; however, I only did the math for the first layer to avoid repetition. Since this is a translation task, we need the initial vocabulary and the vocabulary of the language we are translating to. In this case, the source English vocab size is 4, and the target German vocab size is 6. That means there is just enough for the German translation and for the