Advanced Topic 37 of 51 AI & Machine Learning
Transformers
Attention-based architecture underpinning modern language models.
How it works
The transformer is a neural network architecture built around self-attention instead of recurrence. Input tokens are embedded and combined with positional encodings, then passed through stacked blocks of multi-head self-attention, residual connections, layer normalization and feed-forward layers. Because all positions are processed in parallel, transformers train efficiently on GPUs and scale to huge datasets. Encoder-only models such as BERT, decoder-only ones such as GPT and encoder-decoder models such as T5 are major variants.
Step-by-step walkthrough
- Text is split into tokens.
- Each becomes a vector with position information.
- Self-attention lets every token look at all others in parallel.
- A feed-forward layer processes each token.
- Many such blocks stack, and they train much faster than RNNs.
Where it is used
Language models, translation, summarization, code generation, vision transformers, speech, protein structure prediction and multimodal models use transformers.
Common pitfall
Self-attention cost grows quadratically with sequence length, so long inputs are expensive. They need large data and compute, and can memorize or hallucinate. Training is sensitive to learning-rate schedules and initialization. Use pretrained models and fine-tuning, efficient attention variants and careful evaluation.
Where it fits in the course
Transformers is topic 37 of 51 in AI & Machine Learning, rated Advanced. It follows Attention Mechanism. The next topic is Large Language Models.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the AI & Machine Learning quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the AI & Machine Learning quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Transformers?
Where do people use Transformers?
What is the most common mistake with Transformers?
What should I learn before this topic?
How can I practice it?
Is Transformers hard to learn?
How long does it take to learn Transformers?
Which topics are related to Transformers?
What should I study after Transformers?
Related topics
- Attention Mechanism (AI & Machine Learning)
- Large Language Models (AI & Machine Learning)
- LSTM (AI & Machine Learning)
- Embeddings (AI & Machine Learning)