Practical File
ETCS 352 : ADVANCED ML LAB
Submitted in partial fulfilment of the requirement for the award of the Degree of
Bachelor in Technology in
Computer Science and Engineering
(Artificial Intelligence and Machine Learning)
By
Nakul
(Roll NO. 22BTC35138)
To
Dr. Ravinder Kumar
Shri Vishwakarma Skill University
Palwal, Haryana
LIST OF EXPERIMENTS
Exp. Aim of the Experiment Remark
No
1. Design a single unit perceptron for classification of a linearly separable binary
dataset without using pre-defined models. Use the Perceptron() from sklearn.
2. Identify the problem with single unit Perceptron. Classify using Or-, And and
Xor-ed data and analyze the result.
3. Build an Artificial Neural Network by implementing the Backpropagation algorithm
and test the same using appropriate data sets. Vary the activation functions used
and compare the results
4. Build a Deep Feed Forward ANN by implementing the Backpropagation
algorithm and test the same using appropriate data sets. Use the number of
hidden layers >=4.
5. For a given set of training data examples stored in a .CSV file, implement and
demonstrate the Candidate-Elimination algorithm to output a description of the
set of all hypotheses consistent with the training examples.
6. Write a program to demonstrate the working of the decision tree based ID3
Algorithm. Use an appropriate data set for building the decision tree and apply
this knowledge to classify a new sample.
7. Write a program to construct a Bayesian network considering medical data. Use
this model to demonstrate the diagnosis of heart patients using standard Heart
Disease Data Set. You can use Java/Python ML library classes/API.
8. Implement Generative Adversarial Networks to generate realistic Images. Use
MNIST, Fashion MNIST or any human face datasets.
9. Implement the standard LeNet-5 CNN architecture model to classify
multicategory image dataset (MNIST, Fashion MNIST) and check the accuracy.
10. Implement the standard VGG-16 & 19 CNN architecture model to classify multi
category image dataset and check the accuracy.
11. Implement RNN for sentiment analysis on movie reviews.
12. Implement Bidirectional LSTM for sentiment analysis on movie reviews.
13. Implement Generative Adversarial Networks to generate realistic Images. Use
MNIST, Fashion MNIST or any human face datasets.
14. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same
data set for clustering using k-Means algorithm. Compare the results of these two
algorithms and comment on the quality of clustering. You can add Java/Python
ML library classes/API in the program.
15. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same
data set for clustering using k-Means algorithm. Compare the results of these two
algorithms and comment on the quality of clustering. You can add Java/Python
ML library classes/API in the program.
16. Lab on Reinforcement.
17. An Ensemble Learning Model for COVID-19 Detection from Blood Test
Samples.
Experiment No: 1
Title: Design a single unit perceptron for classification of a linearly separable binary dataset
without using pre-defined models. Use the Perceptron() from sklearn.
Objective: To study about single unit perceptron by using a binary dataset.
Pre-requisites:
To be familiar with the basic concepts of Advance machine learning
Code/Algorithm/Theory:
Source Code:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Perceptron
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
# Generate a linearly separable dataset
X, y = make_classification(n_samples=100, n_features=2, n_classes=2, n_clusters_per_class=1,
n_redundant=0, random_state=42)
# Convert labels to {0, 1} for Perceptron
y = np.where(y == 0, -1, 1)
# Split dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize the dataset
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Train the Perceptron model
model = Perceptron(max_iter=1000, eta0=0.1, random_state=42)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f'Perceptron Accuracy: {accuracy:.2f}')
# Plot decision boundary
def plot_decision_boundary(X, y, model):
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.3)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Perceptron Decision Boundary')
plt.show()
plot_decision_boundary(X_test, y_test, model)
OUTPUT :
Experiment No: 2
Title: Identify the problem with single unit Perceptron. Classify using Or-, And and Xor-ed
data and analyze the result.
Objective: To study about the single unit perceptron using gates.
Pre-requisites:
To be familiar with the basic concepts of Advance Machine Learning
Code/Algorithm/Theory:
Source Code:
import numpy as np
# Define Activation Function
def step_function(x):
return 1 if x >= 0 else 0
# Perceptron Training
def perceptron_train(inputs, labels, learning_rate=0.1, epochs=100):
weights = np.zeros(inputs.shape[1])
bias = 0
for epoch in range(epochs):
for i in range(len(inputs)):
linear_output = np.dot(weights, inputs[i]) + bias
prediction = step_function(linear_output)
# Update weights and bias if the prediction is wrong
error = labels[i] - prediction
weights += learning_rate * error * inputs[i]
bias += learning_rate * error
return weights, bias
# Perceptron Test
def perceptron_test(inputs, weights, bias):
outputs = []
for x in inputs:
linear_output = np.dot(weights, x) + bias
outputs.append(step_function(linear_output))
return outputs
# Input Data for OR, AND, and XOR
inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
labels_or = np.array([0, 1, 1, 1])
labels_and = np.array([0, 0, 0, 1])
labels_xor = np.array([0, 1, 1, 0])
# Train Perceptron for OR Gate
weights_or, bias_or = perceptron_train(inputs, labels_or)
outputs_or = perceptron_test(inputs, weights_or, bias_or)
print("OR Gate Results:", outputs_or)
# Train Perceptron for AND Gate
weights_and, bias_and = perceptron_train(inputs, labels_and)
outputs_and = perceptron_test(inputs, weights_and, bias_and)
print("AND Gate Results:", outputs_and)
# Train Perceptron for XOR Gate
weights_xor, bias_xor = perceptron_train(inputs, labels_xor)
outputs_xor = perceptron_test(inputs, weights_xor, bias_xor)
print("XOR Gate Results (should fail):", outputs_xor)
Result:
Experiment No: 3
Title: Build an Artificial Neural Network by implementing the Backpropagation algorithm and test
the same using appropriate data sets. Vary the activation functions used and compare the results.
Objective: To study the use of ANN using Backpropagation with diff. Activation functions.
Pre-requisites:
To be familiar with the basic concepts of AML.
Code/Algorithm/Theory:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
# Activation Functions
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
def relu(x):
return np.maximum(0, x)
def relu_derivative(x):
return np.where(x > 0, 1, 0)
def tanh(x):
return np.tanh(x)
def tanh_derivative(x):
return 1 - x ** 2
# Backpropagation Function
def train_neural_network(X, y, activation, activation_derivative, epochs=1000,
learning_rate=0.01):
input_layer_neurons = X.shape[1]
hidden_layer_neurons = 5
output_layer_neurons = y.shape[1]
# Initialize Weights and Biases
hidden_weights = np.random.uniform(size=(input_layer_neurons, hidden_layer_neurons))
hidden_bias = np.random.uniform(size=(1, hidden_layer_neurons))
output_weights = np.random.uniform(size=(hidden_layer_neurons, output_layer_neurons))
output_bias = np.random.uniform(size=(1, output_layer_neurons))
for _ in range(epochs):
# Forward Propagation
hidden_layer_input = np.dot(X, hidden_weights) + hidden_bias
hidden_layer_output = activation(hidden_layer_input)
output_layer_input = np.dot(hidden_layer_output, output_weights) + output_bias
predicted_output = activation(output_layer_input)
# Backward Propagation
error = y - predicted_output
d_predicted_output = error * activation_derivative(predicted_output)
error_hidden_layer = d_predicted_output.dot(output_weights.T)
d_hidden_layer = error_hidden_layer * activation_derivative(hidden_layer_output)
# Update Weights and Biases
output_weights += hidden_layer_output.T.dot(d_predicted_output) * learning_rate
output_bias += np.sum(d_predicted_output, axis=0) * learning_rate
hidden_weights += X.T.dot(d_hidden_layer) * learning_rate
hidden_bias += np.sum(d_hidden_layer, axis=0) * learning_rate
return predicted_output
# Load Iris Dataset
iris = load_iris()
X = iris.data
y = iris.target.reshape(-1, 1)
# One-Hot Encode Labels
# One-Hot Encode Labels
encoder = OneHotEncoder(sparse_output=False)
y = encoder.fit_transform(y)
# Split Dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train and Test with Different Activation Functions
for activation, activation_derivative in [(sigmoid, sigmoid_derivative), (relu, relu_derivative),
(tanh, tanh_derivative)]:
predicted_output = train_neural_network(X_train, y_train, activation, activation_derivative)
print(f"Results with {activation.__name__} activation:")
print(predicted_output)
Result: 1. Sigmoid function:
2. Relu function: 3. Tanh Function:
Experiment No: 4
Title: Build a Deep Feed Forward ANN by implementing the Backpropagation algorithm and test the
same using appropriate data sets. Use the number of hidden layers >=4.
Objective: To study Deep forward network ANN by BPP Algorithm.
Pre-requisites:
To be familiar with the basic concepts of AML.
Code/Algorithm/Theory:
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
# Activation Functions and Derivatives
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
# Build and Train the Deep ANN
def train_deep_ann(X, y, hidden_layers, epochs=1000, learning_rate=0.01):
np.random.seed(42)
# Initialize weights and biases
layer_sizes = [X.shape[1]] + hidden_layers + [y.shape[1]]
weights = [np.random.rand(layer_sizes[i], layer_sizes[i+1]) for i in range(len(layer_sizes)-1)]
biases = [np.random.rand(1, layer_sizes[i+1]) for i in range(len(layer_sizes)-1)]
# Training Loop
for epoch in range(epochs):
# Forward Propagation
activations = [X]
for w, b in zip(weights, biases):
activations.append(sigmoid(np.dot(activations[-1], w) + b))
# Backward Propagation
deltas = [activations[-1] - y]
for i in range(len(weights)-1, 0, -1):
deltas.append(deltas[-1].dot(weights[i].T) * sigmoid_derivative(activations[i]))
deltas.reverse()
# Update Weights and Biases
for i in range(len(weights)):
weights[i] -= learning_rate * activations[i].T.dot(deltas[i])
biases[i] -= learning_rate * np.sum(deltas[i], axis=0, keepdims=True)
return weights, biases
# Predict Function
def predict(X, weights, biases):
activations = X
for w, b in zip(weights, biases):
activations = sigmoid(np.dot(activations, w) + b)
return np.argmax(activations, axis=1)
# Generate Synthetic Dataset
X, y = make_classification(n_samples=1000, n_features=10, n_classes=3, n_informative=3,
random_state=42)
y = y.reshape(-1, 1)
# One-Hot Encode Labels
encoder = OneHotEncoder(sparse_output=False)
y = encoder.fit_transform(y)
# Split Dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train Deep ANN with 4 Hidden Layers
hidden_layers = [16, 32, 32, 16]
weights, biases = train_deep_ann(X_train, y_train, hidden_layers)
# Test the Model
y_pred = predict(X_test, weights, biases)
y_test_labels = np.argmax(y_test, axis=1)
# Evaluate Performance
accuracy = np.mean(y_pred == y_test_labels)
print(f"Accuracy: {accuracy * 100:.2f}%")
Result:
Experiment No: 5
Title: For a given set of training data examples stored in a .CSV file, implement and demonstrate the
Candidate-Elimination algorithm to output a description of the set of all hypotheses consistent with
the training examples.
Objective: To study the Elimination Algorithm of a candidate from training dataset.
Pre-requisites:
To be familiar with the basic concepts of AML.
Code/Algorithm/Theory:
import pandas as pd
def candidate_elimination(data):
# Initialize S and G
S = ['0'] * (len(data.columns) - 1) # Specific boundary
G = [['?'] * (len(data.columns) - 1)] # General boundary
# Iterate through each training example
for index, row in data.iterrows():
if row['Play'] == 'Yes':
# Update S
for i in range(len(S)):
if S[i] == '0':
S[i] = row[i]
elif S[i] != row[i]:
S[i] = '?'
# Remove inconsistent hypotheses from G
G = [g for g in G if all(g[i] == '?' or g[i] == row[i] for i in range(len(g)))]
else:
# Update G
new_G = []
for g in G:
if all(g[i] == '?' or g[i] == row[i] for i in range(len(g))):
continue
for i in range(len(g)):
if g[i] != row[i]:
new_hypothesis = g.copy()
new_hypothesis[i] = '?' # Generalize
new_G.append(new_hypothesis)
G = new_G
return S, G
# Load the training data from an Excel file
data = pd.read_excel('training_data.csv.xlsx') # Use read_excel for .xlsx files
# Print the columns and the first few rows to diagnose the issue
print("Columns in DataFrame:", data.columns.tolist()) # Convert to list for better readability
print("First few rows of the DataFrame:")
print(data.head())
# Strip whitespace from column names
data.columns = data.columns.str.strip()
# Check if 'Play' column exists
if 'Play' not in data.columns:
raise KeyError("The 'Play' column does not exist in the DataFrame. Please check the column
names.")
# Run the Candidate-Elimination algorithm
S, G = candidate_elimination(data)
# Output the results
print("Specific Boundary (S):", S)
print("General Boundary (G):", G)
Result:
Experiment No: 6
Title: Write a program to demonstrate the working of the decision tree based ID3 Algorithm. Use an
appropriate data set for building the decision tree and apply this knowledge to classify a new sample.
Objective: To study about ID3 Algorithm.
Pre-requisites:
To be familiar with the basic concepts of AML.
Code/Algorithm/Theory:
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
import matplotlib.pyplot as plt
# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Convert to DataFrame for better visualization
iris_df = pd.DataFrame(data=X, columns=iris.feature_names)
iris_df['species'] = pd.Categorical.from_codes(y, iris.target_names)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a Decision Tree Classifier using the ID3 algorithm
clf = DecisionTreeClassifier(criterion='entropy', random_state=42) # 'entropy' is used for ID3
clf.fit(X_train, y_train)
# Evaluate the model
accuracy = clf.score(X_test, y_test)
print(f"Accuracy of the Decision Tree Classifier: {accuracy * 100:.2f}%")
# Visualize the Decision Tree
plt.figure(figsize=(12, 8))
tree.plot_tree(clf, filled=True, feature_names=iris.feature_names,
class_names=iris.target_names)
plt.title("Decision Tree using ID3 Algorithm")
plt.show()
# Classify a new sample
new_sample = [[5.0, 3.5, 1.5, 0.2]] # Example: Sepal length=5.0, Sepal width=3.5, Petal
length=1.5, Petal width=0.2
predicted_class = clf.predict(new_sample)
predicted_species = iris.target_names[predicted_class][0]
print(f"The predicted species for the new sample {new_sample} is: {predicted_species}")
Result:
Experiment No: 7
Title: Write a program to demonstrate the use of special functions, constructor and destructor
in the class template. The program is used to find the bigger of two entered numbers..
Objective: To study constructor in C++
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO5- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
Source Code:
#include <iostream>
using namespace std;
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
int main()
{
int i1, i2;
float f1, f2;
char c1, c2;
cout << "Enter two integers:\n";
cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;
cout << "\nEnter two floating-point numbers:\n";
cin >> f1 >> f2;
cout << Large(f1, f2) <<" is larger." << endl;
cout << "\nEnter two characters:\n";
cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";
return 0;
}
Experiment No: 8
Title: Write a program to perform the deletion of white spaces such as horizontal tab, vertical
tab, space, line feed, new line and carriage return from a text file and store the contents of the
file without the white spaces on another file.
Objective: To study files
Pre-requisites:
To be familiar with the basic concepts of C and C++ programming
Targeted CO:
CO5- Solve basic problems using concepts of object-oriented programming.
Code/Algorithm/Theory:
There are six types of whitespace characters by default:
1. Space (' ') - Space is the most common whitespace character. It is usually used to
separate two words.
2. Line feed ('\n') - The line feed character moves the cursor to the next line. It signifies the
end of a line of text.
3. Horizontal tab ('\t') - A horizontal tab moves the cursor to the next tab stop. It is usually
used for indentation purposes.
4. Vertical tab ('\v') - A vertical tab moves the cursor to the next vertical tab stop. It is an
outdated whitespace character and is rarely used anymore.
5. Carriage return ('\r') - The carriage return character is used to reset the cursor's
position to the start of a line of text.
6. Form feed ('\f') - A form feed is a page break character. It is used to end the
Source Code:
#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
int main(){
// Creating a string containing multiple whitespaces.
std::string s = "\tHello \n World";
std::cout << "String s before removing whitespaces: " << s << std::endl << std::endl;
// Using the erase, remove_if, and ::isspace functions.
s.erase(std::remove_if(s.begin(), s.end(), ::isspace),
s.end());
std::cout << "String s after removing whitespaces: " << s;
return 0;
#include <iostream>
#include <algorithm>
#include <locale>
#include <functional>
#include <string>
int main() {
// Creating a string.
std::string s = "\nWhitespace \t String\f";
std::cout << "String s before removing whitespaces: " << s <<
std::endl;
// Using the erase, remove_if, bind, and std::isspace functions.
s.erase(std::remove_if(s.begin(), s.end(), std::bind(std::isspace < char > ,
std::placeholders::_1,
std::locale::classic())),
s.end());
std::cout << "String s after removing whitespaces: " << s;
return 0;
#
include <iostream>
#include <string>
#include <algorithm>
// Function that returns true if a character is a whitespace.
bool isWhitespace(unsigned char c) {
if (c == ' ' || c == '\t' || c == '\n' ||
c == '\r' || c == '\f' || c == '\v') {
return true;
} else {
return false;
int main() {
std::string s = "\nWhitespace \t String ";
std::cout << "String s before removing whitespaces: " << s <<
std::endl << std::endl;
// Using the isWhitespace function we created.
s.erase(std::remove_if(s.begin(), s.end(), isWhitespace), s.end());
std::cout << "String s after removing whitespaces: " << s;
return 0;
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string s = "\rWhitespace String ";
std::cout << "String s before removing whitespaces: " << s <<
std::endl << std::endl;
// Removing whitespaces from string s.
s.erase(std::remove_if(s.begin(), s.end(),
[](char c) { // a lambda function
return (c == ' ' || c == '\n' || c == '\r' ||
c == '\t' || c == '\v' || c == '\f');
}),
s.end());
std::cout << "String s after removing whitespaces: " << s;
return 0;
Output:-
String s before removing whitespaces: Whitespace String
String s after removing whitespaces: WhitespaceString
/* Write a program to perform the deletion of white spaces such as horizontal tab,
vertical tab, space, line feed, new line and carriage return from a text file and store the
contents of the file without the white spaces on another file.
*/ #include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inputFile("input.txt");
ofstream outputFile("output.txt");
if (inputFile.is_open() && outputFile.is_open()) {
string line;
while (getline(inputFile, line)) {
for (char& c : line) {
if (!isspace(c)) {
outputFile << c;
inputFile.close();
outputFile.close();
cout << "White spaces removed and content saved to output.txt" << endl;
else {
cout << "Error opening files" << endl;
} return 0;
}
Experiment No: 9
Title: Implement the standard LeNet-5 CNN architecture model to classify multicategory image
dataset (MNIST, Fashion MNIST) and check the accuracy.
Objective: To study about LeNet-5 CNN Architecture.
Pre-requisites:
To be familiar with the basic concepts AML Lab.
Code/Algorithm/Theory:
Source Code:
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Preprocess the data
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# Define the LeNet-5 model
model = models.Sequential([
# C1 - Convolution Layer
layers.Conv2D(6, kernel_size=(5, 5), activation='tanh', input_shape=(28, 28, 1), padding='same'),
layers.AveragePooling2D(pool_size=(2, 2), strides=2),
# C3 - Convolution Layer
layers.Conv2D(16, kernel_size=(5, 5), activation='tanh', padding='valid'),
layers.AveragePooling2D(pool_size=(2, 2), strides=2),
# C5 - Fully Connected Convolution Layer
layers.Conv2D(120, kernel_size=(5, 5), activation='tanh', padding='valid'),
# Flatten and Fully Connected Layers
layers.Flatten(),
layers.Dense(84, activation='tanh'),
layers.Dense(10, activation='softmax') # Output layer
])
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=10, batch_size=32, validation_data=(x_test, y_test))
# Evaluate the model
loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Accuracy: {accuracy * 100:.2f}%")
Result :
Experiment No: 10
Title: Implement the standard VGG-16 & 19 CNN architecture model to classify multi category
image dataset and check the accuracy.
Objective: To study about VGG-16 & 19 CNN Architecture.
Pre-requisites:
To be familiar with the basic concepts AML Lab.
Code/Algorithm/Theory:
Source Code:
Experiment No: 11
Title: Implement RNN for sentiment analysis on movie reviews.
Objective: To study about RNN for sentimental analysis.
Pre-requisites:
To be familiar with the basic concepts AML Lab.
Code/Algorithm/Theory:
Source Code:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
from tensorflow.keras.preprocessing.sequence import pad_sequences
# Load the IMDB dataset
from tensorflow.keras.datasets import imdb
# Set the number of words to consider as features
vocab_size = 10000
maxlen = 200
# Load the data and keep only the top vocab_size words
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=vocab_size)
# Pad sequences to ensure uniform input length
X_train = pad_sequences(X_train, maxlen=maxlen)
X_test = pad_sequences(X_test, maxlen=maxlen)
# Build the RNN model
model = Sequential([
Embedding(input_dim=vocab_size, output_dim=128, input_length=maxlen),
LSTM(units=64, return_sequences=False),
Dropout(0.5),
Dense(units=1, activation='sigmoid') # Binary classification (positive/negative sentiment)
])
# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Summarize the model
model.summary()
# Train the model
batch_size = 32
epochs = 5
model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_test, y_test))
# Evaluate the model
loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test Loss: {loss}")
print(f"Test Accuracy: {accuracy}")
Result :