Kwame is an NLP researcher at Accra Digital Labs, a startup building AI tools for African languages and content. He's tasked with building a sentiment classifier for a Ghanaian film platform. The platform receives thousands of movie reviews daily in English, and the team wants to automatically flag negative reviews for customer service follow-up.
Kwame knows the solution: an LSTM-based text classifier. But the pipeline is complex: load raw text, tokenize and build vocabulary, create dense embeddings, design an LSTM model that processes sequences, classify output. He needs to understand each step—from raw text to confusion matrix—and deploy a model that predicts sentiment with 85%+ accuracy.
Step 1: Load and explore the movie review dataset
The first step is always data. Kwame has a JSON file with movie reviews and their sentiment labels (positive: 1, negative: 0).
What happened: We loaded 2,000 training reviews and 500 test reviews. Each review is a string, and each has a binary label (positive=1 or negative=0). The challenge: raw text isn't a number. We need to convert words into vectors.
Step 2: Build vocabulary and tokenize
LSTMs operate on integers, not words. We create a vocabulary: a mapping from unique words to integer indices. Rare words (appearing <2 times) are replaced with an "unknown" token.
Why this matters: Vocabulary size is 8,765. That's the input dimension to our embedding layer. Rare words map to index 1 (UNK), and we'll pad short reviews to a fixed length with index 0 (PAD).
Step 3: Create a PyTorch Dataset for batch processing
PyTorch's DataLoader needs a Dataset class. We override __getitem__ to pad reviews to a fixed length (max_len=128) and return (tokenized_text, label) pairs.
Batch mechanics: Each training epoch processes 63 batches of 32 reviews each (32 × 63 ≈ 2,000). Each batch has shape [32, 128]: 32 reviews, each padded/truncated to 128 tokens.
Step 4: Design the embedding + LSTM model
The architecture: (1) Embedding layer converts token indices to dense vectors (embedding_dim=64). (2) LSTM processes the sequence, returning the last hidden state (hidden_dim=128). (3) Fully-connected head (dropout for regularization) outputs a binary classification.
Model breakdown: The embedding layer has vocab_size × embedding_dim parameters (8,765 × 64). The LSTM has ~1.5M parameters (it tracks 4 gates per layer). The fully-connected layers add ~20K parameters. Total: ~2M parameters.
Step 5: Train the model with backpropagation
We use binary cross-entropy loss (since output_dim=1) and Adam optimizer. For each epoch, we iterate through batches, compute loss, and backpropagate.
Training dynamics: Loss drops from 0.35 to 0.07 over 5 epochs. Validation accuracy climbs from 82% to 88%. This is expected: early epochs reduce large losses (underfitting); later epochs fine-tune (slight improvement). The model reaches 87.8% accuracy on the test set, above our 85% target.
Step 6: Evaluate with confusion matrix and metrics
Accuracy alone hides imbalances. A confusion matrix shows true positives (TP), true negatives (TN), false positives (FP), and false negatives (FN). From it, we calculate precision, recall, and F1-score.
Interpretation: Out of 500 test reviews: 132+334=466 are correct, 34 are wrong. Precision is 95% (when the model predicts positive, it's right 95% of the time). Recall is 95% (the model catches 95% of positive reviews). F1-score is 95%, indicating a well-balanced classifier.
Confusion matrix deep dive: where does the model fail?
18 reviews are false positives (predicted positive but actually negative), and 16 are false negatives (predicted negative but actually positive). Let's analyze what went wrong.
Understanding errors
False Positives (18): The model predicted "positive" but the review was actually negative. These might be sarcastic reviews ("This movie is so bad it's hilarious!") that use positive words without meaning.
False Negatives (16): The model predicted "negative" but the review was actually positive. These might be subtly positive reviews with few strong emotional words, or reviews that mention problems but overall recommend the film.
The balance (18 vs 16) suggests the model isn't severely biased toward either class. Reducing these errors would require better preprocessing (handling sarcasm, negation) or more training data.
Training dynamics: loss and accuracy curves
The training curve shows loss decreasing and accuracy increasing. This is a sign of healthy training—no overfitting yet, and the model is learning.
Quiz: 5 questions to test your understanding
You've built a sentiment classifier.
Now master production NLP at scale.
Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Agentic Engineer →