How can a collection of weighted sums represent useful patterns? Today we connect count-based language models to parameterized functions, build feedforward networks, and explore why nonlinear hidden layers matter. We finish with the history of neural networks and perceptron learning.
- Reading for today
- Tuesday, Sept 8: slides 1–54 of Feedforward Nets.
- Time
- About 35–45 minutes including the exercises.
- By the end
- Specify an MLP and count its parameters; explain nonlinearity, XOR, width, and depth; distinguish capacity from learning; work through a perceptron update.
- Assumes
- Handout #1: matrix algebra and Handout #2: sparsity.
- How to use it
- Try each question before opening its answer. The XOR code is a runnable Python example.
| Slides | Reading route |
|---|---|
| 1–2 | §1: From counting to functions |
| 3–31 | §2: Networks and capacity |
| 32–54 | §3: History and perceptrons |
1Language modeling as a learning problem
Slides 1–2 · Connection to Handout #2
A count-based language model stores statistics for discrete contexts. A neural language model instead computes a distribution with a parameterized function $p_\theta(w\mid c)$. The corpus still supplies the examples: a context $c$ is the input and its next word is the target. Self-supervision provides the labels; supervised learning machinery fits the resulting input–target pairs.
1.1 A fixed-window example
Keep the previous four words, look up a learned vector for each word, concatenate the vectors, and feed them to a network. The network produces one logit (an unrestricted score) per vocabulary item. Softmax turns the logits into a probability distribution.
The embeddings and the network weights are shared across contexts. Training on one context can therefore change predictions for another. We still have a finite context window; we have changed how the conditional distribution is estimated, not removed the Markov assumption.
1.2 Parameter sharing and generalization
If “dog” and “cat” acquire similar embeddings, the network can make similar predictions after “the dog slept” and “the cat slept.” This gives a way to transfer evidence to unseen contexts. Useful similarity has to be learned: a neural network is not guaranteed to generalize well merely because it outputs a distribution.
For vocabulary size $V$, embedding size $d$, window size $k$, and one hidden layer of width $h$, a simple untied model has $Vd+(kd)h+h+hV+V$ parameters. This grows linearly in $V$ and $k$ when $d,h$ are fixed, unlike the $V^{k+1}$ possible entries in a dense count table. Sparse tables store only observed entries, but still face the problem of estimating unseen combinations.
A model has never seen the exact context “the cat slept.” Why can it still produce a prediction? Does that prediction have to be good?
Work it out, then reveal the answer
2What a feedforward neural network is
Slides 3–31
A feedforward network is a parameterized computation with no directed cycles: information flows from inputs through intermediate values to outputs. Here we study an MLP, built from fully connected affine layers and nonlinear activations. MLPs are one kind of feedforward network. Hidden values are learned features; they need not be probabilities or class predictions.
2.1 One unit: score, shift, activate
$$z=w^\top x+b=\sum_{i=1}^{d}w_i x_i+b,\qquad a=\sigma(z).$$The weights select and combine input features. The bias shifts the threshold independently of the input. The activation changes how the score is passed onward. The map $w^\top x+b$ is affine; it is linear in $x$ only when $b=0$. We often call the module a “linear layer” even when it includes a bias.
Let $x=(2,-1)^\top$, $w=(1,3)^\top$, and $b=2$. Find the preactivation $z$, the ReLU output, and the sigmoid output.
Work it out, then reveal the answer
2.2 From units to layers, from layers to an architecture
Stack $h$ weight vectors as rows of $W_1$. For a two-layer MLP with input dimension $d$ and output dimension $m$:
$$h_1=\sigma(W_1x+b_1),\qquad s=W_2h_1+b_2,$$ $$W_1\in\mathbb R^{h\times d},\quad b_1\in\mathbb R^h,\quad W_2\in\mathbb R^{m\times h},\quad b_2\in\mathbb R^m.$$We count layers with trainable weights, excluding the input: this is two layers, one of them hidden. Architecture specifies the connectivity, dimensions, and activations; parameters specify the numerical weights and biases. Both are needed to determine a particular function. Width is the size of a hidden layer; depth is the number of successive layers.
Compare networks $4\to6\to3$ and $4\to3\to3\to3$, with a bias in every layer. How many parameters and forward multiply-accumulates does each use per example? Can you infer wall-clock speed from depth alone?
Work it out, then reveal the answer
The first has $(4\cdot6+6)+(6\cdot3+3)=51$ parameters and $24+18=42$ matrix multiply-accumulates. The second has $(4\cdot3+3)+2(3\cdot3+3)=39$ parameters and $12+9+9=30$ multiply-accumulates. These counts exclude bias additions and activations.
Depth alone does not determine cost. Actual speed also depends on matrix sizes, batching, hardware, and the extra sequential operations in the deeper model.
2.3 Nonlinearity is essential
| Function | Definition | Interpretation |
|---|---|---|
| Sigmoid | $\sigma(z)=1/(1+e^{-z})$ | Smoothly maps a scalar into $(0,1)$; saturates near either end. |
| Tanh | $\tanh(z)$ | Maps a scalar into $(-1,1)$; also saturates. |
| ReLU | $\max(0,z)$ | Zero on the negative side; unbounded on the positive side. Derivative is 0 or 1 away from zero. |
| Softmax | $p_j=e^{s_j}/\sum_r e^{s_r}$ | A vector map: outputs are positive and sum to 1 for finite logits in exact arithmetic. |
Sigmoid, tanh, and ReLU act elementwise in a layer. Softmax couples the coordinates. Several sigmoids need not sum to one; use softmax for a choice among mutually exclusive classes. ReLU avoids saturation on its positive side, but inactive units have zero derivative and networks using ReLU can still have vanishing or exploding gradients.
Without an intervening nonlinearity, multiple affine layers collapse to one affine map. Extra depth cannot introduce a nonlinear decision boundary. A narrow intermediate layer may also constrain the rank of the effective weight matrix.
2.4 Expressivity: XOR, width, and depth
XOR labels $(0,0)$ and $(1,1)$ as 0, and $(0,1)$ and $(1,0)$ as 1. A single affine threshold cannot separate the two diagonals. Nonlinear hidden features can: for binary inputs, take $h_1=\operatorname{ReLU}(x_1-x_2)$, $h_2=\operatorname{ReLU}(x_2-x_1)$, and output $h_1+h_2$.
import torch
x = torch.tensor([[0., 0.], [0., 1.], [1., 0.], [1., 1.]])
W = torch.tensor([[1., -1.], [-1., 1.]])
h = torch.relu(x @ W.T)
y = h.sum(dim=1)
assert y.tolist() == [0., 1., 1., 0.]
print(h, y)
# Experiment: remove torch.relu. Predict the outputs before running.
What happens when the two hidden coordinates above are added without ReLU?
Work it out, then reveal the answer
The universal approximation idea in slides 24–31 is an expressivity statement: with a suitable activation and enough hidden units, a one-hidden-layer MLP can approximate any continuous function on a compact domain to arbitrary accuracy. It does not say a fixed small network represents every function, that gradient descent will find the needed weights, or that the result generalizes from finite data. Some function families admit much more compact deep representations than shallow ones; this does not mean deeper is always better.
3History: from fixed features to learned hidden layers
Slides 32–54
The biological analogy motivated simple computational units, but an MLP is not a detailed model of a biological brain. The historical thread to follow is the separation between representing a function and finding its parameters.
| Period | Idea in the slides | Lesson for today |
|---|---|---|
| 1940s–1960s | Threshold logic units; perceptron learning | Simple units compute logic, and an error-driven rule can fit a linear separator. |
| 1970s–early 1980s | Limitations of perceptrons; symbolic approaches | A single linear boundary is limited; useful hidden features raise a training problem. |
| Mid-1980s–1990s | Renewed interest in multilayer learning and backprop | Efficient derivatives make it possible to adjust hidden-layer weights. |
| Mid-1990s–2000s | Strong probabilistic and kernel methods | Optimization, data, computation, and generalization all affect practical success. |
| 2010s onward | Deep learning and self-supervision | More data, faster hardware, and algorithmic improvements reinforce one another. |
3.1 An update before gradients
A threshold perceptron predicts $o=\mathbf{1}[w^\top x+b>0]$. For binary target $t$, the error-driven rule in the slides is
$$w\leftarrow w+\eta(t-o)x,\qquad b\leftarrow b+\eta(t-o).$$A correct prediction causes no update. A missed positive increases the score on that example; a false positive decreases it. The classic perceptron convergence guarantee assumes linearly separable training data. This rule does not differentiate the discontinuous threshold function.
Let $x=(1,0)^\top$, $w=(-3,2)^\top$, $b=0$, $t=1$, and $\eta=1$. Update both weights and bias. Is the new output necessarily correct?
Work it out, then reveal the answer
A single affine threshold cannot express XOR, whereas the hidden-layer construction in §2.4 can. Be careful with historical layer counts: the impossibility is about a linear separator, not an MLP with a nonlinear hidden layer. The next challenge is to learn those hidden features rather than supply their weights by hand.