Single Layer Backpropagation
Compute the gradient of weights for a single linear layer.
Given input , weights , bias , and the gradient of the loss with respect to the output (called grad_output):
For :
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
x = [1, 2] W = [[0.5, 0.3], [-0.2, 0.8]] grad_output = [1.0, 0.5]
([[1.0, 2.0], [0.5, 1.0]], [1.0, 0.5], [0.4, 0.7])
- We calculate by taking the outer product of
grad_outputandx: - Then, we calculate which is simply
grad_output: - Next, we calculate by taking the dot product of and
grad_output: - The final output is
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