By the end
- Distinguish logits, probabilities, predicted classes, and labels.
- Explain why softmax outputs sum to one.
- Calculate cross-entropy for one labeled case.
- Build a one-layer multiclass classifier.
Week 5 · Hands-on foundations
How does a neural classifier turn features into competing class scores?
The current classification materials cover softmax regression, information theory, FashionMNIST, implementation from scratch, and concise PyTorch code. This chapter keeps the durable pathway from logits to probabilities to cross-entropy, using small arrays before a full image dataset.
This chapter condenses materials taught in the 2023–2025 Advanced Data Science course into a smaller core path. Optional depth remains available in the companion notebook.
Use the explorer and three short Python examples before changing a longer model.
Change the data overlap and explain what happens to probability confidence and cross-entropy.
Core ideas
A classifier first produces one unrestricted score per class. Scores are comparative evidence; they are not probabilities and need not be positive or sum to one.
Exponentiation makes transformed scores positive, and normalization makes them sum to one. Adding the same constant to every logit does not change the probabilities.
For a labeled example, cross-entropy penalizes the negative log probability assigned to the correct class. Confident wrong predictions receive a large loss.
Mathematical intuition
The softmax probability p_k comes from all logits together. The loss then selects the probability of the observed class y.
Identify the input, learned quantity, output, and aggregation. Re-create the relationship with a tiny hand-checkable example before using a library layer.
Interactive explorer
Predict the direction of change before moving a control.
Change a control to inspect the relationship.
Hands-on path
Minimal Python
Each example is independent. Explain its output before copying it into a larger workflow.
import torch
logits = torch.tensor([2., 1., 0.])
probs = torch.softmax(logits, dim=0)
print([round(float(v), 3) for v in probs])Predict the result, then click Run.
scores = [0.2, 1.4, -0.3]
print(max(range(len(scores)), key=scores.__getitem__))Predict the result, then click Run.
import math
correct_probability = 0.7
print(round(-math.log(correct_probability), 3))Predict the result, then click Run.
Failure checks
What goes wrong: Probabilities are passed into a loss that expects logits.
Check: Check the loss API and model's last layer.
What goes wrong: Softmax normalizes across observations rather than classes.
Check: Name the batch and class dimensions.
What goes wrong: A majority class dominates the score.
Check: Inspect class counts and the confusion matrix.
Check your understanding
Answer in your own words before opening each panel.
Standard answer: A strong answer connects the central idea to a visible computation and a held-out or shape-based check. This chapter keeps the durable pathway from logits to probabilities to cross-entropy, using small arrays before a full image dataset.
Standard answer: A classifier first produces one unrestricted score per class. Scores are comparative evidence; they are not probabilities and need not be positive or sum to one.
Standard answer: Exponentiation makes transformed scores positive, and normalization makes them sum to one. Adding the same constant to every logit does not change the probabilities.
Standard answer: For a labeled example, cross-entropy penalizes the negative log probability assigned to the correct class. Confident wrong predictions receive a large loss.
Standard answer: The softmax probability p_k comes from all logits together. The loss then selects the probability of the observed class y. It does not by itself prove useful behavior on unseen intended-use cases.
Standard answer: The visible calculation and interpretation should change together. Predict the direction first, then use the result to correct your mental model.
Standard answer: Encode labels as class indices.
Standard answer: Check the loss API and model's last layer.
Standard answer: Record data and split assumptions, input/output shapes, settings, the baseline, held-out metrics, representative failures, and the decision supported by that evidence.
Standard answer: Week 6 moves from model mechanics to the complete classification workflow: splitting, thresholds, confusion matrices, and error trade-offs.
Terminology
Go further
Next: Week 6 moves from model mechanics to the complete classification workflow: splitting, thresholds, confusion matrices, and error trade-offs.