PyTorch Thresholds and Decisions
A threshold is the score a prediction must reach to count as positive. Raising it makes the model stricter, and lowering it makes the model catch more.
The default misses cases
At the default 0.5, every score below it becomes a negative call. Two of these four cases score under the line.
scores = torch.tensor([0.45, 0.35, 0.40, 0.25])
pred = (scores >= 0.5).long()
print(pred)A lower threshold flags more
The same comparison at 0.3 flips every one of these scores to a positive call. The threshold is the only thing that changed.
scores = torch.tensor([0.95, 0.45, 0.35, 0.40])
pred = (scores >= 0.3).long()
print(pred)Example
import torch
scores = torch.tensor([0.95, 0.85, 0.65, 0.55, 0.45, 0.35, 0.40, 0.25, 0.15, 0.05])
labels = torch.tensor([1, 1, 1, 1, 1, 1, 0, 0, 0, 0])
print("Threshold | Precision | Recall")
for threshold in [0.3, 0.5, 0.7]:
pred = (scores >= threshold).long()
tp = ((pred == 1) & (labels == 1)).sum().item()
fp = ((pred == 1) & (labels == 0)).sum().item()
fn = ((pred == 0) & (labels == 1)).sum().item()
precision = tp / (tp + fp) if tp + fp > 0 else 0.0
recall = tp / (tp + fn) if tp + fn > 0 else 0.0
print(f"{threshold:>9.1f} | {precision:>9.2f} | {recall:.2f}")Key points
- Score: a number between 0 and 1 that ranks how positive a case looks
- Use scores >= threshold to turn scores into decisions
- A higher threshold flags fewer cases
- Raising the threshold tends to raise precision and lower recall
- Lowering the threshold tends to raise recall and lower precision
In this example, ten fixed scores come with ten known labels. Six cases are positive and four are negative. The comparison scores >= threshold turns scores into decisions at 0.3, then 0.5, then 0.7. Masks count the true positives, false positives, and false negatives at each level. Precision and recall follow from those counts. At 0.3 the model catches every positive, and recall is 1.00 with precision 0.86. At 0.7 precision reaches 1.00, and recall falls to 0.33. The table prints the trade one row per threshold.
Note
The default threshold of 0.5 is a choice, not a law, and costly misses argue for a lower one
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Python Playground
Exercise
What usually happens to recall when the threshold rises?