Transformer-based Sentiment Classifier
š¬ Sentiment Analysis with Transformers š¬
Classify movie reviews as positive or negative using a Transformer model built with PyTorch!
š Overview
This project uses a custom Transformer model for binary sentiment classification on the IMDb dataset. With PyTorch, torch.nn, datasets, and transformers, this model learns to identify patterns in text data to predict sentiment.
š Getting Started
Prerequisites
Make sure you have the following libraries installed:
pip install torch transformers datasets numpy scikit-learn tqdm
Project Structure
- Data Preparation - Load and preprocess IMDb dataset.
- Model Definition - Build a custom Transformer model for text classification.
- Training Loop - Train the model with a validation phase.
- Inference - Test the model on new data for sentiment prediction.
š ļø Code Walkthrough
PART 1: Data Preparation
- IMDBDataset - Custom PyTorch
Datasetclass to preprocess and tokenize text data. - prepare_data - Function to load IMDb data, split it into training and validation sets, and create data loaders.
PART 2: Model Definition
- PositionalEncoding - Adds position information to tokens.
- TransformerClassifier - Custom Transformer model with an embedding layer, Transformer encoder, and a fully connected classifier.
PART 3: Training the Model
- train_model - Training function that includes memory management and gradient clipping for stability.
šāāļø Running the Code
-
Clone the Repository:
git clone https://github.com/yourusername/transformer-sentiment-classifier.git cd transformer-sentiment-classifier -
Train the Model:
python train.pyThe model will automatically save as
transformer_classifier.pth. -
Run Inference: To classify a sample text, use:
from predict import predict text = "The movie had an excellent plot and fantastic acting!" print("Prediction:", predict(text))
šØ Sample Output
Training Loop Animation
The training progress and loss are updated dynamically with tqdm:
Epoch 1/20
Training |āāāāāāāāāāāāāāāāāāāā| 100% - loss: 0.2453 - acc: 92.30%
Validation |āāāāāāāāāāāāāāāāāāāā| 100% - loss: 0.3501 - acc: 87.50%
Prediction
Text: "The movie was a masterpiece!"
Prediction: Positive
š Model Evaluation
The final model accuracy on validation data is printed after training. Modify hyperparameters for different results and performance.
š Example Animation
Training Loop Animation
Epoch 1 |āāāāā | 30% - loss: 0.255
Epoch 2 |āāāāāāāāā | 55% - loss: 0.195
Epoch 3 |āāāāāāāāāāāāāā | 80% - loss: 0.145
Epoch 4 |āāāāāāāāāāāāāāāāā | 95% - loss: 0.110
...
š License
This project is licensed under the MIT License.
Happy Training! š
