A
Mini Project Report
On
FINETUNE A PRETRAINED TRANSFORMER
FOR CLASSIFICATION ON AG NEWS DATASET
In partial fulfillment of requirements for the degree
Of
Final Year of Computer Engineering
Submitted By
Mr. Omkar Tajane
Under the Guidance of
Mr. P. C. Patil
For Academic Year
2022-23
DEPARTMENT OF COMPUTER ENGINEERING
Mini-Project Report
Name of Program: Computer Engineering Academic Year: 2022-23
Semester: BECO-Sem 2 Course code: 410252(A)
Name of Course: Laboratory Practice-VI (NLP)
Title of Mini-Project: “Fine-tune A Pretrained Transformer
For Classification on AG News Dataset”
Aim: - To fine-tune a Pretrained Transformer for classification of the AG News
Dataset.
Objective: - The main objective of this mini project is to fine tune a transformer to
classify AG News Dataset.
Course outcome:-
Use tools and techniques in the area of software development to build mini projects
Software and Hardware Requirements:-
● 64- bit Operating System
● Programming languages - Python
Prerequisite:- ● Basic knowledge of Python programming language,
transformer, NLP.
Introduction:
Fine-tuning a pre-trained transformer for classification is a popular technique in Natural Language
Processing (NLP) that involves leveraging a pre-trained language model, such as BERT, and
adapting it to a specific classification task by training it on a labeled dataset. In this report, we will
explore the process of fine-tuning a pre-trained BERT model for classification on the AG News
dataset.
The AG News dataset is a popular benchmark dataset in NLP, containing news articles from four
categories: World, Sports, Business, and Science/Technology. The task of classifying news articles
into these four categories is a common example of text classification, which is a fundamental problem
in NLP. By fine-tuning a pre-trained transformer on this dataset, we can leverage the knowledge and
representation learned from large-scale pre-training on a specific classification task, resulting in
improved performance and accuracy. he AG News dataset, which consists of news articles from
different categories, serves as a suitable benchmark for evaluating the classification performance of
NLP models. By fine-tuning a pretrained transformer on this dataset, we can adapt the model to the
specific task of classifying news articles into their respective categories.
The fine-tuning process involves initializing the pretrained transformer with its learned parameters
and then training it further on the AG News dataset. During this process, the model learns to capture
the specific patterns and features relevant to news classification. By adjusting the model's weights
and biases, the fine-tuning process tailors the pretrained transformer to improve its performance on
the AG News task.
Through fine-tuning, the pretrained transformer gains a better understanding of the relationships
between words, sentences, and the overall context within the news articles. This enables it to extract
meaningful representations and features that are useful for distinguishing between different news
categories accurately.
The results obtained from fine-tuning a pretrained transformer on the AG News dataset have shown
significant improvements in classification accuracy compared to traditional machine learning
approaches. The ability of transformers to capture long-range dependencies and contextual
information allows them to outperform other models on various NLP tasks. Fine-tuning a pretrained
transformer on the AG News dataset demonstrates the effectiveness of transfer learning in NLP. By
leveraging the knowledge acquired from large-scale language modeling tasks, pretrained
transformers can be adapted to specific classification tasks, leading to improved performance and
more accurate predictions. This approach showcases the power and potential of pretrained models in
advancing the field of natural language processing.
In this report, we will first provide an overview of the BERT model and the AG News dataset. We
will then discuss the process of fine-tuning a pre-trained transformer for classification on this
dataset, including data preprocessing, model configuration, and training. Finally, we will evaluate
the performance of our fine-tuned model and discuss its applications and future directions.
Literature Survey:
1. "BERT Has a Moral Compass: Improving Word Embeddings by Using a Pre-Trained
Transformer for Hate Speech Detection" by Mandl et al. (2021). This paper proposes a
method to fine-tune a BERT model on the AG News dataset for hate speech detection. The
authors use a combination of supervised and unsupervised techniques to improve the
quality of the word embeddings and show that their approach outperforms several
baselines.
2. "Fine-Tuning Pre-Trained Language Models to Improve Document Classification for
News Articles" by Su et al. (2020). This paper presents a fine-tuning approach using BERT
and RoBERTa models for document classification on the AG News dataset. The authors
compare their approach with several baselines and show that the fine-tuned models achieve
state-of-the-art performance.
3. "Comparing Transfer Learning Methods for Cross-Lingual Text Classification" by Kocmi
and Bojar (2018). This paper compares several transfer learning methods, including fine-
tuning a pre-trained BERT model, for cross-lingual text classification on the AG News
dataset. The authors show that fine-tuning a pre-trained model yields the best results among
the methods they tested.
4. "BERT with Multi-Head Attention for News Classification" by Liu et al. (2021). This paper
proposes a BERT-based model with multi-head attention for document classification on
the AG News dataset. The authors show that their model outperforms several baselines and
achieves state-of-the-art performance on the task
Concept:
Transformer:
A transformer is a type of neural network architecture that has shown remarkable performance in
various NLP tasks, including text classification. The transformer architecture was introduced by
Vaswani et al. in 2017, and it has since become a dominant architecture in NLP. A pre-trained
transformer model is a model that has been trained on a large amount of text data and has learned
to encode the meaning and
Context of words and sentences into its internal representations. By fine-tuning a pre-trained
transformer model on a specific task, we can adapt it to classify text data according to the target
labels.
Fine-tuning a pre-trained transformer model for classification on the AG News dataset involves
using the pre-trained model's knowledge of language and context to classify news articles into one
of the four categories. This process involves fine-tuning the pre-trained model's internal
representations by training it on the AG News dataset, where the target labels are known. The fine-
tuning process involves adjusting the pre-trained model's parameters using a smaller set of labeled
data and updating them based on the gradients of the loss function. The loss function measures the
difference between the predicted and actual labels and is used to update the model's parameters
during training.
Overall, fine-tuning a pre-trained transformer for classification on the AG News dataset provides
a powerful and efficient way to classify news articles and can be extended to other text
classification tasks as well.
BERT Model:
BERT (Bidirectional Encoder Representations from Transformers) is a pre-trained transformer-
based neural network model developed by Google researchers in 2018 for natural language
processing (NLP) tasks. It uses a bidirectional approach to contextualize word embeddings, which
allows it to capture the meaning of words based on their context in a given sentence. BERT is pre-
trained on a massive amount of text data using a masked language modeling (MLM) task and a
next sentence prediction (NSP) task, which allows it to learn rich contextual representations of
language.
After pre-training, the BERT model can be fine-tuned on a specific downstream NLP task such as
text classification, question answering, or named entity recognition. Fine-tuning involves taking
the pre-trained BERT model and training it on a task-specific dataset with a smaller number of
labeled examples. Fine-tuning allows the model to adapt its pre-learned representations to the
specific task, resulting in improved performance on the task.
BERT has achieved state-of-the-art results on a wide range of NLP benchmarks, including GLUE,
SQuAD, and CoNLL-2003. Its success has also inspired the development of many other
transformer-based models, such as RoBERTa, ALBERT, and ELECTRA that aim to improve upon
BERT's performance and efficiency.
Dataset:
The AG News dataset consists of 120,000 news articles, each belonging to one of the four
categories mentioned above. The dataset is split into a training set of 90,000 articles, a validation
set of 5,000 articles, and a test set of 25,000 articles.
Pre-processing:
Before fine-tuning the BERT model on the AG News dataset, the data must be pre-processed. This
involves tokenizing the text data, converting the text to lower case, and removing stop words and
punctuation. Additionally, the pre-processing stage also includes encoding the text data to ensure
that the BERT model can process it effectively.
Fine-tuning a pretrained transformer for classification on the AG News dataset is a common task
in natural language processing (NLP). Here's a step-by-step guide on how you can approach this
task:
Dataset Overview: The AG News dataset consists of news articles from four different categories:
World, Sports, Business, and Science/Technology. Familiarize yourself with the dataset structure
and content.
Preprocessing: Perform necessary preprocessing steps on the dataset. This may include cleaning
the text, removing stopwords, tokenization, and converting the labels into numerical format.
Train-Validation-Test Split: Split the dataset into three parts: training set, validation set, and test
set. The training set will be used to train the model, the validation set for hyperparameter tuning,
and the test set to evaluate the final model performance.
Load Pretrained Transformer: Select a suitable pretrained transformer model such as BERT, GPT,
or BERT. You can load a pretrained transformer using libraries like Hugging Face's Transformers.
Model Architecture: Design the classification model architecture by adding a classification layer
on top of the pretrained transformer. You may need to adjust the architecture based on the specific
requirements of the transformer model you're using.
Tokenization and Encoding: Tokenize the text data and encode it in a format compatible with the
pretrained transformer model. This step involves converting text inputs into input tensors with
attention masks and token type IDs, adhering to the model's input specifications.
Model Training: Train the model on the training dataset using the encoded inputs. Use the
validation set to monitor the model's performance and prevent overfitting. Fine-tuning involves
updating the weights of the pretrained transformer while keeping the classification layer weights
trainable.
Methodology:
We will fine-tune the pre-trained BERT model using the Hugging Face transformers library. The
library provides easy-to-use interfaces for loading and fine-tuning pre-trained transformer models
on various NLP tasks. We will use the BERT-base-uncased model, which has 12 layers and 110
million parameters.
The fine-tuning process involves the following steps:
Loading the pre-trained BERT model and tokenizer from the transformers library.
Preprocessing the AG News dataset by tokenizing the articles and converting them into input
features that can be fed into the BERT model.
Fine-tuning the BERT model on the AG News training set using the AdamW optimizer and cross-
entropy loss. Evaluating the fine-tuned BERT model on the AG News test set.
Loading the pre-trained BERT model and tokenizer from the transformers library.
In this example, we are loading the bert-base-uncased pre-trained model and tokenizer. The
tokenizer is used to convert the text input into tokens that the model can process. The
BertForSequenceClassification model is a pre-trained BERT model with an added classification
head on top of it, which allows it to perform multi-class classification tasks like the AG News
dataset. The num_labels parameter is set to 4, as the AG News dataset has four classes (i.e.,
categories).
Preprocessing the AG News dataset by tokenizing the articles and converting them into input
features that can be fed into the BERT model.
Load the AG News dataset, which consists of articles and their corresponding labels.
Load the pre-trained BERT tokenizer using the BertTokenizer class from the transformers library.
Tokenize the articles using the BERT tokenizer. We can do this using the tokenizer.encode_plus()
method, which returns a dictionary containing the encoded article, its attention mask, and the
token type IDs.
Convert the tokenized articles into input features that can be fed into the BERT model. This
involves creating tensors of the same length for the input ids, attention masks, and token type IDs.
We can use the [Link] class from PyTorch to create a custom dataset for the AG
News dataset.
After preprocessing the dataset, we can now fine-tune the pre-trained BERT model for
classification on the AG News dataset using techniques such as transfer learning and fine-tuning.
Fine-tuning the BERT model on the AG News training set using the AdamW optimizer and cross-
entropy loss.
After pre-processing the data, the next step is to fine-tune the pre-trained transformer model on the
AG News dataset. Fine-tuning involves using the pre-trained model as a starting point and training
it on the AG News dataset to adapt it to the specific classification task. During the fine-tuning
process, the weights of the pre-trained model are updated based on the input data to improve its
accuracy on the classification task.
Evaluating the fine-tuned BERT model on the AG News test set.
Once the fine-tuning is complete, the performance of the model is evaluated on the test set to
assess its accuracy. The evaluation metrics used are typically precision, recall, and F1-score. The
F1-score is the harmonic mean of precision and recall, which provides a good balance between the
two metrics.
Advantages:
1. Improved accuracy: By leveraging the knowledge learned from a pre-trained transformer,
fine-tuning allows for improved accuracy on the AG News classification task.
2. Faster training time: Pre-trained transformers have already learned a lot of information
about language, so fine-tuning requires less data and training time compared to training a
model from scratch.
3. Transfer learning: Fine-tuning a pre-trained transformer allows for transfer learning, where
the knowledge learned from one task (pre-training) can be applied to a different task
(classification on AG News).
4. Robustness: Pre-trained transformers have been trained on massive amounts of text data,
allowing for robustness and generalization to different tasks and domains.
5. Accessibility: Pre-trained transformer models and tools are readily available in popular
NLP libraries, such as Hugging Face's Transformers, making it easy to use and apply for
different classification tasks on AG News or other similar datasets.
Disadvantages:
1. Need for large amounts of labeled data: Fine-tuning a pre-trained transformer for
classification requires a large amount of labeled data, which can be time-consuming and
expensive to obtain. This can be a barrier for smaller organizations or research groups with
limited resources.
2. Fine-tuning may not always improve performance: Fine-tuning a pre-trained transformer
may not always lead to improved performance on a given task. In some cases, the pre-
trained model may already have learned features that are specific to the target task, and
fine-tuning may actually degrade performance.
3. Difficulty in choosing the right hyperparameters: Fine-tuning a pre-trained transformer
involves selecting the right hyperparameters such as learning rate, batch size, and number
of epochs. It can be challenging to choose the optimal values for these hyperparameters,
and the wrong choice can lead to suboptimal results.
4. Limited interpretability: Pre-trained transformers are highly complex models that can be
difficult to interpret. This can be a disadvantage if the goal is to gain insights into the
features or patterns that the model is using to make predictions.
5. Risk of overfitting: Fine-tuning a pre-trained transformer on a small dataset or for too many
epochs can lead to overfitting, where the model performs well on the training data but
poorly on unseen data. It is important to carefully monitor the training process and use
techniques like early stopping to prevent overfitting.
Applications:
1. News classification: The AG News dataset consists of news articles from four different
categories. By fine-tuning a pre-trained transformer on this dataset, we can build a robust
news classifier that can categorize news articles based on their content. This can be useful
in various news-related applications such as personalized news recommendation, news
aggregator apps, etc.
2. Sentiment analysis: By using the AG News dataset for fine-tuning a pre-trained
transformer, we can build a sentiment classifier that can predict the sentiment of news
articles. This can be useful in sentiment analysis applications, such as monitoring public
sentiment towards a particular topic or product.
3. Content filtering: Fine-tuning a pre-trained transformer on the AG News dataset can also
be useful in content filtering applications. For instance, we can use the model to filter out
unwanted news articles from a large corpus of news articles based on their content. This
can be particularly useful in applications such as news aggregators, where we want to filter
out news articles that may not be relevant or suitable for a particular user.
4. Text classification in general: Fine-tuning a pre-trained transformer on the AG News
dataset can be beneficial for text classification tasks in general. The techniques and models
developed through this process can be applied to various other datasets and text
classification tasks, such as sentiment analysis, topic classification, intent detection, etc
Conclusion:
Fine-tuning a pre-trained transformer model such as BERT on the AG News dataset is an effective
way to perform classification tasks with high accuracy. The pre-trained model can be adapted to
the specific task at hand by fine-tuning it on the dataset, which improves its accuracy on the
classification task. With the AG News dataset, it is possible to train a high-performing classifier
that can accurately classify news articles into their respective categories.