Skip to content
DeepForgeby svx
All problems
MediumCalculusca-288

Log-Sum-Exp Gradient Vector

Problem

The gradient of log-sum-exp, LSE(z) = log(sum_i exp(z_i)), is the softmax of z.

Given logits, return the gradient list computed stably by subtracting the maximum logit first.

Hint

This is the same vector as softmax(z).

Test cases

case 1
in: [[0,0]]
expected: [0.5,0.5]
case 2
in: [[1,2,3]]
expected: [0.09003057317038046,0.24472847105479764,0.6652409557748218]
case 3
in: [[1000,1000,1000]]
expected: [0.3333333333333333,0.3333333333333333,0.3333333333333333]
Starter codePython
def logsumexp_gradient(logits):
    # Your code here
    pass

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