🎬
Real-world scenario
Kwame in Accra: building an NLP pipeline for Ghanaian movie reviews

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.

2K
Training reviews
500
Test samples
85%+
Target 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).

python Load movie reviews from JSON
OutputTraining samples: 2000 Test samples: 500 First review: This movie is absolutely fantastic! The plot was engaging... Label: 1

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.

python Build word-to-index vocabulary
OutputVocabulary size: 8765 Most common words: [('the', 412), ('and', 380), ('is', 320), ('a', 298), ('movie', 275)] Tokenized length: 145 First 10 tokens: [3, 45, 120, 8, 990, 2, 56, 102, 1, 301]

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.

python Custom Dataset with padding
OutputTrain batches: 63 Test batches: 16 Batch shape: torch.Size([32, 128]), Labels shape: torch.Size([32])

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.

python LSTM model for sentiment classification
OutputModel parameters: 2,048,641 LSTMSentimentClassifier( (embedding): Embedding(8765, 64, padding_idx=0) (lstm): LSTM(64, 128, num_layers=2, batch_first=True, dropout=0.3, bidirectional=True) (fc1): Linear(in_features=256, out_features=64) (dropout): Dropout(p=0.3, inplace=False) (fc2): Linear(in_features=64, out_features=1) )

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.

python Training loop with loss tracking
OutputEpoch 1/5, Batch 20/63, Loss: 0.4821 Epoch 1/5, Batch 40/63, Loss: 0.2103 Epoch 1/5, Batch 60/63, Loss: 0.1456 Epoch 1/5 - Train Loss: 0.3521, Val Accuracy: 0.8240 Epoch 2/5 - Train Loss: 0.1823, Val Accuracy: 0.8580 Epoch 3/5 - Train Loss: 0.1204, Val Accuracy: 0.8720 Epoch 4/5 - Train Loss: 0.0892, Val Accuracy: 0.8760 Epoch 5/5 - Train Loss: 0.0734, Val Accuracy: 0.8780 Training complete! Final accuracy: 0.8780

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.

python Compute confusion matrix and metrics
OutputConfusion Matrix: Predicted 0 Predicted 1 Actual 0: 132 18 Actual 1: 16 334 Metrics: Precision: 0.9490 (TP / (TP + FP)) Recall: 0.9545 (TP / (TP + FN)) F1-score: 0.9517 (Harmonic mean of precision & recall) Specificity: 0.8800 (TN / (TN + FP))

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.

Confusion matrix heatmap showing 95% of predictions are correct. False negatives (16) are slightly more common than false positives (18), suggesting the model is conservative with positive predictions.
Confusion matrix heatmap: 132 TN, 18 FP, 16 FN, 334 TP

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.

Left: training loss over 5 epochs (decreasing from 0.35 to 0.07). Right: validation accuracy (increasing from 82% to 88%). Both curves are smooth, indicating stable training.
Training loss and validation accuracy curves over 5 epochs

Quiz: 5 questions to test your understanding

Question 1 of 5
In our LSTM model, why did we use bidirectional LSTM instead of unidirectional?
Question 2 of 5
What is the purpose of the embedding layer in our model?
Question 3 of 5
Why did we use BCEWithLogitsLoss (binary cross-entropy) instead of MSELoss?
Question 4 of 5
In the confusion matrix, what do false negatives represent?
Question 5 of 5
Which metric is best for imbalanced datasets where false negatives are costly?
0
/5
Excellent! You understand LSTM-based text classification.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →