Skip to content
DeepForgeby svx
All problems
MediumCalculusca-284

MAE Subgradient Sign Rule

Problem

The mean absolute error has subgradient sign(pred - target) at nonzero residuals and any value in [-1, 1] at zero; use 0 at exact equality.

Given predictions and targets, return the per-example subgradient list with this convention.

Hint

Compare elementwise and return the sign with a zero convention at ties.

Test cases

case 1
in: [[1,2],[2,2]]
expected: [-1,0]
case 2
in: [[0,5,-5],[0,5,5]]
expected: [0,0,-1]
case 3
in: [[1,-1],[-1,1]]
expected: [1,-1]
Starter codePython
def mae_subgradient(preds, targets):
    # Your code here
    pass

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