1. Home
  2. Explore
  3. Computer Science
  4. Backpropagation and Neural Networks

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Backpropagation and Neural Networks

Speaker 1Speaker 2

Backpropagation and Neural Networks

14:09
0:00-14:09
  1. Maria

    Today we're focusing on backpropagation and neural networks, which are core ideas for training modern deep-learning models. Before the technical material, one practical reminder from the lecture: Assignment One was due Thursday, April 20 at 11:59 p.m. on Canvas, project-related TA specialties were listed on Piazza, and students were to receive $100 in Google Cloud credits by the end of that week.

  2. Anna

    Let's connect this to what we already know. A classifier is represented by a function f, parameterized by weights W, that takes data x as input and produces a score for every class.

  3. Maria

    Exactly. We can evaluate those scores with a loss function, such as the SVM loss, which measures how satisfactory or unsatisfactory the scores are. The total loss combines a data-loss term with a regularization term, and regularization expresses a preference for simpler models because simpler models tend to generalize better.

  4. Anna

    So the training goal is to find the weights W that minimize the total loss.

  5. Maria

    Right. Gradient descent does this iteratively by moving in the direction of steepest descent, which is the negative of the gradient. You can picture the loss as a landscape: each update takes a step downhill, ideally toward a low point.

  6. Anna

    There are two main ways to compute a gradient. A numerical gradient uses finite differences, so it's easy to implement but slow and only approximate. An analytic gradient is fast and exact once derived, but deriving it requires calculus and the result can contain implementation mistakes.

  7. Maria

    In practice, the useful strategy is to derive and use the analytic gradient, then check the implementation against a numerical gradient. The numerical version is not usually the efficient training method, but it is a valuable correctness check.

  8. Anna

    And computational graphs give us a systematic way to get analytic gradients for complicated functions.

  9. Maria

    A computational graph represents a function as a sequence of computation nodes. For a linear classifier, inputs such as x and W enter a matrix-multiplication node, producing class scores. Those scores can feed into a hinge-loss node, while a separate node computes regularization, and a final addition node combines the data term and regularization term into the total loss.

  10. Anna

    The key benefit is that once a function is broken into nodes, backpropagation can recursively apply the chain rule to compute gradients with respect to every variable in the graph. That matters because a convolutional neural network may transform an input image through many layers, and manually deriving one enormous gradient expression would be impractical.

  11. Maria

    Let's use the simple example f of x, y, and z equals x plus y, with that result multiplied by z. Define q equals x plus y, so f equals q times z. If x is negative two, y is five, and z is negative four, the forward pass gives q equals three and f equals negative twelve.

  12. Anna

    The forward pass means evaluating the graph from inputs to output and recording the intermediate values. It is helpful to give every intermediate result a name, such as q, because that result will be used again during the backward pass.

  13. Maria

    Now we work backward. The gradient of the output f with respect to f itself is one. Since f equals q times z, the local derivatives are df over dq equals z and df over dz equals q. Using the forward values, df over dq is negative four and df over dz is three.

  14. Anna

    To reach y, we use the chain rule: df over dy equals df over dq multiplied by dq over dy. The addition node has dq over dy equal to one, so df over dy equals negative four times one, or negative four.

  15. Maria

    The same reasoning gives df over dx equals df over dq times dq over dx. Because dq over dx is also one, the gradient with respect to x is negative four. Backpropagation is therefore a recursive chain-rule calculation that begins at the output and moves toward the inputs.

  16. Anna

    Each node only needs to understand its immediate surroundings. It receives local inputs, computes a local output, and knows the local derivatives of that output with respect to its inputs.

  17. Maria

    During backpropagation, each node also receives an upstream gradient: the derivative of the final loss with respect to that node's output. It multiplies that upstream gradient by its own local gradient, then sends the resulting gradients to the preceding nodes. This is why we can work with simple operations such as addition, multiplication, and exponentials instead of differentiating the entire complicated function at once.

  18. Anna

    The gradients are evaluated using the current forward-pass values, but that does not mean the method only works numerically. Each local derivative can still be written as an expression in the variables; during a particular pass, the relevant values are substituted into those expressions.

  19. Maria

    Consider the more complex function one over one plus e to the negative of a weighted expression, specifically w-zero times x-zero plus w-one times x-one plus w-two. Its graph can break the calculation into multiplications, additions, multiplication by negative one, an exponential, addition by one, and finally taking a reciprocal.

  20. Anna

    In the backward pass, the final output starts with gradient one. For a reciprocal node, the local derivative is negative one over the input squared. For adding a constant, the local derivative is one. For an exponential node, the local derivative is e to the x, and for multiplication by a constant a, the local derivative is a.

  21. Maria

    Using the lecture's forward values, the reciprocal step produced approximately negative 0.53, the exponential step produced approximately negative 0.2, and multiplying by negative one changed that to 0.2. At an addition node, that 0.2 was sent down both branches. At the multiplication involving w-zero and x-zero, the gradient with respect to w-zero was 0.2 times x-zero, giving negative 0.2 when x-zero was negative one; the gradient with respect to x-zero was 0.2 times w-zero, giving 0.4 when w-zero was two.

  22. Anna

    This also illustrates an important rule for branching graphs. If one variable influences several later nodes, the total gradient at that variable is the sum of the gradients arriving along all those paths. That is the multivariate chain rule: every route by which a variable affects the final output contributes to the total derivative.

  23. Maria

    Nodes can be chosen at different levels of granularity. We could represent a sigmoid as many small operations, or treat the entire sigmoid as one node if we know its local derivative. For sigma of x equals one over one plus e to the negative x, the derivative is sigma of x multiplied by one minus sigma of x.

  24. Anna

    The lecture showed that if the sigmoid's input is one, its output is about 0.73. Its local derivative is then about 0.2, and multiplying by an upstream gradient of one gives the same result as backpropagating through all the smaller operations. Grouping nodes makes the graph more concise, while breaking them apart makes each local derivative simpler.

  25. Maria

    There are useful intuitive patterns for common gates. An addition gate is a gradient distributor: it sends the same upstream gradient to each input. A maximum gate is a gradient router: it sends the gradient through the input that achieved the maximum and sends zero to the other input.

  26. Anna

    A multiplication gate acts as a gradient scaler or switcher. The gradient with respect to one input is the upstream gradient multiplied by the value of the other input. These interpretations follow directly from the local derivatives.

  27. Maria

    Backpropagation computes gradients; it does not itself update the weights. Once the gradients have been found, we use the optimization procedure from gradient descent to take a step in the appropriate direction and update the parameters.

  28. Anna

    So far the examples used scalars, but the same idea works for vectors and matrices. The derivatives then form Jacobian matrices, whose entries describe how each output element changes with respect to each input element.

  29. Maria

    A 4096-dimensional element-wise maximum with zero produces a 4096-dimensional output. Its full Jacobian would be 4096 by 4096, and with batches it could become even larger. However, because each output element depends only on the corresponding input element, the Jacobian is diagonal, so we usually do not explicitly construct the enormous matrix.

  30. Anna

    A concrete vector example is f of x and W equal to the L-two norm of W multiplied by x, where x is n-dimensional and W is n by n. Let q equal W times x, and f equal the L-two expression q-one squared plus q-two squared in the two-dimensional example.

  31. Maria

    The gradient of f with respect to q-i is two times q-i, so in vector form the gradient with respect to q is two q. The gradient has the same shape as q, and each element tells us how much that element affects the final output.

  32. Anna

    For q equal W times x, the partial derivative of q-k with respect to W-i,j is x-j. Applying the chain rule across all elements of q gives the gradient with respect to W; element by element, it is two times q-i times x-j. Similarly, because the derivative of q-k with respect to x-i is W-k,i, the chain rule gives the gradient with respect to x.

  33. Maria

    A practical sanity check is that a gradient should have the same shape as the variable it describes. The gradient with respect to a vector should be a vector of that size, and the gradient with respect to a matrix should have the matrix's shape.

  34. Anna

    This naturally leads to a modular implementation. Each computational node, or gate, has a forward function that computes its output and a backward function that receives an upstream gradient and returns gradients for its inputs.

  35. Maria

    The forward pass processes gates in topologically sorted order, meaning a node is processed after its inputs are available. The backward pass processes them in reverse order. Forward values are cached because the backward calculation often needs those values; for a multiplication gate, for example, the backward pass uses the cached x and y to return dx equal to y times the upstream gradient and dy equal to x times the upstream gradient.

  36. Anna

    Deep-learning frameworks use this same structure. A framework such as Caffe contains layers that implement forward and backward operations, including things such as sigmoid, convolution, and Argmax layers. A complete network is formed by stacking these computational components.

  37. Maria

    The workflow for an assignment or a new model is therefore: first express the function as a computational graph, identify intermediate variables, derive each node's local gradient, run the forward pass, and then run the chain rule backward. This makes gradients manageable even when the overall expression is very complicated.

  38. Anna

    Now let's define a neural network as a function. A linear score function is f equals W times x. A simple two-layer neural network stacks one linear transformation, a nonlinear activation, and a second linear transformation: first compute W-one times x, apply a function such as the element-wise maximum of zero and that result, and then multiply by W-two to produce class scores.

  39. Maria

    The nonlinearity is essential. If we stacked only linear layers, they would collapse into a single linear function, so the network would not gain the expressive structure we want. By placing nonlinear functions between linear layers, neural networks build complex nonlinear functions from simpler stages.

  40. Anna

    The intermediate representation can also address a weakness of a single linear classifier. In a linear model, each row of W can be viewed as one template for a class, such as one fuzzy red-car template. But a category may have multiple modes, like red cars, yellow cars, left-facing horses, or right-facing horses.

  41. Maria

    In a two-layer network, W-one can contain many templates, and the hidden representation h contains scores indicating how strongly those templates match the input. W-two then forms weighted combinations of those intermediate scores. It is a weighted sum, not a maximum, so different combinations of template matches can support a high final class score.

  42. Anna

    For example, an image of a left-facing horse might produce a high score for a left-facing-horse template and a lower score for a right-facing-horse template. An image with a different orientation could produce another pattern, and the second layer can combine these patterns to recognize the broader horse category.

  43. Maria

    Adding another linear layer and another nonlinearity produces a three-layer neural network. More generally, stacking many such layers gives a deep neural network, which is where the word deep comes from.

  44. Anna

    These are often called fully connected or linear layers because each layer uses a matrix multiplication. A two-layer neural network can also be called a one-hidden-layer neural network, while a three-layer network can be called a two-hidden-layer neural network. The naming depends on whether we count linear layers or hidden layers.

  45. Maria

    Each hidden layer is really a whole vector of neurons. Matrix-vector multiplication lets us compute the outputs of many neurons efficiently at once, followed by an activation function. In general, a network can be written as repeated matrix multiplications and nonlinearities, and backpropagation supplies the gradients needed to train all its parameters.

  46. Anna

    There is a loose biological analogy behind the terminology. Biological neurons receive impulses through dendrites, integrate signals in the cell body, and send signals onward through axons. A computational neuron receives inputs, combines them using weights and perhaps a bias, applies an activation function, and passes its output to connected units.

  47. Maria

    Activation functions can be loosely compared with neuronal firing or spiking rates: the activation value represents the signal passed to later units. The lecture noted that ReLU is considered by some neuroscientists to resemble neuronal behavior more closely than some alternatives; ReLU is zero for negative inputs and linear for positive inputs.

  48. Anna

    But the analogy must not be taken literally. Biological neurons are much more complex, there are many neuron types, dendrites can perform nonlinear computations, and synapses are not simply single scalar weights. Even interpreting an activation as a firing rate is an incomplete description of how biological neurons communicate.

  49. Maria

    So the central picture is this: neural networks compose linear layers and nonlinear activation functions into hierarchical computations, and computational graphs organize those computations. Backpropagation runs the chain rule backward through the graph, using cached forward values, local derivatives, and accumulated upstream gradients.

  50. Anna

    Then gradient descent uses the resulting parameter gradients to update the weights. That combination—forward computation, backward differentiation, and optimization—is the foundation for training increasingly deep neural networks.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent