Neural Networks: Perceptron to MLP
600 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Neural Networks: Perceptron to MLP ## ๐ฏ Learning Objectives - Implement a single perceptron and understand its limitations - Build a multi-layer perceptron (MLP) for non-linear problems - Derive and implement backpropagation - Choose appropriate activation functions ## ๐ Core Content ### 3.1 Intuition: Building...

Neural Networks: Perceptron to MLP
๐ฏ Learning Objectives
- Implement a single perceptron and understand its limitations
- Build a multi-layer perceptron (MLP) for non-linear problems
- Derive and implement backpropagation
- Choose appropriate activation functions
๐ Core Content
3.1 Intuition: Building a Brain-like Computing System
A neural network is a chain of simple computing units (neurons), each taking inputs, performing a weighted sum, applying a non-linear activation, and passing the result forward. Stacking these layers lets the network learn increasingly abstract features โ first edges, then shapes, then objects.
3.2 The Perceptron (1958)
y^โ={10โifย wTx+b>0otherwiseโLimitation: A single perceptron can only learn linearly separable functions โ it can't learn XOR. This was shown by Minsky & Papert (1969) and caused the first "AI winter."
3.3 Multi-Layer Perceptron (MLP)
An MLP adds hidden layers with non-linear activation functions:
(Diagram)
3.4 Activation Functions
| Function | Formula | Range | Use Case |
|---|---|---|---|
| Sigmoid | ฯ(x)=1/(1+eโx) | (0, 1) | Output for binary classification |
| Tanh | tanh(x)=(exโeโx)/(ex+eโx) | (-1, 1) | Hidden layers (zero-centered) |
| ReLU | max(0,x) | [0, โ) | Default for hidden layers |
| Leaky ReLU | max(0.01x,x) | (-โ, โ) | Fixes "dying ReLU" |
3.5 Backpropagation
Backpropagation computes gradients via the chain rule, from output back to input:
- Forward pass: Compute predictions
- Compute loss: L(y^โ,y)
- Backward pass:
- โW(L)โLโ=โy^โโLโโ โz(L)โy^โโโ โW(L)โz(L)โ
- Propagate error backward through layers
python# runnable from sklearn.neural_network import MLPClassifier from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split X, y = make_classification(n_samples=500, n_features=10, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) mlp = MLPClassifier( hidden_layer_sizes=(64, 32), activation='relu', solver='adam', learning_rate='adaptive', max_iter=500, early_stopping=True, random_state=42 ) mlp.fit(X_train, y_train) print(f"MLP test accuracy: {mlp.score(X_test, y_test):.3f}") print(f"Loss curve: {mlp.loss_curve_[:5]}")
๐ Practice Questions
Q1: Why can't a single perceptron learn XOR?XOR is not linearly separable. A single perceptron creates a linear decision boundary (line in 2D). XOR requires a non-linear boundary โ you can't draw a single straight line that separates (0,0) and (1,1) from (0,1) and (1,0). An MLP with at least one hidden layer can learn XOR. Q2: What problem does ReLU solve compared to sigmoid?Vanishing gradient: Sigmoid's gradient saturates near 0 and 1 (gradient โ 0). Deep networks with sigmoid have gradients that vanish to 0 in early layers. ReLU's gradient is 1 for positive inputs and 0 for negative โ it doesn't saturate for positive values, allowing gradients to flow through deep networks. Q3: What is the "dying ReLU" problem?If a ReLU neuron gets a large negative bias update, its output becomes 0 for all inputs, and its gradient is 0 (can't recover). The neuron is "dead." Solutions: use Leaky ReLU (small positive slope for negative inputs) or ELU. Q4: How does backpropagation use the chain rule?For a 2-layer network: L=L(ฯ(W2โฯ(W1โx+b1โ)+b2โ),y)The chain rule gives: โW1โโLโ=โy^โโLโโ โa2โโy^โโโ โz2โโa2โโโ โa1โโz2โโโ โz1โโa1โโโ โW1โโz1โโEach term is a Jacobian matrix. The key insight: many terms are reused โ the error at layer l depends on the error at layer l+1, so we compute backward from output to input. Join Discord PreviousNaive BayesNextKernel Methods