AI Questions & Answers Logo
AI Questions & Answers Part of the Q&A Network
Q&A Logo

How can I fine-tune a pre-trained transformer model for sentiment analysis?

Asked on Oct 01, 2025

Answer

Fine-tuning a pre-trained transformer model for sentiment analysis involves adapting the model to a specific sentiment classification task using a labeled dataset. This process typically requires a smaller dataset and less computational power compared to training a model from scratch.
<!-- BEGIN COPY / PASTE -->
    from transformers import BertTokenizer, BertForSequenceClassification, Trainer, TrainingArguments
    from datasets import load_dataset

    # Load pre-trained model and tokenizer
    model_name = "bert-base-uncased"
    tokenizer = BertTokenizer.from_pretrained(model_name)
    model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)

    # Load and preprocess dataset
    dataset = load_dataset("imdb")
    def tokenize_function(examples):
        return tokenizer(examples["text"], padding="max_length", truncation=True)

    tokenized_datasets = dataset.map(tokenize_function, batched=True)

    # Define training arguments
    training_args = TrainingArguments(
        output_dir="./results",
        evaluation_strategy="epoch",
        per_device_train_batch_size=8,
        per_device_eval_batch_size=8,
        num_train_epochs=3,
        weight_decay=0.01,
    )

    # Initialize Trainer
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=tokenized_datasets["train"],
        eval_dataset=tokenized_datasets["test"],
    )

    # Fine-tune the model
    trainer.train()
    <!-- END COPY / PASTE -->


Additional Comment:
  • Start by selecting a pre-trained transformer model, such as BERT, which is well-suited for text classification tasks.
  • Use a tokenizer to preprocess your text data, ensuring it matches the input format expected by the model.
  • Load a sentiment analysis dataset, such as IMDb, and split it into training and evaluation sets.
  • Set up training arguments, specifying parameters like batch size, number of epochs, and weight decay for regularization.
  • Use the Trainer class from the Transformers library to handle the training loop and evaluation.
  • Fine-tuning adjusts the model's weights to better predict sentiment labels based on your specific dataset.
✅ Answered with AI best practices.

← Back to All Questions
The Q&A Network