Coders In Hoods
  • Courses
  • Roles
  • Blog
  • Setup

Coders In Hoods AI Academy

Every course is free. Sign in to start, and your progress, quiz scores and bookmarks follow you.

AI Academy

  • Courses
  • Paths
  • Setup guides
  • How we teach

Role roadmaps

  • AI Engineer
  • Forward Deployed Engineer
  • Machine Learning Engineer
  • All roles

Coders In Hoods

  • The agency
  • Coaching for teams
  • Blog
  • LinkedIn
  • X (Twitter)

Contact

codersinhoods@gmail.com

© Coders In Hoods · since 2019

Course overview

LLMs for Engineers

Free · sign in to start and track progress

1 · Inside the transformer

  • 1Tokens and embeddings
  • 2Attention, step by step

    Exercise answers

    • Answer: Implement attention and verify it against PyTorch
    • Answer: Find an induction head in GPT-2
  • 3The transformer block

2 · How models are trained

  • 4Pretraining and scaling laws
  • 5Post-training an assistant

3 · Running models in production

  • 6Sampling and hallucination
  • 7Context windows, KV cache and caching
  • 8Reasoning models and extended thinking
  • 9Choosing and comparing models
CapstoneChoose a model for document extractionReference implementation

Module 1 · Inside the transformer · Lesson 2

Attention, step by step

How each token gathers information from the others through queries, keys and values, and why the formula looks the way it does.

60 min 2 videos 3 exercises 7-question quiz Answers
Setup for the exercises:Install Claude CodePython with uvRun Hugging Face models on CPU

By the end of this lesson you can

  • Derive scaled dot-product attention and explain the role of Q, K, V and the √d scaling
  • Compute a small attention example by hand, including the causal mask
  • Explain why transformers use multiple heads and what heads have been observed to learn
  • Place attention inside a transformer block and the residual stream

On this page

  • The problem attention solves
  • Queries, keys and values
  • The formula
  • Why divide by √dₖ?
  • A worked example by hand
  • Implementation
  • Multiple heads
  • Attention inside the transformer block
  • Costs and consequences
  • Summary
  • Sources & further reading
  • Quiz

In this lesson

  1. 01The problem attention solves
  2. 02Queries, keys and values
  3. 03The formula
  4. 04A worked example by hand
  5. 05Implementation
  6. 06Multiple heads
  7. 07Attention inside the transformer block
  8. 08Costs and consequences
  9. 09Summary

Sources & further reading

  • Attention Is All You NeedPaper · Vaswani et al. · Google · 2017The transformer paper. Section 3.2 defines scaled dot-product and multi-head attention; footnote 4 gives the variance argument for the √d scaling.
  • Neural Machine Translation by Jointly Learning to Align and TranslatePaper · Bahdanau, Cho, Bengio · 2014The original attention mechanism, introduced for RNN translation models three years before the transformer.
  • A Mathematical Framework for Transformer CircuitsLab research · Elhage et al. · Anthropic · 2021The residual-stream view of transformers used in this lesson. Read the sections on the residual stream and on attention heads as independent, additive components.
  • In-context Learning and Induction HeadsLab research · Olsson et al. · Anthropic · 2022A concrete, well-evidenced example of what a pair of attention heads learns to do.
  • The Annotated TransformerUniversity · Harvard NLPThe full paper, re-implemented line by line in PyTorch with commentary.
  • CS224N Lecture: Self-Attention and TransformersUniversity · StanfordThe transformers lecture and its notes build attention up from the motivation in this lesson.
Free · sign in to start

Sign in to keep reading.

Free, no card. We use your account to save progress, quiz scores, bookmarks and occasionally send updates from our team.

  • 9 more sections
  • 3 hands-on exercises
  • A 7-question quiz
  • Progress, quiz scores and bookmarks