v9.1.1
Menu

Chain Rule Application

mediumPROBackpropagationDeep LearningCV: Deep Learning

Implement a function to apply the chain rule in the context of backpropagation for computing gradients in deep learning models. This task involves calculating the gradient of a loss function LL with respect to an input xx, given the upstream gradient dLdy\frac{dL}{dy} and the local gradient dydx\frac{dy}{dx}.

The chain rule is a fundamental concept in calculus that allows us to compute the derivative of a composite function. In the context of neural networks, it is crucial for backpropagation, where we need to propagate error gradients through multiple layers to update model parameters. The chain rule states that the derivative of a composite function L(y(x))L(y(x)) with respect to xx can be expressed as the product of the derivatives of LL with respect to yy and yy with respect to xx, i.e., dLdx=dLdy⋅dydx\frac{dL}{dx} = \frac{dL}{dy} \cdot \frac{dy}{dx}.

Here upstream and local are lists of the same length, one entry per component of xx, and the product is taken component by component: (dLdx)i=(dLdy)i⋅(dydx)i\left(\frac{dL}{dx}\right)_i = \left(\frac{dL}{dy}\right)_i \cdot \left(\frac{dy}{dx}\right)_i. Return the result as a list of the same length.

To compute the gradient dLdx\frac{dL}{dx}, we can follow these steps:

  1. Receive the upstream gradient dLdy\frac{dL}{dy} from the previous layer.
  2. Compute the local gradient dydx\frac{dy}{dx} using the activation functions and weights of the current layer.
  3. Apply the chain rule by multiplying the upstream gradient with the local gradient.
dLdx=dLdy⋅dydx\frac{dL}{dx} = \frac{dL}{dy} \cdot \frac{dy}{dx}

This technique is widely used in training deep neural networks.

Example

Input
chain_rule([0.5, 0.5], [1, 0])
Output
[0.5, 0.0]
Reasoning
  • upstream holds dLdy\frac{dL}{dy}: how much the loss changes per unit change in each component of yy. Here both components have sensitivity 0.50.5.
  • local holds dydx\frac{dy}{dx} for each component: y1y_1 moves one-for-one with x1x_1 (slope 11), while y2y_2 does not respond to x2x_2 at all (slope 00), like a ReLU unit whose input was negative.
  • The chain rule pairs each component with its own slope: dLdx1=0.5×1=0.5\frac{dL}{dx_1} = 0.5 \times 1 = 0.5 and dLdx2=0.5×0=0\frac{dL}{dx_2} = 0.5 \times 0 = 0.
  • Each value is rounded to 4 decimal places, giving [0.5, 0.0], a gradient with one entry per input component.
  • Notice that a zero local gradient blocks the signal completely: however large the upstream gradient is, nothing reaches x2x_2. This is how gradients "die" behind inactive units during backpropagation.

Constraints

  • Return gradient rounded to 4 decimal places

Learn this in

More from CV: Deep Learning

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.