Perceptrons & Multi-Layer Networks
The Biological Inspiration
Your brain contains approximately 86 billion neurons, each connected to thousands of others. A single neuron:- Receives signals from other neurons through dendrites
- Processes those signals in the cell body
- Fires (or not) based on whether the combined signal exceeds a threshold
- Transmits that signal to other neurons through its axon
The Perceptron: One Artificial Neuron
Mathematical Formulation
A perceptron computes: Where:- = input features
- = weights (learnable)
- = bias (learnable)
- = activation function
Visual Representation
Building from Scratch
The Perceptron Learning Rule
The training algorithm is beautifully simple:Why This Works
The update rule has an elegant geometric interpretation:- If we predict 0 but should predict 1: increase weights in direction of x (pull the decision boundary toward this point)
- If we predict 1 but should predict 0: decrease weights in direction of x (push the decision boundary away from this point)
- The learning rate controls how big each update is — too large and the boundary oscillates wildly, too small and learning takes forever
Convergence Theorem
Perceptron Convergence Theorem: If the data is linearly separable, the perceptron algorithm will converge to a solution in finite time. The number of updates is bounded by , where is the maximum norm of any data point and is the margin — the distance between the closest points and the separating hyperplane. Wider margins mean faster convergence.Historical Note: Minsky & Papert’s 1969 book Perceptrons showed that single perceptrons can’t solve non-linearly-separable problems (like XOR). This led to the “AI Winter” — but they missed that multiple layers could solve any problem!
The XOR Problem: Why We Need Depth
Multi-Layer Perceptron (MLP)
The Universal Approximation Theorem
A neural network with a single hidden layer containing a finite number of neurons can approximate any continuous function on compact subsets of .In other words: deep networks can learn anything (given enough neurons and data). But here is the catch most people miss: the theorem says such a network exists — it does not say you can find it efficiently. In practice, deeper networks with fewer neurons per layer are far easier to train than enormously wide shallow networks. The theorem is an existence proof, not a training recipe. It is the difference between “a key to this lock exists somewhere in the universe” and “here is the key.”
Architecture
Building an MLP from Scratch
How MLPs Solve XOR
The hidden layer creates a new representation where the problem becomes linearly separable:Visualizing the Decision Boundary
Deeper Networks
Why Go Deep?
The Depth vs Width Tradeoff
Theorem: A 2-layer network of width can approximate functions that require width with a deeper network of width . In practice:- Deep narrow networks learn hierarchical features (more efficient) — they compose simple patterns into complex ones
- Wide shallow networks have more brute-force capacity — they memorize rather than generalize
- Modern architectures are both deep AND wide (but depth usually helps more)
A Deeper Network
PyTorch Implementation
Now let’s see how to build the same networks using PyTorch:Key Concepts Summary
Exercises
Exercise 1: Logic Gates
Exercise 1: Logic Gates
Implement perceptrons for:
- OR gate
- NAND gate
- Can you create XOR using only NAND gates? (Hint: NAND is universal)
Exercise 2: Visualization
Exercise 2: Visualization
Create an animation showing how the decision boundary evolves during training:
Exercise 3: Depth Experiments
Exercise 3: Depth Experiments
Compare networks of different depths on the moons dataset:
- [2, 8, 1]
- [2, 8, 8, 1]
- [2, 8, 8, 8, 1]
- [2, 8, 8, 8, 8, 1]
Exercise 4: MNIST from Scratch
Exercise 4: MNIST from Scratch
Extend our MLP to classify MNIST digits:
- Load MNIST data
- Flatten images to 784-dimensional vectors
- Train a [784, 256, 128, 10] network
- Compare to our PyTorch version
What’s Next
Now that you understand how neurons compute and connect, let’s dive deep into how they learn:Module 3: Backpropagation Deep Dive
The algorithm that makes learning possible — chain rule, computational graphs, and gradient flow.
Interview Deep-Dive
Why do we initialize weights randomly rather than to zero? And why do we initialize them 'small'?
Why do we initialize weights randomly rather than to zero? And why do we initialize them 'small'?
Strong Answer:
- Zero initialization breaks symmetry: if all weights start at zero, every neuron in a layer computes the same output, receives the same gradient, and makes the same update. They remain identical throughout training — effectively, you have one neuron replicated times, wasting all capacity. Random initialization ensures each neuron starts on a different trajectory and learns a different feature.
- Small initialization prevents saturation: for sigmoid and tanh activations, large inputs push the activation into the flat (saturated) regions where the derivative is near zero. If weights are large, the pre-activation values will be large, gradients will vanish, and learning will stall from the very first step. For ReLU, very large weights can cause some neurons to produce extremely large activations in early layers, leading to numerical instability.
- The specific scale matters and depends on the activation function. Xavier/Glorot initialization () is designed for sigmoid/tanh: it preserves the variance of activations and gradients across layers. He initialization () is designed for ReLU: it accounts for the fact that ReLU zeroes out half the activations, so the surviving activations need twice the variance to maintain signal strength.
- The intuition: initialization sets the starting point of optimization. A bad starting point (too large, too uniform) can place you in a region of the loss landscape where gradients are uninformative, making training either impossible or painfully slow.
What is the depth-width trade-off in neural network design? When would you prefer a wider network over a deeper one?
What is the depth-width trade-off in neural network design? When would you prefer a wider network over a deeper one?
Strong Answer:
- Depth provides compositional expressiveness: each layer can build on the representations of the previous layer, enabling hierarchical feature learning. Width provides per-layer capacity: more neurons in a single layer can represent more diverse features at the same level of abstraction.
- Prefer depth when the data has hierarchical structure (images, language, audio) because the compositional structure of deep networks naturally matches the compositional structure of the data. A 10-layer network with 256 neurons per layer will learn edge-to-texture-to-part-to-object hierarchies that a 2-layer network with 1280 neurons per layer cannot.
- Prefer width when the data lacks hierarchical structure (some tabular problems), when training stability is a concern (shallow wide networks are easier to optimize), or when latency matters (wide shallow networks can be parallelized more effectively on hardware, while depth creates sequential dependencies).
- In modern practice, the best architectures are both deep AND wide, with techniques like skip connections and normalization making deep training feasible. The trend in large language models is to scale both depth and width together, following scaling laws that predict optimal ratios given a compute budget.