Open In App

Supervised Fine-Tuning (SFT) for LLMs

Last Updated : 29 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Supervised Fine-Tuning (SFT) is a process of taking a pre-trained language model and further training them on a smaller, task-specific dataset with labeled examples. Its goal is to adjust weights of pre-trained model so that it performs better on our specific task without losing its general knowledge acquired during pre-training.

For example, if you want a Large Language Model to classify emails into "spam" or "not spam" you would provide it a dataset containing email texts along with their corresponding labels. Then model learns to map input sequences to correct outputs based on this dataset.

How Does Supervised Fine-Tuning Work?

The process of SFT typically follows these steps:

prompting
SFT Working

1. Pre-training

LLM is initially trained on a large corpus of unlabeled text using masked language modeling like predicting missing words in sentences. This helps the model develop a broad understanding of language syntax, semantics and context.

2. Task-Specific Dataset Preparation

A smaller dataset relevant to the target task is created. This dataset consists input-output pairs where each input is associated with a label or response. For example, in question-answering tasks the input could be a question and the output would be the correct answer.

3. Fine-Tuning

Pre-trained model is further trained on task-specific dataset using supervised learning. During this process model’s parameters are updated to minimize the difference between its predictions and true labels. Techniques like gradient descent are commonly used for optimization.

4. Evaluation

After fine-tuning the model is evaluated on a validation set to assess its performance on target task. If required hyperparameters are tuned or additional training iterations are conducted.

5. Deployment

Once the model achieves satisfactory results, it can be deployed for real-world use cases, such as customer support chatbots, content generation tools or medical diagnosis systems.

What Does "Supervised" Mean in SFT?

The term "supervised" refers to the use of labeled training data to guide the fine-tuning process. In SFT the model learns to map specific inputs to desired outputs by minimizing prediction errors on a labeled dataset. For example in a customer support system without SFT model can work like this:

Supervised-Fine-Tuning-
Model Working Without SFT

We can use Labeled Data for each training example like a text prompt and a corresponding label or target output such as a correct answer or classification. Model adjusts its parameters based on explicit feedback from the labeled data ensuring it aligns with task-specific objectives. After SFT our model work like this:

supervised_fine_tuning_sft_for_llms
Model Working with SFT

WE can see that the model learns to respond more effectively to prompts or questions and now can be used for task specific work or domain in customer support system.

SFT vs. General Fine-Tuning

While SFT is a type of fine-tuning not all fine-tuning is "supervised." Here’s how SFT differs from broader fine-tuning approaches:


Aspect


Supervised Fine Tuning (SFT)


General Fine Tuning


Data Requirements


Labeled input-output pairs.


Unlabeled data, rewards or indirect feedback.


Objective


Task-specific performance.


General improvement or alignment.


Techniques


Classification, translation, summarization.


RLHF, domain adaptation, unsupervised tuning.


Computational Cost


Lower with PEFT methods.


Higher like RLHF requires training reward models.


Use Case


Well-defined tasks with labeled data.


Alignment, open-ended generation, data scarcity.

Implementing SFT in Python

Let’s break down the steps to fine-tune a pre-trained model for a sentiment analysis task using Python and Hugging Face’s Transformers library.

1. Importing Libraries

  • datasets: Provides easy access to a wide range of ready-to-use datasets from Hugging Face.
  • transformers: A library by Hugging Face for working with pre-trained NLP models.
Python
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import Trainer, TrainingArguments

2. Choose a Pre-trained Model

Select a model suited to your task. For text classification we are using a BERT model here.

  • AutoTokenizer.from_pretrained: Loads the tokenizer associated with the BERT model.
  • AutoModelForSequenceClassification.from_pretrained: Loads BERT with a classification head for binary output (num_labels=2).
Python
model_name = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)

Output:

Screenshot-2025-03-23-182638
Loading BERT Model.

3. Prepare Your Dataset

Use a labeled dataset here we will be using IMDb reviews for sentiment analysis.

  • load_dataset("imdb"): Loads the IMDb movie reviews dataset with labels (positive/negative).
  • preprocess_function: Uses the tokenizer to convert raw text into token IDs with padding and truncation.
  • dataset.map: Applies the preprocessing function to the full dataset in batches.
Python
dataset = load_dataset("imdb")
def preprocess_function(examples):
    return tokenizer(examples["text"], truncation=True, padding=True)
tokenized_dataset = dataset.map(preprocess_function, batched=True)

Output:

Screenshot-2025-03-23-182821
Dataset

4. Fine-Tuning the Model

Train the model on your task-specific data. Use a learning rate scheduler and GPU acceleration for efficiency.

  • TrainingArguments(): Defines how the model will be trained including output location, evaluation strategy, learning rate, batch size and number of epochs.
  • Trainer: A wrapper that handles the training and evaluation process.
  • trainer.train(): Starts the fine-tuning process on the training dataset.
Python
training_args = TrainingArguments(
    output_dir="./results",
    evaluation_strategy="epoch",
    learning_rate=2e-5, 
    per_device_train_batch_size=16,
    num_train_epochs=3,
)

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

Output:

Screenshot-2025-03-23-211306
Fine Tuning Model

5. Evaluating Model

Checking performance of model on a validation set.

Python
results = trainer.evaluate()
print(f"Validation Accuracy: {results['eval_accuracy']}")

Output:

Validation Accuracy: 91.02%

Use Cases of Supervised Fine-Tuning

  1. Text Classification: Fine-tune models like BERT on labeled product reviews to perform sentiment analysis, spam detection or topic classification.
  2. Named Entity Recognition (NER): Train models like RoBERTa on annotated datasets to extract names, dates and locations—helpful in document summarization and information retrieval.
  3. Machine Translation: Use models like T5 with bilingual corpora to improve translation quality for specific language pairs or industry domains.
  4. Question Answering: Fine-tune models like BERT using datasets such as SQuAD to build systems that can accurately answer complex user questions based on given text.
  5. Domain-Specific Applications: Apply SFT to fields like law and medicine by training on domain-specific documents to create specialized, high-performing models.

Advantages of Supervised Fine-Tuning

  1. Improved Task-Specific Performance: Since pre-trained models have already captured general patterns from large datasets fine-tuning helps them perform better on specific tasks with minimal effort.
  2. Flexibility Across Tasks and Domains: SFT is applicable to a wide range of NLP tasks such as text classification, NER, machine translation and question answering system across domains like healthcare, legal and finance.
  3. Faster Development and Deployment: Using pre-trained models speeds up development cycles making SFT ideal for rapid prototyping and quicker deployment of real-world solutions.

Challenges of Supervised Fine-Tuning

  1. Risk of Overfitting: Fine-tuning on small datasets can cause the model to memorize it rather than generalizing. Techniques like dropout, early stopping and regularization can be used to mitigate this.
  2. Dynamic Forgetting: The model might lose general knowledge from pre-training especially if the fine-tuning data is very different. Gradually fine-tuning layers helps avoid this issue.
  3. Importance of Label Quality: The effectiveness of fine-tuning heavily depends on clean, accurate and relevant labeled data. Poor-quality labels can severely effect its performance.
  4. Computational Requirements: While more efficient than training from scratch fine-tuning large models like T5 or GPT-3 still requires significant GPU resources especially in production environments.

Supervised Fine-Tuning is widely used in modern AI development enabling rapid adaptation of pre-trained models to specialized tasks. By following best practices like using careful parameter, data preparation and iterative testing we can build a high-performing models even with limited resources.


Next Article

Similar Reads

  翻译: