The Wayback Machine - https://web.archive.org/web/20240930213243/https://www.geeksforgeeks.org/emotion-detection-using-bidirectional-lstm/
Open In App

Emotion Detection using Bidirectional LSTM

Last Updated : 11 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Emotion Detection is one of the hottest topics in research nowadays. Emotion-sensing technology can facilitate communication between machines and humans. It will also help to improve the decision-making process. Many Machine Learning Models have been proposed to recognize emotions from the text. But, in this article, our focus is on the Bidirectional LSTM Model. Bidirectional LSTMs in short BiLSTM are an addition to regular LSTMs which are used to enhance the performance of the model on sequence classification problems. BiLSTMs use two LSTMs to train on sequential input. The first LSTM is used on the input sequence as it is. The second LSTM is used on a reversed representation of the input sequence. It helps in supplementing additional context and makes our model fast.

The dataset we have used is ISEAR (The International Survey on Emotion Antecedents and Reactions). Here, is a glimpse of the dataset.

ISEAR dataset contains 7652 sentences. It has a total of seven sentiments- Joy, Fear, Anger, Sadness, Guilt, Shame, and Disgust.  

Let’s go step by step in making the model which will predict emotion.

Step 1: Importing the required libraries

Python3




# Importing the required libraries
import keras
import numpy as np
from keras.models import Sequential,Model
from keras.layers import Dense,Bidirectional
from nltk.tokenize import word_tokenize,sent_tokenize
from keras.layers import *
from sklearn.model_selection import cross_val_score
import nltk
import pandas as pd
nltk.download('punkt')


Step 2: Load the dataset

The next step is to load the dataset from our machine and preprocess it. In the dataset, there are some rows that contain -‘No response’. This sentence is completely useless for us. So, we will drop such rows.

To get the dataset and code, click here.

Python3




df=pd.read_csv('isear.csv',header=None)
# The isear.csv contains rows with value 'No response'
# We need to remove such rows
df.drop(df[df[1] == '[ No response.]'].index, inplace = True)
print(df.head())


Output:

         0                                                  1
0      joy  [ On days when I feel close to my partner and ...
1     fear  Every time I imagine that someone I love or I ...
2    anger  When I had been obviously unjustly treated and...
3  sadness  When I think about the short time that we live...
4  disgust  At a gathering I found myself involuntarily si...

Step 3:  Tokeization

Apply a word tokenizer to convert each sentence into a list of words. Example: If there is a sentence- ‘I am happy’. Afterward tokenizing it will get converted into a list [‘I’,’am’, ‘happy’].

Python3




# The feel_arr will store all the sentences
# i.e feel_arr is the list of all sentences
feel_arr=df[1]
 
# Each  sentence in feel_arr is tokenized by the help of work tokenizer.
# If I have a sentence - 'I am happy'.
# After word tokenizing it will convert into- ['I','am','happy']
feel_arr = [word_tokenize(sent) for sent in feel_arr]
print(feel_arr[0])


Output:

['[', 'On', 'days', 'when', 'I', 'feel', 'close', 'to', 'my', 'partner',
 'and', 'other', 'friends', '.', 'When', 'I', 'feel', 'at', 'peace', 'with',
  'myself', 'and', 'also', 'experience', 'a', 'close', 'contact', 'with', 
  'people', 'whom', 'I', 'regard', 'greatly', '.', ']']

Step 4: Padding

The length of each sentence is different. To pass it through the model, the length of each sentence should be equal. By visualizing the dataset, we can see that the length of the sentence in the dataset is not greater than 100 words. So, now we will convert every sentence to 100 words. For this, we will take the help of padding.

Python3




# Defined a function padd in which each sentence length
# is fixed to 100.
# If length is less than 100 , then
# the word- '<padd>' is append
def padd(arr):
    for i in range(100-len(arr)):
        arr.append('<pad>')
    return arr[:100]
   
# call the padd function for each sentence in feel_arr
for i in range(len(feel_arr)):
    feel_arr[i]=padd(feel_arr[i])
 
print(feel_arr[0])


Output:

['[', 'On', 'days', 'when', 'I', 'feel', 'close', 'to', 'my', 'partner',
 'and', 'other', 'friends', '.', 'When', 'I', 'feel', 'at', 'peace', 
 'with', 'myself', 'and', 'also', 'experience', 'a', 'close', 'contact',
  'with', 'people', 'whom', 'I', 'regard', 'greatly', '.', ']', '<pad>', 
  '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', 
  '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', 
  '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', 
  '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', 
  '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>',
   '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', 
   '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>',
    '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>', '<pad>']

Step 5: Word embedding using the glove 

Now, each word needs to be embedded in some numeric representation, as the model understands only numeric digits. So, for this, we have downloaded a predefined glove vector of 50 dimensions from the internet. This vector is used for the purpose of word embedding. Each word is represented in a vector of 50 dimensions. 

The glove vector contains almost all words in the English dictionary.

The first word of each row is the character that is to be embedded. And from the column to the last column, there is the numeric representation of that character in a 50d vector form. 

Create embeddings_index dictionary of words and their corresponding index

Python3




# Glove vector contains a 50 dimensional vector corresponding
# to each word in dictionary.
vocab_f = 'glove.6B.50d.txt'
 
# embeddings_index is a dictionary which contains the mapping of
# word with its corresponding 50d vector.
embeddings_index = {}
with open(vocab_f, encoding='utf8') as f:
    for line in f:
        # splitting each line of the glove.6B.50d in a list of
        # items- in which the first element is the word to be embedded,
        # and from second to the end of line contains the 50d vector.
        values = line.rstrip().rsplit(' ')
        word = values[0]
        coefs = np.asarray(values[1:], dtype='float32')
        embeddings_index[word] = coefs
         
# the embedding index of word 'happy'
embeddings_index['happy']


Output:

array([ 0.092086,  0.2571  , -0.58693 , -0.37029 ,  1.0828  , -0.55466 ,
       -0.78142 ,  0.58696 , -0.58714 ,  0.46318 , -0.11267 ,  0.2606  ,
       -0.26928 , -0.072466,  1.247   ,  0.30571 ,  0.56731 ,  0.30509 ,
       -0.050312, -0.64443 , -0.54513 ,  0.86429 ,  0.20914 ,  0.56334 ,
        1.1228  , -1.0516  , -0.78105 ,  0.29656 ,  0.7261  , -0.61392 ,
        2.4225  ,  1.0142  , -0.17753 ,  0.4147  , -0.12966 , -0.47064 ,
        0.3807  ,  0.16309 , -0.323   , -0.77899 , -0.42473 , -0.30826 ,
       -0.42242 ,  0.055069,  0.38267 ,  0.037415, -0.4302  , -0.39442 ,
        0.10511 ,  0.87286 ], dtype=float32)

 Map each word of our dataset for their corresponding embedding vector

Now, each word of the dataset should be embedded in 50 dimensions vector with the help of the dictionary form above.

Python3




# Embedding each word of the feel_arr
embedded_feel_arr = []
for each_sentence in feel_arr:
    embedded_feel_arr.append([])
    for word in each_sentence:
        if word.lower() in embeddings_index:
            embedded_feel_arr[-1].append(embeddings_index[word.lower()])
        else:
            # if the word to be embedded is '<padd>' append 0 fifty times
            embedded_feel_arr[-1].append([0]*50)
             
print(embedded_feel_arr[0][0])


Output:

[-0.61201   0.98226   0.11539   0.014623  0.23873  -0.067035  0.30632
 -0.64742  -0.38517  -0.03691   0.094788  0.57631  -0.091557 -0.54825
  0.25255  -0.14759   0.13023   0.21658  -0.30623   0.30028  -0.23471
 -0.17927   0.9518    0.54258   0.31172  -0.51038  -0.65223  -0.48858
  0.13486  -0.40132   2.493    -0.38777  -0.26456  -0.49414  -0.3871
 -0.20983   0.82941  -0.46253   0.39549   0.014881  0.79485  -0.79958
 -0.16243   0.013862 -0.53536   0.52536   0.019818 -0.16353   0.30649
  0.81745 ]

Here, in the above example, the dictionary formed i.e embeddings_index contains the word and its corresponding 50d vector, 

Step 6: One Hot encoding for the target variables and split train and test dataset

Now, we are done with all the preprocessing parts, and now we need to perform the following operations:

  • Do one-hot encoding of each emotion.
  • Split the dataset into train and test sets.

Python3




#Converting x into numpy-array
X=np.array(embedded_feel_arr)
 
# Perform one-hot encoding on df[0] i.e emotion
enc = OneHotEncoder(handle_unknown='ignore')
Y = enc.fit_transform(np.array(df[0]).reshape(-1,1)).toarray()
 
# Split into train and test
X_train, X_test, Y_train, Y_test = train_test_split(X, Y,
                                                    test_size=0.2,
                                                    random_state=42)


 Step 7: Build the model

Python3




class BiLSTMModel:
    def __init__(self):
        self.model = Sequential()
        self.model.add(Bidirectional(LSTM(100, input_shape=(100, 50))))
        self.model.add(Dropout(0.2))
        self.model.add(Dense(7, activation='softmax'))
        self.model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
 
    def fit(self, X, Y, epochs, batch_size):
        self.model.fit(X, Y, epochs=epochs, batch_size=batch_size)
 
    def evaluate(self, X, Y, batch_size):
        return self.model.evaluate(X, Y, batch_size=batch_size)
 
    def predict(self, X):
        return self.model.predict(X)


Train the model

Python3




# create an instance of the BiLSTMModel class
model = BiLSTMModel()
 
# fit the model on the input and target data
model.fit(X_train,Y_train, epochs=20, batch_size=64)


Output:

Epoch 1/20
95/95 [==============================] - 13s 99ms/step - loss: 1.8433 - accuracy: 0.2545
Epoch 2/20
95/95 [==============================] - 11s 118ms/step - loss: 1.6367 - accuracy: 0.3751
Epoch 3/20
95/95 [==============================] - 10s 103ms/step - loss: 1.5475 - accuracy: 0.4102
Epoch 4/20
95/95 [==============================] - 9s 98ms/step - loss: 1.4917 - accuracy: 0.4404
Epoch 5/20
95/95 [==============================] - 11s 112ms/step - loss: 1.4453 - accuracy: 0.4531
Epoch 6/20
95/95 [==============================] - 13s 133ms/step - loss: 1.4109 - accuracy: 0.4777
Epoch 7/20
95/95 [==============================] - 12s 129ms/step - loss: 1.3521 - accuracy: 0.4934
Epoch 8/20
95/95 [==============================] - 10s 100ms/step - loss: 1.2956 - accuracy: 0.5262
Epoch 9/20
95/95 [==============================] - 11s 113ms/step - loss: 1.2651 - accuracy: 0.5360
Epoch 10/20
95/95 [==============================] - 11s 110ms/step - loss: 1.2126 - accuracy: 0.5602
Epoch 11/20
95/95 [==============================] - 10s 101ms/step - loss: 1.1970 - accuracy: 0.5719
Epoch 12/20
95/95 [==============================] - 10s 105ms/step - loss: 1.1226 - accuracy: 0.5942
Epoch 13/20
95/95 [==============================] - 9s 91ms/step - loss: 1.0961 - accuracy: 0.6084
Epoch 14/20
95/95 [==============================] - 8s 88ms/step - loss: 1.0746 - accuracy: 0.6168
Epoch 15/20
95/95 [==============================] - 9s 97ms/step - loss: 1.0081 - accuracy: 0.6376
Epoch 16/20
95/95 [==============================] - 10s 102ms/step - loss: 0.9750 - accuracy: 0.6502
Epoch 17/20
95/95 [==============================] - 9s 97ms/step - loss: 0.9394 - accuracy: 0.6683
Epoch 18/20
95/95 [==============================] - 10s 103ms/step - loss: 0.9235 - accuracy: 0.6693
Epoch 19/20
95/95 [==============================] - 11s 112ms/step - loss: 0.8728 - accuracy: 0.6936
Epoch 20/20
95/95 [==============================] - 10s 102ms/step - loss: 0.8256 - accuracy: 0.7079

Python3




# print the summary of the model
model.model.summary()


Output:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 bidirectional (Bidirectiona  (None, 200)              120800    
 l)                                                              
                                                                 
 dropout (Dropout)           (None, 200)               0         
                                                                 
 dense (Dense)               (None, 7)                 1407      
                                                                 
=================================================================
Total params: 122,207
Trainable params: 122,207
Non-trainable params: 0

This is the diagram of the proposed model :

Here, the dimension of input is 100 X 50 where 100 is the number of words in each input sentence of the dataset and 50 represents the mapping of each word in a 50d vector.

The output of Bidirectional(LSTM) is 200 because above we have defined the dimensionality of output space to be 100. As it is a BiLSTM model, so dimensionality will be 100*2 =200, as a BiLSTM contains two LSTM layers- one forward and the other backward.

After this dropout layer is added to prevent overfitting. And at last dense layer is applied to convert the 200 output sequences to 7, as we have only 7 emotions, so the output should be of seven dimensions only.

Testing the model

Python3




#Testing the model
model.evaluate(X_test,Y_test, batch_size=1)


Output:

1515/1515 [==============================] - 11s 7ms/step - loss: 1.4335 - accuracy: 0.5109
[1.4335393905639648, 0.5108910799026489]

The accuracy of the model is 51%



Similar Reads

Difference Between a Bidirectional LSTM and an LSTM
Long Short-Term Memory (LSTM) networks are capable of learning long-term dependencies. They were introduced to address the vanishing and exploding gradient problems that standard RNNs face. While LSTMs are powerful in many sequence-based tasks, a variation called Bidirectional LSTM (BiLSTM) enhances this capability. Let's explore the differences be
3 min read
AWD-LSTM : Unraveling the Secrets of DropConnect in LSTM
AWD LSTM is a machine learning technique that helps in understanding patterns over time, like predicting what comes next in a sequence of data. Let's explore AWD LSTM in the article. What is AWD-LSTM?AWD-LSTM stands for ASGD Weight-Dropped Long Short-Term Memory, represents a significant advancement in the realm of recurrent neural networks (RNNs).
7 min read
Bidirectional LSTM in NLP
In this article, we will first discuss bidirectional LSTMs and their architecture. We will then look into the implementation of a review system using Bidirectional LSTM. Finally, we will conclude this article while discussing the applications of bidirectional LSTM. Bidirectional LSTM (BiLSTM)Bidirectional LSTM or BiLSTM is a term used for a sequenc
8 min read
Emotion Detection Using Convolutional Neural Networks (CNNs)
Emotion detection, also known as facial emotion recognition, is a fascinating field within the realm of artificial intelligence and computer vision. It involves the identification and interpretation of human emotions from facial expressions. Accurate emotion detection has numerous practical applications, including human-computer interaction, custom
15+ min read
Emotion Based Music Player - Python Project
In this article, we will be discussing how can we recommend music based on expressions or say dominant expressions on someone's face. This is a basic project in which we will be using OpenCV, Matplotlib, DeepFace, and Spotify API. Import Packages and ModulesOpenCV: OpenCV is a Python open-source package used for computer vision, machine learning, a
6 min read
LSTM Based Poetry Generation Using NLP in Python
One of the major tasks that one aims to accomplish in Conversational AI is Natural Language Generation (NLG) which refers to employing models for the generation of natural language. In this article, we will get our hands on NLG by building an LSTM-based poetry generator. Note: The readers of this article are expected to be familiar with LSTM. In or
7 min read
Long Short-Term Memory (LSTM) using R
Implementing Long Short-Term Memory (LSTM) networks in R involves using libraries that support deep learning frameworks like TensorFlow or Keras. These frameworks provide high-level interfaces for efficiently building and training LSTM models. Here's a step-by-step guide to implementing LSTM using R Programming Language. What are LSTM Networks?LSTM
5 min read
Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
Edge detection is one of the fundamental image-processing tasks used in various Computer Vision tasks to identify the boundary or sharp changes in the pixel intensity. It plays a crucial role in object detection, image segmentation and feature extraction from the image. In Real-time edge detection, the image frame coming from a live webcam or video
5 min read
Understanding of LSTM Networks
This article talks about the problems of conventional RNNs, namely, the vanishing and exploding gradients, and provides a convenient solution to these problems in the form of Long Short Term Memory (LSTM). Long Short-Term Memory is an advanced version of recurrent neural network (RNN) architecture that was designed to model chronological sequences
9 min read
LSTM - Derivation of Back propagation through time
LSTM (Long short term Memory ) is a type of RNN(Recurrent neural network), which is a famous deep learning algorithm that is well suited for making predictions and classification with a flavour of the time. In this article, we will derive the algorithm backpropagation through time and find the gradient value for all the weights at a particular time
4 min read
Long short-term memory (LSTM) RNN in Tensorflow
This article discusses the concept of "Recurrent Neural Networks(RNN)" and "Long Short Term Memory(LSTM)" and their implementation using Python programming language and the necessary library. RECURRENT NEURAL NETWORKIt is one of the oldest networks which was created in the year the 1980s but at the time there is no computation power of computers so
5 min read
What's the difference of stateless LSTM and a normal feed-forward NN?
Answer: Stateless LSTM disregards past sequences, while a feed-forward neural network lacks internal memory and processes inputs sequentially.Here is a comparison between stateless LSTM and a normal feed-forward NN: AspectStateless LSTMFeed-Forward Neural Network (NN)ArchitectureIncorporates Long Short-Term Memory (LSTM) cells that allow capturing
2 min read
What's the Difference Between the Cell and Hidden State in LSTM?
Answer: In an LSTM (Long Short-Term Memory) network, the cell state represents the memory of the network, storing information over time, while the hidden state contains the information that is passed to the next time step.Here's the difference between the cell state and hidden state in LSTM presented in a tabular form: AspectCell State (Ct)Hidden S
1 min read
LSTM Full Form - Long Short-Term Memory
LSTM stands for Long Short-Term Memory. It is a type of recurrent neural network (RNN) architecture that is designed to remember long-term dependencies in sequence data. Unlike traditional RNNs, LSTM networks have a more complex architecture that allows them to learn and remember over long sequences, making them particularly effective for tasks suc
1 min read
What is LSTM - Long Short Term Memory?
LSTM excels in sequence prediction tasks, capturing long-term dependencies. Ideal for time series, machine translation, and speech recognition due to order dependence. The article provides an in-depth introduction to LSTM, covering the LSTM model, architecture, working principles, and the critical role they play in various applications. What is LST
10 min read
Difference Between "Hidden" and "Output" in PyTorch LSTM
Long Short-Term Memory (LSTM) networks are a type of recurrent neural network (RNN) that are widely used for sequence prediction tasks. In PyTorch, the nn.LSTM module is a powerful tool for implementing these networks. However, understanding the difference between the "hidden" and "output" states of an LSTM can be confusing for many. This article a
4 min read
Adding Attention Layer to a Bi-LSTM
The integration of attention mechanisms with Bidirectional Long Short-Term Memory (Bi-LSTM) networks has become a popular approach in various natural language processing (NLP) tasks. The attention mechanism allows the model to focus on specific parts of the input sequence, enhancing its ability to capture relevant information. This article provides
5 min read
Object Detection with Detection Transformer (DETR) by Facebook
Facebook has just released its State of the art object detection Model on 27 May 2020. They are calling it DERT stands for Detection Transformer as it uses transformers to detect objects.This is the first time that transformer is used for such a task of Object detection along with a Convolutional Neural network. There are other Object detection mod
7 min read
Comparing anomaly detection algorithms for outlier detection on toy datasets in Scikit Learn
Anomaly is a deviation from the expected or normal behavior or pattern. Anomaly detection is the process of identifying these unusual patterns or behaviors in data. It is important because anomalies can indicate important events or problems, such as fraudulent activity, equipment failure, or security breaches. Anomaly detection is used in a variety
9 min read
Sobel Edge Detection vs. Canny Edge Detection in Computer Vision
Both Sobel and Canny algorithms are popular methods for edge detection in images, but they are suited for different purposes based on their characteristics and the nature of the application. Understanding when to use each can help you achieve better results for specific tasks in image processing. In this article, we are going to explore the differe
9 min read
ANN - Bidirectional Associative Memory (BAM)
Bidirectional Associative Memory (BAM) is a supervised learning model in Artificial Neural Network. This is hetero-associative memory, for an input pattern, it returns another pattern which is potentially of a different size. This phenomenon is very similar to the human brain. Human memory is necessarily associative. It uses a chain of mental assoc
2 min read
Bidirectional Associative Memory (BAM) Implementation from Scratch
Prerequisite: ANN | Bidirectional Associative Memory (BAM) Learning AlgorithmTo implement BAM model, here are some essential consideration and approach- Consider the value of M, as BAM will be constructed with M pairs of patterns. Here the value of M is 4.Set A: Input PatternsSet B: Corresponding Target PatternsAssign the neurons in input and outpu
4 min read
ANN - Bidirectional Associative Memory (BAM) Learning Algorithm
Prerequisite: ANN | Bidirectional Associative Memory (BAM) There are three main steps to construct the BAM model. Learning Testing Retrieval Each step has been described with mathematical formulation in the article ANN | Bidirectional Associative Memory (BAM). Here, this learning algorithm is explained iteratively with an example. Assume, Set A: In
4 min read
Bidirectional Recurrent Neural Network
Recurrent Neural Networks (RNNs) are a particular class of neural networks that was created with the express purpose of processing sequential input, including speech, text, and time series data. RNNs process data as a sequence of vectors rather than feedforward neural networks, which process data as a fixed-length vector. Each vector is processed d
9 min read
Bidirectional RNNs in NLP
The state of a recurrent network at a given time unit only knows about the inputs that have passed before it up to that point in the sentence; it is unaware of the states that will come after that. With knowledge of both past and future situations, the outcomes are significantly enhanced in some applications, such as language modeling. In the case
10 min read
Bidirectional Search in AI
Bidirectional search in AI is an algorithm that searches simultaneously from both the initial state and the goal state, meeting in the middle to reduce search time. The aim of the article is to provide an in-depth understanding of the bidirectional search algorithm, its working mechanism, benefits, and practical applications in artificial intellige
9 min read
Python | Smile detection using OpenCV
Emotion detectors are used in many industries, one being the media industry where it is important for the companies to determine the public reaction to their products. In this article, we are going to build a smile detector using OpenCV which takes in live feed from webcam. The smile/happiness detector that we are going to implement would be a raw
3 min read
Gun Detection using Python-OpenCV
Gun Detection using Object Detection is a helpful tool to have in your repository. It forms the backbone of many fantastic industrial applications. We can use this project for real threat detection in companies or organizations. Prerequisites: Python OpenCV OpenCV(Open Source Computer Vision Library) is a highly optimized library with a focus on Re
4 min read
Tumor Detection using classification - Machine Learning and Python
In this article, we will be making a project through Python language which will be using some Machine Learning Algorithms too. It will be an exciting one as after this project you will understand the concepts of using AI & ML with a scripting language. The following libraries/packages will be used in this project: numpy: It's a Python library t
9 min read
Face and Hand Landmarks Detection using Python - Mediapipe, OpenCV
In this article, we will use mediapipe python library to detect face and hand landmarks. We will be using a Holistic model from mediapipe solutions to detect all the face and hand landmarks. We will be also seeing how we can access different landmarks of the face and hands which can be used for different computer vision applications such as sign la
4 min read