A state-of-the-art sentiment analysis model using BERT for movie review classification. This project achieves high accuracy on the IMDB dataset through fine-tuning a pre-trained BERT model for binary sentiment classification.
This project implements a comprehensive sentiment analysis pipeline using BERT (Bidirectional Encoder Representations from Transformers) to classify movie reviews as positive or negative. The implementation includes data preprocessing, model training, evaluation, and testing components with professional-grade code quality.
- ๐ง BERT-based Architecture: Utilizes
bert-base-uncasedfor robust text understanding - ๐ Comprehensive Evaluation: Includes accuracy, F1-score, confusion matrix, and classification reports
- ๐ง Modular Design: Clean, reusable code with proper separation of concerns
- ๐ Visualization: Training loss curves and performance metrics visualization
- ๐งช Testing Suite: Unit tests and code quality checks with flake8 and mypy
- ๐พ Model Persistence: Automated model saving with configuration tracking
sentiment-analysis/
โโโ ๐ data/
โ โโโ imdb_dataset.csv # Original IMDB dataset
โโโ ๐ cleaned_splitted_data/
โ โโโ train_dataset.csv # Training data (72% of dataset)
โ โโโ val_dataset.csv # Validation data (8% of dataset)
โ โโโ test_dataset.csv # Test data (20% of dataset)
โโโ ๐ models/
โ โโโ sentiment_analysis_model_*.pth # Trained model weights
โ โโโ tokenizer/ # BERT tokenizer files
โ โโโ model_config.json # Model configuration and metrics
โโโ ๐ train_model.ipynb # Model training notebook
โโโ ๐ test_model.ipynb # Model testing and validation notebook
โโโ ๐ Utils.py # Utility functions and custom dataset class
โโโ ๐ requirements.txt # Project dependencies
โโโ ๐ README.md # Project documentation
- Chihabeddine Zitouni - Machine Learning Engineer
- Patrick Molina - Data Scientist
- Maลgorzata Gierdewicz - NLP Researcher
Source: IMDB 50K Movie Review Dataset
- Size: 50,000 movie reviews
- Balance: 25,000 positive and 25,000 negative reviews
- Task: Binary sentiment classification
- Format: CSV with 'review' and 'sentiment' columns
- Python 3.8 or higher
- CUDA-compatible GPU (optional, for faster training)
-
Clone the repository
git clone https://github.com/chihab4real/sentiment-analysis.git cd sentiment-analysis -
Install dependencies
pip install -r requirements.txt
-
Download the dataset
- Download the IMDB dataset from Kaggle
- Place
imdb_dataset.csvin thedata/directory
# Open the training notebook
jupyter notebook train_model.ipynbOr run all cells programmatically:
jupyter nbconvert --to script train_model.ipynb
python train_model.py# Open the testing notebook
jupyter notebook test_model.ipynb# Text cleaning and normalization
def clean_text(text):
text = re.sub(r'<.*?>', '', text) # Remove HTML tags
text = re.sub(r'[^a-zA-Z\s]', '', text) # Keep only alphabetic characters
text = text.lower() # Lowercase normalization
text = re.sub(r'\s+', ' ', text).strip() # Remove extra whitespace
return textKey Processing Steps:
- HTML Tag Removal: Strips HTML markup from review text
- Text Normalization: Converts to lowercase and removes special characters
- Label Mapping:
positiveโ1,negativeโ0 - Dataset Splitting: 72% training, 8% validation, 20% testing
# BERT model configuration
MODEL_NAME = 'bert-base-uncased'
MAX_LENGTH = 128
NUM_LABELS = 2
model = BertForSequenceClassification.from_pretrained(
MODEL_NAME,
num_labels=NUM_LABELS
)Architecture Details:
- Base Model:
bert-base-uncased(110M parameters) - Classification Head: Linear layer with 2 output classes
- Tokenizer: BERT WordPiece tokenizer with 128 max sequence length
- Input Processing: Attention masks and special tokens ([CLS], [SEP])
| Parameter | Value | Description |
|---|---|---|
| Optimizer | AdamW | Adaptive learning rate with weight decay |
| Learning Rate | 2e-5 | Optimal rate for BERT fine-tuning |
| Batch Size | 32 | Balanced for memory and convergence |
| Epochs | 3 | Sufficient for convergence without overfitting |
| Max Length | 128 | Captures most review content efficiently |
The model achieves excellent performance on the IMDB test set:
- Accuracy: ~90-95% (typical range for BERT on IMDB)
- F1 Score: Balanced performance across both classes
- Training Time: ~2-3 hours on GPU, ~8-12 hours on CPU
- ๐ Training Loss Curve: Monitors convergence during training
- ๐ฅ Confusion Matrix: Visualizes prediction accuracy by class
- ๐ Classification Report Heatmap: Detailed precision/recall metrics
- ๐ Validation Accuracy: Tracks performance on unseen data
# Code style checking
flake8 Utils.py
# Type checking
mypy Utils.py# Test data preprocessing functions
python -m unittest test_model.pyTest Coverage:
- Text cleaning functionality
- Label mapping accuracy
- Dataset loading and splitting
- Model prediction pipeline
from Utils import predict_sentiment
from transformers import BertTokenizer, BertForSequenceClassification
import torch
# Load trained model
model = BertForSequenceClassification.from_pretrained('./models/')
tokenizer = BertTokenizer.from_pretrained('./models/tokenizer/')
# Predict sentiment
review = "This movie was absolutely fantastic! The acting was superb."
sentiment = predict_sentiment(model, tokenizer, device, 128, review)
print(f"Sentiment: {sentiment}") # Output: Sentiment: PositiveThe trained model can be deployed using:
- Hugging Face Hub: For easy sharing and inference
- TorchScript: For production deployment
- ONNX: For cross-platform compatibility
- Web Interface: Using Gradio or Streamlit
pandas>=1.5 # Data manipulation
torch>=2.0 # Deep learning framework
transformers>=4.30 # Hugging Face transformers
scikit-learn>=1.3 # ML utilities and metrics
matplotlib>=3.7 # Plotting and visualization
seaborn>=0.12 # Statistical visualization
tqdm>=4.65 # Progress bars
numpy>=1.23 # Numerical computing
mypy>=1.5 # Type checking
flake8>=6.0 # Code style checking- Multi-class Classification: Extend to neutral/mixed sentiment
- Model Optimization: Quantization and pruning for deployment
- Real-time Inference: Web API with FastAPI
- Hyperparameter Tuning: Automated optimization with Optuna
- Cross-domain Evaluation: Test on other review types (books, products)
- Ensemble Methods: Combine multiple models for better performance
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Hugging Face for the transformers library
- IMDB Dataset creators
- The PyTorch team for the excellent deep learning framework
Built with โค๏ธ by the Sentiment Analysis Team