Advances in Machine Learning Course
Advances in Machine Learning Course
Group: 1
VISION OF INSTITUTE
MISSION OF INSTITUTE
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
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]()
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]()
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]
)
loss_df = [Link]([Link])
loss_df[['loss', 'val_loss']].plot()
[Link]('Training and Validation Loss')
[Link]('Epochs')
[Link]('Loss')
[Link]()
Output:
Learning Outcomes:
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 [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):
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),
loss=[Link](from_logits=True),
metrics=['accuracy'])
initial_epochs = 10
history = [Link](train_dataset, validation_data=validation_dataset,
epochs=initial_epochs)
#fine-tune
base_model = [Link][2]
base_model.trainable = True
fine_tune_at = 120
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:
Learning Outcomes:
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 [Link] as plt
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)
train_data = train_data.map(preprocess).shuffle(60000).batch(256)
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)
def generator_loss(fake_output):
return cross_entropy(tf.ones_like(fake_output), fake_output)
generator_optimizer = [Link](1e-4)
discriminator_optimizer = [Link](1e-4)
EPOCHS = 50
NOISE_DIM = 100
NUM_EXAMPLES_TO_GENERATE = 16
@[Link]
def train_step(images):
noise = [Link]([BATCH_SIZE, NOISE_DIM])
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))
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]()
if (epoch + 1) % 10 == 0:
print(f'Epoch {epoch + 1}/{epochs} completed')
generate_and_save_images(generator, epoch, seed)
train(train_data, EPOCHS)
[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')
[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]()
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)
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](),
[Link](128, NUM_FEATURES),
[Link]() # Use Tanh for output layer
)
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]()
)
generator = Generator()
discriminator = Discriminator()
criterion = [Link]()
optimizer_G = [Link]([Link](), lr=0.0002)
optimizer_D = [Link]([Link](), lr=0.0002)
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()}")
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}")
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
Data Augmentation
Learning Outcomes:
Experiment No. 4
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 [Link] import imdb
from [Link] import pad_sequences
from [Link] import Sequential
from [Link] import Embedding, LSTM, Dense
import spacy
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.
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).
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
"""
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
)
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
)
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
Args:
max_steps (int): Maximum steps per episode
Returns:
list: Episode rewards
"""
obs, _ = self.eval_env.reset()
done = False
rewards = []
step = 0
[Link](reward)
step += 1
return rewards
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)
if __name__ == "__main__":
main()
Output:
Learning Outcomes:
Experiment No. 6
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')
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)
Output:
(LIME)
(SHAP)
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)
print([Link](X_test, y_test))
[Link]('best_model.py')
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)
y_pred = [Link](X_test)
Learning Outcomes:
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:
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)
})
train = [Link][:-20]
test = [Link][-20:]
model = Prophet()
[Link](train)
future = model.make_future_dataframe(periods=20)
forecast = [Link](future)
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
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.
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)
x = [Link](self.fc1(x))
x = [Link](self.fc2(x))
return self.fc3(x)
model = SimpleModel()
[Link]()
[Link] = quantization.get_default_qconfig("fbgemm")
[Link](model, inplace=True)
[Link](model, inplace=True)
plot_weight_distribution([Link]().dequantize(), "Quantized
Weights of fc1 Layer")
Output:
Learning Outcomes:
Experiment No. 10
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
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)
client_optimizer_fn=[Link].build_sgdm(learning_rate=0.01)
)
state = [Link]()
Output:
Learning Outcomes:
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 .