Fine Tuning in GenAI


Author’s Note: If you're curious about how AI like ChatGPT learns new things (without starting from scratch every time), this blog is for you. No fancy jargon—just a chill breakdown of fine-tuning, with diagrams, a step-by-step guide, and even a bit of code. Let's go!


🤖 What is Fine-Tuning?

Think of a big AI model like ChatGPT as a student who already knows a lot about everything—history, science, grammar, even how to rap.

But what if you want this AI to be extra good at something specific—like answering questions only about chemistry, or writing poems in your style?

👉 That’s where fine-tuning comes in.


🧩 Simple Analogy

Imagine you trained for the Olympics 🏃‍♀️. You’re good at all sports. But now your coach wants you to compete only in archery. You don’t need to relearn everything—you just need to focus on archery.

That’s fine-tuning. We take a pre-trained model and teach it more on a specific topic.


🖼️ Visual: How Fine-Tuning Works

  +----------------------------+
  |     Big Pretrained Model  |  →  Already knows a lot
  +----------------------------+
                |
                ↓
   +-----------------------------+
   |  Add Specific New Training  |  →  Like teaching only chemistry
   +-----------------------------+
                ↓
     +----------------------+
     |  Fine-Tuned Model    |  →  Better at your task!
     +----------------------+

🛠️ When Should You Fine-Tune?

  • You want your AI to speak in your brand's tone

  • You have special data (like customer support chats)

  • You need high accuracy in a niche field (like medical or legal)


🧭 Step-by-Step Guide: How to Fine-Tune

Let’s walk through the process of fine-tuning a small language model like DistilGPT2, a tiny version of GPT-2 from Hugging Face.


✅ Step 1: Install the Tools

pip install transformers datasets accelerate

✅ Step 2: Prepare Your Dataset

Fine-tuning needs examples. Let’s say we want to fine-tune the model to write like Shakespeare.

Save this as sample.json:

[
  {"text": "To be, or not to be, that is the question."},
  {"text": "All that glitters is not gold."},
  {"text": "A fool thinks himself to be wise..."}
]

✅ Step 3: Load and Format the Data

from datasets import load_dataset

dataset = load_dataset('json', data_files='sample.json')

✅ Step 4: Load the Model

from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "distilgpt2"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

✅ Step 5: Tokenize the Data

def tokenize_function(example):
    return tokenizer(example["text"], truncation=True)

tokenized_dataset = dataset.map(tokenize_function)

✅ Step 6: Train the Model

from transformers import Trainer, TrainingArguments

training_args = TrainingArguments(
    output_dir="./results",
    num_train_epochs=3,
    per_device_train_batch_size=2,
    logging_dir="./logs",
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_dataset["train"],
)

trainer.train()

🧪 Test the Model

input_text = "Love looks not with the eyes, but"
input_ids = tokenizer.encode(input_text, return_tensors="pt")

output = model.generate(input_ids, max_length=50)
print(tokenizer.decode(output[0], skip_special_tokens=True))

🎯 Final Thoughts

  • Fine-tuning helps GenAI models specialize

  • It’s like giving a smart student extra coaching

  • Tools like Hugging Face make it easy for anyone (even students!) to try it

0
Subscribe to my newsletter

Read articles from Akash Kumar Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Akash Kumar Yadav
Akash Kumar Yadav