🌏 中文版
Video status: Videos included. This article is written from the official MOOC handout 212_handout.pdf and transcripts of the four official videos, with formulas following the handout. The transcripts are machine transcripts made for this article (Whisper), not official captions, and no human has proofread them. The videos are taught in Mandarin; the slides are in English. Each section opens with its official video and the approximate time ranges. Page numbers refer to the number at the lower right of each slide (0/23 to 23/23), and wherever the text says "in the lecture" it is paraphrasing what is said in the video.
This is Techniques Lecture 12 in Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques. The Roadmap on page 1 of the handout divides Techniques into three ways of handling features. The first two are embedding numerous features with kernels (Embedding Numerous Features) and combining predictive features with aggregation (Combining Predictive Features). This lecture opens the third, Distilling Implicit Features: Extraction Models, in which the model extracts hidden features from the data by itself. The previous lecture, Techniques Lecture 11: Gradient Boosted Decision Trees, takes up a single line on that page: aggregating trees from functional gradient and steepest descent subject to any error measure.
There are four prerequisites: the perceptron from Foundations Lecture 2; the three linear models and (stochastic) gradient descent from Foundations Lecture 9 through Lecture 11; linear aggregation from Techniques Lecture 7; and the chain rule from calculus. After reading, you should be able to build AND, OR, and XOR out of perceptrons on your own, write out the forward computation of a --- network, derive the recursion for from scratch, and compute the gradient of every weight in a small network by hand.
Why stack a second layer: a linear aggregation of perceptrons can implement AND but not XOR
(Video 00:17–04:17 reviews the previous lecture and draws the linear aggregation of perceptrons as a picture, 04:17–09:02 builds AND, 09:02–13:08 covers powerfulness and limitation, 13:08–16:21 stacks up the multi-layer perceptron, 16:21–19:56 discusses biological neurons, and Fun Time follows.)
Drawing a linear aggregation of perceptrons as a network
Page 2 of the handout starts from what the course has already covered. A perceptron multiplies the input by a set of weights, computes a score, and takes its sign. Take perceptrons and apply the linear aggregation of Techniques Lecture 7, giving each a vote of :
The slide draws this formula as a picture. The leftmost column holds the inputs ; every input connects to each of the circles in the middle column, with on the connections; each middle circle then connects to on the far right through a line labelled . Inside every circle is a staircase-shaped function, meaning that the sign is taken once the score is computed. In the lecture, Lin calls the circles nodes.
The handout points out that the picture has two layers of weights ( and ) and two layers of sign functions (in and in ). The question this section answers is written at the bottom of the page: what boundary can implement?
Logic operations with the second layer of weights
Page 3 looks at two perceptrons first. and are each a line in the plane, with on one side (shaded blue in the figure) and on the other (shaded red). Treat as TRUE and as FALSE. The target is : only the region where both are TRUE is blue.
The construction adds a that always outputs and sets the three votes to , , :
List all four cases:
| TRUE | ||||
| FALSE | ||||
| FALSE | ||||
| FALSE |
In the lecture, Lin describes it as two votes minus one: when both are , is still left over; as soon as one of them is , is at most 0, and subtracting 1 makes it negative. So . Lin then points out that the boundary of is now a bent line pieced together from two straight lines, no longer a single straight line.
The handout says OR and NOT can be similarly implemented. The lecture adds how to do NOT: multiply the output by to flip it, that is, . OR is left to this section's Fun Time.
Powerfulness: with enough perceptrons, a smooth boundary can be approximated
The figure in the upper half of page 4 has three panels. The rightmost is the target: a circle, inside and outside. A single perceptron cannot do it. The two panels on the left use 8 and 16 perceptrons; each perceptron is a straight line, and together they enclose a polygon with inside. The shape enclosed by 16 lines is closer to the circle than the one enclosed by 8. In the lecture, Lin says the perceptrons are combined in the same way as the AND above.
As an addition in this article, here is how the combination is written. The AND of perceptrons is
When all are , the bracket is ; when one is , the sum drops to and the bracket is ; the more s there are, the more negative it gets. With it is the formula above. As for how close "closer" is, the following calculation is made up for this article and is not on the slides. Suppose the target is a circle of radius 1 (area ) and it is enclosed by a circumscribed regular -gon, whose area is . For that is 3.3137, 5.5% more than the circle; for it is 3.1826, only 1.3% more.
The handout makes two remarks on this figure. First, such a can implement every convex polygon, which is the convex set hypothesis set from Foundations Lecture 5; its VC dimension is infinite. Second, enough perceptrons give roughly a smooth boundary. In the lecture, Lin says an infinite VC dimension may be good news, since many perceptrons together are very powerful, and may be bad news, since a model that is too complex is in danger of overfitting. Lin also mentions that a similar phenomenon has appeared before: in the random forest figures of Techniques Lecture 10, a pile of irregular boundaries looks much smoother once combined.
Limitation: XOR is still not linearly separable in the new space
The lower half of the same page is about what cannot be done. is "TRUE exactly when one of the two is TRUE". In the figure, two lines cut the plane into four pieces; two opposite pieces are blue and the other two opposite pieces are red. In the lecture, Lin describes the blue as gathered on the two sides of a hyperbola, with the red on the other two sides.
The handout puts it this way: view as a feature transform, and XOR is not linear separable in the transformed space. The second-layer only draws a line in that space, so no setting of it works. The lecture says only that some derivation shows it cannot be done, and that the derivation resembles what Foundations did when discussing how complex the perceptron itself is; the derivation is not carried out. This article fills it in.
After the transform there are only four points. XOR requires and to output , and and to output . Suppose some makes do this. Each of the four points gives one inequality:
Adding the two inequalities in the first row gives ; adding the two in the second row gives . These contradict each other, so no such exists. This is where the resemblance mentioned in the lecture lies: Foundations Lecture 5 counted that for four points in the plane arranged as a quadrilateral, a line cannot produce every labelling, and the one it misses is the labelling where diagonal points share a sign and adjacent points differ. XOR asks for exactly that labelling, only now it happens in the plane.
One more layer of transformation gives the multi-layer perceptron
The data are still not separable in the transformed space. What did the course do in that situation before? The answer in the lecture is to transform once more, into a space that suits the problem better, where a line may be able to separate the data. Page 5 writes out the layer that is needed:
Do two ANDs first, then take the OR of the two results. From left to right, the network on the slide has the inputs, and , two AND nodes, one OR node, and the output . The line from to the first AND and the line from to the second AND are each labelled , meaning that is flipped first.
The handout does not write the weight on every line; the weights below are filled in by this article following the constructions for AND and OR. Let , , and :
The last column is exactly XOR. Put it in a concrete setting (made up for this article): take and , so the two lines are the two coordinate axes. The point has , and the table gives ; the point has , and . This network outputs in the second and fourth quadrants and in the first and third, a boundary that no single straight line can draw.
Why does one more layer work? In the space the four input cases land on only three points: appears twice (the two cases that should output are folded onto each other), and the others are and . One line separates these three points, and OR is that line.
The handout calls this model multi-layer perceptrons, the basic neural network, and lines up three stages in a row: perceptron (simple), aggregation of perceptrons (powerful), multi-layer perceptrons (more powerful).
The connection to biological neurons is only inspiration
Page 6 has four pictures: a micrograph of neurons, the network diagram from before, a seagull, and an airliner. The handout's conclusion is that the neural network is a bio-inspired model.
The correspondence given in the lecture goes like this: each circle in the network diagram corresponds to a neuron, the connections between circles correspond to the links between neurons, and the size of a weight stands for the strength of the link. But neurons in a living body are not necessarily connected layer by layer, and there may be many more cross connections among them, so the diagram is a simplified approximation.
The bird and the airplane illustrate how far the "imitation" goes. People saw birds fly and built airplanes; the airplane's shape does borrow from the bird's, but its wings do not flap the way a bird's do, and it gets into the air by other means. Lin stresses in the lecture that the neural network is the same: it is inspired by biology, but from an engineering point of view we are working with a mathematical model, and it does not have to behave exactly like the neural network in a living body.
Fun Time: which weights give OR
Question (page 7): Let . Which of the following allows to implement ?
Answer: 3. The handout says only that you can easily verify with all four possibilities of . All four choices have ; they differ only in . Over the four cases is ; add and take the sign:
| Function implemented | |||||
|---|---|---|---|---|---|
| always FALSE | |||||
| AND | |||||
| OR | |||||
| always TRUE |
OR outputs FALSE only when both are FALSE, which is the row with . Choice 2 is the AND from before. The role of is to decide "how many votes it takes to pass": requires both votes, and is satisfied with one.
What a neural network hypothesis looks like: a linear model at the output layer, a tanh transformation at every layer in between
(Video 00:01–02:19 covers the output layer, 02:19–08:04 the transformation function in the middle, 08:04–14:45 the notation, 14:45–17:18 the physical interpretation of each layer, and Fun Time follows.)
The output layer can be whichever linear model you like
Read the network from left to right: the input is transformed once, then once more, and the last node multiplies the transformed result by weights to compute a score. Page 8 writes this score as
So the output node is simply a linear model, applied to features that have been transformed several times. The handout says that every linear model from Foundations can be used here, and puts three side by side:
| Output layer | Output | Error |
|---|---|---|
| linear classification | 0/1 | |
| linear regression | squared | |
| logistic regression | cross-entropy |
The handout picks the middle one: from here on it discusses regression with squared error, and the output layer passes the score straight out. In the lecture, Lin says this is for ease of explanation, and that the classification and soft-classification versions can be worked out along the same lines.
Which transformation function the middle nodes should use
Page 9 gives a name to the staircase drawn inside the middle nodes: the transformation function, which turns the incoming score into the node's output. A staircase means every middle node is a perceptron. Can it be replaced by something else? The handout first rules out two choices:
- A linear function. If every node is a linear operation, the whole network is still a linear operation, which the handout calls less useful. The lecture asks more bluntly: then why use so many nodes, when a single linear model would do.
- A staircase function (sign). The output is only , which is discrete, and the handout says that makes it hard to optimize for . The lecture takes pocket from Foundations Lecture 2 as an example: optimizing linear classification is hard to begin with, and only rather heuristic methods are available.
The popular choice is the hyperbolic tangent :
The slide plots three curves together: sign is a staircase, linear is a slanted straight line, and is an S shape between them. As the lecture describes it, is close to the staircase function when is far from 0 and close to the linear function when is near 0. The handout lists three reasons for choosing it:
- It is an 'analog' approximation of sign and is easier to optimize. In the lecture, Lin says sign is like the digital version and is its analog version; the formula is made entirely of exponential functions, so it is smooth and differentiable.
- It is somewhat closer to the biological neuron. The lecture explains that what goes on in a living body is by and large continuous, not discrete.
- It is not that new. This refers to the identity below.
is the logistic function from Foundations Lecture 10. The handout writes only the result; the check goes as follows:
The last step multiplies the numerator and the denominator by . So is the logistic function with its input scaled up by two and its output stretched and shifted from to . Two of its properties are used later: , and its output always lies between and .
The handout says the rest of the lecture uses as the transformation function. The lecture adds that using another function is your choice, but the derivation and its meaning change with it.
Notation: layers, scores, and outputs
Page 10 fixes the notation. In the network diagram the three groups of weights are labelled , , and , and next to the third node of the second layer the diagram marks (before ) and (after ).
| Symbol | Meaning |
|---|---|
| The layer index, . The input counts as layer 0, and is the output layer | |
| The number of nodes in layer (not counting the constant node). | |
| The weight from node of layer to node of layer , with and | |
| The score of node in layer , the value before it enters the transformation function | |
| The output of node in layer , which is also an input to the next layer |
starts from 0 because every layer has one extra constant node , which the lecture compares to the bias term from before. starts from 1 because nobody has to compute the constant node. The two formulas are
The lecture explains one choice of notation: by the earlier convention the transformed quantity should be called , but here it is still called so that integer layer indices can tell the layers apart.
The shape of a network is named by the number of nodes in each layer, written as a ---- NNet. Once the shape is fixed, every set of weights is a hypothesis, and all possible weights together form the hypothesis set. The prediction procedure is at the bottom of the page: apply as the input layer , go through the hidden layers to get layer by layer, and predict at the output layer with .
The total number of weights follows directly from the shape. Layer has nodes, and each node takes the outputs of the previous layer (the extra 1 is the constant node), so the total is . This section's Fun Time uses it.
Numerical example: the forward computation of a 1-2-2-1 network
The following example is made up for this article and is not on the slides; all the numbers were computed separately in Python. The same network is used again when backpropagation comes up. The shape is 1-2-2-1: one input, two hidden layers of two nodes each, and one output, for weights in total:
| Layer | Into node 1 | Into node 2 |
|---|---|---|
| , | , | |
| , , | , , | |
| , , | (only one output node) |
The input is , so , where the first 1 is the constant node.
Layer 1. and . Taking : and .
Layer 2. and . Taking : and .
Layer 3 (the output layer). . The output layer takes no , so the prediction is .
If the label of this example is , the squared error is . The network predicts badly at this point, and what learning has to do is adjust those 13 weights to make the error smaller.
What each layer does: checking whether the input matches the weights
Page 11 gives these computations an interpretation. Each layer is a transformation, and the transformation is to be learned from data:
Every node takes the inner product of the previous layer's output vector with its own weight vector and then applies . The lecture explains: the closer two vectors are to parallel, the larger the inner product and the closer the output is to ; so each node checks whether the handed to it by the previous layer fits its own weight vector, and a fit amounts to confirming some pattern. The handout's wording is whether 'matches' weight vectors in pattern.
Here is a set of numbers from this article. Some node has weights . The input points the same way as the weights; the score is and . The input points the opposite way; the score is and the output is . The input is perpendicular to the weights; the score is 0 and the output is 0. For three inputs of the same length, the node's response depends only on whether the direction fits.
The handout sums it up: NNet is pattern extraction with layers of connection weights. The lecture supplies the second half: the patterns hidden in the data are transformed out layer by layer, until the last layer has to make the decision. This is also what the name Extraction Models on the Roadmap means.
Fun Time: how many weights a 3-5-1 network has
Question (page 12): How many weights are there in a 3-5-1 NNet?
- 9
- 15
- 20
- 26
Answer: 4. The handout's solution is that there are weights in layer 1 and in layer 2, 26 in total. The "plus 1" in each layer is the weights coming out of the constant node.
Each of the other choices leaves something out: 9 adds up the node counts of the three layers () and so counts nodes; 15 is , layer 1 only and without the constant node; 20 is layer 1 only, or both layers with the constant node left out of each ().
How the weights are learned: backpropagation computes the gradient of every weight
(Video 00:00–03:43 sets the goal, 03:43–11:18 derives the gradients for the output layer and for a general layer, 11:18–16:45 derives the recursion for δ, 16:45–21:19 covers the backpropagation algorithm and mini-batch, and Fun Time follows.)
The goal: the partial derivative of the error with respect to every weight
The goal on page 13 is to learn all the so that is minimized. The handout first separates two cases:
- One hidden layer. This is simply aggregation of perceptrons, and the handout says gradient boosting can determine the hidden neurons one by one. As the lecture describes it, one neuron is added at a time until the result seems good enough; it also notes that the perceptrons here are the soft version that uses , and the idea is similar.
- Multiple hidden layers. The handout writes only "not easy"; the lecture says the add-one-at-a-time approach above may no longer work.
Take a different route. Define the error on a single example
Once can be computed, the gradient descent or stochastic gradient descent (SGD) of Foundations Lectures 10 and 11 applies. The lecture draws the distinction: gradient descent adds up the changes over all the data points, while SGD picks one point at random each time and looks only at that point. So only one key question remains: how to compute this partial derivative efficiently. Lin first states the difficulty on the students' behalf: the weight carries an , a , and an , and it is hard to keep them straight at first. So start with the easy part, from the last layer.
The gradient at the output layer
Page 14 looks at the output layer first (). The output layer has only one neuron and applies no transformation, so the network's output is its score :
The path from the weight to the error is a chain: determines , and determines . In the lecture, Lin says that when a derivative is hard to compute, find a go-between and use the chain rule. Here the go-between is :
Compute the two factors separately. The first is the derivative of with respect to : the 2 from the square comes down, the coefficient of inside the bracket is , and the result is . For the second, is a sum of terms , only one of which contains the we want, and differentiating leaves its coefficient . So
The gradient at a general layer: give the hard factor the name δ
The right half of the same page is the general layer (, , ). In the same way, choose as the go-between the score that feeds directly:
The second factor is as easy as at the output layer: , and its derivative with respect to is . The first factor is the partial derivative of the error with respect to a score in some middle layer; several layers lie in between, and it cannot be written down directly. The lecture sets it aside for now and gives it a name:
The gradient at every layer then has the same form:
The output layer is a special case of it, and the there has already been computed:
The handout asks at the bottom of the page: how about others?
δ can be computed backwards from the next layer
Page 15 first draws the path by which affects :
- goes through and becomes .
- is multiplied by and enters the score of every neuron in the next layer, .
- Those scores travel on through the rest of the network and finally reach .
In the lecture, Lin says there are more go-betweens this time: besides the single there is the whole row of scores in the next layer, and each one is a go-between. A change in travels to along each path separately, and the multivariate chain rule adds up the contributions of all the paths:
What each of the three factors is:
- is, by definition, the next layer's . The lecture stresses that this factor needs no computing, since it was defined a moment ago.
- : is a linear combination of the s, and its derivative with respect to is the coefficient .
- : , so this is .
Substitute them in. The last factor does not depend on and can be pulled outside the sum:
This is the sentence at the bottom of the handout page: can be computed backwards from . What is known is in the last layer; from it comes every in layer , then layer , all the way down to layer 1. Here starts from 1: the constant node has no score, and so no .
The handout does not write what is; the lecture says the formula has been given and you can derive it yourself. This article derives it once. Let and , so that and , and use the quotient rule:
So , which can be computed from the left behind by the forward pass, with no need to evaluate exponential functions again.
The backpropagation algorithm
The gradient needs two things: and . comes from feeding the data in and computing forward layer by layer; comes from computing backwards layer by layer from the last one. Page 16 assembles them into an algorithm named backpropagation, backprop for short, and the lecture says the name describes exactly the fact that can be computed backwards:
Initialize all weights , then repeat for :
- Stochastic: randomly pick from .
- Forward: compute all with .
- Backward: compute all subject to the same .
- Gradient descent: .
Finally return .
The handout adds a practical variation: sometimes steps 1 to 3 are done many times (in parallel) and step 4 updates with the average of these , which is called mini-batch. The lecture explains the motivation: looking at a single point requires a complete forward and backward pass before moving one small step, and the direction given by a single point is not very stable. So instead a batch is drawn at random each time, 20 points in the lecture's example; each point computes its own and , and the update uses the averaged gradient. It sits between gradient descent on all the data and SGD on a single point, and Lin says it usually works better in practice.
The handout's summary of this page is: basic NNet algorithm: backprop to compute the gradient efficiently. The handout does not spell out where the efficiency lies, so this article adds a rough count. In the backward step, each is multiplied only once in the recursion for ; assembling the gradient takes one more multiplication per weight. So one forward pass plus one backward pass costs an amount proportional to the number of weights and yields all partial derivatives. With numerical differentiation instead, every weight has to be nudged a little and the forward pass rerun for each, forward passes in all, a cost proportional to .
The lecture closes by saying that replacing the output layer with classification or logistic regression, or replacing with another function, changes the algorithm somewhat, but the changes are minor; as long as the gradient can be computed this way, gradient descent, SGD, and mini-batch all apply.
Numerical example: finishing the backward computation on the same network
Now continue with the network from "Numerical example: the forward computation of a 1-2-2-1 network" (made up for this article), with the data and . The forward pass has already given:
| Layer | ||||
|---|---|---|---|---|
| 0 | ||||
| 1 | ||||
| 2 | ||||
| 3 |
Layer 3. .
Layer 2. The next layer has only one neuron, so the sum has just the one term .
Layer 1. The next layer has two neurons, so the sum has two terms; this is where the in the recursion really comes into play.
Assembling the gradient and updating. The gradient of each weight is . Take and do one update step:
| Weight | Current value | Gradient | After the update | ||
|---|---|---|---|---|---|
The numbers in the table and in the formulas above were all computed from unrounded values and then rounded to four decimal places; working by hand from the printed four-decimal values may differ by 1 or 2 in the last digit (for example, comes to by hand, while the unrounded values give ).
Checking. Whether the gradient is right can be checked by numerical differentiation: add and subtract on one weight, run the forward pass once for each, subtract the two values, and divide by . The values obtained this way for the 13 weights agree with the table to the sixth decimal place; for example, both methods give for .
What this result means. Run the forward pass again with the 13 updated weights: the output moves from to , toward , and falls from to . The sign of the gradient tells each weight which way to move: the gradient of is positive, so it is decreased; the gradient of is negative, so it is increased.
The step size cannot be chosen carelessly. With the same gradients and , the output becomes and ; with , the output shoots to and rises to , worse than before the update. The gradient only says which direction goes downhill near the current position, and too large a step overshoots the bottom of the valley, the same conclusion as when Foundations Lecture 10 discussed .
Fun Time: when the output-layer gradient is 0
Question (page 17): According to , when would ?
- all of the above
Answer: 4. The gradient is a product of two factors, and it is 0 as soon as either one is 0. Choice 1 makes the first factor 0: the network's output on this example equals exactly, there is no error, and nothing needs adjusting. Choice 2 makes the second factor 0: the value coming in along this connection is 0, so the score is the same whatever this weight is changed to. Choice 3 is the one the handout's solution singles out: , and is 0 only at 0, so and are the same thing (if and only if). In the lecture, Lin says choices 1 and 2 should be easy and choice 3 is the one to think about; when the gradient is 0, the SGD update leaves this weight where it is.
This article adds two points. First, choices 2 and 3 are about the nodes with ; the constant node with is never 0 and has no score. Second, a gradient of 0 only means that this weight does not move on this step, not that the network has finished learning: the error can still be large when choice 2 or 3 holds. The discussion of initial values below shows that setting all the weights to 0 runs straight into this situation.
Learning that both moves and generalizes: initial values, the VC dimension, regularization, and early stopping
(Video 00:01–05:12 covers optimization and initial values, 05:12–06:56 the VC dimension, 06:56–11:08 regularizers, 11:08–15:16 early stopping, followed by Fun Time and the summary of the whole lecture.)
The error function is not a single valley
Page 18 writes out in full what is being minimized:
The handout's assessment is that this function is generally non-convex when there are multiple hidden layers. In the lecture, Lin says the point is not which error is used but the many layers of in between. The earlier was like one valley, where a ball rolls down to the lowest point; now there may be peaks stretching to the horizon with many valleys, the ball only rolls into the one nearest to it, and even inside a valley there is no telling whether it is the lowest one globally. The handout lists two consequences:
- It is not easy to reach the global minimum. Gradient descent or SGD with backprop only gives a local minimum.
- Different initial weights lead to different local minima, so the result is somewhat 'sensitive' to the initial weights.
How should the initial values be chosen, then? The lecture is frank about it: nobody knows, because we do not know where the good valleys are. But there are places to avoid. The handout writes: large weights ⟹ saturate (small gradient). The lecture explains: with large weights the score is large (or very negative) and falls in the flat regions at the two ends of ; this is called saturation, and Lin compares it to an amplifier already turned all the way up, in the region where little gain is left. Where the curve is flat the derivative is small, the gradient is small, and each step covers only a little ground, like being stuck on a plain and unable to move.
How severe saturation is can be read directly from (computed by this article):
The recursion for multiplies by once for every layer it goes back. A neuron whose score is 5 shrinks the passed back to roughly two ten-thousandths of its size, and the gradients of the weights in front of it shrink along with it.
The handout's advice is: try some random & small ones. The lecture gives a reason for each word: small, so that training does not start out stuck on the plain; random, so that each run has a chance of reaching a different valley, and some valleys are bound to be better than others.
This article adds one more reason for "random", which can be verified on the 1-2-2-1 network from before. If all 13 weights are set to 0.5, the two neurons in the same layer compute exactly the same thing: , , . Their gradients are the same too, so after the update the two neurons are still the same, and so on at every later step. The two neurons do the same job forever, which amounts to having only one. If everything is set to 0, things are worse: , so every hidden neuron outputs 0; the output-layer weights attached to them have gradient 0 (this is exactly choice 3 of "Fun Time: when the output-layer gradient is 0"), and the output-layer weights being 0 in turn makes every in the earlier layers 0. The only weight with a nonzero gradient is . This article ran it for 200 steps: the other 12 weights never moved, and the network learned only a constant. Problem 9 of Fall 2024 HW7, listed under "Exercises and corresponding reading", asks for a proof of a phenomenon of the same kind as the first one (the network in that problem has only one hidden layer, and its output also takes ).
The handout's conclusion for this page is that NNet is difficult to optimize, but practically works. As Lin puts it in the lecture, although its optimization properties are not good, it does not perform too badly if you do carefully what ought to be done.
The VC dimension: power grows with the number of neurons and the number of weights
Page 19 gives a result without proof: with -like transfer functions, roughly
is the number of neurons and is the number of weights. In the lecture, Lin says the detailed proof is skipped and only the rough result is given, and points out that more neurons bring more connections, so when is large is large too. Take the 3-5-1 network from Fun Time: ; counting 5 hidden neurons plus 1 output neuron, and . hides a constant, so this number is only an order of magnitude; this article uses it to get a feel for one thing: doubling the width of the hidden layer roughly doubles both and and quadruples their product.
The handout splits the meaning into two sides. The pros: with enough neurons ( large) the network can approximate 'anything'. The cons: with too many neurons it can overfit. The lecture puts it vividly: with too many nodes and too many weights, the model has enough power to fixate on tiny details and does well only on the data it has seen. The handout's reminder is to watch out for overfitting.
Regularization: making some of the weights 0
Page 20 deals with overfitting. In the lecture, Lin speaks of stepping on the brakes, and one way of doing that is regularization. The basic choice is the old friend from Foundations Lecture 14, the weight-decay (L2) regularizer:
The handout describes its effect as shrinking the weights: a large weight gets a large shrink and a small weight a small shrink. The numbers in the lecture are a 10 shrinking to 8 and a 5 shrinking to 4, roughly a proportional shrink. The gradient shows why: the derivative of with respect to is , so the push back is proportional to the weight itself. The trouble is that a proportional shrink makes large and small weights all a bit smaller, yet never brings them to 0.
We would like some weights to be exactly 0 (sparse). The lecture gives two reasons: computationally, a weight that is 0 needs no computing; in terms of complexity, contains the number of weights, and a weight becoming 0 amounts to a smaller . The handout's wording is to effectively decrease .
How can that be achieved? The handout lists two choices:
- The L1 regularizer . It does push weights to 0, but the absolute value function is not differentiable at 0. The lecture explains why that is a nuisance: backprop is at heart a method built on differentiation, and bringing in a non-differentiable function breaks the framework.
- The weight-elimination regularizer, which the handout calls a 'scaled' L2:
The handout's description of it is: large weight → median shrink; small weight → median shrink. The inference drawn in the lecture is that a weight that was small to begin with, once shrunk by a medium amount, disappears; the construction divides each L2 term by as a scaling factor. Lin says the details are not covered here and asks everyone to think about why.
This article works it out. This section's Fun Time finds that the partial derivative of a single term with respect to is . Side by side with the of L2:
| L2 gradient | weight elimination gradient | ||
|---|---|---|---|
Three things can be read off the table. First, when is small, and weight elimination is nearly the same as L2, so small weights are still pushed toward 0. Second, the push of L2 on large weights keeps growing with , while the push of weight elimination is bounded: differentiating once more shows that it is largest at , with a maximum of about 0.65. Third, past that point the push gets smaller, and at only 0.002 is left.
Put the handout's words next to this table. The handout writes that large weights and small weights both get a median shrink. What the formula gives is this: the push on small weights is comparable to that of L2, and the push on large weights does not grow with the weight and tends to 0 for very large weights. Neither the handout nor the lecture spells out how these two descriptions fit together. The effect the formula produces is consistent with the purpose of this page: compared with L2, large weights stay almost untouched, and small weights are still pushed toward 0. The table above and the reading in this paragraph are computed by this article from the formula and are not content of the handout.
There is one more reading (an addition in this article): the second column of the table shows that every term lies between 0 and 1, close to 0 for small weights and close to 1 for large ones. Summed up, the whole regularizer roughly counts "how many weights are large enough", and pressing it down reduces the number of weights that matter, which lines up with the in .
Early stopping: taking fewer steps is also a kind of regularization
Page 21 introduces another mechanism, one the lecture says has been used very widely in the history of neural networks. The starting point is how gradient descent moves: taking one small step from a point can be seen as choosing among the weights in a small circle around the current position; at the new point, it chooses again within a new small circle. The figure on the left of the slide draws four points , each with a circle around it, and labels the region covered by the circles . The handout says: GD/SGD (backprop) visits more weight combinations as increases.
Fewer steps mean fewer hypotheses considered. The lecture speaks of an "effective VC dimension": measure complexity by the weights actually visited, and the longer the optimization runs, the larger the effective VC dimension. The handout writes it as smaller effectively decrease .
The figure at the upper right is the one from Foundations Lecture 7: the horizontal axis is , the in-sample error keeps falling, the model complexity keeps rising, the out-of-sample error falls and then rises, and the best is in the middle. Since the number of steps controls the effective , replacing the horizontal axis with the step count should show the same shape. The figure at the lower right is one actual result of that kind: the horizontal axis is the iteration (on a log scale, from to ) and the vertical axis is ; keeps falling with , falls and then rises, and the lowest point lies between and .
So the better approach is to stop in the middle, which is called early stopping. The example in the lecture: the plan is to run 20,000 steps, and at 200 steps it seems good enough, so you stop. Lin also says that just about every gradient-based algorithm can adopt this mechanism for some form of regularization.
Which step to stop at? The handout's answer is validation, and the lecture says to treat "when to stop" as a parameter to be chosen, using the validation of Foundations Lecture 15. The handout does not write out the procedure. The three steps below are not on the slides; they are one common practice put together by this article, built on the idea from Foundations Lecture 15 of holding out part of the data as a validation set:
- Before training, hold out part of the data as a validation set that is not used for computing gradients.
- Run backprop on the remaining data; every fixed number of steps, compute the error on the validation set, , and save the weights at that moment.
- After the planned number of steps, go back and take the weights saved at the lowest as the result.
cannot be used for this decision: in the lower-right figure of the handout it goes down all the way with the step count, and choosing by it would only tell you to run to the last step.
Fun Time: the partial derivative of weight elimination
Question (page 22): For the weight elimination regularizer , what is ?
Answer: 2. The handout's solution is a single joke: Too much calculus in this class, huh? The derivation is as follows. Only one term of the sum contains this weight; abbreviate it as and use the quotient rule, where the derivative of the numerator is and the derivative of the denominator is also :
The four choices differ only in the power of the denominator. In the quotient rule the denominator is always the square of the original denominator, so the power is 2. Choice 1 forgets the term subtracted in the numerator; choices 3 and 4 correspond to no calculation at all. This article checked with numerical differentiation: at the formula gives and numerical differentiation also gives ; at both give .
The lecture explains the point of this question: to put weight elimination into backprop, you need its partial derivative with respect to every weight. This article adds a sentence on how to use it: at update time, multiply this term by the regularization strength and add it to the original gradient .
The summary on page 23 reduces the four sections to one line each: multi-layer for power with biological inspirations; layered pattern extraction until linear hypothesis; backprop to compute gradient efficiently; tricks on initialization, regularizer, early stopping. The preview of the next lecture is making neural network 'deeper'.
Self-check
1. How many weights does a 2-4-3-1 network have? For one backprop pass on a single example, how many δ values have to be computed?
Answer: weights. There is one for every neuron that has a score, and none for the constant nodes, for in total.
2. Let . Write a set of that makes implement NAND (FALSE when both are TRUE, TRUE in every other case), and verify it with a table.
Answer: flip the sign of all three AND weights, . Over the four cases is (both ), , , , and taking the sign gives . This also shows that a NOT placed after an AND needs no extra layer: just flips the sign of the whole set of weights.
3. A 1-1-1 network: , , , . The data are and . Compute the output, , the two δ values, and the gradients of the four weights.
Answer: forward, and ; , which is the output; . Backward, ; . Gradients: for it is ; for it is ; for it is ; for it is . All four gradients are positive, so all four weights should be decreased, which agrees with "the output 2.02 is larger than the target 1". (This article checked these four values with numerical differentiation.)
4. One person sets all the initial weights to 10, and another sets all the initial weights to 0.01. What problem does each run into?
What a correct answer needs: for the first person, it has to mention that the scores are large, saturates, and is close to 0, so the gradient is tiny and training hardly moves. The second person's weights are small, so there is no saturation, but all the weights are identical: the neurons in the same layer compute the same outputs and the same and are still identical after the update, which amounts to one neuron per layer; the weights should be small and random. Answering only "it gets stuck in a local minimum" for both does not count as correct.
5. For the two weights and , what gradients do L2 and weight elimination give them? Which regularizer is more inclined to keep the large one and eliminate the small one?
Answer: L2 gives , which is and ; the push on the large weight is 30 times the push on the small one. Weight elimination gives , which is and ; the small weight gets the larger push. So weight elimination is the one more inclined to keep large weights and eliminate small ones.
Exercises and corresponding reading
The corresponding weeks in two semesters.
| Edition | Week | Slides | LFD (as marked on the course page) |
|---|---|---|---|
| MOOC | Techniques Lecture 12 | 212 | not marked |
| Fall 2024 | W13 (11/25), the same week as Techniques Lecture 13 | 212u | e-7.1, e-7.2, e-7.3, e-7.4 (selected parts) |
| Fall 2026 | W15 (12/16), scheduled after the final exam in W14 | 212u was still a 404 on 2026-09-30 and has not been released | same as above |
Textbook. e-Chapter 7 of Learning from Data (LFD) is an online chapter of the textbook and is not in the printed book; see amlbook.com for how to get it.
Homework. Fall 2024 HW7 (released 2024-12-02, due 12-16) has three problems directly related to this lecture, with no official solutions:
| Problem | Type | What it practises |
|---|---|---|
| Q4 | auto-graded | With 20 input units, 3 output units, and 50 hidden units (the units count too), where the hidden units can be arranged into layers freely and adjacent layers are fully connected, what is the maximum number of weights |
| Q9 | human-graded | With one hidden layer, all neurons (the output included) using , and all initial weights set to 0.5, prove that after backprop (mini-batch GD) the first-layer weights satisfy |
| Q13 (bonus) | human-graded | The problem attaches an answer from chatGPT claiming that a --1 feed-forward network with sign as the transformation function can implement the -dimensional XOR; you are asked to point out where it departs from the conclusion of the Fall 2023 bonus homework and to prove mathematically that this is impossible |
Q4 is a scaled-up version of the Fun Time "how many weights a 3-5-1 network has", and you can first write a small program that enumerates every way of layering to check your answer. Q9 should be thought about together with the advice to try some random and small initial values. The symmetry this article observed on the 1-2-2-1 network is a phenomenon of the same kind, but the network's shape differs from that problem's (which has only one hidden layer, with an output that also takes ), and it is only one set of numbers, not a proof. Q13 asks you to judge for yourself which step of that argument goes wrong, and the proof in this article's "Limitation: XOR is still not linearly separable in the new space" is a place to start. The guide to the whole assignment is in Techniques Homework and Final Project.
Hands-on. Derive the recursion for δ yourself once, then write a -3-1 network in numpy and compare the gradient from backprop against numerical differentiation. The 1-2-2-1 example in this article can serve as test data to check your answers against.
What came after this lecture. The recording of this lecture was uploaded in 2016 and uses and basic SGD. In Fall 2024 Lin added two decks of modern deep learning slides, 302u and 303u, which this series places in the article for Techniques Lecture 16.
Further reading. The following articles on this site overlap with this lecture and offer another way of explaining the same material:
- Reading CMU 11-785 Deep Learning, especially Lecture 5: Backpropagation: a whole course devoted to deep learning.
- Reading Stanford CS230: leans toward the practice of deep learning projects.
- Reading MIT 6.7960: graduate-level deep learning.
- Reading NTU Hung-yi Lee Machine Learning 2026 Spring: the parallel track at NTU, leaning toward deep learning and generative AI.
- CMU 10-301 HW5: Neural Networks: an assignment on the same topic from another school.
Next lecture. Techniques Lecture 13: Deep Learning stacks this lecture's network deeper and deals with how much harder initial values and regularization become once there are more layers.
Course video sources
Original videos: Motivation, Neural Network Hypothesis, Neural Network Learning, Optimization and Regularization
Checked: 2026-10-13. The video IDs, titles, and lengths were taken from the official playlist that day; the videos have no caption track on YouTube, and what this article reports from the lecture comes from transcripts made for it.
Update Log
- 2026-10-10: Marked the video status and checked the recording sources and how to access them.
- 2026-10-10: Rechecked the video status. The embedded videos match the lectures in the official playlist.
- 2026-10-13: Previously combined with Lecture 13 in one article; changed to one article per lecture and rewritten from the handout and the video transcripts.
References
- Techniques Lecture 12 handout, 212_handout.pdf: the main source for this article. The derivations in the four sections, the four Fun Time questions, and the figures on each page all come from this handout.
- Machine Learning Techniques YouTube playlist: the IDs and titles of the four videos; what the text reports as said "in the lecture" is paraphrased from machine transcripts of these four videos.
- Hsuan-Tien Lin > MOOCs: the official entry for handouts and recordings.
- Machine Learning, Fall 2024 course page: the W13 schedule, the 212u slides, and the LFD e-7 chapter marking.
- Machine Learning, Fall 2026 course page: the W14 final exam and the W15 schedule.
- Fall 2024 Homework 7 (hw7.pdf): the content of Q4, Q9, and Q13.
- Learning from Data textbook website: the source of e-Chapter 7.
- Reading NTU Hsuan-Tien Lin Machine Learning Foundations & Techniques: Course Overview: where this lecture sits in the whole course and the reading routes.