Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-075

Softmax with Temperature

Problem

Return softmax(logits / temperature), computed with the max-subtraction trick for numerical stability.

temperature is assumed positive and non-zero. Return an empty list for empty logits.

Hint

Low temperature sharpens the distribution; dividing by it amplifies the logits.

Test cases

case 1
in: [[1,1],1]
expected: [0.5,0.5]
case 2
in: [[1,2,3],1]
expected: [0.09003057317038046,0.24472847105479764,0.6652409557748218]
case 3
in: [[1,2,3],0.5]
expected: [0.015876239976466765,0.11731042782619838,0.8668133321973349]
case 4
in: [[2,2],0.5]
expected: [0.5,0.5]
case 5
in: [[],1]
expected: []
Starter codePython
import math
def softmax_temperature(logits, temperature):
    # Your code here
    pass

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