Skip to main content

Neural Networks: The Foundation of Deep Learning

Neural Network Architecture

From Brains to Math

Your brain has about 86 billion neurons, each connected to thousands of others. A single neuron:
  1. Receives inputs from other neurons
  2. Weighs how important each input is
  3. Sums them up
  4. Activates if the sum exceeds a threshold
  5. Sends output to other neurons
That’s literally what an artificial neuron does!
Tesla Autopilot Neural Network

The Perceptron: One Artificial Neuron

How It Works

Math version: output=activation(i=1nwixi+b)=activation(wx+b)output = activation\left(\sum_{i=1}^{n} w_i x_i + b\right) = activation(w \cdot x + b) Where:
  • xix_i = inputs
  • wiw_i = weights (learnable)
  • bb = bias (also learnable)
  • activationactivation = a function that decides to “fire” or not

Building a Perceptron from Scratch


The XOR Problem: Why We Need More Layers

A single perceptron can only learn linearly separable patterns! XOR is not linearly separable — you cannot draw a single straight line to separate the 0s from the 1s. Think of it like a bouncer at a club who can only apply one rule: “everyone taller than 6 feet gets in” works fine, but “people get in if they have an ID or they are on the list, but not both” requires understanding two conditions simultaneously. A single perceptron is that one-rule bouncer. Solution: Stack multiple layers of neurons = Multi-Layer Perceptron (MLP). The first layer learns simple patterns, the second layer combines those patterns into more complex ones — just like how the visual cortex processes edges first, then shapes, then objects.

Activation Functions

The step function (0 or 1) has a problem: its gradient is 0 everywhere except at the threshold, where it is undefined. This means gradient descent has no signal to work with — it is like trying to roll a ball downhill on a perfectly flat surface with a single cliff edge. We need smooth, differentiable activation functions that provide a gradient at every point — a gentle slope the optimization can follow:
Practical default: Use ReLU for hidden layers and sigmoid/softmax for the output layer. This covers 90% of use cases. Only switch to Leaky ReLU or GELU if you observe dying neurons (training loss plateaus while many neurons output zero).

Multi-Layer Perceptron: The Universal Approximator

By stacking layers, we can learn ANY function! This is not hand-waving — the Universal Approximation Theorem (Cybenko, 1989) proves that a neural network with just one hidden layer and enough neurons can approximate any continuous function to arbitrary accuracy. The catch: “enough neurons” might mean millions, and finding the right weights is the hard part. In practice, deeper networks with fewer neurons per layer learn hierarchical features more efficiently than one massive wide layer.

Backpropagation: How Networks Learn

Backpropagation uses the chain rule from calculus to compute gradients efficiently.
Math Connection: Backpropagation is just repeated application of the chain rule. See Chain Rule for the mathematical foundation.
The key insight:
  1. Compute error at output
  2. Propagate error backward through layers
  3. Update each weight proportionally to how much it contributed to the error
Lossw=Lossoutputoutputhiddenhiddenw\frac{\partial Loss}{\partial w} = \frac{\partial Loss}{\partial output} \cdot \frac{\partial output}{\partial hidden} \cdot \frac{\partial hidden}{\partial w}

Using PyTorch (The Professional Way)


Using scikit-learn


Network Architectures

Rule of thumb for tabular data:
  • Start with 2 hidden layers
  • Hidden size: between input and output size
  • Use ReLU activation
  • Use dropout for regularization

Regularization for Neural Networks

Dropout

Randomly “turn off” neurons during training. Think of it like a team where you randomly bench different players in each practice session. No single player can carry the team alone, so every player has to be competent. This forces the network to build redundant representations rather than relying on a few “star” neurons — which means it generalizes better to new data.
Practical tip: Start with dropout rate of 0.2-0.3 for hidden layers. If the model still overfits, increase toward 0.5. Never apply dropout to the output layer. Remember to call model.eval() during inference — dropout must be disabled for predictions.

Early Stopping

Stop training when validation loss stops improving — the simplest and most effective regularization technique. Training too long is like studying for an exam past the point of understanding into the territory of memorizing typos in the textbook.

Key Hyperparameters


When to Use Neural Networks

Good for:
  • Image data (use CNNs)
  • Text data (use Transformers)
  • Sequential data (use RNNs/LSTMs)
  • Very large datasets
  • Complex non-linear patterns
Not great for:
  • Small datasets (overfits easily — neural nets are data-hungry by nature)
  • When interpretability matters (explaining why a 10-layer network made a decision is much harder than explaining a decision tree)
  • Tabular data with fewer than 10,000 rows (tree-based models like XGBoost or Random Forest are almost always better here, and this is backed by extensive benchmarks)
Industry reality: For tabular data in production, gradient boosted trees (XGBoost, LightGBM) beat neural networks in the majority of Kaggle competitions and real-world deployments. Neural networks shine on unstructured data: images, text, audio, and video. If someone suggests a neural network for a 5,000-row CSV, push back.

🚀 Mini Projects

Project 1: Digit Recognizer

Build a neural network to recognize handwritten digits

Project 2: Neural Network from Scratch

Implement a neural network without libraries

Project 3: Activation Function Explorer

Compare different activation functions

Project 4: Hyperparameter Tuner

Find optimal architecture through experimentation

Project 1: Digit Recognizer

Build a neural network to recognize handwritten digits from the MNIST dataset.

Project 2: Neural Network from Scratch

Implement a simple neural network using only NumPy.

Project 3: Activation Function Explorer

Compare different activation functions and their effects on learning.

Project 4: Hyperparameter Tuner

Systematically find the best neural network architecture.

Key Takeaways

Neurons = Weighted Sums

Input × weights + bias → activation → output

Layers = Power

More layers = learn more complex patterns

Backprop = Chain Rule

Gradients flow backward to update weights

Regularize!

Dropout and early stopping prevent overfitting

What’s Next?

Now that you understand neural networks, let’s learn about regularization in more depth - the key to preventing overfitting in any model!

Continue to Module 13: Regularization

Learn L1, L2 regularization and other techniques to prevent overfitting