AI by Hand ✍️

AI by Hand ✍️

Self Attention by Hand ✍️

Calculating AI by Hand: 10 of 28

Prof. Tom Yeh's avatar
Prof. Tom Yeh
Jan 16, 2024
∙ Paid

Library › Calculating AI by Hand ✍️

  1. Matrix Multiplication by Hand ✍️

  2. Multi Layer Perceptron (MLP) by Hand ✍️

  3. Backpropagation by Hand ✍️

  4. SVM by Hand ✍️

  5. Batch Normalization by Hand ✍️

  6. Dropout by Hand ✍️

  7. Recurrent Neural Network (RNN) by Hand ✍️

  8. LSTM by Hand ✍️

  9. Deep RNN by Hand ✍️

  10. Self Attention by Hand ✍️

  11. Transformer by Hand ✍️

  12. Autoencoder by Hand ✍️

  13. Variational Auto Encoder (VAE) by Hand ✍️

  14. Sparse Auto Encoder (SAE) by Hand ✍️

  15. Generative Adversarial Network (GAN) by Hand ✍️

  16. Sampling a Sentence by Hand ✍️

  17. Residual Network by Hand ✍️

  18. U-Net by Hand ✍️

  19. Discrete Fourier Transform by Hand ✍️

  20. Graph Convolutional Network (GCN) by Hand ✍️

  21. CLIP by Hand ✍️

  22. Vector Database by Hand ✍️

  23. Mixture of Experts (MoE) by Hand ✍️

  24. Switch Transformer by Hand ✍️

  25. Mamba's S6 by Hand ✍️

  26. Sora's Diffusion Transformer (DiT) by Hand ✍️

  27. BitNet by Hand ✍️

  28. Reinforcement Learning with Human Feedback (RLHF) by Hand ✍️

Self-attention is what enables LLMs to understand context.

How does it work?

This exercise demonstrates how to calculate a 6-3 attention head by hand. Note that if we have two instances of this, we get 6-6 attention (i.e., multi-head attention, n=2).

Goal:

Transform [6D Features 🟧] to [3D Attention Weighted Features 🟦]

Setup

Step 1 of 9: Given

  • A set of 4 feature vectors (6-D)


Step 2 of 9: Query, Key, Value

  • Multiply features x's with linear transformation matrices WQ, WK, and WV, to obtain query vectors (q1,q2,q3,q4), key vectors (k1,k2,k3,k4), and value vectors (v1,v2,v3,v4).

  • "Self" refers to the fact that both queries and keys are derived from the same set of features.

Attention Scores

Step 3 of 9: Prepare for MatMul

  • Copy query vectors

  • Copy the transpose of key vectors

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2026 Tom Yeh · Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture