Multiclass Classification with Neural Networks
Learn how to extend binary classification to multiclass classification using neural networks, where the output layer consists of multiple units representing different classes, and the final prediction is made by selecting the class with the highest output value.
Multiclass Classification with Neural Networks
Extending Binary Classification
In binary classification, our hypothesis outputs a single value:
For multiclass classification, instead of returning a single value,
our hypothesis returns a vector of probabilities.
Simplified Cost
This is the binary cost for a single output unit — useful for building intuition, but it is not the multi-class cost itself (there's no sum over classes k yet). If we ignore multiclass and regularization, the cost for training example is:
The actual multi-class cost sums this expression over every output unit k (and adds regularization) — see 6-Cost-Function for the full formula.
Example: Four-Class Classification
Suppose we want to classify an image into one of four categories:
- 🚗 Car
- 🏃 Pedestrian
- 🚚 Truck
- 🛵 Motorcycle
Instead of one output unit, we use four output units.
Network Structure
Each output unit corresponds to one class.
graph LR
%% Styling
classDef input fill:#1e293b,stroke:#38bdf8,color:#ffffff,stroke-width:2px;
classDef output fill:#111827,stroke:#f59e0b,color:#ffffff,stroke-width:2px;
%% ===== Input Layer =====
subgraph "Input Layer"
x0(((x0=1)))
x1(((x1)))
x2(((x2)))
x3(((x3)))
end
%% ===== Hidden Layer =====
subgraph "Hidden Layer"
a0(((a0=1)))
a1{a1}
a2{a2}
a3{a3}
end
%% ===== Output Layer =====
subgraph "Output Layer (4 Classes)"
y1(((hθx1 = 🚗)))
y2(((hθx2 = 🏃)))
y3(((hθx3 = 🚚)))
y4(((hθx4 = 🛵)))
end
%% Input → Hidden
x0 --> a1
x0 --> a2
x0 --> a3
x1 --> a1
x1 --> a2
x1 --> a3
x2 --> a1
x2 --> a2
x2 --> a3
x3 --> a1
x3 --> a2
x3 --> a3
%% Hidden → Output
a0 --> y1
a0 --> y2
a0 --> y3
a0 --> y4
a1 --> y1
a1 --> y2
a1 --> y3
a1 --> y4
a2 --> y1
a2 --> y2
a2 --> y3
a2 --> y4
a3 --> y1
a3 --> y2
a3 --> y3
a3 --> y4
%% Assign classes
class x1,x2,x3 input
class y1,y2,y3,y4 output
Output Representation
Our hypothesis now returns:
Where:
- → Probability of Car
- → Probability of Pedestrian
- → Probability of Truck
- → Probability of Motorcycle
Training Labels (One-Hot Encoding)
Each training example has a label vector:
Examples:
Car:
Motorcycle:
This is called one-hot encoding.
Example Output
In practice the network rarely outputs exact 0s and 1s — each unit outputs a probability, e.g.:
We pick the predicted class via:
So the predicted class is the third category (highest probability), even though the output isn't an exact one-hot vector.
If we defined:
1 → Car
2 → Pedestrian
3 → Truck
4 → Motorcycle
Then the model predicts:
Decision Rule
In practice, we select:
That is, we choose the class with the largest output value.
Key Idea
- Binary classification → 1 output unit
- Multiclass classification → K output units
- Output layer size = number of classes
- Final prediction = index of largest output
Neural networks naturally extend logistic regression to multiple classes by simply increasing the number of output neurons.
Modern Relevance
Every LLM generating text is doing exactly this — multi-class classification — on every single token it produces.
Llama 3.1's vocabulary is 128,256 tokens. At each generation step, the model runs a forward pass and produces a 128,256-dimensional output vector. Softmax converts it to a probability distribution. The selected token is the "predicted class."
- Temperature sampling modifies the softmax: dividing logits by temperature
Tbefore softmax makes the distribution sharper (T < 1, more confident) or flatter (T > 1, more random).T = 0is argmax — always pick the highest-probability class. - Top-k and top-p sampling are post-softmax filters: restrict generation to the top-k most probable tokens, or the smallest set of tokens whose cumulative probability exceeds p. These map to restricting the K-class output to a subset before drawing.
- One-hot encoding as training labels is exactly how LLM pre-training works: the correct next token is a one-hot vector over 128K classes, and cross-entropy loss penalizes the model for assigning low probability to the true next token.
At 70B tokens/second across a DGX cluster, the system runs this 128K-class softmax millions of times per second — hardware-fused into the final linear layer by TensorRT-LLM for maximum throughput.
Related Posts
- Logistic Regression II: Multi-Class and One-vs-All — neural networks replace the K separate one-vs-all classifiers with a single network and one-hot output labels
- Cost Function for Neural Networks — the multi-class cost function is the generalized version of what's defined here applied across K output nodes
- Examples and Intuitions II — the binary-output network this generalizes to multiple classes
