Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts

Wednesday, 22 July 2026

🧠 Why Can't We Train ChatGPT on a CPU? Understanding CPU vs GPU vs TPU

 

Introduction

Every time we hear about ChatGPT, Gemini, Claude, or any modern AI model, another term appears alongside it:

GPU.

Sometimes we even hear about Google TPUs.

This made me wonder:

If my laptop already has a CPU, why do companies spend millions of dollars buying GPUs and TPUs?

The answer lies in how these processors are designed.

Although all three process information, they solve problems very differently.

Let's understand why.


Imagine Three Engineers...

Suppose a company receives 10,000 customer invoices that need to be verified.

Three engineers volunteer.

👨 Engineer 1 (CPU)

He is extremely intelligent.

He checks one invoice carefully.

Finishes.

Moves to the next.

His strength isn't speed.

His strength is decision making.


👨‍👨‍👨 Engineer 2 (GPU)

Instead of working alone...

He brings 5,000 assistants.

Each assistant verifies one invoice.

The work finishes much faster.


🏭 Engineer 3 (TPU)

Google hires a specialist.

Instead of hiring more people...

They build an assembly line where invoices continuously move through dedicated stations.

Each station performs exactly one mathematical operation.

No unnecessary decisions.

Only maximum throughput.


That is essentially the difference between CPU, GPU and TPU.

Now let's see what happens internally.


🧠 CPU — Built for Decisions, Not Repetition

A CPU (Central Processing Unit) is designed to execute many different kinds of instructions efficiently.

Internally it consists of:

  • Control Unit (CU)
  • Arithmetic Logic Unit (ALU)
  • Multiple CPU Cores
  • Cache hierarchy (L1 → L2 → L3)
  • Registers
  • Main Memory Interface



How a CPU Executes an Instruction

Every instruction follows a cycle.

Instruction

↓

Fetch

↓

Decode

↓

Execute

↓

Store

Let's understand each stage.

1. Fetch

The Control Unit fetches an instruction from RAM.

Example:

ADD A, B

2. Decode

The CPU determines:

  • What operation?
  • Which registers?
  • Which memory locations?

3. Execute

The ALU performs:

  • arithmetic
  • logical comparison
  • branching

4. Store

The result is written back into memory.

Then the CPU starts again.

Millions of times every second.


Why CPUs Are So Good

Because they optimize for:

✔ Low latency

✔ Branch prediction

✔ Context switching

✔ Complex operating systems

✔ Database transactions

✔ Application execution

This is why Oracle Database, Linux, browsers and web servers primarily run on CPUs.


But Then Deep Learning Arrived...

Training a neural network isn't about making complicated decisions.

Instead, it performs the same mathematical operation...

again...

and again...

and again...

Imagine multiplying two huge matrices.

A × B

Not once.

Millions of times.

A CPU quickly becomes the bottleneck.


GPU — Designed for Massive Parallelism

A GPU wasn't originally built for AI.

It was built for graphics.

Rendering a 4K image means calculating millions of pixels simultaneously.

To solve this problem...

NVIDIA designed GPUs with thousands of smaller cores.




How GPU Architecture Works

Instead of a few powerful cores...

A GPU contains:

  • Streaming Multiprocessors (SMs)

Each SM contains:

  • CUDA cores
  • Shared memory
  • Registers

Multiple SMs share:

  • L2 Cache
  • High Bandwidth Memory (HBM)

Thousands of threads execute simultaneously.


Imagine multiplying a matrix.

CPU:

1
↓

2

↓

3

GPU:

1 2 3 4 5 6 7 ...

All together

This is called SIMD/SIMT parallelism.


Why GPUs Changed AI

Almost every deep learning computation eventually becomes:

Matrix Multiplication

Examples:

  • Convolution
  • Attention
  • Embeddings
  • Transformers

GPUs excel at these operations.

This is why companies like OpenAI use thousands of NVIDIA GPUs for training.


Then Even GPUs Became a Limitation...

Although GPUs are excellent...

They still contain logic designed for graphics.

Google asked:

What if we removed everything unnecessary...

...and built hardware only for tensors?


TPU — Built Specifically for AI

TPU stands for:

Tensor Processing Unit

Unlike CPUs and GPUs...

A TPU is designed almost entirely around matrix multiplication.




Inside a TPU

Instead of CUDA cores...

A TPU contains:

  • Systolic Array
  • Multiply-Accumulate (MAC) Units
  • Weight Buffer
  • Activation Buffer
  • On-chip SRAM
  • High Bandwidth Memory

Rather than instructions moving around...

Data flows continuously through the array.

Think of it like an automobile assembly line.

Each station performs one operation.

The output immediately moves to the next station.

This dramatically reduces memory movement.

And memory movement is often slower than computation itself.


Why TPUs Are So Fast

Because neural networks mostly perform:

Tensor × Tensor

↓

Matrix Multiplication

↓

Activation

↓

Repeat

TPUs optimize exactly this workload.

Nothing more.

Nothing less.


Python Example

TensorFlow automatically uses available GPUs.

import tensorflow as tf

print(tf.config.list_physical_devices("GPU"))

If a compatible GPU exists...

TensorFlow offloads tensor operations automatically.


Quick Comparison

CPUGPUTPU
Few powerful coresThousands of smaller coresThousands of MAC units
SequentialParallelTensor optimized
Operating systemsGraphics & AILarge-scale AI
Low latencyHigh throughputMaximum AI efficiency

So... What Happens When You Ask ChatGPT Something?

Prompt

↓

CPU
(Tokenization,
Networking,
Scheduling)

↓

GPU
(Matrix multiplication,
Transformer inference)

↓

Generated Response

For Google Gemini:

Prompt

↓

TPU

↓

Tensor Operations

↓

Response

Final Thoughts

When I first learned about CPUs, GPUs, and TPUs, I thought the difference was simply "more cores."

But the real difference lies in how they are architected to solve problems.

  • CPUs are designed for decision making.
  • GPUs are designed for parallel computation.
  • TPUs are designed for tensor computation.

Understanding this also explains why modern AI became possible.

It wasn't just because algorithms improved.

It was because hardware evolved alongside them.

Thursday, 25 December 2025

🧠 Deep Learning Models You Should Know

Deep Learning is a powerful subset of Machine Learning that allows systems to learn complex patterns from data using neural networks.

When I started learning Deep Learning as part of my Data Science journey, I realized that different problems need different neural network architectures.
This blog covers the most important deep learning models, what they are best at, and where they are used in real life.


1️⃣ Feedforward Neural Networks (FNN)

Feedforward Neural Networks are the simplest form of neural networks.

Information flows in one direction only:
Input → Hidden Layers → Output

There are no loops or memory.

🔹 Where are FNNs used?

  • Structured / tabular data

  • Classification problems

  • Regression problems

🔹 Example:

Predicting house prices based on:

  • Area

  • Number of rooms

  • Location




2️⃣ Convolutional Neural Networks (CNN)

CNNs are designed to work with images and spatial data.

Instead of looking at the entire image at once, CNNs:

  • Extract edges

  • Detect shapes

  • Identify patterns

This makes them extremely powerful for vision tasks.

🔹 Where are CNNs used?

  • Image classification

  • Face recognition

  • Medical image analysis

  • Object detection

🔹 Example:

Detecting whether an image contains a cat or a dog.




3️⃣ Recurrent Neural Networks (RNN)

RNNs are designed for sequential data — where order matters.

Unlike FNNs, RNNs have a memory that remembers previous inputs.

🔹 Where are RNNs used?

  • Time series forecasting

  • Text generation

  • Speech recognition

🔹 Example:

Predicting tomorrow’s temperature based on previous days.




4️⃣ Long Short-Term Memory (LSTM)

LSTM is a special type of RNN designed to handle long-term dependencies.

Standard RNNs struggle when sequences are long.
LSTMs solve this using gates:

  • Forget gate

  • Input gate

  • Output gate

🔹 Where are LSTMs used?

  • Stock price prediction

  • Language modeling

  • Machine translation

🔹 Example:

Predicting stock trends using data from the past few months.





5️⃣ Gated Recurrent Unit (GRU)

GRU is a lighter and faster alternative to LSTM.

It combines gates and reduces complexity while still maintaining good performance.

🔹 Where are GRUs used?

  • Real-time NLP applications

  • Chat systems

  • Speech processing

🔹 Example:

Real-time chatbot response generation.




6️⃣ Autoencoders

Autoencoders are used for unsupervised learning.

They work in two parts:

  • Encoder → compresses data

  • Decoder → reconstructs data

The goal is to learn meaningful representations.

🔹 Where are Autoencoders used?

  • Anomaly detection

  • Noise removal

  • Data compression

🔹 Example:

Detecting fraudulent transactions by learning normal behavior.





7️⃣ Generative Adversarial Networks (GANs)

GANs consist of two neural networks:

  • Generator → creates fake data

  • Discriminator → checks if data is real or fake

They compete with each other — like a game.

🔹 Where are GANs used?

  • Image generation

  • Deepfakes

  • Art generation

🔹 Example:

Generating realistic human faces that don’t exist.




8️⃣ Transformer Models

Transformers are the foundation of modern NLP and LLMs.

They rely on:

  • Attention mechanism

  • Parallel processing

Transformers replaced RNNs for most NLP tasks.

🔹 Where are Transformers used?

  • Chatbots (ChatGPT)

  • Translation

  • Text summarization

🔹 Example:

Answering questions in natural language.




🧩 Summary Table

ModelBest For
FNNTabular data
CNNImages
RNNSequences
LSTMLong sequences
GRUFast sequential tasks
AutoencoderAnomaly detection
GANData generation
TransformerNLP & LLMs

🌱 Final Thoughts

Each deep learning model is designed for a specific type of problem.
Understanding why and when to use each architecture is far more important than memorizing names.

Deep Learning is not magic — it’s structured thinking implemented through neural networks.


🔗 You can link this blog to:


Monday, 15 December 2025

🌀 Unsupervised Learning: How Machines Discover Patterns on Their Own

After understanding Supervised Learning (where models learn using labeled data), the next big concept in Machine Learning is Unsupervised Learning.

This time, the story is different — there are no labels, no correct answers, and no teacher guiding the model.

The model is left with raw data and one goal:

👉 Find hidden patterns, groups, or structures automatically.

This capability is what makes unsupervised learning incredibly powerful in exploratory analysis, recommendations, anomaly detection, and customer segmentation.

Let’s break it down in the simplest way possible.


🌱 What Is Unsupervised Learning? 

Unsupervised learning is a machine learning method where a model learns patterns from unlabeled data.

There is:

  • No target variable

  • No outputs to predict

  • No “right answer” given

The model must discover structure purely from the input data.

Think of it like:
🔍 Exploring a new city without a map
🔍 Finding similarities naturally
🔍 Grouping things based on relationships




🎯 What Unsupervised Learning Tries to Do

Unsupervised algorithms try to discover:

✔ Patterns
✔ Groups (clusters)
✔ Similarities
✔ Outliers
✔ Structures
✔ Important features
✔ Density regions

Basically, they help us understand data when we don’t know what we are looking for yet.


🔍 Types of Unsupervised Learning

1️⃣ Clustering (Grouping Similar Items)

The algorithm groups data points based on similarity.

Examples:

  • Customer segmentation

  • Market segmentation

  • Grouping documents

  • Image grouping

  • Finding similar products

Popular Algorithms :

  • K-Means Clustering

  • Hierarchical Clustering

  • DBSCAN

  • Gaussian Mixture Models (GMM)

💡 K-Means groups customers with similar buying patterns.
💡 DBSCAN finds clusters with irregular shapes.




2️⃣ Dimensionality Reduction

Used when data has too many features.

These algorithms reduce the number of variables while keeping the important information.

Examples:

  • Visualizing high-dimensional data

  • Noise reduction

  • Preprocessing before ML models

  • Feature extraction

Popular Algorithms:

  • PCA (Principal Component Analysis)

  • t-SNE

  • UMAP

  • Autoencoders

💡 PCA is used heavily for simplifying datasets before training models.




3️⃣ Association Rule Learning

This finds relationships between items.

Examples:

  • Market Basket Analysis

  • “People who bought X also bought Y”

  • Amazon & Flipkart recommendations

Algorithms:

  • Apriori

  • ECLAT

  • FP-Growth

💡 If a customer buys bread, they often buy butter too.


4️⃣ Anomaly Detection

Identify unusual or rare patterns.

Examples:

  • Fraud detection

  • Network intrusion detection

  • Detecting manufacturing defects

  • Finding abnormal health data

Algorithms:

  • Isolation Forest

  • One-Class SVM

  • Local Outlier Factor (LOF)

💡 Used widely in cybersecurity and banking.


🧠 How Unsupervised Learning Works (Simple Steps)

Let’s take clustering as an example:

1️⃣ You give the model unlabeled data
2️⃣ It measures similarity between data points
3️⃣ It groups similar points together
4️⃣ It outputs cluster labels (Cluster 1, 2, 3…)
5️⃣ You interpret the pattern

There is no accuracy or F1-score, because there is no ground truth to compare with.

So evaluation is done using:

  • Silhouette Score

  • Davies-Bouldin Index

  • Cluster cohesion metrics


📘 Real-Life Examples You Already Use

Spotify / YouTube
Clusters songs/videos by listening behavior

Credit Card Fraud Detection
Detects unusual transactions

E-commerce Recommendations
“Similar items” come from clustering

Google Photos
Groups faces using unsupervised learning

Marketing Teams
Segment customers without labels

Healthcare
Cluster patients with similar symptoms


🧪 Simple Example (Easy to Visualize)

Imagine you have the following data:

CustomerAgeAnnual Spend
C122₹25,000
C224₹27,000
C346₹1,20,000
C448₹1,10,000

You run K-Means with k=2.

The model groups:

  • Young low-spending customers → Cluster 1

  • Older high-spending customers → Cluster 2

No labels needed.
The algorithm automatically discovers these patterns.


Thursday, 9 October 2025

☀️ From Words to Numbers: How Embeddings Give Meaning to Language

 Have you ever wondered how a computer understands words like “coffee,” “tea,” or “mug”?

Machines don’t understand words directly — they understand numbers.
So how can numbers capture meaning, context, and relationships between words?

That’s where Word Embeddings come in — the mathematical magic behind how machines “understand” language.
They’re the foundation of NLP (Natural Language Processing) and LLMs (Large Language Models) like ChatGPT.


🌐 What Are Word Embeddings?

Word embeddings are a way to represent words as vectors — lists of numbers that capture their meanings and relationships.

Instead of treating words as separate labels, embeddings place them into a continuous vector space where similar words appear closer together.

For example:

coffee → [0.8, 0.3, 0.6, 0.9] tea → [0.7, 0.2, 0.5, 0.8] keyboard → [0.1, 0.9, 0.4, 0.2]

Here, “coffee” and “tea” are closer in meaning — both are beverages — while “keyboard” is far away in vector space.




🧩 Why Do We Need Embeddings?

Before embeddings, computers used one-hot encoding — a system where each word was represented by a long vector with a single “1” and many “0”s.

That approach had two problems:

  • Huge, sparse vectors (very memory heavy)

  • No relationship between words (“coffee” and “tea” looked completely unrelated)

Word embeddings solved this by learning from context — the way words appear near each other.

“You shall know a word by the company it keeps.” — J.R. Firth

So if “coffee” often appears near “cup,” “brew,” and “morning,” it’s likely similar to “tea,” which also appears in similar contexts.


⚙️ How Are Word Embeddings Created?

Two main methods are used:

1. Count-Based Methods (like TF-IDF, Co-occurrence Matrix)

They analyze how often words appear together.
Good for finding statistical associations but not deeper meaning.

2. Prediction-Based Methods (like Word2Vec, GloVe)

They train neural networks to predict words from their context (or vice versa).
For example:

“I need a cup of ___” → likely “coffee” or “tea”.

These models learn that “coffee” and “tea” occur in similar contexts — so they must be semantically close.




🧮 Visualizing Word Relationships

In vector space, similar words form clusters.

WordClosest Words
coffeetea, latte, espresso
doctornurse, surgeon, hospital
sunmoon, light, solar

Embeddings can even show relationships using vector math!

For example:

doctor - hospital + school ≈ teacher

It means embeddings capture the role and context relationships between words.




📐 Measuring Similarity: Cosine Similarity

To check how similar two words are, we use Cosine Similarity, which measures the angle between two vectors.

Cosine Similarity=ABA×B\text{Cosine Similarity} = \frac{A \cdot B}{||A|| \times ||B||}

If:

  • 1 → words are very similar

  • 0 → unrelated

  • -1 → opposites

This helps models like chatbots or search systems find words or meanings that are close together.




🧠 Embeddings in Modern AI

Embeddings are now used not only for words but also for:

  • Sentences

  • Documents

  • Images

  • Even code!

In Large Language Models (LLMs), embeddings are the first step — converting text into numbers so neural networks can process meaning and context.

You can think of embeddings as the language of thought for AI.


🔗 Related Reads

📘 Understanding Natural Language Processing (NLP)
📗
Demystifying LLMs: How Large Language Models Work


🌟 Conclusion

Word embeddings transformed language from text into meaningful numbers.
They allow machines to understand relationships, similarities, and analogies, which power almost every AI application we use today — from Google Search to ChatGPT.

Every word has a number — but those numbers tell a story.

🔄 Why Did the Industry Shift from ETL to ELT?

Understanding the evolution of modern data pipelines. Imagine You're Moving to a New House... Suppose you're moving from Mumbai to B...