10/05/26: Lect-06 (cont.)
Overview
Joseph Zambreno explains why XOR exposes the limits of linear models and how a multilayer perceptron (MLP) uses hidden units and nonlinear activations to learn nonlinear decision boundaries and features. He connects MLP forward propagation to computational graphs and PyTorch autograd, then shows why deep networks face vanishing or exploding gradients depending on activation functions and weight initialization.
Key takeaways
- A linear classifier cannot represent XOR because one hyperplane cannot separate its classes; a ReLU MLP can construct XOR with two hidden units using h1 = ReLU(x1 + x2), h2 = ReLU(x1 + x2 - 1), and output h1 - 2h2.
- Training and validation losses can match without indicating a good model: the XOR logistic regression example stays near 0.7 binary cross-entropy, close to chance-level performance.
- Hidden layers act as learned feature transformations, allowing an MLP to discover relationships between inputs instead of relying entirely on manually engineered features.
- PyTorch autograd records forward operations in a computational graph, then uses the chain rule during backward() to accumulate gradients for parameters.
- Deep networks can suffer vanishing or exploding gradients because backpropagation repeatedly multiplies activation derivatives and weight-related Jacobians.
- Sigmoid’s maximum derivative is about 0.25, while ReLU has derivative 1 on positive inputs; weight initialization also matters, since repeated products can grow with standard-deviation-1 weights or shrink with very small weights.
Chapters
- Linear models assume a linear relationship between input features, while XOR requires a nonlinear relationship.
- A single linear layer creates one hyperplane and cannot separate the XOR classes.
- The lecture frames hidden layers and nonlinear activations as a way to learn relationships that manual feature engineering could otherwise add.
- The logistic regression example produces training and validation curves that track one another, but both remain near a binary cross-entropy loss of 0.7.
- A loss around 0.7 is close to the roughly 0.693 loss of a chance-level binary prediction, so matching curves do not mean the model learned XOR.
- The synthetic dataset’s nonlinear pattern explains why a linear classifier remains near guessing despite no obvious train-validation divergence.
- A one-hidden-layer MLP adds a width hyperparameter; Zambreno uses 8 hidden units in the example.
- A sum-of-products representation suggests 2 hidden units can represent XOR in theory, but stochastic gradient descent may not find that solution reliably.
- The TensorFlow Playground example illustrates the gap between a representation being possible and training being able to discover it.
- The first hidden unit computes ReLU(x1 + x2), which is positive when either binary input is 1.
- The second computes ReLU(x1 + x2 - 1), which activates only when both inputs are 1.
- The output h1 - 2h2 cancels the both-inputs-are-1 case and produces the XOR truth table.
- The two hidden units behave like OR and AND detectors, and the output combines them to form XOR.
- Each hidden unit applies an affine transformation and ReLU, dividing the input plane with a line or hyperplane.
- The example’s hand-designed weights demonstrate a valid construction; optimization is intended to learn useful representations from data.
- An MLP composes linear transformations and activation functions, feeding each layer’s output into the next.
- Hidden layers can be understood as learning new features, such as a useful relationship between x1 and x2 that was not manually specified.
- For classification, a framework may apply a probability transformation such as softmax at the output; regression can use the final linear output directly.
- A scratch implementation needs separate weights and biases for each layer, such as W1, B1, W2, and B2, plus a manually written forward pass.
- PyTorch Sequential composes layers into a callable model and handles the forward pass across the specified modules.
- LazyLinear infers a layer’s input dimension on the first forward pass, avoiding manual dimension bookkeeping in deeper networks.
- A forward pass calculates layer outputs and intermediate values, which are needed later for training.
- The optimization target is a loss computed from the model output and the true label, not the prediction alone.
- For a one-hidden-layer MLP, the hidden width H follows from multiplying an H-by-D weight matrix by a D-dimensional input.
- The example adds an L2 weight penalty, S = λ/2 times the squared weights, to the prediction loss L.
- The combined objective J = L + S discourages large weights, with the regularization gradient contributing λW for each weight matrix.
- Backpropagation applies the chain rule from the objective through the output layer and hidden layers to calculate gradients for W1 and W2.
- The gradient for an early-layer weight depends on the downstream loss gradient and the derivative of the activation function.
- ReLU’s derivative is 0 for negative inputs and 1 for positive inputs; implementations choose a convention at the zero point.
- A ReLU unit that remains inactive blocks gradient flow through that path, preventing those parameters from receiving updates from it.
- Backpropagation works from the loss toward earlier parameters, reusing intermediate derivatives as it applies the chain rule step by step.
- When multiple computational paths contribute to a parameter’s gradient, their contributions are added.
- The resulting gradients describe the direction of increase in loss; an optimizer such as stochastic gradient descent uses the opposite direction to reduce it.
- Zambreno simplifies the network to D = 2 inputs, H = 2 hidden units, and Q = 1 output, then uses x = [1, 2] and label y = 0.
- The forward pass computes z = W1x, applies ReLU, then computes the scalar output with W2; a negative preactivation becomes zero.
- For the example’s squared loss, the output is -2 and the loss is 2; the backward pass calculates gradients for both weight matrices.
- PyTorch builds a computational graph during the forward pass and tracks the operations and derivatives needed for backward propagation.
- Calling backward on a scalar loss triggers chain-rule calculations and stores gradients for parameters marked to require gradients.
- Retaining gradients on intermediate, non-leaf tensors is generally for debugging, such as inspecting where gradients vanish or grow.
- With many layers, backpropagation multiplies Jacobians and activation derivatives repeatedly, making gradient magnitudes sensitive to each factor.
- A derivative that is small at each layer can shrink gradients toward zero; large matrix products can instead make them explode.
- These numerical problems can emerge even when the chain-rule calculus is correct, because software uses finite-precision arithmetic.
- The sigmoid derivative peaks at about 0.25, so repeatedly multiplying by sigmoid derivatives can shrink gradients in deep networks.
- ReLU provides a derivative of 1 for positive inputs, which can preserve gradient magnitude along active paths, though its derivative is 0 for negative inputs.
- Repeated matrix products with standard deviation 1 can produce exploding values, while very small initialization such as 0.01 can drive values toward zero.
- Zambreno presents activation choice and initialization as practical design challenges and previews techniques for stabilizing them in the next lecture.
Transcript excerpt
0:00 So let's let's go ahead and get started. Welcome back. Uh last week Wednesday we kind of left off we were trying to really just motivate why we have multi-layer perceptrons in the first place as well as the value of these nonlinear activation functions. Kind of got into the weeds of let's look at this that and the other activation function and how mathematically and visually they're they're somewhat different. I don't think that itself is necessarily that important. But the important takeaway from really our last Wednesday uh get together was that uh this these linear models that we had been using up to this point in the semester, they really only make sense if the assumption you're making is that there's a linear relationship in our data that our input features have a linear relationship and the purpose of the model is to discover that linear relationship. If there's a nonlinear relationship, well, there are
0:47 some things you can do. You can do some feature feature engineering manually to kind of introduce those nonlinearities or you have to go to a more complex model in which one can attempt to learn those nonlinearities. The example we started with was the exor example which is really meant to be the simplest illustrative case. Exor doesn't seem like it but it's nonlinear. It's x1 x2 bar or x1 bar x2. The terms are multiplied together. There's a nonlinearity there to do that exor computation. There there's some interesting theory, you know, behind all that. Um it's been shown and proven that when you have multi-layer feed forward networks with these nonlinear activation functions, they've been proved to be so-called universal function approximators. Specifically for our boolean function case, there's well-developed theory that shows that even with one hidden layer, right?
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Joseph Zambreno.