Project · AI & ML

Sentence Emotion Classifier

Recurrent networks written by hand in PyTorch to tell the emotion of a sentence, compared with a pretrained DistilBERT.

Ref.
01.04
Period
Oct 2024
Status
Completed
Organization
M2 AI — UCBL
Contributors
Axel COLMANT and Mohamed Massamba SENE
Stack
  • Python
  • PyTorch
  • scikit-learn
  • Hugging Face Transformers
  • DistilBERT

Key points

  1. RNN, LSTM and GRU cells written by hand in PyTorch, gates included.
  2. Six emotions, 16,000 training sentences; the tuned GRU reaches 85.4 % on the test set.
  3. A pretrained DistilBERT, fine-tuned for comparison, reaches 92.25 %.

The task

Read a short English sentence and say which emotion it expresses: joy, sadness, anger, fear, love or surprise. The project came from the Bio-Inspired Machine Learning course, taught by Frédéric Armetta, and we did it as a team of two with Mohamed Massamba Sene.

The rule was to build our own network and train it only on the data provided. A pretrained language model was allowed only at the end, as the “pragmatic” answer to compare against.

The data is the public Emotion dataset: 16,000 sentences to train on, 2,000 to validate and 2,000 to test. The classes are far from balanced: 5,362 training sentences for joy, only 572 for surprise.

Building the networks

Each sentence becomes a sequence of one-hot word vectors, padded or cut to a fixed length, and the network reads it one word at a time (Fig. 1). We started from the character-level RNN of the PyTorch tutorial and adapted it:

  • an embedding layer in front, to shrink the huge one-hot vectors;
  • a hidden state sized for a whole batch instead of one sentence;
  • a LogSoftmax output trained with the negative log-likelihood loss.
Fig. 1How a sentence is read: one word per step, the hidden state carrying what came before, then one score per emotion.
A training sentence, “i didnt feel humiliated”, split into four words; each word becomes a one-hot vector, then a dense embedding, then enters a recurrent cell; the hidden state passes from cell to cell, and the last one goes through a linear layer and a LogSoftmax to pick one of six emotions, here sadness

Fig. 1How a sentence is read: one word per step, the hidden state carrying what came before, then one score per emotion.

Then we wrote the two gated variants the same way, with every weight matrix and gate as our own parameters rather than PyTorch's ready-made layers: an LSTM (input, forget and output gates) and a GRU (reset and update gates). All three train for up to 10 epochs, with early stopping when the validation loss starts rising.

The gated cells converge in a few epochs, while the plain RNN keeps improving through all ten (Fig. 2).

Fig. 2Validation accuracy per epoch; early stopping ends the gated runs.
Validation accuracy per epoch: the GRU reaches 86 % at epoch 2, the LSTM 83 % at epoch 3, the plain RNN climbs slowly to 82 % at epoch 10; the pretrained DistilBERT starts at 90 % and ends at 92 %

Fig. 2Validation accuracy per epoch; early stopping ends the gated runs.

Tuning the GRU

The GRU was the best of the three, so we tried to push it further:

  • a class-weighted loss, to give the rare emotions more say;
  • text cleaning: punctuation, stop words, words rare by TF-IDF, lemmatisation;
  • a grid search on batch size, embedding size, hidden size and learning rate, scored on the validation set.

Results

Each gated cell beat the plain RNN (Fig. 3). The LSTM lifted love from an F1 of 0.64 to 0.73; the GRU did a little better overall, and tuning added almost one more point.

The fine-tuned DistilBERT, a pretrained model, jumped to 92.25 %.

Fig. 3Test accuracy on the 2,000 held-out sentences.
Bar chart of test accuracy: RNN 81 %, LSTM 83.5 %, GRU 84.55 %, tuned GRU 85.4 %, all our own networks; pretrained DistilBERT 92.25 %

Fig. 3Test accuracy on the 2,000 held-out sentences.

The weak spots follow the data (Fig. 4): roughly, the fewer training sentences an emotion has, the worse our networks read it. The class weights did little for the two rarest.

DistilBERT's gain is largest exactly there: love rose from an F1 of 0.69 to 0.84, surprise from 0.65 to 0.79.

Fig. 4F1-score per emotion, from the most to the least common in training.
F1-score per emotion for the RNN, the tuned GRU and DistilBERT, emotions sorted by training size: joy and sadness above 0.85 for all; love and surprise lowest, near 0.65 for our networks and about 0.8 for DistilBERT

Fig. 4F1-score per emotion, from the most to the least common in training.

The confusions show where the rare emotions go (Fig. 5). The tuned GRU reads almost a third of the love sentences as joy, and a quarter of the surprise sentences as fear. DistilBERT's errors lean the other way, with some joy read as love and some fear as surprise.

Fig. 5Where each model goes wrong, in % of each true emotion.
Two confusion matrices in percent of each true emotion. Tuned GRU: 31 % of love read as joy, 27 % of surprise read as fear. DistilBERT: 97 % of love and 97 % of surprise correct, but 8 % of joy read as love and 11 % of fear read as surprise

Fig. 5Where each model goes wrong, in % of each true emotion.

References

  1. [1]DAIR.AI. Emotion dataset. Hugging Face
  2. [2]Saravia, Liu, Huang, Wu, Chen (2018). CARER: Contextualized Affect Representations for Emotion Recognition. EMNLP
  3. [3]Classifying names with a character-level RNN. PyTorch tutorials
  4. [4]Hochreiter, Schmidhuber (1997). Long Short-Term Memory. Neural Computation
  5. [5]Cho et al. (2014). Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation. EMNLP
  6. [6]Sanh, Debut, Chaumond, Wolf (2019). DistilBERT, a distilled version of BERT
#nlp#rnn#lstm#gru#text-classification