A comprehensive sentiment analysis project using deep learning models (LSTM and BERT) to classify text sentiment as positive, negative, or neutral.
-
Multiple Model Architectures:
- LSTM (Long Short-Term Memory) with bidirectional layers
- BERT (Bidirectional Encoder Representations from Transformers)
-
Multiple Dataset Support:
- IMDB Movie Reviews
- Sentiment140 (Twitter data)
- Yelp Reviews
- Generic CSV format
-
Complete Pipeline:
- Data preprocessing and cleaning
- Model training with callbacks
- Model evaluation and metrics
- Inference on new text
pip install -r requirements.txtDownload a dataset (IMDB, Sentiment140, or Yelp) or use your own CSV file with text and sentiment columns.
LSTM (Recommended for beginners):
python train.py --model lstm --data_path your_dataset.csv --dataset_type auto --epochs 10BERT (Better accuracy, needs more memory):
python train.py --model bert --data_path your_dataset.csv --dataset_type auto --epochs 3 --batch_size 16# LSTM
python predict.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl --text "This movie is great!"
# BERT
python predict.py --model bert --model_path models/saved/bert_sentiment_model --text "This movie is great!"For detailed instructions, see QUICKSTART.md
Windows Users: You can also use the batch scripts:
run_training.bat- Interactive training scriptrun_prediction.bat- Interactive prediction script
-
Clone or download this repository
-
Install required dependencies:
pip install -r requirements.txtDownload from Kaggle IMDB Dataset
Expected CSV format:
review: Text of the reviewsentiment: 'positive' or 'negative'
Download from Sentiment140
Expected CSV format:
text: Tweet texttarget: 0 (negative) or 4 (positive)
Download from Kaggle Yelp Reviews
Expected CSV format:
textorreview: Review textstarsorrating: 1-5 stars (1-2: negative, 4-5: positive)
python train.py --model lstm --data_path path/to/dataset.csv --dataset_type imdb --epochs 20 --batch_size 64python train.py --model bert --data_path path/to/dataset.csv --dataset_type imdb --epochs 3 --batch_size 16python train.py --model both --data_path path/to/dataset.csv --dataset_type imdbArguments:
--model: Model type (lstm,bert, orboth)--data_path: Path to your dataset CSV file--dataset_type: Type of dataset (imdb,sentiment140,yelp, orauto)--output_dir: Directory to save models (default:models/saved)--epochs: Number of training epochs (default: 20 for LSTM, 3 for BERT)--batch_size: Batch size (default: 64 for LSTM, 16 for BERT)--test_size: Test set size (default: 0.2)--bert_model: BERT model name (default:bert-base-uncased)
python evaluate.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl --data_path path/to/test_data.csvpython evaluate.py --model bert --model_path models/saved/bert_sentiment_model --data_path path/to/test_data.csvpython predict.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl --text "This movie is amazing!"python predict.py --model bert --model_path models/saved/bert_sentiment_model --text "This movie is amazing!"python predict.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl --file texts.txtpython predict.py --model lstm --model_path models/saved/lstm_sentiment_model.h5 --tokenizer_path models/saved/lstm_tokenizer.pkl.
├── data_preprocessing.py # Data loading and preprocessing
├── models/
│ ├── __init__.py
│ ├── lstm_model.py # LSTM model implementation
│ └── bert_model.py # BERT model implementation
├── train.py # Training script
├── evaluate.py # Evaluation script
├── predict.py # Inference script
├── requirements.txt # Python dependencies
└── README.md # This file
- Architecture: Embedding → Bidirectional LSTM → Dense layers
- Embedding Dimension: 128
- LSTM Units: 128
- Max Sequence Length: 200
- Dropout: 0.3
- Optimizer: Adam (learning rate: 0.001)
- Base Model: bert-base-uncased (configurable)
- Max Sequence Length: 128
- Learning Rate: 2e-5
- Optimizer: Adam
- Early Stopping: Stops training when validation loss stops improving
- Learning Rate Reduction: Reduces learning rate on plateau
- Model Checkpointing: Saves best model based on validation accuracy
- Stratified Splitting: Maintains class distribution in train/test splits
- Accuracy
- Precision
- Recall
- F1-Score
- Confusion Matrix
- BERT models require more memory and training time but generally achieve better accuracy
- LSTM models are faster to train and require less memory
- For large datasets, consider using a GPU for faster training
- The tokenizer is saved separately for LSTM models to enable inference on new data
- Out of Memory: Reduce batch size or sequence length
- Slow Training: Use GPU or reduce model size
- Poor Accuracy: Try different hyperparameters or use more training data
- Import Errors: Ensure all dependencies are installed correctly
This project is open source and available for educational purposes.