PIXELBANKv9.1.0
Menu

Chain Rule Application

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}.

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:
  • The function receives two vectors: upstream gradient dL/dy = [0.5, 0.5] and local gradient dy/dx = [1, 0]
  • Apply the chain rule through element-wise multiplication: dLdx=dLdy⋅dydx\frac{dL}{dx} = \frac{dL}{dy} \cdot \frac{dy}{dx}
  • For the first element: 0.5×1=0.50.5 \times 1 = 0.5
  • For the second element: 0.5×0=0.00.5 \times 0 = 0.0
  • The final output is [0.5, 0.0], representing how changes in xx affect the loss through the intermediate variable yy[5]

Constraints:

  • Return gradient rounded to 4 decimal places
🔒

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

0/0
Run code to see test results.