Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-206

Backprop Through Linear Layer

Problem

Backpropagate through the linear layer y = W*x. Given the upstream gradient grad_out = dL/dy, return [grad_W, grad_x] where

grad_W[i][j] = grad_out[i] * x[j] (outer product)

grad_x = W^T * grad_out.

Hint

grad_W is the outer product of the upstream gradient with the input; grad_x is the transpose product.

Test cases

case 1
in: [[[1,2],[3,4]],[1,0],[1,1]]
expected: [[[1,0],[1,0]],[4,6]]
case 2
in: [[[1,0],[0,1]],[2,3],[1,-1]]
expected: [[[2,3],[-2,-3]],[1,-1]]
case 3
in: [[[1,0,0],[0,1,0]],[1,2,3],[2,1]]
expected: [[[2,4,6],[1,2,3]],[2,1,0]]
case 4
in: [[[1]],[5],[3]]
expected: [[[15]],[3]]
Starter codePython
def backprop_linear(W, x, grad_out):
    # Your code here
    pass

4 test cases · run your code against them in the editor