VOOZH about

URL: https://www.geeksforgeeks.org/deep-learning/hate-speech-detection-using-deep-learning/

⇱ Hate Speech Detection using Deep Learning - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Hate Speech Detection using Deep Learning

Last Updated : 23 Jul, 2025

There must be times when you have come across some social media post whose main aim is to spread hate and controversies or use abusive language on social media platforms. As the post consists of textual information to filter out such Hate Speeches NLP comes in handy. This is one of the main applications of NLP which is known as Sentence Classification tasks.

In this article we’ll walk through a stepwise implementation of building an NLP-based sequence classification model to classify tweets as Hate Speech, Offensive Language or Neutral .

Step 1: Importing Required Libraries

Before we begin let’s import the necessary libraries for data processing, model building and visualization.

Step 2: Loading the Dataset

We’ll use the Hate Speech Dataset which contains labeled tweets classified into three categories:

  • 0 - Hate Speech : Content explicitly targeting individuals or groups with harmful intent.
  • 1 - Offensive Language : Content containing offensive language but not necessarily hate speech.
  • 2 - Neither : Neutral content without any offensive or hateful intent.

The dataset consists of 19,826 rows and 2 columns : tweet (textual content) and class (label). Let’s load the dataset and explore its structure. You can download the dataset from here.

Output:

πŸ‘ Screenshot-2025-03-21-120148
First Five rows of the dataset

To check how many such tweets data we have let's print the shape of the data frame.

Output:

(24783, 2)

Although there are only two columns in this dataset let's check the info about their columns.

Output:

πŸ‘ Screenshot-2025-03-21-120547
Info about the dataset

The shape of the data frame and the number of non-null values are the same hence we can say that there are no null values in the dataset.

Output:

πŸ‘ Image

Here the three labels are as follows:

  • 0 - Hate Speech
  • 1 - Offensive Language
  • 2 - Neither

Step 3: Balancing the Dataset

The dataset is imbalanced so we balance it using a combination of upsampling and downsampling.

Output:

πŸ‘ Image

Step 4: Text Preprocessing

Textual data is highly unstructured and need attention on many aspects like:

  • Stopwords Removal
  • Punctuations Removal
  • Stemming or Lemmatization

Although removing data means loss of information but we need to do this to make the data perfect to feed into a machine learning model.

Output:

πŸ‘ Dataset after removal of punctuation's
Dataset after removal of punctuation's

The below function is a helper function that will help us to remove the stop words and Lemmatize the important words.

Word cloud is a text visualization tool that help's us to get insights into the most frequent words present in the corpus of the data.

Output:

πŸ‘ Image

Step 5: Tokenization and Padding

In this step we convert text data into numerical sequences and pad them to a fixed length

Step 6: Build the Model

We will implement a Sequential model like LSTM which will contain the following parts:

  • Embedding Layers to learn a featured vector representations of the input vectors.
  • Bidirectional LSTM layer to identify useful patterns in the sequence.
  • We have included BatchNormalization layers to enable stable and fast training and a Dropout layer before the final layer to avoid any possibility of overfitting.

The final layer is the output layer which outputs soft probabilities for the three classes. 

While compiling a model we provide these three essential parameters:

  • optimizer - This is the method that helps to optimize the cost function by using gradient descent.
  • loss - The loss function by which we monitor whether the model is improving with training or not.
  • metrics - This helps to evaluate the model by predicting the training and the validation data.

Output:

πŸ‘ Model
Model Training

Step 7: Training the Model

Train the model using callbacks like EarlyStopping and ReduceLROnPlateau.

Let's now train the model:

Output:

πŸ‘ Training progress

Step 8: Evaluating the Model

Visualize the training progress and evaluate the model’s performance.

Output:

πŸ‘ Graph of loss and accuracy epoch by epoch

Test Accuracy

Output:

75/75 ━━━━━━━━━━━━━━━━━━━━ 1s 12ms/step - accuracy: 0.9182 - loss: 0.446
Validation Accuracy: 0.91

The trained model achieves 90% accuracy on the validation set, demonstrating the effectiveness of deep learning techniques like LSTM for hate speech detection. While the model shows some overfitting, regularization techniques can be applied to improve generalization.

Get the Complete Notebook

Notebook: click here.

Comment