The code is described step by step. Follow each steps properly.
Google Coolab accessing
Copy and paste the following link to open google colab
https://colab.research.google.com/notebooks/welcome.ipynb
Then click File --> New notebook after that Click Runtime-->Changes runtime to T4 GPU
Sentemental Analysis Code Explanation
You can find the dataset link here 👉 LINK_OF_DATASET
Coolab link can be found in here 👉 LINK_OF_CODE
CODE
!pip install tensorflow
import tensorflow as tf
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
Install Tensorflow to use its libraries.
import Sequential which allows you to build a linear stack of layers for LSTM.
Embedding Converts integer-encoded words into dense vectors of fixed size.
LSTM Implements Long Short-Term Memory layers, useful for processing sequential data.
Dense Creates fully connected layers in the neural network.
Dropout Helps prevent overfitting by randomly setting a fraction of input units to zero during training.
pad_sequences which is used to ensure all input sequences are the same length.
Tokenizer used to convert text into sequences of integers based on word frequency.
df = pd.read_csv('sentimental_dataset.csv')
X = df['review_text']
y = df['class_index']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
VOCAB_SIZE = 10000
MAX_SEQUENCE_LENGTH = 100
tokenizer = Tokenizer(num_words=VOCAB_SIZE)
tokenizer.fit_on_texts(X_train)
X_train_seq = tokenizer.texts_to_sequences(X_train)
X_test_seq = tokenizer.texts_to_sequences(X_test)
X_train_pad = pad_sequences(X_train_seq, maxlen=MAX_SEQUENCE_LENGTH)
X_test_pad = pad_sequences(X_test_seq, maxlen=MAX_SEQUENCE_LENGTH)
VOCAB_SIZE and MAX_SEQUENCE_LENGTH set limits on unique words and sequence lengths.
Tokenizer converts text to integer sequences with texts_to_sequences Function.
pad_sequences padded for uniform length.
model = Sequential()
EMBEDDING_DIM = 128
model.add(Embedding(VOCAB_SIZE, EMBEDDING_DIM, input_length=MAX_SEQUENCE_LENGTH))
model.add(LSTM(64))
model.add(Dense(24, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=tf.keras.optimizers.Adam(1e-4),
metrics=['accuracy'])
model.summary()
Sequential model is constructed with the following layers.
Embedding Layer: Converts word indices into dense vectors.
LSTM Layer: Processes sequences with 64 units.
Dense Layer: A hidden layer with 24 neurons and ReLU activation.
Output Dense Layer: A single neuron with a sigmoid activation for binary classification.
Model Compilation: The model is compiled with binary cross-entropy loss, the Adam optimizer, and accuracy as a metric.
Model Summary: The architecture of the model is displayed.
model.fit(X_train_pad, y_train, epochs=10, batch_size=32, validation_split=0.2)
loss, accuracy = model.evaluate(X_test_pad, y_test)
print(f'Test Loss: {loss:.4f}, Test Accuracy: {accuracy:.4f}')
Model fit [Training]: The model is trained on the padded training data for 10 epochs.
32 samples of data at a time before updating its weights.
using 20% of the training data for validation.
Model Evaluation: The model's performance is evaluated on the test set, printing the loss and accuracy.
END PART