Shallow neural networks

Scroll to begin
A torso-less blacksmith raises a hammer over a glowing metal rod resting on an anvil. The rod is being shaped into a sine wave.

01 / A shallow network

A linear equation, drawn as a network.

ŷ = wx + b = x + 0.5

The arrow from x has weight w = 1. The faint constant-1 node adds the intercept b = 0.5; it is not a second observed feature.

Bias connections disappear from the drawing for clarity. The intercept remains part of the equation and the model.

A ReLU clips the negative part.

ReLU keeps positive inputs and clips negative inputs to zero, creating a joint at zero.

ReLU(x) = max(0, x)

Move the joint. Change the slope.

First move the joint with t. Then change a to steepen the slope and turn it downward.

a ReLU(x − t)

The contribution stays zero before t. Faint dashed curves show comparison examples.

One neuron starts the climb.

The first neuron activates at −1, adding +1 to the slope.

f(x) = ReLU(x + 1)

A second neuron turns it downward.

The second activates at zero and adds −2 to the slope: +1 − 2 = −1. Both neurons keep contributing.

f(x) = ReLU(x + 1) − 2 ReLU(x)

A third neuron flattens it again.

The third activates at +1 and adds +1, returning the total slope to zero.

f(x) = ReLU(x + 1)− 2 ReLU(x) + ReLU(x − 1)

That is a shallow neural network.

One hidden layer computes the ReLUs. A linear output adds their weighted activations and a bias.

f(x) = c + ∑j aj ReLU(wjx + bj)

02 / What width changes

One neuron, one place to bend.

The width is the number of neurons in our single hidden layer. One neuron can introduce at most one joint, dividing the input interval into two linear pieces.

A second neuron adds another joint.

Add a second neuron with a different threshold. The output sums both hinges: two joints, three linear pieces.

Three neurons, four straight pieces.

A third neuron adds another slope change. All three receive the same scalar input in parallel, producing three joints and four linear pieces.

A wider layer allows more joints.

The layer widens from three neurons to six, giving this example six joints and seven linear pieces.

In general, width m allows at most m joints and m + 1 pieces. Inactive units, zero weights, coincident joints, or cancellation can reduce the count.

03 / Approximation by adding joints

Approximate a smooth target.

Approximate sin(x) on [0, 2π] with the same shallow architecture. Six hidden neurons supply the initial slope and five joints, joining selected points with straight segments.

Zoom into the gap.

Between joints, the straight segment misses the curved target. The vertical gap is the error at this input.

Add a neuron and a joint

Add a joint on the sine curve at the marked input. One new neuron, plus updated existing output weights, replaces one segment with two and makes the error zero here.

Keep adding places to bend.

The layer widens from seven neurons to twenty-six. Shorter segments approach the sine curve; the result looks smooth but remains piecewise linear.

Approximate a continuous curve.

A continuous function on a closed, bounded interval can be approximated uniformly as closely as we want by a sufficiently wide, one-hidden-layer ReLU network. Continuity lets sufficiently fine straight segments stay close throughout the interval.

This guarantees that suitable weights exist. It does not guarantee that training finds them, specify data requirements, or justify predictions outside the interval.

04 / Sigmoid classification

Early Application: Beck, King, and Zeng (2000)

Beck, King, and Zeng train a neural network with a sigmoid activation function to predict international conflict. Equation 3 is:

Yi∼Bernoulli⁡(πi)Y_i\sim\operatorname{Bernoulli}(\pi_i)

πi=sigmoid⁡ ⁣[γ0+∑j=1Mγjsigmoid⁡(Xiβ(j))]\pi_i=\operatorname{sigmoid}\!\left[\gamma_0+\sum_{j=1}^{M}\gamma_j\operatorname{sigmoid}(X_i\beta_{(j)})\right]

sigmoid⁡(z)=11+e−z\operatorname{sigmoid}(z)=\frac{1}{1+e^{-z}}

Nathaniel Beck, Gary King, and Langche Zeng. 2000. “Improving Quantitative Studies of International Conflict: A Conjecture.” American Political Science Review 94(1): 21–35, Equation 3.

From logistic regression to a neural network.

Both models use a Bernoulli outcome. Logistic regression applies the sigmoid directly to an affine predictor:

πi=sigmoid⁡(Xiβ)\pi_i=\operatorname{sigmoid}(X_i\beta)

The network combines several sigmoid features before the final sigmoid:

πi=sigmoid⁡ ⁣[γ0+∑j=1Mγjsigmoid⁡(Xiβ(j))]\pi_i=\operatorname{sigmoid}\!\left[\gamma_0+\sum_{j=1}^{M}\gamma_j\operatorname{sigmoid}(X_i\beta_{(j)})\right]

With one input, logistic regression gives a monotone probability curve; with two inputs, its classification boundary is a straight line. A sigmoid network can instead express a hump and a curved, enclosed boundary. The illustrative weights are chosen to show capacity, and the observations stay fixed throughout the comparison. The following scenes show how adding hidden neurons changes the probability curve.

More neurons allow more complicated shapes.

Start with one hidden sigmoid, which gives a rising probability curve. Keep it fixed and introduce a second sigmoid centered at 0.4. Give its output a negative weight. As the second activation increases, its negative contribution pulls the score down. The resulting probability rises and then falls.

p^(x)=σ ⁣(−3+6h1(x)−6h2(x))\widehat p(x)=\sigma\!\left(-3+6h_1(x)-6h_2(x)\right)

05 / Learned representations

Two curved classes, two hidden neurons.

Our binary example has two inputs and two adjacent curved groups. No straight line separates the classes in the original coordinates. We fit a shallow network with two ReLU hidden neurons and one sigmoid output. We have changed the hidden activation from the historical sigmoid example; the representation-plus-head interpretation is the same.

h1(x)≈ReLU⁡ ⁣(4.948−3.354x1−1.711x2)h2(x)≈ReLU⁡ ⁣(0.891+2.204x1−3.770x2)p^≈sigmoid⁡ ⁣(27.845−6.217h1−4.456h2)\begin{aligned}h_1(\mathbf x)&\approx\operatorname{ReLU}\!\left(4.948-3.354x_1-1.711x_2\right)\\[.25em]h_2(\mathbf x)&\approx\operatorname{ReLU}\!\left(0.891+2.204x_1-3.770x_2\right)\\[.25em]\widehat p&\approx\operatorname{sigmoid}\!\left(27.845-6.217h_1-4.456h_2\right)\end{aligned}

This is a new binary fit inspired by the course’s curved three-class example. It is not the original three-class fitted network. The displayed parameters are rounded to three decimals. Every plot and hidden activation is calculated with the full fitted precision.

One observation gets two new coordinates.

Follow the marked observation through the two hidden neurons. Each takes a weighted sum of its inputs, adds a bias, and applies ReLU. The resulting pair of activations is this observation’s location in representation space.

(x1,x2)⟼(h1(x),h2(x))(x_1,x_2)\longmapsto(h_1(\mathbf x),h_2(\mathbf x))

Move the observations into representation space.

Every point now moves from its input coordinates to its actual hidden activations, preserving its identity and class. The network has exactly two hidden neurons, so we can plot the entire representation directly, without dimensionality reduction.

The movement illustrates a change of coordinates for an already fitted network. It is not an animation of training. The classifier’s straight boundary remains hidden until the movement is complete.

Now one straight line separates the classes.

Reveal the fitted classification head. Its probability equals 0.5 where its affine score is zero, giving a straight decision boundary in the learned coordinates.

p^=σ(ϕ0+ϕ1h1+ϕ2h2)\widehat p=\sigma(\phi_0+\phi_1h_1+\phi_2h_2)

p^=12  ⟺  ϕ0+ϕ1h1+ϕ2h2=0\widehat p=\tfrac12\iff\phi_0+\phi_1h_1+\phi_2h_2=0

This head correctly classifies every training observation at the 0.5 cutoff. We use the network’s own head, not a newly fitted classifier. Its probabilities are not exactly zero or one.

A simple head on learned features.

The same fitted classifier has a nonlinear boundary in the original inputs and a straight boundary in the hidden representation. The hidden layer learned features that make these training observations linearly separable; the classification head is logistic regression on those features.

The representation and head were learned together from the labels. Perfect training classification does not imply perfect probability estimates or perfect predictions for new observations.

06 · Width and generalization

Return to the original coordinates.

Enlarge the original-coordinate graph. We can keep plotting the decision boundary here as the hidden layer grows, even when the learned representation has more than two dimensions.

Add variation around the class curves.

The same 82 observations receive independent vertical Gaussian perturbations. Class labels and horizontal coordinates stay fixed. Hold this new sample constant throughout the width comparison.

x2=−sin⁡2(x1)+0.9y+ε,ε∼N(0,0.182)x_2=-\sin^2(x_1)+0.9y+\varepsilon,\qquad\varepsilon\sim\mathcal N(0,0.18^2)

For this experiment, jointly fit the ReLU hidden layer and sigmoid head using cross-entropy and gradient updates, with no explicit penalty. Each width gets the same training procedure; choose among four initializations using training loss alone.

More neurons, a rounder boundary.

Increase the hidden width from 2 to 4, 8, 16, 32, 64, 128, and 256. Every displayed boundary comes from that width’s fitted parameters. The scroll transition crossfades separate fits; it does not depict a training trajectory.

All eight models classify all 82 training observations correctly. The wide model has a rounder boundary here, although every finite ReLU boundary is still piecewise linear. More width does not guarantee a smoother boundary at every step.

Does it predict new observations better?

Generate 40,000 independent observations from the same two-class process after freezing the models. The plot shows a fixed subset as hollow markers; rings identify mistakes. Both networks are evaluated on the same complete test sample.

The two-neuron network makes 378 mistakes, an error rate of 0.945%. The 256-neuron network makes 281 mistakes, or 0.7025%. It corrects 150 mistakes and introduces 53 new ones. In this example, the wider network improves classification beyond a perfect training fit.

This comparison concerns class predictions at a 0.5 cutoff. It does not establish calibrated probabilities or guarantee that wider networks generalize better.

Training accuracy is already perfect.

The measured error curve shows every fitted width. Training classification error is zero throughout; test error varies and does not decline at every step. The two-neuron model already separates the training labels, so this experiment does not locate an interpolation threshold or measure a complete double-descent curve.

Three additional training draws checked the same procedure on the fresh test sample. The wider endpoint made fewer classification mistakes in each draw, but one two-neuron fit failed to fit its training data, and one improvement was smaller than test-sample uncertainty. These checks support an illustration, not a general rule about width.

Beyond interpolation: double descent.

The final diagram is a schematic, separate from our measured results. In double descent, test error first falls, rises near the capacity needed to interpolate the training data, then falls again at greater capacity. The fitting procedure influences which solution the larger model selects.

Our example motivates the last part of that argument: adding capacity can improve predictions even after every training label is classified correctly. The full pattern must be evaluated across an appropriate range of capacities; it is not implied by a rounder-looking boundary.

Belkin et al. (2019), Reconciling modern machine-learning practice and the classical bias–variance trade-off.

Back to the beginning