Study notesWeek 3: Neural Networks
AI and LLMs from zero

Neural Networks

Outcomes

What You Will Be Able To Do

Idea

From A Line To A Neuron

Mechanism

What One Neuron Computes

total = w1*x1 + w2*x2 + b
output = activation(total)
Example

A Neuron Deciding On A Walk

x1 = 0.8   # warmth, scaled 0 to 1
x2 = 0     # raining? 1 = yes
total = 2*x1 + (-3)*x2 + (-0.5)
# total = 1.1  (leans yes)
Activation

Why A Bend Is Needed

Activation

Three Common Activations

NameRuleOutput range
ReLUmax(0, x)0 and up
Sigmoid1 / (1 + e^-x)0 to 1
Tanh(e^x - e^-x) / (e^x + e^-x)-1 to 1
sigmoid(z) = 1 / (1 + e^-z)   # 0 to 1
relu(z)    = max(0, z)         # 0 or z
softmax    -> probabilities summing to 1
Structure

Layers

inputs -> [hidden layer] -> [hidden layer] -> outputs
  x1 \    o  o  o            o  o        p1
  x2 -->  o  o  o   ...      o  o   -->  p2
Computation

The Forward Pass

Depth

Deep Means Many Layers

0714222925.815.311.76.73.620112012201320142015
Training

The Training Loop Again

for each batch:
    out  = network(x)        # forward pass
    loss = compare(out, y)   # how wrong?
    loss.backward()          # gradients
    update the weights
Intuition

Backpropagation: Passing Blame

Practice

Batches, Epochs, And Starts

Code

Training A Tiny Network

import torch, torch.nn as nn
X = torch.tensor([[0.,0.],[0.,1.],[1.,0.],[1.,1.]])
y = torch.tensor([[0.],[1.],[1.],[0.]])
net = nn.Sequential(nn.Linear(2,4), nn.ReLU(), nn.Linear(4,1), nn.Sigmoid())
opt = torch.optim.SGD(net.parameters(), lr=0.1)
for epoch in range(5000):
    loss = nn.functional.binary_cross_entropy(net(X), y)
    opt.zero_grad(); loss.backward(); opt.step()
print(net(X).round().flatten())
Judgement

Strengths And Limits

Summary

Recap

Next week

Next Week: Deep Learning To Transformers

Wrap up

Questions And Exercise