Lecture 7d
Overview
The lecture derives mini-batch backpropagation for a neural network, then converts scalar gradient equations into vectorized matrix operations that GPUs can execute efficiently. It also walks through a PyTorch training workflow using tensors, DataLoader, a sigmoid network, binary cross-entropy, and AdamW, and interprets a 2,000-epoch run that reaches about 90% training accuracy despite the network's nonconvex loss.
Key takeaways
- Vectorizing mini-batch equations turns per-sample gradient calculations into matrix products and Hadamard products, allowing GPUs to process batches far more efficiently than nested loops.
- A DataLoader typically processes shuffled, non-overlapping batches within each epoch, then reshuffles the data for the next epoch.
- PyTorch autograd constructs gradients from the network's forward computation graph; the training loop explicitly clears gradients, calls loss.backward(), and updates parameters with the optimizer.
- The example uses AdamW with learning rate 0.01 and trains for 2,000 epochs, reaching roughly 90% training accuracy while its loss declines.
- A sigmoid hidden-layer network is more expressive than logistic regression, but its nonlinearities make the optimization problem nonconvex and harder to optimize.
Chapters
- Mini-batch stochastic gradient descent estimates the loss gradient from a small subset of training samples instead of computing it over the full dataset.
- Backpropagation uses a computational graph: the forward pass computes the loss, and the backward pass applies the chain rule from outputs back to parameters.
- The derivation moves from scalar variables and batch size 1 to batched and vector-valued quantities.
- The data matrix X stores mini-batch samples in rows and features in columns, so a batch of B samples has B rows.
- Stacking per-sample equations forms matrix operations; multiplying a bias vector by a row of ones repeats the bias across all B samples.
- Element-wise activation functions apply to every entry of the batched pre-activation matrix.
- Summations over shared indices become matrix-matrix or matrix-vector products, while activation derivatives use the element-wise Hadamard product.
- The resulting forward and backward passes use matrix operations rather than nested loops, enabling much faster execution on GPUs such as NVIDIA GPUs.
- The lecture distinguishes dense layers from convolutional layers and transformer attention, whose structures call for specialized computation.
- Large models or datasets may exceed one processor's memory, requiring data and gradient synchronization across multiple machines.
- Training data is typically shuffled, divided into non-overlapping mini-batches, and processed once per epoch.
- The data is reshuffled for the next epoch, making it unlikely that the same samples form the same batch again; progress is commonly tracked in epochs.
- For binary classification, binary cross-entropy produces a specific gradient with respect to the network output logits.
- For K-class classification with K greater than 2, softmax cross-entropy uses the softmax output in its gradient.
- Linear regression with a quadratic loss has a linear gradient, illustrating how the chosen loss determines the output-layer gradient.
- The lecture contrasts TensorFlow, introduced by Google in 2015, with Keras's simpler but more constrained interface and PyTorch, released in 2017.
- PyTorch is presented as easier to customize and debug, and as widely used in both research and industry.
- PyTorch tensors carry values and computational-graph information needed for automatic differentiation, unlike ordinary NumPy arrays.
- NumPy feature and label arrays are converted to PyTorch tensors, with float32 used for the example and required by its binary cross-entropy loss.
- A Dataset groups features and labels, while a DataLoader handles mini-batching, shuffling, and potentially more complex data access.
- A loop over the DataLoader yields one batch of features and targets at a time, continuing until the epoch's samples are processed.
- The example network has two linear layers with a sigmoid activation, configured by the input, hidden, and output dimensions.
- PyTorch's nn.Module defines the network's initialization and forward pass, while autograd handles its backward pass.
- The optimizer is AdamW with a learning rate of 0.01; binary cross-entropy is selected as the classification loss.
- Training uses an outer loop over 2,000 epochs and an inner loop over mini-batches, with each batch passed through the model to compute loss.
- The target tensor is reshaped to a two-dimensional column form because the binary cross-entropy criterion expects compatible dimensions.
- Each update clears old gradients with zero_grad, computes new gradients with loss.backward, and updates parameters with optimizer.step.
- The sigmoid output is rounded to 0 or 1 for a hard classification decision, and absolute differences from labels count prediction errors.
- torch.no_grad prevents PyTorch from extending the computation graph for accuracy calculations that do not need gradients.
- The epoch's accuracy and loss are calculated after accounting for a final mini-batch that may be smaller than the configured batch size.
- Across 2,000 epochs, the example's loss decreases and training accuracy approaches 90%, while accuracy fluctuates because it is not a smooth measure like cross-entropy loss.
- Logistic regression with its linear stage and cross-entropy loss is convex in its parameters, but adding hidden-layer nonlinearities makes neural-network optimization nonconvex.
- For plotting, PyTorch tensors can be converted to NumPy with detach().numpy(); trained parameters are available through the model state dictionary.
Transcript excerpt
0:01 All right, welcome back everyone. Um so we were talking about how we adjust the network parameters in our neural net. So we have the weights and biases for the hidden layer, the weights and biases for the output layer. And we said we're going to um define a loss J, which depends on usually the negative log likelihood. And then we said um we're going to use gradient descent, but we're going to use a version of gradient descent called mini-batch stochastic gradient descent that only uses a few training samples at a time when computing uh the gradient in the gradient descent algorithm.
0:48 And this is because if you really did pure gradient descent and you use it all the training samples, it would take too long. And it wouldn't be worthwhile because the gradient doesn't point you actually in the direction you want to go. And so there's really no point in computing that gradient perfectly. So this is how we're doing it. This is the overall algorithm. And what we were talking about last time was how exactly do we compute the gradient of this term here? And that is um what we call the uh forward or the forward pass and the backward pass. So we start by um
1:34 writing a graph that shows how the different variables depend on each other, called the computational graph. And we're focusing just on a single iteration of stochastic gradient descent. So at the start of this iteration, we have the previous values of our parameters. And we have our training data in that mini-batch. And then what we do is we do a forward pass where we start on the left and move our way right and we use um you know, whatever ingredients it takes to compute the intermediate variables
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, Prof. Schniter.