Summary
- Task A (sexist vs. not sexist): 82% accuracy and 82% F1 with GloVe embeddings and a bidirectional LSTM.
- Task B (four categories of sexism): 55% accuracy and 49% macro-F1 — limited by class imbalance and subtle category boundaries.
- The biggest lesson: for fine-grained classification, data balance and evaluation choices matter as much as the architecture.
Online platforms struggle to moderate harmful content at scale, and sexism is particularly hard because it ranges from explicit threats to subtle stereotypes. For my NLP II course (CSE440) at BRAC University I worked on the Explainable Detection of Online Sexism (EDOS) problem, introduced as a shared task at SemEval-2023. This post explains the approach, the results, and the mistakes worth avoiding. The code is on GitHub and the full project report is available.
The problem
The EDOS data consists of English social-media posts (from Reddit and Gab) labelled by trained annotators. We worked on two of its tasks:
- Task A — binary: is a post sexist or not?
- Task B — category: for sexist posts, which of four categories does it belong to — threats, derogation, animosity, or prejudiced discussions?
Both are hard for reasons that go beyond modelling: most posts are not sexist (so Task A is imbalanced), and within Task B the categories are uneven in size and sometimes overlap in meaning — an insulting post can also contain a stereotype.
Preprocessing
Social-media text is noisy. A simple, reproducible preprocessing pipeline for this kind of data looks like this:
- Lower-casing, and replacing URLs and user mentions with placeholder tokens (the dataset already masks many of these).
- Expanding common contractions and separating punctuation, while keeping emoticons and repeated punctuation that carry tone.
- Tokenizing with Keras and padding every post to a fixed length chosen from the length distribution (covering the vast majority of posts without wasting computation on very long outliers).
Model: GloVe + bidirectional LSTM
We initialised the embedding layer with pre-trained GloVe word vectors so the model starts with a general sense of word meaning instead of learning it from a few thousand examples. A bidirectional LSTM then reads each post forwards and backwards, so the representation of each word reflects both what came before and after it — useful for negation and sarcasm ("not that women can't drive, but…").
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
def build_model(embedding_matrix, num_classes, max_len=64):
vocab_size, dim = embedding_matrix.shape
inputs = keras.Input(shape=(max_len,))
x = layers.Embedding(vocab_size, dim,
embeddings_initializer=keras.initializers.Constant(embedding_matrix),
trainable=False, mask_zero=True)(inputs)
x = layers.SpatialDropout1D(0.2)(x)
x = layers.Bidirectional(layers.LSTM(64, return_sequences=True))(x)
x = layers.GlobalMaxPooling1D()(x)
x = layers.Dense(64, activation="relu")(x)
x = layers.Dropout(0.4)(x)
if num_classes == 2:
outputs = layers.Dense(1, activation="sigmoid")(x)
loss = "binary_crossentropy"
else:
outputs = layers.Dense(num_classes, activation="softmax")(x)
loss = "sparse_categorical_crossentropy"
model = keras.Model(inputs, outputs)
model.compile(optimizer=keras.optimizers.Adam(1e-3), loss=loss, metrics=["accuracy"])
return model
The code above freezes the embeddings and should be trained with early stopping on validation loss. Freezing prevents a small dataset from distorting the pre-trained vectors; you can unfreeze for a few epochs at a low learning rate afterwards. (The exact configuration used in the project is in the GitHub repository.)
Results
| Task | Classes | Accuracy | F1 |
|---|---|---|---|
| Task A | 2 (sexist / not sexist) | 82% | 82% |
| Task B | 4 categories | 55% | 49% (macro) |
Task A was solid for a non-transformer model. Task B was much harder, and the gap between accuracy and macro-F1 tells the story: the model did reasonably on the larger categories and poorly on the smallest ones.
Why Task B was so hard
1. Class imbalance
Some categories had many times more examples than others. A neural network minimising average loss learns that predicting the big classes is "safe". Accuracy hides this; macro-F1, which averages F1 equally across classes, exposes it. That's why we reported both.
2. Subtle, overlapping categories
Distinguishing derogation from animosity often depends on intent and tone rather than specific words. Static word embeddings like GloVe give each word one fixed vector regardless of context, which limits how well the model can capture those differences.
3. Less data per class
Task B only uses the sexist posts, so it has far fewer training examples than Task A, spread across four labels.
How to push the results further
- Class weights in the loss function (larger weights for rare classes) typically improve recall on small categories, at some cost to the large ones.
- Threshold tuning on the validation set for Task A usually gives a better balance of precision and recall than the default 0.5.
- Transformers. The clear next step is fine-tuning a pre-trained transformer such as BERT or RoBERTa, whose contextual embeddings handle tone and negation much better. Strong SemEval EDOS submissions were transformer-based.
- Data augmentation for rare classes (back-translation or careful paraphrasing) and hierarchical training — first sexist vs. not, then category — so each stage solves a simpler problem.
- Error analysis. Reading a sample of misclassified posts is more informative than any extra epoch — on a task like this, many "errors" turn out to be genuinely ambiguous even to humans.
Responsible-AI considerations
Automated moderation affects real people. A false positive silences legitimate speech; a false negative leaves harmful content up. Models also inherit biases from their training data and annotators, and a model trained on English Reddit and Gab posts won't transfer to Nepali or code-mixed social media without new data. For any production use, automated classification should support human moderators, with appeal processes and regular audits — not replace them.
Takeaways for students
- Always report a metric that reflects class balance (macro-F1) alongside accuracy.
- Start with a simple, well-understood baseline before reaching for bigger models.
- Spend time reading errors — it's the cheapest way to improve.
- Document limitations honestly; a clear "this is where it fails" is part of good research.
If you're interested in how these ideas apply to Nepali text — where data is even scarcer — read Building NLP for the Nepali language.
Spotted an error or have a question about this article? Send me a message — corrections are applied and noted with an updated date. See the editorial policy.