0% found this document useful (0 votes)
7 views52 pages

Aml File Sam

The document outlines the B.Tech program in AIML at Vivekananda Institute of Professional Studies, detailing the course 'Advances in Machine Learning' and its practical experiments. It includes objectives, methodologies, and source code for implementing deep neural networks, transfer learning, and other machine learning techniques. The institute emphasizes quality education and innovative practices to prepare future engineers for significant contributions to society.

Uploaded by

epencil32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views52 pages

Aml File Sam

The document outlines the B.Tech program in AIML at Vivekananda Institute of Professional Studies, detailing the course 'Advances in Machine Learning' and its practical experiments. It includes objectives, methodologies, and source code for implementing deep neural networks, transfer learning, and other machine learning techniques. The institute emphasizes quality education and innovative practices to prepare future engineers for significant contributions to society.

Uploaded by

epencil32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS

Grade A++ Accredited Institution by NAAC


NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution

SCHOOL OF ENGINEERING & TECHNOLOGY

B.TECH Programme: AIML

Course Title: Advances in Machine Learning

Course Code: AIML411P

Submitted To: Submitted By:


Dr. Alpana Name: Vaibhav Sharma
Assistant Professor Enrollment No: 20217711621
Section: B
Group: 2
VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

VISION OF INSTITUTE

To be an educational institute that empowers the field of engineering to build a


sustainable future by providing quality education with innovative practices that
supports people, planet and profit.

MISSION OF INSTITUTE

To groom the future engineers by providing value-based education and awakening


students' curiosity, nurturing creativity and building
capabilities to enable them to make significant contributions to the world.

2 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

INDEX
Updated
Faculty
Marks Remark Marks
Signature
(If any)
S.No Experiment Date
Class
Laboratory
Participation Viva (5
Assessment
(5 Marks) Marks)
(15 Marks)
Implement a deep neural network from
1 scratch using TensorFlow or PyTorch,
gaining Handson experience in building
complex neural architectures
Utilize pre-trained models and perform
2 transfer learning to solve real-world
problems efficiently.

Implement a GAN to generate synthetic


3 data and explore its applications in
image generation and data
augmentation.
Apply NLP techniques to process and
4 analyze textual data, including
sentiment analysis and named entity
recognition.
Build RL agents and train them using
5 OpenAI Gym or Stable Baselines to
solve challenging tasks.

Understand the interpretability of ML


6 models by using LIME or SHAP to
explain model predictions.

Use AutoML and Hyperparameter


7 tuning tools to automate the model
selection and optimization process.

Analyze time series data, perform


8 forecasting, and evaluate model
performance using Prophet or stats
models.
Compress and quantize large ML
9 models to make them suitable for
deployment on resource constrained
devices.
Explore federated learning concepts
10 and implement distributed ML
models using TensorFlow
Federated.

3 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Experiment No. 1

Problem Statement: Implement a deep neural network from scratch using TensorFlow or
PyTorch, gaining handson experience in building complex neural architectures.

Theory:
Implementing a deep neural network (DNN) from scratch involves constructing and training a
model composed of multiple layers, where each layer is responsible for learning hierarchical
patterns from the input data. A DNN typically includes an input layer, several hidden layers
(each containing neurons or units), and an output layer for predictions. In frameworks like
TensorFlow or PyTorch, you manually define each layer, activation functions (such as ReLU,
Sigmoid), and optimizers (e.g., Adam, SGD) for training. Backpropagation is used to update
weights through gradient descent based on the error from predictions. During training, the
network learns to minimize the loss function, thereby improving accuracy. This hands-on
approach gives deeper insight into how DNNs operate and allows for fine-tuning of architecture
and hyperparameters for better performance.

Source Code:
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt

df = pd.read_csv('/content/winequality-red.csv')
df.head()

train_df = df.sample(frac=0.75, random_state=42)


val_df = df.drop(train_df.index)

# Normalize the dataset


max_val = train_df.max(axis=0)
min_val = train_df.min(axis=0)
range_val = max_val - min_val

train_df = (train_df - min_val) / range_val


val_df = (val_df - min_val) / range_val

4 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

X_train = train_df.drop('quality', axis=1)


X_val = val_df.drop('quality', axis=1)
y_train = train_df['quality']
y_val = val_df['quality']

input_shape = [X_train.shape[1]]
print("Input shape:", input_shape)

model = tf.keras.Sequential([
tf.keras.layers.Dense(units=128, activation='relu',
input_shape=input_shape),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(units=64, activation='relu'),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(units=32, activation='relu'),
tf.keras.layers.Dense(units=1)
])

model.summary()

model.compile(optimizer='adam', loss='mae', metrics=['mse'])

early_stopping = tf.keras.callbacks.EarlyStopping(patience=5,
restore_best_weights=True)
history = model.fit(
X_train, y_train,
validation_data=(X_val, y_val),
batch_size=256,
epochs=50,
callbacks=[early_stopping]
)

predictions = model.predict(X_val.iloc[0:3, :])


print("Predictions:", predictions)
print("True Values:", y_val.iloc[0:3].values)

loss_df = pd.DataFrame(history.history)

5 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

loss_df[['loss', 'val_loss']].plot()
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.show()

Output:

6 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

7 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Learning Outcomes:

8 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Experiment No. 2

Problem Statement: Utilize pre-trained models and perform transfer learning to solve real-
world problems efficiently.

Theory:
Pre-trained models are neural networks trained on large datasets like ImageNet that capture
generic patterns applicable to many tasks. Transfer learning leverages these models by fine-
tuning them on smaller, task-specific datasets. This approach dramatically reduces training time
and computational costs while improving performance, especially when data is limited. By
freezing early layers (which capture basic features like edges or textures) and retraining the later
layers on new data, the model adapts to the target problem. Transfer learning is widely used in
applications such as image classification, object detection, and natural language processing.

Source Code:
import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
import tensorflow.keras.layers as tfl
from tensorflow.keras.preprocessing import image_dataset_from_directory
seed = 7
BATCH_SIZE = 32
IMG_SIZE = (160, 160)
directory = "/content/my_extracted_folder/rural_and_urban_photos/train"
train_dataset = image_dataset_from_directory(directory,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE,
validation_split=0.4,
subset='training',
seed=seed)
validation_dataset = image_dataset_from_directory(directory,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE,
validation_split=0.4,

9 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

subset='validation',
seed=seed)
class_names = train_dataset.class_names
plt.figure(figsize=(10, 10))
for images, labels in train_dataset.take(1):
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
AUTOTUNE = tf.data.experimental.AUTOTUNE
train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)
def data_augmenter():
data_augmentation = tf.keras.Sequential()
data_augmentation.add(tfl.RandomZoom(.5, .2))
data_augmentation.add(tfl.RandomFlip('horizontal'))
data_augmentation.add(tfl.RandomRotation(0.2))
return data_augmentation
data_augmentation = data_augmenter()
for image, _ in train_dataset.take(1):
plt.figure(figsize=(10, 10))
first_image = image[0]
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
augmented_image = data_augmentation(tf.expand_dims(first_image, 0))
plt.imshow(augmented_image[0] / 255)
plt.axis('off')
preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input

def classification_model(image_shape=IMG_SIZE,
data_augmentation=data_augmenter()):
input_shape = IMG_SIZE + (3,)
base_model = tf.keras.applications.MobileNetV2(input_shape=input_shape,
include_top=False,weights='imagenet')
base_model.trainable = False
inputs = tf.keras.Input(shape=input_shape)
x = data_augmentation(inputs)
x = preprocess_input(x)

10 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

x = base_model(x, training=False)
x = tfl.GlobalAveragePooling2D()(x)
x = tfl.Dropout(0.2)(x)
outputs = tfl.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
return model
model2 = classification_model(IMG_SIZE, data_augmentation)
base_learning_rate = 0.01
model2.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learn
ing_rate),
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
initial_epochs = 10
history = model2.fit(train_dataset, validation_data=validation_dataset,
epochs=initial_epochs)

acc = [0.] + history.history['accuracy']


val_acc = [0.] + history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()),1])
plt.title('Training and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Cross Entropy')
plt.ylim([0,1.0])
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()

11 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

#fine-tune
base_model = model2.layers[2]
base_model.trainable = True

print("Number of layers in the base model: ", len(base_model.layers))

fine_tune_at = 120

for layer in base_model.layers[:fine_tune_at]:


layer.trainable = False
loss_function = tf.keras.losses.BinaryCrossentropy(from_logits=True)
optimizer = tf.keras.optimizers.Adam(learning_rate=base_learning_rate *
0.1)

metrics=['accuracy']
model2.compile(loss=loss_function,
optimizer = optimizer,
metrics=metrics)

fine_tune_epochs = 5
total_epochs = initial_epochs + fine_tune_epochs
history_fine = model2.fit(train_dataset,
epochs=total_epochs,
initial_epoch=history.epoch[-1],
validation_data=validation_dataset)

#evaluate
directory = "/content/my_extracted_folder/rural_and_urban_photos/val"
test_dataset = image_dataset_from_directory(directory, shuffle=True,
batch_size=BATCH_SIZE, image_size=IMG_SIZE, validation_split=0.4,
subset='training', seed=seed)
model2.evaluate(test_dataset)

12 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Output:

13 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

14 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Learning Outcomes:

15 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Experiment No. 3

Problem Statement: Implement a GAN to generate synthetic data and explore its applications
in image generation and data augmentation.

Theory:
Generative Adversarial Networks (GANs) consist of two neural networks: a generator that
creates synthetic data and a discriminator that distinguishes between real and synthetic data. Both
networks are trained simultaneously in a competitive setting, with the generator improving to
"fool" the discriminator. GANs are popular for generating high-quality images, video frames,
and data augmentation. Applications include image synthesis (such as faces or artwork), super-
resolution, and filling gaps in missing data. GANs have proven useful in creating synthetic
datasets for training machine learning models when real data is scarce or expensive to collect.

Source Code:
Image Generation
import tensorflow as tf
import tensorflow_datasets as tfds
import numpy as np
import matplotlib.pyplot as plt

(train_data, test_data), ds_info = tfds.load('fashion_mnist',


split=['train', 'test'],
shuffle_files=True,
as_supervised=True, with_info=True)

def visualize_data(dataset):
plt.figure(figsize=(8, 8))
for i, (image, label) in enumerate(dataset.take(16)):
plt.subplot(4, 4, i + 1)
plt.imshow(image.numpy().squeeze(), cmap='gray')
plt.axis('off')
plt.show()

visualize_data(train_data)

16 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

def preprocess(image, label):


image = tf.cast(image, tf.float32)
image = (image - 127.5) / 127.5 # Normalize to [-1, 1]
return image, label

train_data = train_data.map(preprocess).shuffle(60000).batch(256)

LATENT_DIM = 100
def build_generator():
model = tf.keras.Sequential([
tf.keras.layers.Dense(7 * 7 * 256, use_bias=False,
input_shape=(LATENT_DIM,)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Reshape((7, 7, 256)),
tf.keras.layers.Conv2DTranspose(128, (5, 5), strides=(1, 1),
padding='same', use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Conv2DTranspose(64, (5, 5), strides=(2, 2),
padding='same', use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Conv2DTranspose(1, (5, 5), strides=(2, 2),
padding='same', use_bias=False, activation='tanh')
])
return model

def build_discriminator():
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same',
input_shape=[28, 28, 1]),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Conv2D(128, (5, 5), strides=(2, 2),
padding='same'),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Dropout(0.3),

17 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1)
])
return model

generator = build_generator()
discriminator = build_discriminator()

cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)

def generator_loss(fake_output):
return cross_entropy(tf.ones_like(fake_output), fake_output)

def discriminator_loss(real_output, fake_output):


real_loss = cross_entropy(tf.ones_like(real_output), real_output)
fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
return real_loss + fake_loss

generator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)

EPOCHS = 50
NOISE_DIM = 100
NUM_EXAMPLES_TO_GENERATE = 16

seed = tf.random.normal([NUM_EXAMPLES_TO_GENERATE, NOISE_DIM])

@tf.function
def train_step(images):
noise = tf.random.normal([BATCH_SIZE, NOISE_DIM])

with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:


generated_images = generator(noise, training=True)

real_output = discriminator(images, training=True)


fake_output = discriminator(generated_images, training=True)

gen_loss = generator_loss(fake_output)

18 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

disc_loss = discriminator_loss(real_output, fake_output)

gradients_of_generator = gen_tape.gradient(gen_loss,
generator.trainable_variables)
gradients_of_discriminator = disc_tape.gradient(disc_loss,
discriminator.trainable_variables)

generator_optimizer.apply_gradients(zip(gradients_of_generator,
generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator
, discriminator.trainable_variables))

def generate_and_save_images(model, epoch, test_input):


predictions = model(test_input, training=False)
fig = plt.figure(figsize=(4, 4))

for i in range(predictions.shape[0]):
plt.subplot(4, 4, i+1)
plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
plt.axis('off')

plt.savefig(f'image_at_epoch_{epoch:04d}.png')
plt.show()

def train(dataset, epochs):


for epoch in range(epochs):
for image_batch, _ in dataset:
train_step(image_batch)

if (epoch + 1) % 10 == 0:
print(f'Epoch {epoch + 1}/{epochs} completed')
generate_and_save_images(generator, epoch, seed)

train(train_data, EPOCHS)

generate_and_save_images(generator, EPOCHS, seed)

generator.save('fashion_mnist_generator.h5')

19 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

discriminator.save('fashion_mnist_discriminator.h5')

loaded_generator =
tf.keras.models.load_model('fashion_mnist_generator.h5')
loaded_discriminator =
tf.keras.models.load_model('fashion_mnist_discriminator.h5')

noise = tf.random.normal([NUM_EXAMPLES_TO_GENERATE, NOISE_DIM])


generated_images = loaded_generator(noise, training=False)

plt.figure(figsize=(4, 4))
for i in range(generated_images.shape[0]):
plt.subplot(4, 4, i + 1)
plt.imshow(generated_images[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
plt.axis('off')
plt.show()

Data Augmentation
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, accuracy_score

iris = datasets.load_iris()
X = iris.data
y = iris.target

scaler = MinMaxScaler()
X = scaler.fit_transform(X)

real_data = pd.DataFrame(X, columns=['a', 'b', 'c', 'd'])

20 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

real_labels = y

one_hot_encoder = OneHotEncoder(sparse_output=False)
one_hot_labels =
one_hot_encoder.fit_transform(np.array(real_labels).reshape(-1, 1))

NOISE_DIM = 100
NUM_CLASSES = 3
NUM_FEATURES = 4
BATCH_SIZE = 64
TRAINING_STEPS = 20000

class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.model = nn.Sequential(
nn.Linear(NOISE_DIM + NUM_CLASSES, 256),
nn.ReLU(),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, NUM_FEATURES),
nn.Tanh() # Use Tanh for output layer
)

def forward(self, noise, labels):


input = torch.cat((noise, labels), dim=1)
return self.model(input)

class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.model = nn.Sequential(
nn.Linear(NUM_FEATURES + NUM_CLASSES, 256),
nn.ReLU(),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, 1),

21 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

nn.Sigmoid()
)

def forward(self, data, labels):


input = torch.cat((data, labels), dim=1)
return self.model(input)

generator = Generator()
discriminator = Discriminator()

criterion = nn.BCELoss()
optimizer_G = optim.Adam(generator.parameters(), lr=0.0002)
optimizer_D = optim.Adam(discriminator.parameters(), lr=0.0002)

for step in range(TRAINING_STEPS):


idx = np.random.randint(0, real_data.shape[0], BATCH_SIZE)
real_batch = torch.tensor(real_data.iloc[idx].values,
dtype=torch.float32)
labels_batch = torch.tensor(one_hot_labels[idx], dtype=torch.float32)

noise = torch.randn(BATCH_SIZE, NOISE_DIM)


generated_batch = generator(noise, labels_batch).detach()

real_loss = criterion(discriminator(real_batch, labels_batch),


torch.ones((BATCH_SIZE, 1)))
fake_loss = criterion(discriminator(generated_batch, labels_batch),
torch.zeros((BATCH_SIZE, 1)))
discriminator_loss = (real_loss + fake_loss) / 2

optimizer_D.zero_grad()
discriminator_loss.backward()
optimizer_D.step()
noise = torch.randn(BATCH_SIZE, NOISE_DIM)
generator_loss = criterion(discriminator(generator(noise,
labels_batch), labels_batch), torch.ones((BATCH_SIZE, 1)))

optimizer_G.zero_grad()
generator_loss.backward()

22 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

optimizer_G.step()

if step % 500 == 0:
print(f"Step: {step}, Discriminator Loss:
{discriminator_loss.item()}, Generator Loss: {generator_loss.item()}")

def generate_data(generator, data_class, num_instances):


one_hot_class = one_hot_encoder.transform(np.array([[data_class]]))
one_hot_class_tensor = torch.tensor(np.repeat(one_hot_class,
num_instances, axis=0), dtype=torch.float32)
noise = torch.randn(num_instances, NOISE_DIM)
generated_data = generator(noise,
one_hot_class_tensor).detach().numpy()
return pd.DataFrame(generated_data, columns=['a', 'b', 'c', 'd'])

synthetic_data = pd.DataFrame()
for class_label in range(NUM_CLASSES):
class_data = generate_data(generator, class_label, 50)
synthetic_data = pd.concat([synthetic_data, class_data])

rf_real = RandomForestClassifier(n_estimators=100)
rf_real.fit(real_data.values, real_labels)
y_pred_real = rf_real.predict(real_data.values)
accuracy_real = accuracy_score(real_labels, y_pred_real)
print("Classification Report for Real Data:")
print(classification_report(real_labels, y_pred_real))
print(f"Accuracy for Real Data: {accuracy_real:.4f}")

synthetic_labels = np.random.choice(real_labels, synthetic_data.shape[0])


rf_synthetic = RandomForestClassifier(n_estimators=100)
rf_synthetic.fit(synthetic_data.values, synthetic_labels)
y_pred_synthetic_on_real_data = rf_synthetic.predict(real_data.values)
accuracy_synthetic_on_real_data = accuracy_score(real_labels,
y_pred_synthetic_on_real_data)
print("Classification Report for Synthetic Data:")
print(classification_report(real_labels, y_pred_synthetic_on_real_data))
print(f"Accuracy for Synthetic Data:
{accuracy_synthetic_on_real_data:.4f}")

23 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Output:
Image Generation
Training Data

Generated Images

24 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Data Augmentation

Learning Outcomes:

25 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Problem Statement: Apply NLP techniques to process and analyze textual data, including
sentiment analysis and named entity recognition.

Theory:
Natural Language Processing (NLP) involves using computational techniques to analyze and
understand human language. Common tasks include sentiment analysis (determining the
emotional tone of text), named entity recognition (extracting entities like names, locations, or
organizations), text summarization, and translation. Techniques like tokenization, word
embeddings (Word2Vec, GloVe), and attention mechanisms (such as in Transformer models)
allow machines to process textual data more effectively. NLP is applied in chatbots, search
engines, recommendation systems, and sentiment analysis for customer reviews, providing
valuable insights from unstructured text data.

Source Code:
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
import spacy

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)


maxlen = 200
x_train = pad_sequences(x_train, maxlen=maxlen)
x_test = pad_sequences(x_test, maxlen=maxlen)
train_reviews = pd.DataFrame({'review': [' '.join([str(word) for word in
review]) for review in x_train], 'sentiment': y_train})
model = Sequential()
model.add(Embedding(input_dim=10000, output_dim=128, input_length=maxlen))
model.add(LSTM(128))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])

26 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

model.fit(x_train, y_train, epochs=5, batch_size=64,


validation_data=(x_test, y_test))
loss, accuracy = model.evaluate(x_test, y_test)
print(f'Test Accuracy: {accuracy:.4f}')
nlp = spacy.load("en_core_web_sm")
example_reviews = [
"The movie Inception was directed by Christopher Nolan.",
"I loved the performance of Leonardo DiCaprio in The Revenant."
]
for review in example_reviews:
doc = nlp(review)
print(f"Review: {review}")
for ent in doc.ents:
print(f"Entity: {ent.text}, Label: {ent.label_}")

Output:

Learning Outcomes:

27 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Experiment No. 5
Problem Statement: Build RL agents and train them using OpenAI Gym or Stable Baselines
to solve challenging tasks.

Theory:

To build and train reinforcement learning (RL) agents in OpenAI Gym or Stable Baselines, we
can use various RL algorithms like Q-Learning, Policy Gradient Methods (such as
REINFORCE or A2C), or Deep Q-Networks (DQN), which allow agents to learn through
interactions with the environment. Here’s a brief theoretical outline and code example for
training an RL agent with Stable Baselines in the OpenAI Gym environment.

Key Concepts in RL Theory

1. Agent-Environment Interaction:
o An agent interacts with an environment in discrete time steps, choosing actions
based on the current state.
o The environment responds by providing the next state and a reward.
o The agent's goal is to maximize cumulative rewards over time (expected return).

2. Markov Decision Process (MDP):


o RL problems are often framed as MDPs where each state-action pair has an
associated probability and reward.
o The agent's policy π(a∣s)\pi(a|s)π(a∣s) represents the probability of taking action
aaa in state sss, and it seeks to improve this policy.

3. Policy-Based vs. Value-Based Methods:


o Value-Based Methods (e.g., DQN) estimate the value of each state-action pair to
determine optimal actions.
o Policy-Based Methods (e.g., A2C) directly optimize the policy, which can
perform better in continuous action spaces.

28 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Source Code:
import gymnasium as gym
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.common.evaluation import evaluate_policy
from stable_baselines3.common.callbacks import EvalCallback
import numpy as np
import matplotlib.pyplot as plt

class RLTrainer:
def __init__(self, env_name="CartPole-v1", total_timesteps=50000):
"""
Initialize the RL trainer with specified environment and training
parameters.

Args:
env_name (str): Name of the Gymnasium environment
total_timesteps (int): Total timesteps for training
"""
self.env_name = env_name
self.total_timesteps = total_timesteps

# Create environment
self.env = gym.make(env_name)
self.vec_env = DummyVecEnv([lambda: gym.make(env_name)])

# Initialize model
self.model = PPO(
"MlpPolicy",
self.vec_env,
learning_rate=0.0003,
n_steps=2048,
batch_size=64,
n_epochs=10,
gamma=0.99,
gae_lambda=0.95,
clip_range=0.2,
verbose=1

29 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

# Initialize evaluation environment


self.eval_env = gym.make(env_name)

def train(self):
"""Train the RL agent with evaluation callback"""
# Set up evaluation callback
eval_callback = EvalCallback(
self.eval_env,
best_model_save_path="./best_model",
log_path="./logs/",
eval_freq=1000,
deterministic=True,
render=False
)

# Train the agent


self.model.learn(
total_timesteps=self.total_timesteps,
callback=eval_callback
)

# Save the final model


self.model.save(f"{self.env_name}_final_model")

def evaluate(self, n_eval_episodes=10):


"""
Evaluate the trained agent

Args:
n_eval_episodes (int): Number of evaluation episodes

Returns:
tuple: Mean reward and standard deviation
"""
mean_reward, std_reward = evaluate_policy(
self.model,

30 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

self.eval_env,
n_eval_episodes=n_eval_episodes,
deterministic=True
)
return mean_reward, std_reward

def visualize_episode(self, max_steps=1000):


"""
Run a single episode and return the rewards for visualization

Args:
max_steps (int): Maximum steps per episode

Returns:
list: Episode rewards
"""
obs, _ = self.eval_env.reset()
done = False
rewards = []
step = 0

while not done and step < max_steps:


action, _ = self.model.predict(obs, deterministic=True)
obs, reward, terminated, truncated, _ =
self.eval_env.step(action)
done = terminated or truncated
rewards.append(reward)
step += 1

return rewards

def plot_episode_rewards(self, rewards):


"""
Plot the rewards from a single episode

Args:
rewards (list): List of rewards from an episode
"""

31 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

plt.figure(figsize=(10, 5))
plt.plot(rewards, label='Reward per step')
plt.title(f'Episode Rewards - {self.env_name}')
plt.xlabel('Step')
plt.ylabel('Reward')
plt.legend()
plt.grid(True)
plt.show()

# Example usage
def main():
# Initialize trainer
trainer = RLTrainer(env_name="CartPole-v1", total_timesteps=50000)

# Train the agent


print("Starting training...")
trainer.train()
print("Training completed!")

# Evaluate the agent


mean_reward, std_reward = trainer.evaluate()
print(f"Mean reward: {mean_reward:.2f} +/- {std_reward:.2f}")

# Visualize a single episode


rewards = trainer.visualize_episode()
trainer.plot_episode_rewards(rewards)

if __name__ == "__main__":
main()

32 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Output:

33 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

34 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Learning Outcomes:

35 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Experiment No. 6

Problem Statement: Understand the interpretability of ML models by using LIME or SHAP


to explain model predictions.

Theory:
Interpretable machine learning (IML) is essential for understanding how models make predictions,
especially in high-stakes domains like healthcare and finance. Techniques like LIME (Local
Interpretable Model-agnostic Explanations) and SHAP (SHapley Additive exPlanations) provide
insights into model behavior.
LIME focuses on local interpretability, generating interpretable models (typically linear) around
individual predictions to highlight feature contributions. It approximates the complex model's
behavior by perturbing input data and observing output changes.
Conversely, SHAP is grounded in cooperative game theory, offering a unified measure of feature
importance across individual predictions. It calculates the contribution of each feature to the
overall prediction using Shapley values, ensuring consistent and theoretically sound explanations.
Both methods empower stakeholders to trust and validate machine learning models by elucidating
how input features influence outputs, thus enhancing decision-making and accountability in model
deployment.

Source Code:
LIME:
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from lime import lime_tabular

iris = load_iris()
X = pd.DataFrame(data=iris.data, columns=iris.feature_names)
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

model = RandomForestClassifier()

36 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

model.fit(X_train, y_train)

explainer = lime_tabular.LimeTabularExplainer(X_train.values,
feature_names=X_train.columns, class_names=iris.target_names,
mode='classification')
idx = 0
exp = explainer.explain_instance(X_test.values[idx], model.predict_proba,
num_features=4)

exp.show_in_notebook(show_table=True)

SHAP:
import shap
import xgboost as xgb
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score

data = load_iris()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
model = xgb.XGBClassifier(use_label_encoder=False, eval_metric='mlogloss')
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)

shap.summary_plot(shap_values, X_test, feature_names=X_test.columns)

37 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Output:
(LIME)

(SHAP)

Learning Outcomes:

38 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Experiment No. 7

Problem Statement: Use AutoML and Hyperparameter tuning tools to automate the model
selection and optimization process.

Theory:
Using AutoML and hyperparameter tuning tools for reinforcement learning (RL) involves
automating the process of model selection and optimization to improve agent performance. Here’s
a concise theory:
1. Automated Algorithm Selection: AutoML can assist in selecting the most suitable RL
algorithm (e.g., PPO, DQN, SAC) based on the environment and task complexity. Each
algorithm has different strengths, and automating this choice can help identify the best-
suited one without manual trials.
2. Hyperparameter Optimization: Hyperparameters in RL, like learning rate, batch size,
and discount factor, can significantly impact performance. Libraries such as Optuna or
Ray Tune can be used to perform automated tuning by running trials that adjust these
parameters to maximize a predefined metric (e.g., average reward).
3. Search Space Definition: Define ranges or distributions for each hyperparameter,
allowing the tuning tool to explore various configurations. For example, learning rate may
be searched in a logarithmic scale, while network architecture parameters might vary by
layer size or count.
4. Optimization Methods: Tools like Optuna use techniques such as Bayesian
Optimization or Tree-structured Parzen Estimators (TPE) to efficiently explore the
search space and identify optimal configurations with fewer trials than brute-force grid or
random search.
5. Experiment Tracking and Early Stopping: Automated tools often include features to
track experiments and implement early stopping, ending unpromising trials early to save
computation time.

Source Code:
from tpot import TPOTClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

39 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

tpot = TPOTClassifier(verbosity=2, generations=5, population_size=20,


random_state=42)
tpot.fit(X_train, y_train)

print(tpot.score(X_test, y_test))

tpot.export('best_model.py')

from flaml import AutoML


from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

# Initialize and fit AutoML


automl = AutoML()
automl.fit(X_train, y_train, task="classification", time_budget=60)

y_pred = automl.predict(X_test)

40 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Learning Outcomes:

41 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Experiment No. 8

Problem Statement: Analyze time series data, perform forecasting, and evaluate model
performance using Prophet or statsmodels .

Theory:

Analyzing and forecasting time series data using Prophet or statsmodels involves a systematic
approach to identify trends, seasonality, and make predictions. Here’s a concise theoretical
framework:

1. Data Preprocessing:
o Convert the data into a time series format, ensuring consistent intervals (e.g.,
daily, monthly).
o Handle any missing values, and create time-based features if needed.
2. Model Selection:
o Prophet: Developed by Facebook, Prophet is highly effective for time series data
with strong seasonal effects and robust to missing data or outliers. It decomposes
time series into trend, seasonality, and holiday effects, making it well-suited for
forecasting with complex seasonality.
o statsmodels (e.g., ARIMA): The ARIMA (Auto-Regressive Integrated Moving
Average) model in statsmodels is commonly used for time series with patterns of
auto-correlation and no strong seasonal component. SARIMA (Seasonal ARIMA)
extends ARIMA for seasonality.
3. Model Training and Forecasting:
o Prophet: Fits trend, seasonality, and holiday components using additive or
multiplicative models. Once trained, it forecasts future values and can visualize
each component separately.
o ARIMA/SARIMA: Uses past values and error terms to model the time series and
create future predictions. ARIMA orders (p, d, q) are chosen based on auto-
correlation and differencing to ensure stationarity.
4. Model Evaluation:
o Common metrics for evaluating forecast accuracy include Mean Absolute Error
(MAE), Mean Squared Error (MSE), or Mean Absolute Percentage Error
(MAPE).
o Cross-validation or splitting the data into training and test sets is recommended to
assess model accuracy and avoid overfitting.
5. Interpretation:

42 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

o Plot predictions against actual values and visualize model components (trend,
seasonality) to interpret the results and assess model fit.

Source Code:
!pip install prophet

import pandas as pd
from prophet import Prophet
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt

df = pd.DataFrame({
'date': pd.date_range(start='2020-01-01', periods=100),
'value': range(100)
})

df.rename(columns={'date': 'ds', 'value': 'y'}, inplace=True)

train = df.iloc[:-20]
test = df.iloc[-20:]

model = Prophet()
model.fit(train)

future = model.make_future_dataframe(periods=20)
forecast = model.predict(future)

fig = model.plot(forecast)

43 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

plt.title('Prophet Forecast')
plt.show()

y_true = test['y'].values
y_pred = forecast['yhat'][-20:].values
mse = mean_squared_error(y_true, y_pred)
print(f'Mean Squared Error: {mse}')

Output:

Learning Outcomes:

44 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Experiment No. 9

Problem Statement: Compress and quantize large ML models to make them suitable for
deployment on resource constrained devices.

Theory:

Deploying large machine learning (ML) models on resource-constrained devices, such as mobile
or edge devices, can be challenging due to limited memory, storage, and processing power.
Model compression and quantization are two widely used techniques to optimize models for
such environments.

Key Concepts in Model Compression and Quantization

1. Model Compression:
o Compression reduces the number of parameters in a model, which decreases
memory usage and inference latency.
o Pruning is a common compression technique that removes unnecessary weights
or neurons from the model, reducing the model size while minimally impacting
accuracy.
2. Quantization:
o Quantization reduces the precision of the model parameters, often from 32-bit
floating-point (FP32) to lower bit widths (e.g., 16-bit or 8-bit integers).
o By representing weights and activations with lower precision, the model requires
less memory and can execute faster on hardware that supports integer arithmetic.

Source Code:
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.utils.prune as prune
import torch.quantization as quantization
import matplotlib.pyplot as plt

class SimpleModel(nn.Module):
def __init__(self):

45 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

super(SimpleModel, self).__init__()
self.fc1 = nn.Linear(784, 256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 10)

def forward(self, x):


x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
return self.fc3(x)

model = SimpleModel()

def plot_weight_distribution(tensor, title):


plt.hist(tensor.cpu().detach().numpy().ravel(), bins=50)
plt.title(title)
plt.xlabel("Weight values")
plt.ylabel("Frequency")
plt.show()

plot_weight_distribution(model.fc1.weight, "Original Weights of fc1


Layer")

for module in [model.fc1, model.fc2, model.fc3]:


prune.l1_unstructured(module, name="weight", amount=0.2)

plot_weight_distribution(model.fc1.weight, "Pruned Weights of fc1 Layer")

for module in [model.fc1, model.fc2, model.fc3]:


prune.remove(module, "weight")

model.eval()

model.qconfig = quantization.get_default_qconfig("fbgemm")

quantization.prepare(model, inplace=True)

dummy_input = torch.randn(1, 784)


with torch.no_grad():

46 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

model(dummy_input)

quantization.convert(model, inplace=True)

plot_weight_distribution(model.fc1.weight().dequantize(), "Quantized
Weights of fc1 Layer")

Output:

47 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Learning Outcomes:

48 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Experiment No. 10

Problem Statement: Explore federated learning concepts and implement distributed ML


models using TensorFlow Federated.

Theory:
Federated Learning (FL) is a distributed approach that enables training machine learning models
directly on decentralized data sources (e.g., mobile devices or local servers) without sharing raw data.
This approach is privacy-preserving because it sends only model updates (e.g., gradients or model
parameters) rather than the data itself to a central server. The server aggregates these updates to
improve the global model iteratively.
Key Concepts in Federated Learning
1. Client-Server Architecture:
o Clients: Devices that hold local data. Each client trains a local model on its data and
sends updates to the server.
o Server: Receives model updates from clients, aggregates them, and sends the
improved global model back to clients.
2. Federated Averaging (FedAvg):
o The server aggregates model updates using a weighted average, which considers the
number of samples each client has. This method is efficient and commonly used in
FL.
3. Privacy and Security:
o Techniques like differential privacy and secure aggregation are often used to
ensure privacy by adding noise to the updates or encrypting them during transmission.

Source Code:
import collections
import tensorflow as tf
import tensorflow_federated as tff

# Load simulation data from EMNIST dataset


source, _ = tff.simulation.datasets.emnist.load_data()

def client_data(n):
return source.create_tf_dataset_for_client(source.client_ids[n]).map(

49 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

lambda e: (tf.reshape(e['pixels'], [-1]), e['label'])


).repeat(10).batch(20)

# Select a subset of clients to simulate training


train_data = [client_data(n) for n in range(3)]

# Define a more complex Keras model for classification


keras_model = tf.keras.models.Sequential([
tf.keras.layers.Dense(128, activation='relu', input_shape=(784,),
kernel_initializer='he_uniform'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])

# Wrap the Keras model for use with TFF


tff_model = tff.learning.models.functional_model_from_keras(
keras_model,
loss_fn=tf.keras.losses.SparseCategoricalCrossentropy(),
input_spec=train_data[0].element_spec,
metrics_constructor=collections.OrderedDict(
accuracy=tf.keras.metrics.SparseCategoricalAccuracy)
)

# Build the federated averaging algorithm with a different learning rate


trainer = tff.learning.algorithms.build_weighted_fed_avg(
tff_model,
client_optimizer_fn=tff.learning.optimizers.build_sgdm(learning_rate=0
.01)
)

state = trainer.initialize()

for round_num in range(100):


result = trainer.next(state, train_data)
state = result.state
metrics = result.metrics
print(f'Round {round_num + 1}, Training Accuracy:
{metrics["client_work"]["train"]["accuracy"]:.4f}')

50 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Output:

51 Vaibhav Sharma | 20217711621 |


VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES - TECHNICAL CAMPUS
Grade A++ Accredited Institution by NAAC
NBA Accredited for MCA Programme; Recognized under Section 2(f) by UGC;
Affiliated to GGSIP University, Delhi; Recognized by Bar Council of India and AICTE
An ISO 9001:2015 Certified Institution
SCHOOL OF ENGINEERING & TECHNOLOGY

Learning Outcomes:

52 Vaibhav Sharma | 20217711621 |

You might also like