Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-064

Softmax of Logits

Problem

Compute the softmax of a list of logits: exp(z_i - max(z)) / sum(exp(z_j - max(z))).

Return the probability list; empty input returns [].

Hint

Subtract the maximum logit first to avoid overflow, then normalize.

Test cases

case 1
in: [[1,1,1]]
expected: [0.3333333333333333,0.3333333333333333,0.3333333333333333]
case 2
in: [[0,1]]
expected: [0.2689414213699951,0.7310585786300049]
case 3
in: [[1000,1000]]
expected: [0.5,0.5]
case 4
in: [[]]
expected: []
Starter codePython
import math
def softmax(logits):
    # Your code here
    pass

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