0% found this document useful (0 votes)
16 views50 pages

Advances in Machine Learning Course

Uploaded by

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

Advances in Machine Learning Course

Uploaded by

05717711621ml
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

[Link] Programme: AIML

Course Title: Advances in Machine Learning

Course Code: AIML411P

Submitted To: Submitted By:


Dr. Alpana Name: Samridhi Bisht
Assistant Professor Enrollment No: 07417711621
Section: B
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

Group: 1

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 Samridhi Bisht | 07417711621 |


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)
[Link] Experiment Date
Class
Laboratory
Participation Viva (5
Assessment
(5 Marks) Marks)
(15 Marks)

.
2

10

Experiment No. 1

3 Samridhi Bisht | 07417711621 |


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: 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 [Link] as plt

df = pd.read_csv('/content/[Link]')
[Link]()

train_df = [Link](frac=0.75, random_state=42)


val_df = [Link](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

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']

4 Samridhi Bisht | 07417711621 |


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

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

model = [Link]([
[Link](units=128, activation='relu',
input_shape=input_shape),
[Link](0.3),
[Link](units=64, activation='relu'),
[Link](0.3),
[Link](units=32, activation='relu'),
[Link](units=1)
])

[Link]()

[Link](optimizer='adam', loss='mae', metrics=['mse'])

early_stopping = [Link](patience=5,
restore_best_weights=True)
history = [Link](
X_train, y_train,
validation_data=(X_val, y_val),
batch_size=256,
epochs=50,
callbacks=[early_stopping]
)

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


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

loss_df = [Link]([Link])
loss_df[['loss', 'val_loss']].plot()
[Link]('Training and Validation Loss')
[Link]('Epochs')
[Link]('Loss')
[Link]()

Output:

5 Samridhi Bisht | 07417711621 |


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

6 Samridhi Bisht | 07417711621 |


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 Samridhi Bisht | 07417711621 |


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:

Experiment No. 2

8 Samridhi Bisht | 07417711621 |


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: 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 [Link] as plt
import numpy as np
import os
import tensorflow as tf
import [Link] as tfl
from [Link] 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,
subset='validation',
seed=seed)
class_names = train_dataset.class_names
[Link](figsize=(10, 10))
for images, labels in train_dataset.take(1):
for i in range(9):

9 Samridhi Bisht | 07417711621 |


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

ax = [Link](3, 3, i + 1)
[Link](images[i].numpy().astype("uint8"))
[Link](class_names[labels[i]])
[Link]("off")
AUTOTUNE = [Link]
train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)
def data_augmenter():
data_augmentation = [Link]()
data_augmentation.add([Link](.5, .2))
data_augmentation.add([Link]('horizontal'))
data_augmentation.add([Link](0.2))
return data_augmentation
data_augmentation = data_augmenter()
for image, _ in train_dataset.take(1):
[Link](figsize=(10, 10))
first_image = image[0]
for i in range(9):
ax = [Link](3, 3, i + 1)
augmented_image = data_augmentation(tf.expand_dims(first_image, 0))
[Link](augmented_image[0] / 255)
[Link]('off')
preprocess_input = [Link].mobilenet_v2.preprocess_input

def classification_model(image_shape=IMG_SIZE,
data_augmentation=data_augmenter()):
input_shape = IMG_SIZE + (3,)
base_model = [Link].MobileNetV2(input_shape=input_shape,
include_top=False,weights='imagenet')
base_model.trainable = False
inputs = [Link](shape=input_shape)
x = data_augmentation(inputs)
x = preprocess_input(x)
x = base_model(x, training=False)
x = tfl.GlobalAveragePooling2D()(x)
x = [Link](0.2)(x)
outputs = [Link](1)(x)
model = [Link](inputs, outputs)
return model
model2 = classification_model(IMG_SIZE, data_augmentation)
base_learning_rate = 0.01
[Link](optimizer=[Link](learning_rate=base_learn
ing_rate),

10 Samridhi Bisht | 07417711621 |


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=[Link](from_logits=True),
metrics=['accuracy'])
initial_epochs = 10
history = [Link](train_dataset, validation_data=validation_dataset,
epochs=initial_epochs)

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


val_acc = [0.] + [Link]['val_accuracy']
loss = [Link]['loss']
val_loss = [Link]['val_loss']
[Link](figsize=(8, 8))
[Link](2, 1, 1)
[Link](acc, label='Training Accuracy')
[Link](val_acc, label='Validation Accuracy')
[Link](loc='lower right')
[Link]('Accuracy')
[Link]([min([Link]()),1])
[Link]('Training and Validation Accuracy')
[Link](2, 1, 2)
[Link](loss, label='Training Loss')
[Link](val_loss, label='Validation Loss')
[Link](loc='upper right')
[Link]('Cross Entropy')
[Link]([0,1.0])
[Link]('Training and Validation Loss')
[Link]('epoch')
[Link]()

#fine-tune
base_model = [Link][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]:


[Link] = False
loss_function = [Link](from_logits=True)
optimizer = [Link](learning_rate=base_learning_rate *
0.1)

11 Samridhi Bisht | 07417711621 |


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

metrics=['accuracy']
[Link](loss=loss_function,
optimizer = optimizer,
metrics=metrics)

fine_tune_epochs = 5
total_epochs = initial_epochs + fine_tune_epochs
history_fine = [Link](train_dataset,
epochs=total_epochs,
initial_epoch=[Link][-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)
[Link](test_dataset)

Output:

12 Samridhi Bisht | 07417711621 |


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

13 Samridhi Bisht | 07417711621 |


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 Samridhi Bisht | 07417711621 |


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:

Experiment No. 3

15 Samridhi Bisht | 07417711621 |


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: 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 [Link] as plt

(train_data, test_data), ds_info = [Link]('fashion_mnist',


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

def visualize_data(dataset):
[Link](figsize=(8, 8))
for i, (image, label) in enumerate([Link](16)):
[Link](4, 4, i + 1)
[Link]([Link]().squeeze(), cmap='gray')
[Link]('off')
[Link]()

visualize_data(train_data)

def preprocess(image, label):


image = [Link](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)

16 Samridhi Bisht | 07417711621 |


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

LATENT_DIM = 100
def build_generator():
model = [Link]([
[Link](7 * 7 * 256, use_bias=False,
input_shape=(LATENT_DIM,)),
[Link](),
[Link](),
[Link]((7, 7, 256)),
[Link].Conv2DTranspose(128, (5, 5), strides=(1, 1),
padding='same', use_bias=False),
[Link](),
[Link](),
[Link].Conv2DTranspose(64, (5, 5), strides=(2, 2),
padding='same', use_bias=False),
[Link](),
[Link](),
[Link].Conv2DTranspose(1, (5, 5), strides=(2, 2),
padding='same', use_bias=False, activation='tanh')
])
return model

def build_discriminator():
model = [Link]([
[Link].Conv2D(64, (5, 5), strides=(2, 2), padding='same',
input_shape=[28, 28, 1]),
[Link](),
[Link](0.3),
[Link].Conv2D(128, (5, 5), strides=(2, 2),
padding='same'),
[Link](),
[Link](0.3),
[Link](),
[Link](1)
])
return model

generator = build_generator()
discriminator = build_discriminator()

cross_entropy = [Link](from_logits=True)

17 Samridhi Bisht | 07417711621 |


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 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 = [Link](1e-4)
discriminator_optimizer = [Link](1e-4)

EPOCHS = 50
NOISE_DIM = 100
NUM_EXAMPLES_TO_GENERATE = 16

seed = [Link]([NUM_EXAMPLES_TO_GENERATE, NOISE_DIM])

@[Link]
def train_step(images):
noise = [Link]([BATCH_SIZE, NOISE_DIM])

with [Link]() as gen_tape, [Link]() 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)
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):

18 Samridhi Bisht | 07417711621 |


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

predictions = model(test_input, training=False)


fig = [Link](figsize=(4, 4))

for i in range([Link][0]):
[Link](4, 4, i+1)
[Link](predictions[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
[Link]('off')

[Link](f'image_at_epoch_{epoch:04d}.png')
[Link]()

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)

[Link]('fashion_mnist_generator.h5')
[Link]('fashion_mnist_discriminator.h5')

loaded_generator =
[Link].load_model('fashion_mnist_generator.h5')
loaded_discriminator =
[Link].load_model('fashion_mnist_discriminator.h5')

noise = [Link]([NUM_EXAMPLES_TO_GENERATE, NOISE_DIM])


generated_images = loaded_generator(noise, training=False)

[Link](figsize=(4, 4))
for i in range(generated_images.shape[0]):
[Link](4, 4, i + 1)
[Link](generated_images[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
[Link]('off')
[Link]()

19 Samridhi Bisht | 07417711621 |


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
import numpy as np
import pandas as pd
import torch
import [Link] as nn
import [Link] as optim
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import MinMaxScaler, OneHotEncoder
from [Link] import RandomForestClassifier
from [Link] import classification_report, accuracy_score

iris = datasets.load_iris()
X = [Link]
y = [Link]

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

real_data = [Link](X, columns=['a', 'b', 'c', 'd'])


real_labels = y

one_hot_encoder = OneHotEncoder(sparse_output=False)
one_hot_labels =
one_hot_encoder.fit_transform([Link](real_labels).reshape(-1, 1))

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

class Generator([Link]):
def __init__(self):
super(Generator, self).__init__()
[Link] = [Link](
[Link](NOISE_DIM + NUM_CLASSES, 256),
[Link](),
[Link](256, 128),
[Link](),

20 Samridhi Bisht | 07417711621 |


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

[Link](128, NUM_FEATURES),
[Link]() # Use Tanh for output layer
)

def forward(self, noise, labels):


input = [Link]((noise, labels), dim=1)
return [Link](input)

class Discriminator([Link]):
def __init__(self):
super(Discriminator, self).__init__()
[Link] = [Link](
[Link](NUM_FEATURES + NUM_CLASSES, 256),
[Link](),
[Link](256, 128),
[Link](),
[Link](128, 1),
[Link]()
)

def forward(self, data, labels):


input = [Link]((data, labels), dim=1)
return [Link](input)

generator = Generator()
discriminator = Discriminator()

criterion = [Link]()
optimizer_G = [Link]([Link](), lr=0.0002)
optimizer_D = [Link]([Link](), lr=0.0002)

for step in range(TRAINING_STEPS):


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

noise = [Link](BATCH_SIZE, NOISE_DIM)


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

real_loss = criterion(discriminator(real_batch, labels_batch),


[Link]((BATCH_SIZE, 1)))

21 Samridhi Bisht | 07417711621 |


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

fake_loss = criterion(discriminator(generated_batch, labels_batch),


[Link]((BATCH_SIZE, 1)))
discriminator_loss = (real_loss + fake_loss) / 2

optimizer_D.zero_grad()
discriminator_loss.backward()
optimizer_D.step()
noise = [Link](BATCH_SIZE, NOISE_DIM)
generator_loss = criterion(discriminator(generator(noise,
labels_batch), labels_batch), [Link]((BATCH_SIZE, 1)))

optimizer_G.zero_grad()
generator_loss.backward()
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([Link]([[data_class]]))
one_hot_class_tensor = [Link]([Link](one_hot_class,
num_instances, axis=0), dtype=torch.float32)
noise = [Link](num_instances, NOISE_DIM)
generated_data = generator(noise,
one_hot_class_tensor).detach().numpy()
return [Link](generated_data, columns=['a', 'b', 'c', 'd'])

synthetic_data = [Link]()
for class_label in range(NUM_CLASSES):
class_data = generate_data(generator, class_label, 50)
synthetic_data = [Link]([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 = [Link](real_labels, synthetic_data.shape[0])

22 Samridhi Bisht | 07417711621 |


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

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}")
Output:
Image Generation
Training Data

Generated Images

23 Samridhi Bisht | 07417711621 |


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

24 Samridhi Bisht | 07417711621 |


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:

Experiment No. 4

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

25 Samridhi Bisht | 07417711621 |


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

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 [Link] import imdb
from [Link] import pad_sequences
from [Link] import Sequential
from [Link] 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 = [Link]({'review': [' '.join([str(word) for word in
review]) for review in x_train], 'sentiment': y_train})
model = Sequential()
[Link](Embedding(input_dim=10000, output_dim=128, input_length=maxlen))
[Link](LSTM(128))
[Link](Dense(1, activation='sigmoid'))
[Link](loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
[Link](x_train, y_train, epochs=5, batch_size=64,
validation_data=(x_test, y_test))
loss, accuracy = [Link](x_test, y_test)
print(f'Test Accuracy: {accuracy:.4f}')
nlp = [Link]("en_core_web_sm")
example_reviews = [
"The movie Inception was directed by Christopher Nolan.",
"I loved the performance of Leonardo DiCaprio in The Revenant."
]

26 Samridhi Bisht | 07417711621 |


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

for review in example_reviews:


doc = nlp(review)
print(f"Review: {review}")
for ent in [Link]:
print(f"Entity: {[Link]}, Label: {ent.label_}")

Output:

Learning Outcomes:

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

27 Samridhi Bisht | 07417711621 |


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

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.

Source Code:
import gymnasium as gym
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.[Link] import evaluate_policy
from stable_baselines3.[Link] import EvalCallback
import numpy as np
import [Link] 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

28 Samridhi Bisht | 07417711621 |


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.env_name = env_name
self.total_timesteps = total_timesteps

# Create environment
[Link] = [Link](env_name)
self.vec_env = DummyVecEnv([lambda: [Link](env_name)])

# Initialize model
[Link] = 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
)

# Initialize evaluation environment


self.eval_env = [Link](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


[Link](
total_timesteps=self.total_timesteps,
callback=eval_callback
)

29 Samridhi Bisht | 07417711621 |


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

# Save the final model


[Link](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(
[Link],
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, _ = [Link](obs, deterministic=True)
obs, reward, terminated, truncated, _ =
self.eval_env.step(action)
done = terminated or truncated

30 Samridhi Bisht | 07417711621 |


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

[Link](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
"""
[Link](figsize=(10, 5))
[Link](rewards, label='Reward per step')
[Link](f'Episode Rewards - {self.env_name}')
[Link]('Step')
[Link]('Reward')
[Link]()
[Link](True)
[Link]()

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

# Train the agent


print("Starting training...")
[Link]()
print("Training completed!")

# Evaluate the agent


mean_reward, std_reward = [Link]()
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()

31 Samridhi Bisht | 07417711621 |


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:

32 Samridhi Bisht | 07417711621 |


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

33 Samridhi Bisht | 07417711621 |


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:

Experiment No. 6

34 Samridhi Bisht | 07417711621 |


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: 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 [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import RandomForestClassifier
from lime import lime_tabular

iris = load_iris()
X = [Link](data=[Link], columns=iris.feature_names)
y = [Link]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

model = RandomForestClassifier()
[Link](X_train, y_train)

explainer = lime_tabular.LimeTabularExplainer(X_train.values,
feature_names=X_train.columns, class_names=iris.target_names,
mode='classification')

35 Samridhi Bisht | 07417711621 |


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

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 [Link] as plt
from sklearn.model_selection import train_test_split
from [Link] import load_iris
from [Link] import accuracy_score

data = load_iris()
X = [Link]([Link], columns=data.feature_names)
y = [Link]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
model = [Link](use_label_encoder=False, eval_metric='mlogloss')
[Link](X_train, y_train)

y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

explainer = [Link](model)
shap_values = explainer.shap_values(X_test)

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

Output:
(LIME)

(SHAP)

36 Samridhi Bisht | 07417711621 |


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:

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
37 Samridhi Bisht | 07417711621 |
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

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 [Link] 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)

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


random_state=42)
[Link](X_train, y_train)

print([Link](X_test, y_test))

[Link]('best_model.py')

38 Samridhi Bisht | 07417711621 |


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

from flaml import AutoML


from [Link] 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()
[Link](X_train, y_train, task="classification", time_budget=60)

y_pred = [Link](X_test)

Learning Outcomes:

39 Samridhi Bisht | 07417711621 |


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:

40 Samridhi Bisht | 07417711621 |


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 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:
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 [Link] import mean_squared_error
import [Link] as plt

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

[Link](columns={'date': 'ds', 'value': 'y'}, inplace=True)

train = [Link][:-20]
test = [Link][-20:]

model = Prophet()
[Link](train)

future = model.make_future_dataframe(periods=20)
forecast = [Link](future)

41 Samridhi Bisht | 07417711621 |


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

fig = [Link](forecast)
[Link]('Prophet Forecast')
[Link]()

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:

Experiment No. 9

42 Samridhi Bisht | 07417711621 |


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: 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 [Link] as nn
import [Link] as optim
import [Link] as prune
import [Link] as quantization
import [Link] as plt

class SimpleModel([Link]):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc1 = [Link](784, 256)
self.fc2 = [Link](256, 128)
self.fc3 = [Link](128, 10)

def forward(self, x):

43 Samridhi Bisht | 07417711621 |


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 = [Link](self.fc1(x))
x = [Link](self.fc2(x))
return self.fc3(x)

model = SimpleModel()

def plot_weight_distribution(tensor, title):


[Link]([Link]().detach().numpy().ravel(), bins=50)
[Link](title)
[Link]("Weight values")
[Link]("Frequency")
[Link]()

plot_weight_distribution([Link], "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([Link], "Pruned Weights of fc1 Layer")

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


[Link](module, "weight")

[Link]()

[Link] = quantization.get_default_qconfig("fbgemm")

[Link](model, inplace=True)

dummy_input = [Link](1, 784)


with torch.no_grad():
model(dummy_input)

[Link](model, inplace=True)

plot_weight_distribution([Link]().dequantize(), "Quantized
Weights of fc1 Layer")

Output:

44 Samridhi Bisht | 07417711621 |


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

45 Samridhi Bisht | 07417711621 |


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:

Experiment No. 10

Problem Statement: Explore federated learning concepts and implement distributed ML


models using TensorFlow Federated.
46 Samridhi Bisht | 07417711621 |
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

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, _ = [Link].load_data()

def client_data(n):
return source.create_tf_dataset_for_client(source.client_ids[n]).map(
lambda e: ([Link](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 = [Link]([

47 Samridhi Bisht | 07417711621 |


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

[Link](128, activation='relu', input_shape=(784,),


kernel_initializer='he_uniform'),
[Link](64, activation='relu'),
[Link](10, activation='softmax')
])

# Wrap the Keras model for use with TFF


tff_model = [Link].functional_model_from_keras(
keras_model,
loss_fn=[Link](),
input_spec=train_data[0].element_spec,
metrics_constructor=[Link](
accuracy=[Link])
)

# Build the federated averaging algorithm with a different learning rate


trainer = [Link].build_weighted_fed_avg(
tff_model,

client_optimizer_fn=[Link].build_sgdm(learning_rate=0.01)
)

state = [Link]()

for round_num in range(100):


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

Output:

48 Samridhi Bisht | 07417711621 |


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

49 Samridhi Bisht | 07417711621 |


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:

50 Samridhi Bisht | 07417711621 |

Common questions

Powered by AI

LIME and SHAP differ primarily in their approach to providing explanations. LIME focuses on local interpretability, generating linear surrogate models to approximate the complex model's behavior around specific predictions by perturbing input data and observing the changes in output . In contrast, SHAP offers a unified framework based on cooperative game theory, calculating Shapley values to provide a global measure of feature importance across all predictions, which are consistent and theoretically sound . These distinctions allow LIME to provide instance-specific insights while SHAP conveys overall feature importance .

Data augmentation enhances image classification by artificially increasing the diversity of the training dataset through transformations such as random zooming, flipping, and rotation . These augmentations help models generalize better by making them invariant to such transformations, thereby improving robustness and reducing overfitting. By simulating varied scenarios, augmentation ensures that the model learns adaptive features, which strengthens its predictive capabilities in practical, unseen scenarios .

GANs generate realistic images from latent spaces through a generator-discriminator framework. The generator transforms random noise from latent space into data resembling real samples, while the discriminator evaluates the authenticity of both real and generated samples . The generator iteratively improves its output by minimizing the discriminator's ability to differentiate between real and synthetic images, guided by adversarial training that fine-tunes its parameters to encode meaningful information from the latent space to the generated outputs .

Reinforcement learning optimizes decision-making processes by training agents to maximize cumulative rewards through interactions within an environment. The agent improves its performance by exploring actions and receiving feedback, as illustrated by the CartPole-v1 environment example. Reward structures guide the agent toward desirable outcomes, while evaluation metrics such as mean and standard deviation of rewards assess its learning efficacy. Visualization of rewards per episode aids in understanding and fine-tuning agent behavior for improved decision strategies .

Transfer learning with pre-trained models significantly reduces computational cost and improves performance by using models initially trained on large datasets to capture generalized features, which can be fine-tuned on smaller, specific datasets . This approach reduces the necessity for large volumes of data and minimizes the time and computational resources needed for training from scratch. By freezing layers that capture basic features and retraining only on the necessary layers, models efficiently adapt to new tasks, enhancing their applicability in data-limited environments .

Data preprocessing is vital in time series analysis as it ensures data integrity and the accuracy of subsequent forecasts by handling missing values, ensuring consistent intervals, and creating necessary time-based features . Models like Prophet and statsmodels require properly formatted data to detect patterns and trends effectively, with preprocessing serving as the foundational step for accurate model training and forecasting. Preprocessing aligns the dataset format to the models' requirements, thereby facilitating the extraction of reliable insights from temporal patterns .

Generative models, like the described GANs, augment data by generating synthetic examples that mimic real data. The generator model creates plausible data while the discriminator attempts to differentiate between real and generated data, thereby improving the quality of generated samples with each iteration . This adversarial setup enables models to create high-fidelity data representations, assisting in scenarios where acquiring real data is costly or infeasible, and consequently expanding the possibility for training robust machine learning models .

Interpretability is crucial for trusting and understanding machine learning models, particularly in critical applications. Techniques like LIME and SHAP provide insights into model predictions by attributing feature importance to outputs. LIME generates local interpretable models that approximate the complex model's behavior around individual instances, while SHAP explains using Shapley values from game theory, offering a global significance of feature importance . These methodologies enhance transparency and accountability by elucidating the rationale behind predictions, facilitating informed decision-making .

Neural networks enhance performance through normalization and dropouts by scaling input features to a uniform range, which speeds up convergence during training by minimizing the chance of local minima entrapment. Normalization involves adjusting training and validation datasets using the maximum and minimum values across features . Dropouts act as a form of regularization by randomly setting a fraction of input units to zero at each update during model training, thereby preventing overfitting .

AutoML and hyperparameter optimization tools contribute to enhancement by automating algorithm selection and hyperparameter tuning processes, which are critical in aligning the model with environmental complexities and task requirements . AutoML aids in choosing optimal algorithms based on performance criteria, while tools like Optuna explore hyperparameter spaces efficiently, identifying configurations that maximize learning efficacy. This automation reduces manual effort and potential biases in trial-and-error, leading to effectively fine-tuned models that demonstrate superior performance .

You might also like