Neural networks

Lecture 18 · room 202

Prof. Niccolò Salvini

HEMA · Università Cattolica del Sacro Cuore, Rome

Monday 30 November 2026

Last time

Lecture 17 · Random forests and boosting · Fri 27 Nov

  • Bagging averages away a tree’s variance; a random forest decorrelates the trees with mtry
  • Boosting fits each small tree to the previous mistakes, slowly — the learning rate is the brake
  • Every tree in a forest is wrong; the forest is right because they are wrong in different ways

Today

Time Min
17:00 Last time · Set 15, the In class questions 10’
17:10 From linear to nonlinear · the perceptron, by hand 40’
17:50 Gradient descent 10’
18:00 Break 10’
18:10 One neuron is a GLM 15’
18:25 Hidden layers 25’
18:50 Set 16 · in Python 10’

Objectives

  • See where neural networks come from, and what problem they were built to solve
  • Meet the perceptron, and train one by hand — four rows, two epochs, no software
  • Understand gradient descent, the engine under every network
  • Discover that a network with one neuron is a GLM — and what that buys you
  • Add a hidden layer, and see flexibility and overfitting arrive together

Why a manager cares

The most over-sold and under-understood model in business

  • Every vendor deck says AI. Underneath, on a table of customers, it is usually a neural network — or a gradient-boosted tree pretending to be one
  • You will be asked to approve, fund, or trust one. You should know what it is: a regression that learned to bend
  • And you should know when it is the wrong tool — which, on tabular data, is most of the time. Friday is about that.

Section 1 From linear to nonlinear

The motivation, in one picture

A linear boundary — logistic regression — cannot follow a curve. Neural networks exist to draw the second kind. That is the whole motivation; everything else is machinery.

Note

They were inspired by the brain, in the 1940s. The inspiration is real and the analogy is thin: inputs arrive like signals at dendrites, the body sums them, the axon fires or does not. Keep the picture; do not lean on it.

Two dialects, revisited

From lecture 10 — now it matters:

Statistics Machine learning
Coefficients Weights W
Intercept Bias W_0
Transformation, link Activation function g
Fitting Training
Observations Training set

The trade, stated once more

You are about to gain flexibility and lose interpretability. A neural network learns whatever relationship is there. It does not tell you what it learned, and it gives you no standard errors. Choose it when prediction is the whole job.

Section 2 The perceptron

McCulloch & Pitts, 1943

Inputs X_1, \dots, X_p, weights W_1, \dots, W_p, a bias W_0, and a step:

\hat y \;=\; \operatorname{sign}\!\Big(W_0 + \sum_{j=1}^{p} W_j X_j\Big), \qquad \operatorname{sign}(z) = \begin{cases} +1 & z \ge 0 \\ -1 & z < 0\end{cases}

In general: \hat y = g\big[\phi(X; W)\big] — a linear combination \phi, then an activation g.

Now make both linear: g(z) = z. You have \hat y = W_0 + \sum_j W_j X_j. That is linear regression. The simplest possible neural network is the model you started the course with.

The difference that is not in the formula

Same equation, two philosophies:

  • Linear regression has a stochastic part: y = X\beta + \varepsilon. The noise is in the model; that is where tests and intervals come from.
  • The network is a deterministic map from input to output. No \varepsilon, no distribution, no inference — just find weights that predict well.
  • Regression imposes the form of the relationship. The network learns it.

Note

This is why a network can be more accurate and less useful at the same time. Both statements are about the same object.

Training: the perceptron rule

Supervised: we have the true y. Start with random weights, then for each observation:

W(t+1) \;=\; W(t) + \eta\,(y - \hat y)\,X

  • \eta \in (0, 1]: the learning rate — how big a step. Set by you, not learned. A hyperparameter.
  • (y - \hat y): the error. Correct prediction → zero → no update.
  • Wrong with y = +1: weights move up along X. Wrong with y = -1: down.

One pass over all observations is an epoch. Repeat until nothing changes, or you run out of patience.

By hand: the AND gate

Two binary inputs, output 1 only when both are 1. W_1 = 0.3, W_2 = -0.1, no bias, \eta = 0.1, threshold \theta = 0.25.

Obs X_1 X_2 y z \hat y error W after
1 0 0 0 0 0 0 —
2 0 1 0 −0.1 0 0 —
3 1 0 0 0.3 1 −1 (0.2,\,-0.1)
4 1 1 1 0.1 0 +1 (0.3,\,0.0)

End of epoch 1: W = (0.3, 0.0), two mistakes. Epoch 2: two more. Epoch 3: one. Epoch 4: none, W = (0.2, 0.1), and it stops. Four rows, one rule, and the network has learned AND.

By hand, watched

Every row, every number. W moves only on the rows marked wrong, and only along that row’s X.

The same thing, as geometry

Each mistake turns the boundary toward the point it got wrong. An epoch with no mistake means nothing will ever change again — that is convergence.

Section 3 Gradient descent

The general engine

The perceptron rule is a special case of something universal. Given a loss L(W) — say mean squared error — move the weights downhill:

W_j(t+1) \;=\; W_j(t) - \eta\,\frac{\partial L(W)}{\partial W_j}

  • Derivative positive: L rises with W_j, so decrease it. Negative: increase it. The minus sign is the whole idea.
  • Repeat until the gradient is (near) zero: a minimum
  • For a network, computing \partial L / \partial W layer by layer is called backpropagation. It is the chain rule, applied carefully, and nothing more.

Attention

A convex loss is a bowl: you reach the minimum. A network’s loss is not. You find a valley, which depends on where you started — as with k-means, run it more than once.

The learning rate, and what it does to you

Too small: it creeps — thousands of steps. Too large: it overshoots the valley and oscillates. Right: a smooth, decelerating descent. Practical variants: shrink \eta over time (annealing), or add inertia (momentum, \mu \approx 0.9) — each one more hyperparameter.

Gradient descent on linear regression

Apply the update to \hat y = XW with squared loss: W(t+1) = W(t) + \eta\, X^\top(y - XW).

It converges to \hat W = (X^\top X)^{-1} X^\top y. Exactly OLS. The network, trained by gradient descent, arrives where the formula was already standing.

OLS NN, identity activation
Intercept 1.01 W_0 = 1.01
X_1 −0.02 W_1 = −0.02
X_2 −0.35 W_2 = −0.35
X_3 −0.52 W_3 = −0.52

Identical coefficients, identical predictions. Nothing was gained — yet.

Section 4 One neuron is a GLM

The bridge

Train a network on the negative log-likelihood and gradient descent finds the MLE. More precisely: a single-neuron network with activation g is a GLM whose link is g^{-1}.

Target Regression Link h(\mu) Activation g(\lambda)
Continuous Linear identity identity
Binary Logistic \log \frac{\mu}{1-\mu} sigmoid \;\frac{1}{1+e^{-\lambda}}
Binary Probit \Phi^{-1}(\mu) \Phi(\lambda)
Counts Poisson \log \mu e^{\lambda}
Categorical Multinomial \log \frac{\mu_k}{1-\mu_k} softmax

Note

Take the link function, invert it, and you have the activation. Take the activation, invert it, and you have the link. You already know every model in this table from Module 1.

The sigmoid, since it is everywhere

Soften the step into \sigma(\lambda) = 1/(1+e^{-\lambda}) and the output is a probability. One sigmoid neuron with cross-entropy loss is logistic regression, weights and all: cross-entropy is the Bernoulli negative log-likelihood under another name.

Section 5 Hidden layers

Adding a layer

So far: input → output. Two layers, and nothing a GLM cannot do.

Now put L hidden neurons between them. Each one is a little GLM of the inputs, and the output is a GLM of them:

a_l = f\Big(W^{(1)}_{0l} + \sum_j W^{(1)}_{jl} X_j\Big), \qquad \hat y = g\Big(W^{(2)}_0 + \sum_l W^{(2)}_l\, a_l\Big)

  • Hidden: you never observe a_l. They are features the network invents.
  • Fully connected: every input feeds every hidden neuron. Weights: (p+1)L + (L+1).
  • Two activations, f and g, and they are usually different.

What a hidden layer buys

One hidden neuron: still a line. Two: one bend. Five: the boundary follows the data. Each neuron is a line; the output layer blends them.

Why the activations must bend

Set f and g both to the identity. Expand:

\hat y = W^{(2)}_0 + \sum_l W^{(2)}_l \Big(W^{(1)}_{0l} + \sum_j W^{(1)}_{jl} X_j\Big) \;=\; \text{a linear function of } X

  • Three inputs, two hidden, one output: 11 weights to express what 4 coefficients already did
  • Same model, more parameters, less interpretability, nothing gained

Attention

A hidden layer with linear activations is over-parameterised linear regression. The bend comes from f — sigmoid, tanh, ReLU. Without it, there is no network, only a slower way to run lm().

More flexibility, and what it costs

Ten inputs, one hidden layer of five sigmoid neurons, 90/10 split

Training error keeps falling. Test error falls, reaches a minimum, then rises: past that point the network is learning the noise of the training set. Overfitting, drawn.

Three ways to stop it

  • Early stopping: watch a validation set; stop when its error turns up. Simple, empirical, no guarantee it is the true optimum
  • Regularisation: add a penalty \lambda J(W) to the loss. J = \sum W^2 is ridge — weight decay in the ML dialect. J = \sum |W| is lasso. Same idea as Module 1, same \lambda to tune
  • Fewer neurons: the honest fix, and the one people resist

Note

Everything from lecture 7 — ridge, lasso, cross-validation for \lambda — applies unchanged. A network is a very flexible regression, and it overfits like one.

In R

library(tidyverse); library(nnet); library(ISLR2)
d <- Default |> mutate(default = default == "Yes", balance = scale(balance), income = scale(income))

set.seed(1); tr <- sample(nrow(d), 7000)

fit <- nnet(default ~ balance + income + student, data = d[tr, ],
            size = 5, decay = 0.01, maxit = 500, trace = FALSE)     # 5 hidden, weight decay

p <- predict(fit, d[-tr, ])                                          # probabilities

mean((p > 0.5) == d$default[-tr])                                    # held-out accuracy

size is L. decay is \lambda. Both are hyperparameters, and Friday’s lab is about choosing them without lying to yourself.

A neural network is a regression that learned to bend. Whether it needed to is the question you must answer before you fit one.

Before the lab

  • Run the block. Change size to 1. Compare with glm(family = binomial) on the same rows: are the coefficients close? (They should be — why?)
  • size = 20, decay = 0. Training accuracy? Test accuracy? Draw the curve you saw on slide 17 in your head.
  • Reread lecture 14, last section: autoencoders. Friday we make the connection.

Practice: Set 16

Neural networks, in Python · about 40 minutes · before Fri 4 Dec

  1. Do it in your own script, with the seed the set gives you.
  2. Check your numbers on Blackboard: test Set 16. Feedback the moment you submit — as many attempts as you like.
  3. Bring the sentences. Questions marked In class have no number to check: we discuss them at the lab on Fri 4 Dec.

Not graded. The test is a mirror, not an exam.

Reading

  • ISLR §10.1–10.2 — single-layer networks; the parameter-counting examples
  • ISLR §10.7 — fitting a neural network; skim for the vocabulary
  • Provost & Fawcett chapter 4, Fitting a model to data — the overfitting discussion, which applies here twice over