Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. ›
  3. posts
  4. ›
  5. …

  6. ›
  7. 5 Multi Class Classification

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🤯 Your stomach gets a new lining every 3–4 days.

🍪 This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ⏳
Fetching content, this won’t take long…


💡 Did you know?

🦥 Sloths can hold their breath longer than dolphins 🐬.
AI-DeepLearning

    AI-AgenticAI

    AI-DeepLearning
    • Deep Learning Path 🤖

    • Neural Network Hypothesis and Intuition

    • Forward Propagation in Neural Networks

    • Vectorized Neural Networks Model Representation

    • Examples and Intuitions I — Neural Networks as Logical Gates

    • Examples and Intuitions II — Building XNOR with a Hidden Layer

    • Multiclass Classification with Neural Networks

    • Cost Function for Neural Networks

    • Backpropagation Algorithm

    • Gradient Checking and Random Initialization

    • Training a Neural Network

    • Revision Cheat Sheet

    • AI-DeepLearning Index


    AI-GenAI

    AI-Infrastructure

    AI-Machine-Learning

    AI-Math

    AWS

    Azure

    Hobbies

    kubernetes

    Management

    Programming

    Terraform

    Z_Appendix

    0-root

Cover Image for Multiclass Classification with Neural Networks
AI-DeepLearning

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.

Data Science
Machine Learning
Deep Learning
Neural Networks
Artificial Intelligence
Computational Graphs
← Previous

Examples and Intuitions II — Building XNOR with a Hidden Layer

Next →

Cost Function for Neural Networks

Multiclass Classification with Neural Networks

Extending Binary Classification

In binary classification, our hypothesis outputs a single value:

hΘ(x)∈[0,1]h_\Theta(x) \in [0,1]hΘ​(x)∈[0,1]

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 ttt is:

cost(t)=y(t)log⁡(hΘ(x(t)))+(1−y(t))log⁡(1−hΘ(x(t)))\text{cost}(t) = y^{(t)} \log(h_\Theta(x^{(t)})) + (1 - y^{(t)}) \log(1 - h_\Theta(x^{(t)}))cost(t)=y(t)log(hΘ​(x(t)))+(1−y(t))log(1−hΘ​(x(t)))

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:

  1. 🚗 Car
  2. 🏃 Pedestrian
  3. 🚚 Truck
  4. 🛵 Motorcycle

Instead of one output unit, we use four output units.

Network Structure

Input Image→Hidden Layers→4 Output Units\text{Input Image} \rightarrow \text{Hidden Layers} \rightarrow \text{4 Output Units}Input Image→Hidden Layers→4 Output Units

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:

hΘ(x)=[hΘ(x)1hΘ(x)2hΘ(x)3hΘ(x)4]h_\Theta(x) = \begin{bmatrix} h_\Theta(x)_1 \\ h_\Theta(x)_2 \\ h_\Theta(x)_3 \\ h_\Theta(x)_4 \end{bmatrix}hΘ​(x)=​hΘ​(x)1​hΘ​(x)2​hΘ​(x)3​hΘ​(x)4​​​

Where:

  • hΘ(x)1h_\Theta(x)_1hΘ​(x)1​ → Probability of Car
  • hΘ(x)2h_\Theta(x)_2hΘ​(x)2​ → Probability of Pedestrian
  • hΘ(x)3h_\Theta(x)_3hΘ​(x)3​ → Probability of Truck
  • hΘ(x)4h_\Theta(x)_4hΘ​(x)4​ → Probability of Motorcycle

Training Labels (One-Hot Encoding)

Each training example has a label vector:

y(i)∈R4y^{(i)} \in \mathbb{R}^4y(i)∈R4

Examples:

Car:

[1000]\begin{bmatrix} 1 \\ 0 \\ 0 \\ 0 \end{bmatrix}​1000​​

Motorcycle:

[0001]\begin{bmatrix} 0 \\ 0 \\ 0 \\ 1 \end{bmatrix}​0001​​

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.:

hΘ(x)=[0.020.050.900.03]h_\Theta(x) = \begin{bmatrix} 0.02 \\ 0.05 \\ 0.90 \\ 0.03 \end{bmatrix}hΘ​(x)=​0.020.050.900.03​​

We pick the predicted class via:

arg⁡max⁡k hΘ(x)k=3\arg\max_k \ h_\Theta(x)_k = 3argkmax​ hΘ​(x)k​=3

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:

Truck\text{Truck}Truck

Decision Rule

In practice, we select:

Prediction=arg⁡max⁡khΘ(x)k\text{Prediction} = \arg\max_k h_\Theta(x)_kPrediction=argkmax​hΘ​(x)k​

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.

Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Fri Feb 27 2026

Share This on

← Previous

Examples and Intuitions II — Building XNOR with a Hidden Layer

Next →

Cost Function for Neural Networks

AI-DeepLearning/5-Multi-Class-Classification
Let's work together
+49 176-2019-2523
hiteshkrsahu@gmail.com
WhatsApp
Skype
Munich 🥨, Germany 🇩🇪, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
  Home/About
  Skills
  Work/Projects
  Lab/Experiments
  Contribution
  Awards
  Art/Sketches
  Thoughts
  Contact
Links
  Sitemap
  Legal Notice
  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| © 2026 All rights reserved.