From fecb57fe37d706a8e5310d9abb089614b98f707b Mon Sep 17 00:00:00 2001 From: Sultan Alnuaimi Date: Fri, 4 Dec 2026 11:09:28 +0400 Subject: [PATCH 1/2] Add problem 189 meta: Tournament Odds (Softmax with Temperature) --- .../meta.json | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 questions/189_tournament-odds-softmax-with-temperature/meta.json diff --git a/questions/189_tournament-odds-softmax-with-temperature/meta.json b/questions/189_tournament-odds-softmax-with-temperature/meta.json new file mode 100644 index 00000000..9c5bd89d --- /dev/null +++ b/questions/189_tournament-odds-softmax-with-temperature/meta.json @@ -0,0 +1,15 @@ +{ + "id": "189", + "title": "Tournament Odds: Softmax with Temperature", + "difficulty": "easy", + "category": "Deep Learning", + "video": "https://www.youtube.com/watch?v=3msJK1tIUb0", + "likes": "0", + "dislikes": "0", + "contributor": [ + { + "profile_link": "https://github.com/TheExterminator67", + "name": "Sultan Alnuaimi" + } + ] +} From 996b5d6855fcb07b7bd97467f5e22988fad13901 Mon Sep 17 00:00:00 2001 From: Sultan Alnuaimi Date: Fri, 4 Dec 2026 11:09:51 +0400 Subject: [PATCH 2/2] Add problem 189: Tournament Odds (Softmax with Temperature) --- .../description.md | 10 +++++++ .../example.json | 5 ++++ .../learn.md | 29 +++++++++++++++++++ .../solution.py | 8 +++++ .../starter_code.py | 4 +++ .../tests.json | 26 +++++++++++++++++ 6 files changed, 82 insertions(+) create mode 100644 questions/189_tournament-odds-softmax-with-temperature/description.md create mode 100644 questions/189_tournament-odds-softmax-with-temperature/example.json create mode 100644 questions/189_tournament-odds-softmax-with-temperature/learn.md create mode 100644 questions/189_tournament-odds-softmax-with-temperature/solution.py create mode 100644 questions/189_tournament-odds-softmax-with-temperature/starter_code.py create mode 100644 questions/189_tournament-odds-softmax-with-temperature/tests.json diff --git a/questions/189_tournament-odds-softmax-with-temperature/description.md b/questions/189_tournament-odds-softmax-with-temperature/description.md new file mode 100644 index 00000000..61f15da5 --- /dev/null +++ b/questions/189_tournament-odds-softmax-with-temperature/description.md @@ -0,0 +1,10 @@ +The **Grand Arena** tournament is about to begin! Every fighter has a power level, but anything can happen in a fight, and the tournament's **chaos level** decides how much luck matters. + +Write a function `tournament_odds(power_levels, chaos)` that returns each fighter's probability of winning, using **softmax with temperature**: + +$$P(i) = \frac{e^{p_i / T}}{\sum_j e^{p_j / T}}, \quad T = \text{chaos}$$ + +- **Low chaos** → the strongest fighter almost always wins. +- **High chaos** → every fighter has a similar chance. + +Power levels can be huge, so your function must not overflow. Round each probability to 4 decimal places. You can assume `chaos > 0`. diff --git a/questions/189_tournament-odds-softmax-with-temperature/example.json b/questions/189_tournament-odds-softmax-with-temperature/example.json new file mode 100644 index 00000000..9ba80de4 --- /dev/null +++ b/questions/189_tournament-odds-softmax-with-temperature/example.json @@ -0,0 +1,5 @@ +{ + "input": "tournament_odds([9000, 8000, 7000], 1000)", + "output": "[0.6652, 0.2447, 0.09]", + "reasoning": "Dividing each power level by the chaos (1000) gives [9, 8, 7]. Applying softmax: the champion wins 66.52% of the time, but the underdog still pulls off the upset 9% of the time." +} \ No newline at end of file diff --git a/questions/189_tournament-odds-softmax-with-temperature/learn.md b/questions/189_tournament-odds-softmax-with-temperature/learn.md new file mode 100644 index 00000000..86054f33 --- /dev/null +++ b/questions/189_tournament-odds-softmax-with-temperature/learn.md @@ -0,0 +1,29 @@ +## Softmax with Temperature + +Regular softmax turns a list of scores into probabilities: + +$$\text{softmax}(x)_i = \frac{e^{x_i}}{\sum_j e^{x_j}}$$ + +**Temperature** $T$ adds one extra step: divide every score by $T$ first. + +$$\text{softmax}(x, T)_i = \frac{e^{x_i / T}}{\sum_j e^{x_j / T}}$$ + +### What temperature does + +Softmax only cares about the **gaps** between scores. Dividing by $T$ changes those gaps: + +- $T < 1$ stretches the gaps, so the output gets **sharper**. As $T \to 0$, it picks the max with probability 1. +- $T = 1$ is plain softmax. +- $T > 1$ shrinks the gaps, so the output gets **flatter**. As $T \to \infty$, every option gets the same probability. + +### Where it is used + +This is exactly the **temperature** setting in language models. When a model picks its next word, it samples from a softmax over every word. A low temperature gives safe, predictable text, and a high temperature gives more creative, surprising text. Temperature also shows up in knowledge distillation and contrastive learning. + +### Numerical stability + +A power level of 9000 with $T = 1$ means computing $e^{9000}$, which overflows. The fix is to subtract the largest scaled score $m$ before exponentiating: + +$$\frac{e^{x_i/T - m}}{\sum_j e^{x_j/T - m}} = \frac{e^{x_i/T}}{\sum_j e^{x_j/T}}$$ + +The $e^{-m}$ cancels out on top and bottom, so the answer is unchanged, but now the largest exponent is $e^0 = 1$ and nothing can blow up. diff --git a/questions/189_tournament-odds-softmax-with-temperature/solution.py b/questions/189_tournament-odds-softmax-with-temperature/solution.py new file mode 100644 index 00000000..30e1f439 --- /dev/null +++ b/questions/189_tournament-odds-softmax-with-temperature/solution.py @@ -0,0 +1,8 @@ +import math + +def tournament_odds(power_levels: list[float], chaos: float) -> list[float]: + scaled = [p / chaos for p in power_levels] + m = max(scaled) + exps = [math.exp(s - m) for s in scaled] + total = sum(exps) + return [round(e / total, 4) for e in exps] diff --git a/questions/189_tournament-odds-softmax-with-temperature/starter_code.py b/questions/189_tournament-odds-softmax-with-temperature/starter_code.py new file mode 100644 index 00000000..f9c570c3 --- /dev/null +++ b/questions/189_tournament-odds-softmax-with-temperature/starter_code.py @@ -0,0 +1,4 @@ +def tournament_odds(power_levels: list[float], chaos: float) -> list[float]: + # Return each fighter's probability of winning (softmax with temperature = chaos) + # Round each probability to 4 decimal places + pass diff --git a/questions/189_tournament-odds-softmax-with-temperature/tests.json b/questions/189_tournament-odds-softmax-with-temperature/tests.json new file mode 100644 index 00000000..c2b62894 --- /dev/null +++ b/questions/189_tournament-odds-softmax-with-temperature/tests.json @@ -0,0 +1,26 @@ +[ + { + "test": "print(tournament_odds([9000, 8000, 7000], 1000))", + "expected_output": "[0.6652, 0.2447, 0.09]" + }, + { + "test": "print(tournament_odds([9000, 8000, 7000], 100))", + "expected_output": "[1.0, 0.0, 0.0]" + }, + { + "test": "print(tournament_odds([9000, 8000, 7000], 100000))", + "expected_output": "[0.3367, 0.3333, 0.33]" + }, + { + "test": "print(tournament_odds([5, 5, 5], 1))", + "expected_output": "[0.3333, 0.3333, 0.3333]" + }, + { + "test": "print(tournament_odds([1000, 0], 1))", + "expected_output": "[1.0, 0.0]" + }, + { + "test": "print(tournament_odds([2, 1], 0.5))", + "expected_output": "[0.8808, 0.1192]" + } +] \ No newline at end of file