-
Loading the Dataset:
- The code starts by importing necessary libraries and loading the IMDB dataset using Pandas.
-
Data Cleaning and Preprocessing:
- The 'review' column is cleaned by removing non-alphabetic characters and converting the text to lowercase.
- Stop words are removed using the NLTK library.
-
Data Splitting:
- The dataset is split into training and testing sets using the
train_test_split
function from scikit-learn.
- The dataset is split into training and testing sets using the
-
Label Encoding:
- Sentiment labels are encoded using
LabelEncoder
for both the training and testing sets.
- Sentiment labels are encoded using
-
Tokenization and Padding:
- Tokenization is performed using Keras's
Tokenizer
, and the sequences are padded to ensure uniform length usingpad_sequences
.
- Tokenization is performed using Keras's
-
Embedding Layer:
- The input sequences are passed through an Embedding layer to convert them into dense vectors of fixed size.
-
Transformer Block:
- The core of the model is a Transformer block, which consists of self-attention mechanisms. It's applied multiple times (
num_heads
) to capture different aspects of the input sequence.
- The core of the model is a Transformer block, which consists of self-attention mechanisms. It's applied multiple times (
-
Convolution and Global Average Pooling:
- After the attention mechanism, a 1D convolutional layer is applied to capture local patterns. Global Average Pooling is used to obtain a fixed-size output from the convolutional layer.
-
Dense Layers:
- Dropout is applied for regularization, and a Dense layer with ReLU activation is used to capture complex patterns. The final layer uses a sigmoid activation for binary sentiment classification.
- Compilation and Training:
- The model is compiled using the Adam optimizer and binary cross-entropy loss. It is then trained on the training data for a specified number of epochs.
-
Saving the Model:
- The trained model is saved in the HDF5 format for future use.
-
Model Evaluation:
- The model is evaluated on the test set, and metrics like accuracy and a classification report are printed.
-
User Input Prediction Function:
- A function
predict_sentiment_transformer
is defined to predict the sentiment of user input. The function processes the input similarly to the training data.
- A function
-
Interactive User Input:
- The user is prompted to enter a review, and the model predicts the sentiment (Negative, Neutral, or Positive).
This code showcases the implementation of a Transformer model for sentiment analysis, providing a comprehensive example for understanding and applying this architecture.