PIXELBANKv9.1.0
Menu

Single Layer Backpropagation

Compute the gradient of weights for a single linear layer.

Given input xx, weights WW, bias bb, and the gradient of the loss with respect to the output βˆ‚Lβˆ‚o\frac{\partial L}{\partial o} (called grad_output):

For o=Wβ‹…x+bo = W \cdot x + b: βˆ‚Lβˆ‚W=grad_outputβŠ—x(outerΒ product)\frac{\partial L}{\partial W} = \text{grad\_output} \otimes x \quad (\text{outer product}) βˆ‚Lβˆ‚b=grad_output\frac{\partial L}{\partial b} = \text{grad\_output} βˆ‚Lβˆ‚x=WTβ‹…grad_output\frac{\partial L}{\partial x} = W^T \cdot \text{grad\_output}

Return a tuple (grad_W, grad_b, grad_x) where grad_W is a 2D list and grad_b, grad_x are 1D lists. All rounded to 4 decimal places.

Example:

Input:
x = [1, 2]
W = [[0.5, 0.3], [-0.2, 0.8]]
grad_output = [1.0, 0.5]
Output:
([[1.0, 2.0], [0.5, 1.0]], [1.0, 0.5], [0.4, 0.7])
Reasoning:
  • We calculate βˆ‚Lβˆ‚W\frac{\partial L}{\partial W} by taking the outer product of grad_output and x: βˆ‚Lβˆ‚W=[1.00.5]βŠ—[12]=[1.0βˆ—11.0βˆ—20.5βˆ—10.5βˆ—2]=[1.02.00.51.0]\frac{\partial L}{\partial W} = \begin{bmatrix} 1.0 \\ 0.5 \end{bmatrix} \otimes \begin{bmatrix} 1 \\ 2 \end{bmatrix} = \begin{bmatrix} 1.0*1 & 1.0*2 \\ 0.5*1 & 0.5*2 \end{bmatrix} = \begin{bmatrix} 1.0 & 2.0 \\ 0.5 & 1.0 \end{bmatrix}
  • Then, we calculate βˆ‚Lβˆ‚b\frac{\partial L}{\partial b} which is simply grad_output: βˆ‚Lβˆ‚b=[1.00.5]\frac{\partial L}{\partial b} = \begin{bmatrix} 1.0 \\ 0.5 \end{bmatrix}
  • Next, we calculate βˆ‚Lβˆ‚x\frac{\partial L}{\partial x} by taking the dot product of WTW^T and grad_output: βˆ‚Lβˆ‚x=[0.5βˆ’0.20.30.8]β‹…[1.00.5]=[0.5βˆ—1.0+βˆ’0.2βˆ—0.50.3βˆ—1.0+0.8βˆ—0.5]=[0.40.7]\frac{\partial L}{\partial x} = \begin{bmatrix} 0.5 & -0.2 \\ 0.3 & 0.8 \end{bmatrix} \cdot \begin{bmatrix} 1.0 \\ 0.5 \end{bmatrix} = \begin{bmatrix} 0.5*1.0 + -0.2*0.5 \\ 0.3*1.0 + 0.8*0.5 \end{bmatrix} = \begin{bmatrix} 0.4 \\ 0.7 \end{bmatrix}
  • The final output is (βˆ‚Lβˆ‚W,βˆ‚Lβˆ‚b,βˆ‚Lβˆ‚x)=([1.02.00.51.0],[1.00.5],[0.40.7])(\frac{\partial L}{\partial W}, \frac{\partial L}{\partial b}, \frac{\partial L}{\partial x}) = (\begin{bmatrix} 1.0 & 2.0 \\ 0.5 & 1.0 \end{bmatrix}, \begin{bmatrix} 1.0 \\ 0.5 \end{bmatrix}, \begin{bmatrix} 0.4 \\ 0.7 \end{bmatrix})

Constraints:

  • x: 1D list (d_in), W: 2D list (d_out x d_in)
  • grad_output: 1D list (d_out) - gradient from upstream
  • Return (grad_W, grad_b, grad_x)
  • grad_W: 2D list (d_out x d_in), grad_b: 1D (d_out), grad_x: 1D (d_in)
  • Round 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.
Single Layer Backpropagation - Medium | PixelBank