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.
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.
Each part matters on its own, but the magic is in tuning them together.
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.
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:
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.
Choosing an initializer in Keras is a one-word argument:
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.
Both are just layers you drop into the model:
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.
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:
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.
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.
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.