Experiment 10
Experiment 10
## Title
## Aim
To implement a Recurrent Neural Network (RNN) for classifying IMDB movie reviews as
either positive or negative.
## Objectives
- Preprocess text data and convert it into sequences using word embeddings.
---
Below is the complete Python code to implement an RNN for sentiment classification
on the IMDB dataset:
```python
import tensorflow as tf
batch_size = 32
model = Sequential([
])
```
batch_size = 32
```
- The IMDB dataset contains 50,000 movie reviews (25,000 for training and 25,000 for
testing).
```python
```
- Reviews vary in length, so they are padded or truncated to a fixed length of 500
words.
- This ensures all input sequences have the same shape, which is required for the
RNN.
```python
model = Sequential([
])
```
- **Embedding Layer**: Converts word indices into dense vectors of size 32, learning
word representations during training.
- **SimpleRNN Layer**: A basic RNN with 32 units that processes the sequence and
captures temporal dependencies between words.
```python
```
```python
```
- Uses training data (`x_train`, `y_train`) and validates on test data (`x_test`, `y_test`)
after each epoch.
```python
```
- Evaluates the model on the test dataset and prints the test accuracy, showing
performance on unseen data.
---
## Expected Output
After training for 5 epochs, the output might look like this:
```
Epoch 1/5
Epoch 2/5
Epoch 3/5
Epoch 4/5
Epoch 5/5
```
The model typically achieves a test accuracy of around 85–87%, meaning it correctly
classifies reviews as positive or negative about 85% of the time.
---
## Conclusion
- The model effectively learns sentiment patterns, achieving good accuracy on the
test set.
This experiment demonstrates the power of RNNs in handling sequential data like text
for sentiment analysis tasks.