🚀 Introduction to Generative AI (For Absolute Beginners)


🤖 What is Generative AI?
Generative AI is a type of Artificial Intelligence that can create new things—like text, images, music, or even videos—based on what it has learned.
Imagine giving a computer a sentence like:
"Once upon a time..."
And it completes the story:
"...there was a smart robot who learned how to write stories!"
That’s Generative AI. It doesn't just understand things—it creates things.
🧠 What Can Generative AI Do?
Generative AI can:
✍️ Write content (like ChatGPT)
🎨 Create pictures (like DALL·E)
🧑💻 Help write code (like GitHub Copilot)
🗣️ Make realistic voices (like ElevenLabs)
📚 Summarize or translate text
💬 Chat like a human
It’s like having a smart assistant that can do creative tasks with you!
🧩 What is GPT?
GPT stands for Generative Pre-trained Transformer.
Let’s break that down in simple words:
Generative: It can create stuff (like text).
Pre-trained: It has already learned from a huge amount of data (like books, articles, websites).
Transformer: This is just the type of brain the model uses. It helps it understand language better.
In short:
GPT is a smart AI model that can understand and generate human-like text.
⚙️ How Does It Work?
GPT works by guessing what comes next in a sentence.
For example, if you write:
“The sun is in the…”
The model might guess:
“sky.”
It does this one word (or letter) at a time, using patterns it learned during training.
🔄 Steps Inside GPT (How it Understands Text)
When you give a sentence to GPT, here’s what happens behind the scenes:
1️⃣ Tokenization
Before GPT can understand the text, it breaks it into smaller parts called tokens.
For example:
"Hello AI" → ["Hello", "AI"]
These tokens are then turned into numbers, because AI works with numbers.
🔍 Example in Code:
pythonCopyEditfrom transformers import GPT2Tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
text = "Hello AI"
tokens = tokenizer.tokenize(text)
token_ids = tokenizer.convert_tokens_to_ids(tokens)
print("Tokens:", tokens)
print("Token IDs:", token_ids)
2️⃣ Vector Embedding
Next, each token (word) is turned into a vector — a special list of numbers that helps the AI understand the meaning of the word.
It’s like turning "AI" into:
[0.23, 0.98, -0.11, ...]
These numbers help the AI understand that "AI" is related to words like "machine", "robot", etc.
🔍 Example:
pythonCopyEditfrom transformers import GPT2Model, GPT2Tokenizer
import torch
model = GPT2Model.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
inputs = tokenizer("AI is amazing", return_tensors="pt")
outputs = model(**inputs)
embeddings = outputs.last_hidden_state
print("Embeddings shape:", embeddings.shape)
3️⃣ Positional Encoding
The AI also needs to know the order of the words.
For example, "cat sat on mat" is different from "mat sat on cat" 🐱🧘♂️
So, the model adds position info to each word using math. This helps the model understand what comes first, second, third, etc.
You don’t have to do this manually—it’s done inside the model.
4️⃣ Self-Attention
Now comes the most powerful part: self-attention.
This allows the AI to look at all the words in the sentence and decide which ones are important for each word. ( this helps to decide which word to use if there are similar meaning words)
For example:
In "The cat sat on the mat",
While reading "mat", it might focus on "sat" and "cat" to understand the meaning.
It’s like the AI is asking:
"Which other words should I care about to understand this word?"
🔍 Example (Getting Attention Info):
pythonCopyEditfrom transformers import GPT2Tokenizer, GPT2Model
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2Model.from_pretrained("gpt2", output_attentions=True)
inputs = tokenizer("The cat sat on the mat", return_tensors="pt")
outputs = model(**inputs)
attentions = outputs.attentions
print("Number of attention layers:", len(attentions))
📚 Quick Summary
Step | What It Does | Example |
Tokenization | Breaks sentence into smaller parts | “Hello AI” → ["Hello", "AI"] |
Embedding | Converts words into numbers | "AI" → [0.23, 0.98, ...] |
Positional Encoding | Adds info about word order | “cat sat” ≠ “sat cat” |
Self-Attention | Finds important words in the sentence | "mat" → focuses on "sat", "cat" |
🏁 Final Thoughts
Generative AI may sound complex at first, but when broken down, it's all about:
Breaking down the text
Converting it into numbers
Understanding the meaning and order
Generating the next word
This is just the first step of your journey into the world of AI. In future articles, you can learn:
How to build apps using GPT
How to fine-tune a model
How to create your own AI assistant
Subscribe to my newsletter
Read articles from Aryan Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
