Handbooks / Machine Learning Basics / Chapter 5

Neural Network Basics

7 pages · ~5 min✓ Reviewed

Builds on Decision Trees and Ensembles.

Machine Learning BasicsPage 1

Perceptron and Layers: The Mental Model

1. The Perceptron

  • output = activation(w·x + b)
  • Weights scale inputs; bias shifts the decision threshold
  • One perceptron = one linear boundary
  • XOR is not linearly separable, so it fails
  1. Inputs x
  2. w·x + bweighted sum
  3. Activation
  4. Output

2. Dense Layer

  • Many neurons, same input: y = f(W x + b)
  • W is shaped (out, in)
  • Params = in × out + out (weights + biases)
python
layer = nn.Linear(4, 3)  # W: (3, 4), b: (3,)
sum(p.numel() for p in layer.parameters())  # 4*3 + 3 = 15

call-site fragment

3. Why Stacking Needs Nonlinearity

  • Linear layers only: W2(W1 x) = one linear map
  • Each layer needs a nonlinear activation
  • Nonlinearity is what lets a stack solve XOR
Depth needs activations

Without activations, N linear layers collapse into one linear layer.

  • Why can one perceptron not solve XOR? It draws only one linear boundary.
  • Params in Linear(784, 128)? 784 × 128 + 128 = 100,480
  • Why do N linear layers collapse? A product of matrices is a single matrix.
Machine Learning BasicsPage 2

Activations: ReLU, Sigmoid, Softmax

1. The three activations

ReLUSigmoidSoftmax
Formulamax(0, x)1/(1+e^-x)e^xi / Σ e^xj
Output0 to ∞(0, 1)probabilities, sum to 1
Usehidden layers (cheap default)binary outputmulti-class output
Catchdying ReLU: negative input, zero gradientsaturates, gradients vanishoverflow without max shift

2. Match output to task

TaskOutputLoss
Binarysigmoidbinary cross-entropy
Multi-classsoftmaxcategorical cross-entropy
Regressionnone (linear)MSE

3. Stable softmax

  • Subtract max(x) before exp
  • Result is unchanged, exp never overflows
python
import math
def softmax(xs):
    m = max(xs)
    e = [math.exp(x - m) for x in xs]
    return [v / sum(e) for v in e]

definition only, call it with your logits

Remember

Hidden layers: ReLU. Output layer: sigmoid, softmax or nothing, chosen by the task.

Machine Learning BasicsPage 3

Forward Pass and Backpropagation

1. Forward Pass

  • Feed input layer by layer: a = f(W a_prev + b)
  • Cache each layer's input a_prev and pre-activation z
  • Backward pass reuses the cache instead of recomputing

2. Backpropagation

  • Chain rule from loss to each weight: dL/dW = dL/da · da/dz · dz/dW
  • Vanishing gradients: repeated factors below 1 shrink the signal
  • Sigmoid derivative peaks at 0.25; ReLU (slope 1 for x > 0) eases it
QuantityFormula
Weight gradientdW = δ · a_prevᵀ
Bias gradientdb = δ
Error passed backδ_prev = Wᵀ δ ⊙ f'(z)

3. Training Step

  1. Forward
  2. Loss
  3. Zero grads
  4. Backward
  5. Step
python
pred = model(x)
loss = loss_fn(pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()

call-site fragment; model, x, y defined elsewhere

Zero the gradients

PyTorch accumulates gradients across batches. Skip zero_grad() and each step uses a sum of old and new gradients.

  • Why cache z? Backward needs f'(z) and a_prev.
  • Why does sigmoid vanish? Derivative is at most 0.25, so deep products shrink.
Machine Learning BasicsPage 4

Loss Functions and Optimizers: SGD vs Adam

1. Loss and the Update

TaskLossFormula
RegressionMSEmean of (ŷ − y)²
ClassificationCross-entropy−Σ y log(ŷ)
  • Gradient descent: w ← w − lr · ∂L/∂w
  • Momentum keeps a velocity of past gradients
  • Smooths noisy steps, speeds convergence

2. SGD vs Adam

SGD + momentumAdam
Tracksvelocityrunning mean m, variance v
Step sizeone global lrper-parameter
Tuninglr and schedulelittle
Resultoften generalizes betterconverges fast
Defaultsnonelr=1e-3, β1=0.9, β2=0.999
python
torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
torch.optim.Adam(model.parameters(), lr=1e-3)

call-site fragment

3. Learning Rate

  • Too high: diverges or oscillates
  • Too low: crawls
Tune first

The learning rate is the first hyperparameter to tune.

Machine Learning BasicsPage 5

Epochs, Batches and Dropout

1. Epochs, Batches, Order

  • Epoch = one full pass over the training set
  • Iteration = one batch update
  • Iterations per epoch = N / batch_size
  • Shuffle training data every epoch
  • Never shuffle or augment the validation set
Batch sizeBehaviour
Full batchstable, slow, memory heavy
Mini-batchthe usual compromise
Size 1noisy updates

2. Dropout and Early Stopping

  1. Trainingzero fraction p, scale 1/(1−p)
  2. model.eval()dropout off
  • Dropout is a regularizer
  • Typical p: 0.2–0.5 for dense layers
  • Early stopping: val loss rises while train loss falls
python
model.train()  # dropout on
...
model.eval()  # dropout off
with torch.no_grad():
    preds = model(x_val)

call-site fragment

Forgot eval()?

Dropout stays on at inference, so predictions are noisy.

  • Why shuffle each epoch? Stops order-dependent learning; gradients are less correlated, more representative.
  • Why rescale by 1/(1−p)? Keeps expected activation equal in training and eval.
Machine Learning BasicsPage 6

Neural Network Cheat Sheet

1. Layer and Loss Pairing

UseActivationLoss
Hidden layersReLUn/a
RegressionLinearMSE
Binary outputSigmoidBinary cross-entropy
Multi-class outputSoftmaxCategorical cross-entropy

2. Training Loop

  1. Forward
  2. Loss
  3. zero_grad
  4. backward
  5. step
python
pred = model(x)
loss = loss_fn(pred, y)
opt.zero_grad()
loss.backward()
opt.step()

call-site fragment: model, x, y, opt defined elsewhere

3. Starting Points

SettingStart at
OptimizerAdam, lr=1e-3
Batch size32–128
Dropout0.2–0.5

4. Debug Checklist

  • Overfit one small batch first
  • Loss NaN: lr too high, or bad softmax/log
  • Train/val gap: more regularization
Order matters

Skip zero_grad and gradients accumulate across batches.

Machine Learning BasicsPage 7

Check yourself

Three dense layers have no activation functions. Why is this no better than one layer?
  • Each layer is linear: W x + b
  • Composing linear maps gives one linear map
  • A nonlinear activation such as ReLU between layers fixes it
A loop runs forward, loss, backward, step, but never zero_grad. What goes wrong?
  • Gradients accumulate across batches (PyTorch)
  • Each update uses old plus new gradients, so training is erratic
  • Zero the gradients before backward
A dense layer has 4 inputs and 3 outputs. How many parameters?
  • Weights: 4 × 3 = 12
  • Biases: 3
  • Total 15; W is shaped (3, 4)
The same input gives different predictions on each inference call. Likely cause?
  • Dropout is still active
  • Call model.eval() to disable it
  • Then the full network runs, so output is stable