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
- RNN, LSTM and GRU cells written by hand in PyTorch, gates included.
- Six emotions, 16,000 training sentences; the tuned GRU reaches 85.4 % on the test set.
- 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.
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).
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 %.
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.
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.
References
- [1]DAIR.AI. Emotion dataset. Hugging Face
- [2]Saravia, Liu, Huang, Wu, Chen (2018). CARER: Contextualized Affect Representations for Emotion Recognition. EMNLP
- [3]Classifying names with a character-level RNN. PyTorch tutorials
- [4]Hochreiter, Schmidhuber (1997). Long Short-Term Memory. Neural Computation
- [5]Cho et al. (2014). Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation. EMNLP
- [6]Sanh, Debut, Chaumond, Wolf (2019). DistilBERT, a distilled version of BERT