Advanced Neural Network Techniques
Learn the practical knobs that turn a network that barely trains into one that trains fast and generalizes well — activations, initialization, normalization, regularization, optimizers, and learning-rate schedules.
Learn Advanced Neural Network Techniques in our free AI & Machine Learning course — a beginner-friendly interactive lesson with worked examples, a practice…
Part of the free AI & Machine Learning course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
🔧 Real-World Analogy: Tuning an Engine
A raw neural network is like an engine straight off the assembly line: the parts are all there, but it sputters, stalls, and burns fuel. The techniques in this lesson are the tuning that turns it into a smooth, powerful machine.
- Activation functions are the spark plugs — they decide which signals fire.
- Weight initialization is the cold-start setting — get it wrong and the engine floods before it even runs.
- Normalization is the cooling system — it keeps internal values in a safe range so nothing overheats.
- Regularization is the rev limiter — it stops the model over-revving and memorizing instead of learning.
- The optimizer is the throttle response — how the model converts feedback into movement.
- The learning-rate schedule is the gearbox — big steps to get going, smaller steps to cruise precisely.
Each part matters on its own, but the magic is in tuning them together.
1 Activation Functions — ReLU, LeakyReLU, GELU
An activation function sits after each layer and decides what the neuron passes on. Without one, stacking layers would just be one big linear function — the network could not learn curves or anything interesting. The activation adds the non-linearity that lets a network model complex patterns.
- ReLU (Rectified Linear Unit): outputs the value if positive, else 0. Fast and the most common default.
- LeakyReLU : like ReLU, but negatives leak through with a small slope so neurons cannot get permanently stuck at 0.
- GELU (Gaussian Error Linear Unit): a smooth curve used in transformers and most modern large language models.
Here is each one built from scratch in plain Python so you can watch what they do. Run it:
Notice ReLU flattens every negative to 0.0 , while LeakyReLU keeps a faint negative signal ( -2.0 becomes -0.02 ). That tiny leak is what keeps the neuron alive during training.
In Keras, you do not write the math yourself — you name the activation:
2 Weight Initialization — Xavier & He
Before training starts, every weight needs a starting value. Set them all to zero and every neuron learns the same thing (useless). Set them too large and the signal explodes; too small and it vanishes. Smart initialization scales the random starting weights by the layer size so signals stay a healthy size on the very first forward pass.
- Xavier (Glorot) : tuned for symmetric activations like tanh and sigmoid .
- He : scales variance up by 2 to account for ReLU discarding the negative half — use it with ReLU.
Choosing an initializer in Keras is a one-word argument:
3 Normalization — BatchNorm & LayerNorm
As data flows through a deep network, the scale of values can drift wildly between layers, which slows training. Normalization re-centers and re-scales these values so each layer sees inputs in a stable range — like a cooling system keeping the engine from overheating.
- BatchNorm : normalizes each feature across the examples in a mini-batch. Great for CNNs; needs a decent batch size and behaves differently at inference.
- LayerNorm : normalizes across the features of a single example. Independent of batch size, identical in training and inference — the default in transformers.
Both are just layers you drop into the model:
4 Regularization — Dropout, L2, Early Stopping
Overfitting is when a model memorizes the training data instead of learning the general pattern — it scores great on data it has seen and badly on anything new. Regularization is the set of tools that prevent this.
- Dropout : randomly switch off a fraction of neurons each training step, so the network cannot lean on any single unit.
- L2 (weight decay) : add a penalty for large weights, nudging the model toward simpler, smoother solutions.
- Early stopping : watch validation loss and stop training the moment it stops improving.
5 Optimizers — SGD with Momentum & Adam
The optimizer is the rule that turns gradients into weight updates. Plain SGD takes a step straight downhill. Momentum adds memory of the previous direction so the optimizer keeps rolling through small bumps and noisy gradients — like a ball gaining speed downhill. Adam goes further, giving every weight its own adaptive step size.
Below is a single momentum update step in plain Python. The formula is just two lines: velocity = beta * velocity - lr * gradient , then weight = weight + velocity . Run it:
In Keras you pick the optimizer by name when you compile:
6 Learning-Rate Schedules — Decay, Cosine, Warm-up
The learning rate is the size of each step. A fixed rate is a compromise: too big and the model bounces around the target; too small and training crawls. A schedule changes the rate over time — big early to cover ground, small later to settle precisely. It is the gearbox of training.
- Step decay : drop the rate by a factor every few epochs.
- Cosine annealing : smoothly curve the rate down to near zero.
- Warm-up : start tiny and ramp up over the first few hundred steps so an untrained net is not destabilized.
! Common Errors (And How to Fix Them)
These five mistakes trip up almost everyone at first. Here is how to spot and fix each one:
Many neurons output 0 forever and stop learning — usually caused by a too-high learning rate or large negative bias pushing inputs permanently negative.
✅ Fix: switch to LeakyReLU or GELU, lower the learning rate, and use He initialization.
Loss is nan on the first step, or it barely moves. Weights started too large (exploding) or too small (vanishing).
✅ Fix: use he_normal with ReLU or glorot_uniform with tanh — never all-zeros.
A deep network trains painfully slowly or diverges because activations drift to extreme scales between layers.
✅ Fix: add BatchNorm (CNNs) or LayerNorm (transformers) between layers.
Loss explodes to nan (rate too high) or hardly changes over many epochs (rate too low).
✅ Fix: start around 1e-3 for Adam, add warm-up, and use a decay schedule.
Predictions are randomly different each run because dropout is still zeroing out neurons during evaluation.
✅ Fix: use model.predict() (which disables it), or pass training=False in a custom loop.
📋 Quick Reference
Technique
What It Does
When To Use
❓ Frequently Asked Questions
🎯 Mini Challenge: A Two-Step Momentum Optimizer
Time to fade the scaffolding. You get only a comment outline — write the optimizer loop yourself. Run two momentum update steps and print the weight after each.
Lesson complete — you can now tune a network like an engine!
You can choose an activation (ReLU, LeakyReLU, GELU), initialize weights with He or Xavier, stabilize training with BatchNorm or LayerNorm, fight overfitting with dropout, L2, and early stopping, pick between SGD-with-momentum and Adam, and shape the learning rate with a schedule. These are the everyday levers professionals reach for on every serious model.
🚀 Up next: Transformers — see how attention, LayerNorm, GELU, and warm-up schedules combine into the architecture behind modern AI.
Practice quiz
Why does a neural network need a non-linear activation function?
- To speed up the GPU
- To normalise the inputs
- Without it, stacked layers collapse into a single linear function
- To reduce the number of weights
Answer: Without it, stacked layers collapse into a single linear function. Activations add the non-linearity that lets a deep stack model curves; without them the network is just linear.
What does ReLU output for a negative input?
- 0
- The input unchanged
- A small negative value
- 1
Answer: 0. ReLU keeps positive values and clamps everything negative to 0.
How does LeakyReLU differ from ReLU?
- It squashes outputs to 0..1
- It only works on the output layer
- It removes the bias term
- It lets negatives leak through with a small slope so neurons don't die
Answer: It lets negatives leak through with a small slope so neurons don't die. LeakyReLU passes negatives through scaled by a small slope (e.g. 0.01), preventing permanently 'dead' neurons.
Which weight initialization is designed to pair with ReLU?
- Xavier (Glorot)
- He initialization
- All zeros
- All ones
Answer: He initialization. He initialization scales variance up by 2 to account for ReLU discarding the negative half; Xavier suits tanh/sigmoid.
What happens if you initialize all weights to zero?
- Every neuron learns the same thing, so the network can't learn
- Training is fastest
- The loss is always NaN
- It is the recommended default
Answer: Every neuron learns the same thing, so the network can't learn. With identical zero weights every neuron computes and updates the same way, breaking the network's ability to learn.
BatchNorm normalises across what?
- The features of a single example
- The output classes
- Each feature across the examples in a mini-batch
- The learning-rate schedule
Answer: Each feature across the examples in a mini-batch. BatchNorm normalises each feature across the batch, so it needs a decent batch size and shines in CNNs.
Which normalization do transformers typically use, and why?
- BatchNorm, because it is faster
- LayerNorm, because it is independent of batch size
- No normalization at all
- Min-max scaling on the weights
Answer: LayerNorm, because it is independent of batch size. LayerNorm normalises across a single example's features, so it behaves the same in training and inference regardless of batch size.
How does L2 regularization (weight decay) fight overfitting?
- By adding more layers
- By increasing the learning rate
- By dropping random rows of data
- By penalising large weights, nudging toward simpler solutions
Answer: By penalising large weights, nudging toward simpler solutions. L2 adds a penalty proportional to weight magnitude, favouring smaller, smoother weights that generalise better.
Why is Adam often preferred over plain SGD as a default?
- It never overfits
- It keeps a per-parameter adaptive learning rate, so it's forgiving of the initial LR
- It uses no gradients
- It requires no loss function
Answer: It keeps a per-parameter adaptive learning rate, so it's forgiving of the initial LR. Adam tracks gradient momentum and variance to adapt each weight's step size, making it quick and robust to start training.
What does a learning-rate schedule like cosine decay do?
- Holds the rate fixed forever
- Increases the rate every epoch
- Lowers the rate over time so the model settles into a good minimum
- Sets the rate to zero immediately
Answer: Lowers the rate over time so the model settles into a good minimum. A schedule starts larger to cover ground, then shrinks the rate so the model fine-tunes into a precise minimum.
Continue this course
- Previous: Computer Vision
- Next: Transformers