Neural Networks
Deep LearningStacked layers of simple units that learn their own features.
🟢 In simple words
A neural network is a stack of tiny decision-makers ('neurons'). Each one takes numbers in, multiplies them by learned weights, adds them up, and passes the result through a squishing function. Stack enough of these in layers and the network can learn incredibly complex patterns — like recognising a face — without anyone writing the rules by hand.
🔬 How it actually works
Each neuron computes output = activation(w·x + b). Layers transform the input step by step; the final layer produces a prediction. Training runs data forward (forward pass), measures error with a loss function, then uses backpropagation to compute how much each weight contributed to the error, and gradient descent nudges every weight to reduce it. Non-linear activations (ReLU, GELU) are what let deep networks model curves, not just straight lines.
💡 Real example
A network that reads the 784 pixels of a handwritten digit and outputs 10 probabilities (one per digit 0–9). Hidden layers learn edges, then loops and strokes, then whole digits — features nobody hand-coded.
🎤 Interview Q&A15 questions
What is a neural network?
A model made of layers of simple units (neurons); each computes a weighted sum of its inputs plus a bias, passed through a non-linear activation. Stacked layers learn hierarchical features that map inputs to outputs.
What does an activation function do?
It introduces non-linearity so the network can model curved, complex relationships. Without it, any stack of layers collapses into a single linear function.
Why can't a deep network be entirely linear?
Composing linear layers just gives another linear layer, so depth adds nothing. Non-linear activations (ReLU, GELU, tanh) are what make depth expressive.
Explain forward and backward passes.
The forward pass runs inputs through the layers to produce a prediction and a loss. The backward pass (backpropagation) applies the chain rule to compute each weight's gradient, which gradient descent uses to update the weights.
What is backpropagation?
An efficient algorithm that computes the gradient of the loss with respect to every weight by propagating error backwards through the network using the chain rule.
What is gradient descent?
An optimisation method that iteratively moves each weight in the direction that reduces the loss, scaled by a learning rate. Variants: SGD, momentum, Adam.
What is the learning rate and why does it matter?
The step size for weight updates. Too high and training diverges or oscillates; too low and it crawls or gets stuck. It is often scheduled to decay over training.
What are vanishing and exploding gradients?
In deep nets, gradients can shrink toward zero (vanishing) or blow up (exploding) as they propagate back, stalling or destabilising training. Mitigations: ReLU-family activations, careful initialisation, residual connections, and normalisation.
What is the difference between ReLU, sigmoid, and softmax?
ReLU (max(0,x)) is the default hidden activation; sigmoid squashes to (0,1) for binary outputs; softmax turns a vector of scores into a probability distribution for multi-class outputs.
What is a loss function? Give examples.
It quantifies how wrong predictions are. MSE for regression; cross-entropy (log-loss) for classification. Training minimises it.
What is overfitting and how do you prevent it in neural nets?
When the net memorises training data and generalises poorly. Prevent with more data, dropout, weight decay (L2), early stopping, data augmentation, and reducing capacity.
What does dropout do?
During training it randomly zeroes a fraction of activations, forcing the network not to rely on any single unit — a cheap, effective regulariser.
Why do we normalise inputs and use batch/layer normalisation?
Normalisation keeps activations in a stable range, which speeds up and stabilises training. Batch norm normalises across the batch; layer norm across features (standard in transformers).
What is an epoch, a batch, and an iteration?
An epoch is one full pass over the training data; a batch is the subset processed per weight update; an iteration is one such update. Epochs = iterations × batch_size / dataset_size.
What is the difference between parameters and hyperparameters?
Parameters (weights, biases) are learned from data during training. Hyperparameters (learning rate, layers, batch size) are set by you before training and tuned on a validation set.
🛠 Project idea & 📚 resources
🛠 Build it — project idea
Handwritten digit classifier from scratch. Build a small feed-forward net in PyTorch, train on MNIST, and plot how accuracy and loss evolve per epoch. Then break it on purpose (remove the activation) to see why non-linearity matters.
📂 Dataset: MNIST (built into torchvision) or Fashion-MNIST