This project demonstrates how to build a deep learning model using an LSTM (Long Short-Term Memory) neural network for sentiment analysis of movie reviews. The model is trained on the IMDB dataset to classify movie reviews as positive or negative.
Sentiment analysis is a common natural language processing (NLP) task that involves classifying text into positive or negative sentiments. This project uses a deep learning approach with a Bidirectional LSTM neural network to perform sentiment analysis on movie reviews from the IMDB dataset. LSTMs are well-suited for this task because they are capable of learning long-term dependencies in sequential data.
- LSTM-based Neural Network: A deep learning model using LSTM layers to handle the sequential nature of text data.
- Bidirectional LSTM: Utilizes a bidirectional LSTM to capture dependencies from both forward and backward sequences.
- Embedding Layer: Converts words into dense vector representations to capture semantic relationships.
- Dropout Regularization: Prevents overfitting by randomly dropping neurons during training.
- Text Preprocessing: Automatic tokenization and padding to handle varying input lengths.
- Sentiment Prediction: Predicts whether a given movie review is positive or negative.
Ensure you have the following installed:
- Python 3.x
- TensorFlow
- Matplotlib
If you haven't installed TensorFlow and Matplotlib yet, you can do so using pip:
pip install tensorflow matplotlib
-
Clone the Repository:
Clone the repository to your local machine:
git clone https://github.com/your-username/sentiment-analysis-lstm.git
-
Navigate to the Directory:
Go to the project directory:
cd sentiment-analysis-lstm
-
Run the Script:
Run the script using Python:
python sentiment_analysis_lstm.py
When you run the script, it will:
- Load the IMDB dataset.
- Preprocess the text data (tokenization and padding).
- Define and compile an LSTM-based neural network model.
- Train the model on the training set and validate it on a validation set.
- Evaluate the model on the test set.
- Plot the training and validation accuracy and loss over epochs.
- Predict the sentiment of a new movie review sample.
The script will produce outputs similar to:
-
Test Accuracy: The accuracy of the model on the test dataset, for example:
Test Accuracy: 0.86
-
Training and Validation Curves: Plots of accuracy and loss over the training epochs.
-
Predicted Sentiment: Displays the predicted sentiment (positive or negative) for a new review.
Predicted Sentiment: Positive
To predict the sentiment of a new movie review, you can modify the new_review
variable in the script:
new_review = "This movie was fantastic! I really enjoyed the story and the acting was superb."
Run the script again to see the predicted sentiment.
Contributions are welcome! If you have ideas for new features, improvements, or bug fixes, please feel free to open an issue or create a pull request.
- Fork the Repository: Click the 'Fork' button at the top right of this page.
- Clone Your Fork: Clone your forked repository to your local machine.
git clone https://github.com/your-username/sentiment-analysis-lstm.git
- Create a Branch: Create a new branch for your feature or bug fix.
git checkout -b feature/your-feature-name
- Make Changes: Make your changes and commit them with a descriptive message.
git commit -m "Add: feature description"
- Push Changes: Push your changes to your forked repository.
git push origin feature/your-feature-name
- Create a Pull Request: Go to the original repository on GitHub and create a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
Thank you for using the Sentiment Analysis with LSTM! If you have any questions or feedback, feel free to reach out. Happy coding! 😊