← Lesson
Softmax
Joeven
Run
Reset
Python loads on first run
import math def softmax(zs): m = max(zs) exps = [math.exp(z - m) for z in zs] total = sum(exps) return [e / total for e in exps] def pretty(ps): return [round(p, 3) for p in ps] print("equal logits", pretty(softmax([1, 1, 1]))) print("clear winner", pretty(softmax([4, 1, 0]))) print("sum", round(sum(softmax([4, 1, 0])), 6)) # subtracting max does not change p z = [1000.0, 999.0, 998.0] print("stable huge", pretty(softmax(z))) # without the max trick this would overflow: # math.exp(1000) # inf print("argmax", ["search", "sql", "finish"][softmax([2.0, 0.1, -1.0]).index(max(softmax([2.0, 0.1, -1.0])))])
Run to execute this in your browser. Nothing is sent to a server.