CS 601.471/671  ·  NLP: Self-supervised Models  ·  Fall 2026

Handout #3 — From Counting to Learning

Session 3 (Tue, Sept 8)  ·  Slides: Feedforward Nets  ·  Course page

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.

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.

blah blah blah discarded and our problems turning into context window of size 4 target word look up embeddings ○○○○○○ ○○○○○○ f( context , Θ ) neural network, trainable parameters probabilities over the vocabulary mat table into ant chair … push this up
A fixed-window neural language model (Bengio et al., 2003). Same Markov assumption as a trigram; entirely different way of computing the conditional.

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.

Quick check · What did we gain?

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
The embedding lookup and shared network define a function on every in-vocabulary context. Training may transfer useful patterns from related contexts, but a defined prediction is not a guarantee of accuracy. Held-out evaluation checks whether this sharing helped.

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.

Try one neuron

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
$z=2-3+2=1$. ReLU gives $1$; sigmoid gives $1/(1+e^{-1})\approx0.7311$. A hidden ReLU value is not restricted to the probability range.

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.

Slides 21–23 · Parameters and computation

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

FunctionDefinitionInterpretation
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.

Derive the collapse $$W_2(W_1x+b_1)+b_2=(W_2W_1)x+(W_2b_1+b_2).$$

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.
Code check · Remove the activation

What happens when the two hidden coordinates above are added without ReLU?

Work it out, then reveal the answer
Every output becomes zero: the two linear scores are opposites and cancel. With ReLU, each hidden unit detects one direction of disagreement, and their sum represents XOR on these four inputs.

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.

Three separate questions Can the architecture represent the target (capacity)? Can our algorithm find good parameters (optimization)? Does the fitted function work on new examples (generalization)? A positive answer to one does not answer the others.

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.

PeriodIdea in the slidesLesson for today
1940s–1960sThreshold logic units; perceptron learningSimple units compute logic, and an error-driven rule can fit a linear separator.
1970s–early 1980sLimitations of perceptrons; symbolic approachesA single linear boundary is limited; useful hidden features raise a training problem.
Mid-1980s–1990sRenewed interest in multilayer learning and backpropEfficient derivatives make it possible to adjust hidden-layer weights.
Mid-1990s–2000sStrong probabilistic and kernel methodsOptimization, data, computation, and generalization all affect practical success.
2010s onwardDeep learning and self-supervisionMore 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.

Slides 39–40 · Does the prediction necessarily flip?

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
The old score is $-3$ and $o=0$. The update gives $w=(-2,2)^\top$ and $b=1$, so the new score is $-1$ and the output is still 0. The score increased, but it did not cross the threshold. More generally its change on this same example is $\eta(t-o)(\|x\|^2+1)$ when the bias is updated.

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.