# there no change change in the first several cells from last lecture
Building makemore Part 4: Becoming a Backprop Ninja
Neural Networks: Zero to Hero
WIP
Course Page: https://karpathy.ai/zero-to-hero.html
Setup
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt # for making figures
%matplotlib inline
# read in all the words
= open('names.txt', 'r').read().splitlines()
words print(len(words))
print(max(len(w) for w in words))
print(words[:8])
32033
15
['emma', 'olivia', 'ava', 'isabella', 'sophia', 'charlotte', 'mia', 'amelia']
# build the vocabulary of characters and mappings to/from integers
= sorted(list(set(''.join(words))))
chars = {s:i+1 for i,s in enumerate(chars)}
stoi '.'] = 0
stoi[= {i:s for s,i in stoi.items()}
itos = len(itos)
vocab_size print(itos)
print(vocab_size)
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z', 0: '.'}
27
# build the dataset
= 3 # context length: how many characters do we take to predict the next one?
block_size
def build_dataset(words):
= [], []
X, Y
for w in words:
= [0] * block_size
context for ch in w + '.':
= stoi[ch]
ix
X.append(context)
Y.append(ix)= context[1:] + [ix] # crop and append
context
= torch.tensor(X)
X = torch.tensor(Y)
Y print(X.shape, Y.shape)
return X, Y
import random
42)
random.seed(
random.shuffle(words)= int(0.8*len(words))
n1 = int(0.9*len(words))
n2
= build_dataset(words[:n1]) # 80%
Xtr, Ytr = build_dataset(words[n1:n2]) # 10%
Xdev, Ydev = build_dataset(words[n2:]) # 10% Xte, Yte
torch.Size([182625, 3]) torch.Size([182625])
torch.Size([22655, 3]) torch.Size([22655])
torch.Size([22866, 3]) torch.Size([22866])
# ok biolerplate done, now we get to the action:
# utility function we will use later when comparing manual gradients to PyTorch gradients
def cmp(s, dt, t):
= torch.all(dt == t.grad).item()
ex = torch.allclose(dt, t.grad)
app = (dt - t.grad).abs().max().item()
maxdiff print(f'{s:15s} | exact: {str(ex):5s} | approximate: {str(app):5s} | maxdiff: {maxdiff} | shapes: {dt.shape} vs. {t.shape}')
= 10 # the dimensionality of the character embedding vectors
n_embd = 64 # the number of neurons in the hidden layer of the MLP
n_hidden
= torch.Generator().manual_seed(2147483647) # for reproducibility
g = torch.randn((vocab_size, n_embd), generator=g)
C # Layer 1
= torch.randn((n_embd * block_size, n_hidden), generator=g) * (5/3)/((n_embd * block_size)**0.5)
W1 = torch.randn(n_hidden, generator=g) * 0.1 # using b1 just for fun, it's useless because of BN
b1 # Layer 2
= torch.randn((n_hidden, vocab_size), generator=g) * 0.1
W2 = torch.randn(vocab_size, generator=g) * 0.1
b2 # BatchNorm parameters
= torch.randn((1, n_hidden))*0.1 + 1.0
bngain = torch.randn((1, n_hidden))*0.1
bnbias
# Note: I am initializating many of these parameters in non-standard ways
# because sometimes initializating with e.g. all zeros could mask an incorrect
# implementation of the backward pass.
= [C, W1, b1, W2, b2, bngain, bnbias]
parameters = dict(
parameters_named =C,
C=W1,
W1=b1,
b1=W2,
W2=b2,
b2=bngain,
bngain=bnbias,
bnbias
)print(sum(p.nelement() for p in parameters)) # number of parameters in total
for p in parameters:
= True p.requires_grad
4137
= 32
batch_size = batch_size # a shorter variable also, for convenience
n # construct a minibatch
= torch.randint(0, Xtr.shape[0], (batch_size,), generator=g)
ix = Xtr[ix], Ytr[ix] # batch X,Y Xb, Yb
# forward pass, "chunkated" into smaller steps that are possible to backward one at a time
= C[Xb] # embed the characters into vectors
emb = emb.view(emb.shape[0], -1) # concatenate the vectors
embcat # Linear layer 1
= embcat @ W1 + b1 # hidden layer pre-activation
hprebn # BatchNorm layer
= 1/n*hprebn.sum(0, keepdim=True)
bnmeani = hprebn - bnmeani
bndiff = bndiff**2
bndiff2 = 1/(n-1)*(bndiff2).sum(0, keepdim=True) # note: Bessel's correction (dividing by n-1, not n)
bnvar = (bnvar + 1e-5)**-0.5
bnvar_inv = bndiff * bnvar_inv
bnraw = bngain * bnraw + bnbias
hpreact # Non-linearity
= torch.tanh(hpreact) # hidden layer
h # Linear layer 2
= h @ W2 + b2 # output layer
logits # cross entropy loss (same as F.cross_entropy(logits, Yb))
= logits.max(1, keepdim=True).values
logit_maxes = logits - logit_maxes # subtract max for numerical stability
norm_logits = norm_logits.exp()
counts = counts.sum(1, keepdims=True)
counts_sum = counts_sum**-1 # if I use (1.0 / counts_sum) instead then I can't get backprop to be bit exact...
counts_sum_inv = counts * counts_sum_inv
probs = probs.log()
logprobs = -logprobs[range(n), Yb].mean()
loss
# PyTorch backward pass
for p in parameters:
= None
p.grad for t in [logprobs, probs, counts, counts_sum, counts_sum_inv, # afaik there is no cleaner way
norm_logits, logit_maxes, logits, h, hpreact, bnraw,
bnvar_inv, bnvar, bndiff2, bndiff, hprebn, bnmeani,
embcat, emb]:
t.retain_grad()
loss.backward() loss
tensor(3.3390, grad_fn=<NegBackward0>)
Backprop through the atomic compute graph
from torchviz import make_dot
=parameters_named) make_dot(loss, params
# Exercise 1: backprop through the whole thing manually,
# backpropagating through exactly all of the variables
# as they are defined in the forward pass above, one by one
= torch.zeros_like(logprobs)
dlogprobs # get all rows, and index into the correct column for the labels
range(n), Yb] = -1/n
dlogprobs[= dlogprobs * 1 / probs
dprobs = (dprobs * counts).sum(1, keepdim=True)
dcounts_sum_inv = dcounts_sum_inv * -counts_sum**-2
dcounts_sum = counts_sum_inv * dprobs
dcounts # gradients flow through other vertex!
+= dcounts_sum * 1
dcounts
= dcounts * norm_logits.exp()
dnorm_logits = (-dnorm_logits).sum(1, keepdim=True)
dlogit_maxes
= dnorm_logits.clone()
dlogits += F.one_hot(logits.max(1).indices, num_classes=logits.shape[1]) * dlogit_maxes
dlogits
= dlogits @ W2.T
dh
= h.T @ dlogits
dW2 = dlogits.sum(0)
db2
= dh * (1 - h**2)
dhpreact
= (dhpreact * bnraw).sum(0, keepdim=True)
dbngain = dhpreact.sum(0, keepdim=True)
dbnbias = dhpreact * bngain
dbnraw
= (dbnraw * bndiff).sum(0, keepdim=True)
dbnvar_inv = bnvar_inv * dbnraw
dbndiff = dbnvar_inv * -.5 * (bnvar + 1e-5)**-1.5
dbnvar
= torch.ones_like(bndiff2) * 1 / (n-1) * dbnvar
dbndiff2 += (2*bndiff) * dbndiff2
dbndiff = dbndiff.clone()
dhprebn
= (-dbndiff).sum(0, keepdim=True)
dbnmeani += dbnmeani * torch.ones_like(hprebn) * 1 / n
dhprebn
= dhprebn @ W1.T
dembcat = embcat.T @ dhprebn
dW1 = dhprebn.sum(0)
db1 = torch.ones_like(emb) * dembcat.view(n, 3, -1)
demb
= torch.zeros_like(C)
dC for k in range(Xb.shape[0]):
for j in range(Xb.shape[1]):
= Xb[k,j]
ix += demb[k,j]
dC[ix]
cmp('logprobs', dlogprobs, logprobs)
cmp('probs', dprobs, probs)
cmp('counts_sum_inv', dcounts_sum_inv, counts_sum_inv)
cmp('counts_sum', dcounts_sum, counts_sum)
cmp('counts', dcounts, counts)
cmp('norm_logits', dnorm_logits, norm_logits)
cmp('logit_maxes', dlogit_maxes, logit_maxes)
cmp('logits', dlogits, logits)
cmp('h', dh, h)
cmp('W2', dW2, W2)
cmp('b2', db2, b2)
cmp('hpreact', dhpreact, hpreact)
cmp('bngain', dbngain, bngain)
cmp('bnbias', dbnbias, bnbias)
cmp('bnraw', dbnraw, bnraw)
cmp('bnvar_inv', dbnvar_inv, bnvar_inv)
cmp('bnvar', dbnvar, bnvar)
cmp('bndiff2', dbndiff2, bndiff2)
cmp('bndiff', dbndiff, bndiff)
cmp('bnmeani', dbnmeani, bnmeani)
cmp('hprebn', dhprebn, hprebn)
cmp('embcat', dembcat, embcat)
cmp('W1', dW1, W1)
cmp('b1', db1, b1)
cmp('emb', demb, emb)
cmp('C', dC, C)
logprobs | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 27]) vs. torch.Size([32, 27])
probs | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 27]) vs. torch.Size([32, 27])
counts_sum_inv | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 1]) vs. torch.Size([32, 1])
counts_sum | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 1]) vs. torch.Size([32, 1])
counts | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 27]) vs. torch.Size([32, 27])
norm_logits | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 27]) vs. torch.Size([32, 27])
logit_maxes | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 1]) vs. torch.Size([32, 1])
logits | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 27]) vs. torch.Size([32, 27])
h | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 64]) vs. torch.Size([32, 64])
W2 | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([64, 27]) vs. torch.Size([64, 27])
b2 | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([27]) vs. torch.Size([27])
hpreact | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 64]) vs. torch.Size([32, 64])
bngain | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([1, 64]) vs. torch.Size([1, 64])
bnbias | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([1, 64]) vs. torch.Size([1, 64])
bnraw | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 64]) vs. torch.Size([32, 64])
bnvar_inv | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([1, 64]) vs. torch.Size([1, 64])
bnvar | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([1, 64]) vs. torch.Size([1, 64])
bndiff2 | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 64]) vs. torch.Size([32, 64])
bndiff | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 64]) vs. torch.Size([32, 64])
bnmeani | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([1, 64]) vs. torch.Size([1, 64])
hprebn | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 64]) vs. torch.Size([32, 64])
embcat | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 30]) vs. torch.Size([32, 30])
W1 | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([30, 64]) vs. torch.Size([30, 64])
b1 | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([64]) vs. torch.Size([64])
emb | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([32, 3, 10]) vs. torch.Size([32, 3, 10])
C | exact: True | approximate: True | maxdiff: 0.0 | shapes: torch.Size([27, 10]) vs. torch.Size([27, 10])
Backprop through cross-entropy in one go
Computation graph
Backprop through Batchnorm in one go
Computation graph
Forward Pass Equations
given: \[ \displaylines{\mu = \frac{1}{n}\sum_{i}^{n}x_i \\ \sigma^2 = \frac{1}{n-1}\sum_{i}^{n}(x_i - \mu)^2 } \]
(note Bessel’s correction)
then: \[ \hat{x_i} = \frac{x_i - \mu}{\sqrt{\sigma^2 + \epsilon}} \ \] and finally: \[y_i = \gamma \hat{x_i} + \beta\]