Unblock all features
Sign in to track progress, get AI help and save conversations.
Now we can assemble the modules from the previous chapter into the full transformer.
forward computes the output by calling each module in order Embedding -> Attention -> MLP -> Linear (output layer).backward computes the gradient values by calling the same modules in the reverse order.predict runs the forward pass and computes the probability distribution over the vocabulary for the last token.train runs the full training loop calculating the loss first and then running the backward pass and updating the parameters.Our model has a single transformer block. If we want to scale it we will need to stack several blocks ().
The code is ready to be tested now. Let's train the model on our small corpus and run the generator predicting the next tokens for a given sequence.
Note that the corpus is very small in our example. The model will just memorize the exact order of the text.
The full code is available separately, so you can try it on a bigger corpus and at a larger scale.