Study notesWeek 4: Deep Learning To Transformers
AI and LLMs from zero

Deep Learning To Transformers

Outcomes

What You Will Be Able To Do

2017

Attention Is All You Need

Step 2

What An Embedding Is

cat -> [ 0.21, -0.40,  0.77, ... ]
dog -> [ 0.19, -0.35,  0.70, ... ]
car -> [-0.62,  0.11, -0.05, ... ]
Intuition

Meaning As A Map

Measure

Cosine Similarity

cos(a, b) = (a . b) / (|a| * |b|)

 1  same direction
 0  unrelated
-1  opposite
Context

Same Word, Different Meaning

Intuition

Attention In One Sentence

Mechanism

Query, Key, Value

PartPlain meaning
QueryWhat this token is looking for
KeyWhat each token offers as a label
ValueThe information passed on if matched
Mechanism

Scores, Softmax, Blend

scores  = query . every key
weights = softmax(scores)    # sums to 1
new_vec = sum(weights * values)
Design

Many Heads

Design

The Transformer Block

tokens
  -> multi-head attention
  -> add input back (residual)
  -> small network per token
  -> add input back, normalize
  -> next block (repeat many times)
Prediction

From Transformer To Language Model

Reasons

Why This Design Won

Recurrent networkTransformer
Reads textOne token at a timeAll tokens at once
Long range linksFade over distanceDirect through attention
Training on GPUsHard to paralleliseEasy to parallelise
Code

Compute Similarity In Python

import numpy as np
def cos(a, b):
    a, b = np.array(a), np.array(b)
    return a @ b / (np.linalg.norm(a) * np.linalg.norm(b))
print(cos([1, 2, 3], [2, 4, 6]))   # 1.0
print(cos([1, 0], [0, 1]))         # 0.0
Summary

Recap

Next week

Next Week: How LLMs Work

Wrap up

Questions And Exercise