Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -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."
}
29 changes: 29 additions & 0 deletions questions/189_tournament-odds-softmax-with-temperature/learn.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions questions/189_tournament-odds-softmax-with-temperature/meta.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions questions/189_tournament-odds-softmax-with-temperature/tests.json
Original file line number Diff line number Diff line change
@@ -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]"
}
]