Fine-tuning BERT for text classification with KerasNLP

A guide to mastering text classification with BERT and KerasNLP

Fine-tuning BERT for text classification with KerasNLP


Text classification is a basic job in natural language processing (NLP) that is used in sentiment analysis, spam detection, and content categorization. Transformer-based models, like BERT (Bi-directional Encoder Representations from Transformers), have become popular recently because of their outstanding performance in different NLP tasks.

In this article, we'll explore how to implement text classification using BERT and the KerasNLP library, providing examples and code snippets to guide you through the process.

Understanding BERT

BERT, introduced by Google in 2018, is a pre-trained transformer-based model created for understanding natural language. Unlike traditional models that analyze text in one direction, BERT looks at context from both sides, which helps it capture complex relationships within sentences effectively.

BERT Architecture

BERT's architecture consists of layers of attention mechanisms and feedforward neural networks. It employs a transformer encoder stack, allowing it to learn contextualized representations of words. The model is pre-trained on large corpora, gaining a deep understanding of language nuances.

Tokenization with BERT

Before delving into text classification, it's crucial to understand tokenization, a process that breaks down text into smaller units, such as words or subwords. BERT utilizes WordPiece tokenization, which divides text into subword tokens, enhancing its ability to handle out-of-vocabulary words.

BERT-enhanced tokenization. (c) Batuhan Gundogdu

Setting Up the Environment

To get started with BERT-based text classification, you need to set up your Python environment. Ensure you have the required libraries installed:

pip install tensorflow
pip install keras-nlp
pip install transformers

These packages include TensorFlow, KerasNLP, and the Hugging Face Transformers library, which provides pre-trained BERT models.

Import the libraries

from keras_nlp import load_bert_model
import pandas as pd
from sklearn.model_selection import train_test_split

from keras.layers import Input, Dense
from keras.models import Model
from keras.optimizers import Adam

from keras_nlp import Tokenizer

from keras_nlp import load_bert_finetuned_model

Loading BERT Model with KerasNLP

KerasNLP simplifies the process of working with BERT models in Keras. Let's load a pre-trained BERT model using KerasNLP:

model_name = 'bert-base-uncased'
bert_model = load_bert_model(model_name)

Note: You can choose other variants based on your requirements, such as multilingual models or models fine-tuned for specific tasks.

Text Classification

Now, let's move on to text classification using BERT. For this example, we'll create a binary sentiment analysis model. Assume you have a dataset with labeled sentiments (positive or negative). First, load and preprocess the data

data = pd.read_csv('sentiment_data.csv')

train_data, test_data = train_test_split(data, test_size=0.2, random_state=42)

tokenizer = Tokenizer(model_name)
X_train = tokenizer.tokenize(train_data['text'].tolist())
X_test = tokenizer.tokenize(test_data['text'].tolist())

y_train = train_data['sentiment'].map({'negative': 0, 'positive': 1}).values
y_test = test_data['sentiment'].map({'negative': 0, 'positive': 1}).values

In this example, we assume that your dataset has a 'text' column containing the text data and a 'sentiment' column with labels ('negative' or 'positive'). Adjust the column names based on your dataset structure.

Building the BERT Text Classification Model

Now, let's build the BERT-based text classification model using Keras:

input_layer = Input(shape=(tokenizer.max_seq_length,), dtype='int32')

bert_output = bert_model(input_layer)

output_layer = Dense(1, activation='sigmoid')(bert_output['pooled_output'])

model = Model(inputs=input_layer, outputs=output_layer)

model.compile(optimizer=Adam(learning_rate=2e-5), loss='binary_crossentropy', metrics=['accuracy'])

This code snippet creates a simple neural network for sentiment analysis. The BERT output is fed into a dense layer with a sigmoid activation function for binary classification. Adjust the architecture based on your specific task and requirements.

Training the BERT Text Classification Model

Now, let's train the BERT text classification model using the prepared data:, y_train, epochs=3, batch_size=32, validation_split=0.1)

loss, accuracy = model.evaluate(X_test, y_test)
print(f'Test Loss: {loss}, Test Accuracy: {accuracy}')

This code snippet trains the model for three epochs with a batch size of 32 and validates on a 10% subset of the training data. After training, it evaluates the model on the test set, providing insights into its performance.

Fine-Tuning BERT for Specific Tasks

While the above example demonstrates a basic BERT text classification model, fine-tuning allows you to adapt BERT to specific tasks or domains. Fine-tuning involves training the pre-trained BERT model on a task-specific dataset, enabling it to learn task-specific features.

Loading a Fine-Tuned BERT Model

Assuming you have a fine-tuned BERT model saved, you can load it using KerasNLP:

fine_tuned_model_path = 'path/to/fine_tuned_model'
fine_tuned_model = load_bert_finetuned_model(fine_tuned_model_path)

Replace 'path/to/fine_tuned_model' with the actual path to your fine-tuned BERT model.

Fine-Tuning BERT for Text Classification

Let's explore how to fine-tune BERT for text classification using KerasNLP. Assume you have a task-specific dataset with text and corresponding labels:

task_data = pd.read_csv('task_specific_data.csv')

X_task = tokenizer.tokenize(task_data['text'].tolist())

y_task = task_data['label'].values

Now, fine-tune the BERT model on your task-specific dataset:, y_task, epochs=5, batch_size=16, validation_split=0.1)'path/to/save/fine_tuned_model')

This code snippet fine-tunes the BERT model on the task-specific dataset for five epochs with a batch size of 16, validating on a 10% subset. After fine-tuning, it saves the model for future use.


In this article, we explored text classification using BERT and KerasNLP. We explained the fundamentals of BERT, prepared the environment, loaded a pre-trained BERT model, and created a basic text classification model. Furthermore, we talked about fine-tuning BERT for particular tasks, offering code snippets and examples to help you along the way.

Implementing text classification with BERT offers numerous opportunities for NLP applications. Whether you're focusing on sentiment analysis, spam detection, or any classification task, using BERT can greatly improve the accuracy and reliability of your models. As NLP progresses, keeping abreast of the newest developments and integrating them into your projects will keep you ahead in this dynamic and thrilling field.