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.
Examples and Intuitions II — Building XNOR with a Hidden Layer
Cost Function for Neural Networks
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.
