0% found this document useful (0 votes)
4 views

file

The document outlines the Fundamentals of Deep Learning Lab course at Vivekananda Institute of Professional Studies, detailing its objectives, theoretical foundations, and practical applications using TensorFlow and Keras. It covers essential concepts such as tensors, layers, and model training for both regression and classification tasks, along with Python code examples. The institute aims to empower engineering students with quality education and innovative practices to contribute to a sustainable future.

Uploaded by

onebackup2435
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)
4 views

file

The document outlines the Fundamentals of Deep Learning Lab course at Vivekananda Institute of Professional Studies, detailing its objectives, theoretical foundations, and practical applications using TensorFlow and Keras. It covers essential concepts such as tensors, layers, and model training for both regression and classification tasks, along with Python code examples. The institute aims to empower engineering students with quality education and innovative practices to contribute to a sustainable future.

Uploaded by

onebackup2435
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
You are on page 1/ 81

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

BTECH Programme: AI&DS

Course Title: Fundamentals of Deep


Learning Lab

Course Code: AIDS304P

Submitted To: Dr. Dimple Tiwari


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

VISION OF INSTITUTE

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


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

MISSION OF INSTITUTE

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


awakening students' curiosity, nurturing creativity and building
capabilities to enable them to make significant contributions to the world.
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
S.No Experiment Name Date Marks Remark Updated Faculty
Laboratory Class Viva Marks Signature
Assessment Participation (5 Marks)
(15 Marks) (5 Marks)

INDEX
S.No Experiment Name Date Marks Remark Updated Faculty
Laboratory Class Viva Marks Signature
Assessment Participation (5 Marks)
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

(15 Marks) (5 Marks)


Experiment 1

Aim: To explore the basic features of Tensorflow and Keras packages in Python.

Objectives:

 Understand the foundational functionalities of TensorFlow and Keras for deep learning.
 Explore how to build, compile, and train basic neural network models using Keras.
Theory:

TensorFlow and Keras are powerful libraries in Python that form the backbone of deep learning
model implementation. They provide a comprehensive ecosystem for building, training, and
deploying machine learning models. These libraries are pivotal for both academic and practical
experiments in the field of deep learning.

1. TensorFlow:

TensorFlow, developed by Google, is an open-source machine learning framework designed for high-
performance numerical computations. It enables researchers and developers to efficiently create and
train machine learning models across various platforms. TensorFlow's core strength lies in its ability
to handle tensors, which are multi-dimensional arrays that form the foundation of all computations in
deep learning.

Key features of TensorFlow include:

 Automatic Differentiation: TensorFlow computes gradients automatically, simplifying the


process of backpropagation for complex neural networks.

 GPU/TPU Acceleration: TensorFlow optimally utilizes hardware resources like GPUs and
TPUs for faster training and inference.

 Scalability: Its distributed training capabilities allow it to handle large-scale models and
datasets across multiple devices and servers.

 Versatility: TensorFlow supports both low-level control for custom operations and high-level
APIs like Keras for simplicity.

2. Keras:

Keras is a high-level neural network API integrated with TensorFlow. It is designed to be user-
friendly while still providing powerful features for advanced customization. Keras reduces the
complexity of building deep learning models by offering intuitive syntax and ready-to-use
components for common tasks.

Some of the features of Keras include:

 Simplified Model Building: Keras offers Sequential and Functional APIs to define models
effortlessly.

1 03817711922_ROHIT KUMAR SAXENA


 Ease of Use: It includes pre-built layers, loss functions, optimizers, and metrics, allowing for
rapid prototyping.

 Modularity: Every component in Keras is independent, making it easy to adapt and


customize.

 Integration: Since it is built on TensorFlow, it inherits the performance and scalability


benefits of TensorFlow.

3. Core Concepts:

 Tensors:
The primary data structure in TensorFlow, tensors represent multi-dimensional arrays that
enable efficient numerical computation. Operations on tensors are highly optimized for both
CPU and GPU execution.

 Layers:
Layers are the building blocks of a deep learning model. In Keras, layers perform specific
transformations, such as linear computations, activation functions, or normalization, to
process input data progressively.

 Compilation and Training:


Keras provides a streamlined workflow for compiling models, training them on datasets, and
evaluating their performance. Functions like compile(), fit(), and evaluate() abstract the
complexities of training, making the process user-friendly.

 Backpropagation and Optimization:


TensorFlow and Keras automate the process of calculating gradients and updating model
parameters using optimizers like SGD, Adam, or RMSprop. This minimizes loss functions
and improves model accuracy over successive iterations.

4. Regression and Classification with TensorFlow and Keras

 Regression:
In regression tasks, the model predicts continuous outputs. TensorFlow and Keras simplify
the creation of regression models by providing built-in loss functions (e.g., Mean Squared
Error) and metrics to evaluate model performance.

 Classification:
For classification tasks, the model predicts categorical outputs, such as labels for data points.
TensorFlow and Keras support multi-class and binary classification through layers like
Dense, activation functions like softmax or sigmoid, and loss functions like
categorical_crossentropy.

By exploring TensorFlow and Keras, learners gain hands-on experience with key deep learning
principles. This experiment lays the groundwork for understanding tensor operations, building neural
networks, and solving real-world problems through regression and classification tasks. These tools
bridge the gap between theoretical concepts and practical applications in the realm of deep learning.

2 03817711922_ROHIT KUMAR SAXENA


Code:

# Regression

import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

housing = fetch_california_housing()
X_train_full, X_test, y_train_full, y_test =
train_test_split(housing.data, housing.target, random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full,
y_train_full, random_state=42)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)

model = tf.keras.models.Sequential([
tf.keras.layers.Dense(30, activation='relu',
input_shape=X_train.shape[1:]),
tf.keras.layers.Dense(1)
])

model.compile(loss='mean_squared_error',
optimizer=tf.keras.optimizers.SGD(learning_rate=0.01))

history = model.fit(X_train, y_train, epochs=20,


validation_data=(X_valid, y_valid))

mse_test = model.evaluate(X_test, y_test)

print(f"Test MSE: {mse_test}")

# Classification

import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris

3 03817711922_ROHIT KUMAR SAXENA


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

y = tf.keras.utils.to_categorical(y, num_classes=3)

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu', input_shape=(4,)),
tf.keras.layers.Dense(3, activation='softmax')
])

model.compile(optimizer='sgd',
loss='categorical_crossentropy',
metrics=['accuracy'])

model.fit(X_train, y_train, epochs=50, batch_size=32, verbose=1)

loss, accuracy = model.evaluate(X_test, y_test, verbose=0)


print(f"Test Loss: {loss:.4f}")
print(f"Test Accuracy: {accuracy:.4f}")

Output:

# Regression

4 03817711922_ROHIT KUMAR SAXENA


# Classification

Learning Outcomes:

 Understand how to build and train regression and classification models using TensorFlow and
Keras.
 Gain insights into key evaluation metrics like Mean Squared Error (MSE) for regression and
Accuracy or F1-Score for classification tasks.
 Develop the ability to preprocess data and apply activation functions, optimizers, and loss
functions suitable for different problem domains.

5 03817711922_ROHIT KUMAR SAXENA


Experiment 2

Aim: Implementation of ANN model for regression and classification problem in


Python.

Objectives:

 To implement an ANN model in Python for regression and evaluate its performance using
MSE.
 To implement an ANN model in Python for classification and assess its accuracy using
performance metrics.

Theory:

Introduction to Artificial Neural Networks (ANNs)

Artificial Neural Networks (ANNs) are computational models inspired by the human brain's
structure and functioning. They consist of layers of interconnected nodes (neurons) which
process and transform input data through various layers to produce an output. The power of
ANNs lies in their ability to learn from data, identify patterns, and generalize predictions,
making them highly suitable for various tasks such as regression, classification, and time
series forecasting.

ANNs are composed of three primary types of layers:

1. Input Layer: Receives the raw input data.


2. Hidden Layers: Contain neurons that process the data using weights and activation
functions.
3. Output Layer: Provides the final result or prediction.

Each neuron in the hidden layers and output layer processes inputs from previous layers and
passes them through an activation function, which introduces non-linearity into the model.
The weights of the connections between neurons are learned during training by using
optimization techniques like gradient descent.

Regression Problems with ANN

In regression tasks, the objective is to predict a continuous numerical output based on input
features. For example, predicting house prices based on location, size, and other features is a
typical regression problem.

The basic architecture of an ANN for regression consists of:

 Input Layer: Takes in feature data (e.g., square footage, number of bedrooms).
 Hidden Layers: Processes the data through neurons.

6 03817711922_ROHIT KUMAR SAXENA


 Output Layer: Produces a continuous output, which could be a single value (e.g.,
price).

To train an ANN for regression, the model adjusts its weights to minimize the difference
between the predicted output and the actual target value using a loss function like Mean
Squared Error (MSE). The training process involves backpropagation, where errors are
propagated backward through the network, and weights are updated to reduce the loss.

Key Steps in ANN Regression:

1. Data Preprocessing: Normalization and scaling of data are crucial for ensuring
efficient learning.
2. Model Construction: A neural network model is constructed with appropriate layers
and activation functions.
3. Training the Model: The model is trained using a dataset, adjusting weights based
on the error at each step.
4. Evaluation: The model’s performance is evaluated using metrics like MSE or Root
Mean Squared Error (RMSE).

Python libraries like Keras and TensorFlow are often used to implement ANNs for
regression, as they provide simple APIs for defining and training neural networks.

Classification Problems with ANN

In classification tasks, the goal is to categorize data into distinct classes or categories. For
example, classifying images as either "cat" or "dog" or predicting whether a customer will
buy a product based on their behavior are typical classification problems.

The structure of an ANN for classification is similar to that of regression, but with key
differences in the output layer:

 Input Layer: Receives features of the data (e.g., image pixels, demographic
features).
 Hidden Layers: Processes the data through neurons.
 Output Layer: Uses a softmax or sigmoid activation function to output class
probabilities. In binary classification, the output layer consists of a single neuron with
a sigmoid activation function. For multi-class classification, a softmax activation
function is used.

For classification tasks, the model is trained using a loss function like Cross-Entropy Loss,
which measures the difference between predicted probabilities and actual class labels. During
training, the model learns to adjust its weights to reduce this loss.

Key Steps in ANN Classification:

1. Data Preprocessing: This involves encoding categorical variables (e.g., one-hot


encoding for labels) and normalizing input features.
2. Model Construction: The model is designed with hidden layers and an output layer
with appropriate activation functions (sigmoid for binary classification or softmax for
multi-class).

7 03817711922_ROHIT KUMAR SAXENA


3. Training the Model: The neural network is trained using a labeled dataset, and the
weights are adjusted through backpropagation.
4. Evaluation: The performance is evaluated using accuracy, precision, recall, F1-
score, or AUC (for binary classification).

Challenges in ANN Models

While ANNs are powerful, they come with several challenges:

1. Overfitting: ANN models, especially deep networks with many layers, can easily
overfit the training data, leading to poor generalization. Regularization techniques
such as dropout, L2 regularization, or early stopping can help mitigate overfitting.
2. Data Quality: ANN models are sensitive to the quality and quantity of data. Proper
data preprocessing, including handling missing values, scaling, and feature
engineering, is crucial.
3. Model Tuning: The performance of an ANN heavily depends on hyperparameters
like the number of hidden layers, neurons, learning rate, and batch size. Grid search
or random search methods are often employed to optimize these hyperparameters.
4. Computational Complexity: Training deep neural networks can be computationally
expensive, requiring powerful hardware (e.g., GPUs) and careful resource
management.

Python Libraries for Implementing ANN

Several Python libraries make it easy to implement ANN models for both regression and
classification:

1. TensorFlow/Keras: These are the most popular frameworks for building and training
neural networks. Keras provides a high-level API that is user-friendly, while
TensorFlow provides more flexibility and control for advanced users.
2. PyTorch: Another powerful deep learning framework that offers flexibility and
dynamic computation graphs, making it suitable for research and development.
3. Scikit-learn: Although primarily used for traditional machine learning algorithms,
Scikit-learn provides simple neural network implementations (MLPRegressor and
MLPClassifier) that are useful for smaller-scale tasks.

Conclusion

ANNs are versatile tools for both regression and classification tasks in machine learning. By
leveraging their ability to model complex relationships and learn from data, they can achieve
high accuracy in predicting continuous values and categorizing data into distinct classes.
Python, with its rich ecosystem of libraries like Keras and TensorFlow, provides a powerful
environment for implementing, training, and evaluating ANN models. However, successful
deployment requires careful attention to data quality, model tuning, and computational
resources to avoid common pitfalls like overfitting and high computational costs.

8 03817711922_ROHIT KUMAR SAXENA


Code:

#Regression

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

housing = fetch_california_housing()
df = pd.DataFrame(housing.data, columns=housing.feature_names)
df['target'] = housing.target

df.head()
df=df.dropna()

X=df.iloc[:,:-1] ## independent features


y=df.iloc[:,-1] ## dependent features

X.isnull()
y.isnull()

sns.pairplot(df)

df.corr()

corrmat = df.corr()
top_corr_features = corrmat.index
plt.figure(figsize=(20,20))
g=sns.heatmap(df[top_corr_features].corr(),annot=True,cmap="RdYlGn")

corrmat.index

from sklearn.ensemble import ExtraTreesRegressor


import matplotlib.pyplot as plt
model = ExtraTreesRegressor()
model.fit(X,y)

X.head()

print(model.feature_importances_)

9 03817711922_ROHIT KUMAR SAXENA


feat_importances = pd.Series(model.feature_importances_,
index=X.columns)
feat_importances.nlargest(5).plot(kind='barh')
plt.show()
sns.distplot(y)

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.3, random_state=0)

import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LeakyReLU,PReLU,ELU
from keras.layers import Dropout

NN_model = Sequential()

# The Input Layer :


NN_model.add(Dense(128, kernel_initializer='normal',input_dim =
X_train.shape[1], activation='relu'))

# The Hidden Layers :


NN_model.add(Dense(256, kernel_initializer='normal',activation='relu'))
NN_model.add(Dense(256, kernel_initializer='normal',activation='relu'))
NN_model.add(Dense(256, kernel_initializer='normal',activation='relu'))

# The Output Layer :


NN_model.add(Dense(1, kernel_initializer='normal',activation='linear'))

# Compile the network :


NN_model.compile(loss='mean_absolute_error', optimizer='adam',
metrics=['mean_absolute_error'])
NN_model.summary()

# Fitting the ANN to the Training set


model_history=NN_model.fit(X_train, y_train,validation_split=0.33,
batch_size = 10, epochs = 100)

prediction=NN_model.predict(X_test)

y_test

sns.distplot(y_test.values.reshape(-1,1)-prediction)
plt.scatter(y_test,prediction)

from sklearn import metrics


print('MAE:', metrics.mean_absolute_error(y_test, prediction))
print('MSE:', metrics.mean_squared_error(y_test, prediction))

10 03817711922_ROHIT KUMAR SAXENA


print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, prediction)))

# Classification

import numpy as np
import pandas as pd
import tensorflow as tf

from sklearn.model_selection import train_test_split


from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris

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

y = tf.keras.utils.to_categorical(y, num_classes=3)

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

ann = tf.keras.models.Sequential()

ann.add(tf.keras.layers.Dense(units=6,activation="relu"))
ann.add(tf.keras.layers.Dense(units=6,activation="relu"))
ann.add(tf.keras.layers.Dense(units=3,activation="sigmoid"))

ann.compile(optimizer="adam",loss="categorical_crossentropy",metrics=['
accuracy'])
ann.fit(X_train,y_train,batch_size=32,epochs = 100)

loss, accuracy = ann.evaluate(X_test, y_test, verbose=0)


print(f"Test Loss: {loss:.4f}")
print(f"Test Accuracy: {accuracy:.4f}")

11 03817711922_ROHIT KUMAR SAXENA


Output:

#Regression
df.head()

X.isnull()

y.isnull()

12 03817711922_ROHIT KUMAR SAXENA


sns.pairplot(df)

df.corr()

13 03817711922_ROHIT KUMAR SAXENA


sns.heatmap()

corrmat.index

14 03817711922_ROHIT KUMAR SAXENA


model.fit(X,y)

X.head()

print(model.feature_importances_)

plt.show()

15 03817711922_ROHIT KUMAR SAXENA


sns.distplot(y)

NN_model.fit()

16 03817711922_ROHIT KUMAR SAXENA


prediction=NN_model.predict(X_test)

y_test

sns.distplot(y_test.values.reshape(-1,1)-prediction)

17 03817711922_ROHIT KUMAR SAXENA


plt.scatter(y_test,prediction)

# MAE, MSE and RMSE

#Classification

Shape of : X_train , y_train

ann.fit()

18 03817711922_ROHIT KUMAR SAXENA


#Loss and accuracy

Learning Outcomes:

 Gain the ability to build and train ANN models for both regression and classification tasks in
Python.
 Learn to preprocess data and evaluate model performance using key metrics like MSE,
accuracy, and F1-score.

19 03817711922_ROHIT KUMAR SAXENA


Experiment 3

Aim: Implementation of Convolution Neural Network for MRI Data Set in Python.

Objectives:

 To implement CNN model in Python on MRI images.


 To implement CNN model for classifying images and evaluate its performance using
performance metrics.

Theory:

Introduction to Deep Learning and CNNs

Deep learning is a subset of machine learning that focuses on training artificial neural
networks to learn from large amounts of data. Among deep learning architectures,
Convolutional Neural Networks (CNNs) are particularly effective for analyzing image-
based data, making them suitable for medical imaging tasks such as MRI (Magnetic
Resonance Imaging) analysis.

MRI scans provide detailed internal body structures, helping in medical diagnosis, treatment
planning, and research. However, manually interpreting MRI images is time-consuming and
subject to variability. CNNs help automate and improve MRI analysis by detecting patterns,
classifying abnormalities, and segmenting regions of interest.

Convolutional Neural Networks (CNNs) in Medical Imaging

CNNs are specialized deep learning models designed for image processing tasks. They use
convolutional layers to extract spatial features from images, making them efficient in
identifying patterns and structures within MRI scans. The fundamental components of a CNN
include:

 Convolutional Layers: Apply filters to input images to extract features such as


edges, textures, and shapes.
 Pooling Layers: Reduce the dimensionality of the data while preserving important
features, improving computational efficiency.
 Fully Connected Layers: Flatten the extracted features and connect them to output
neurons for classification or segmentation tasks.
 Activation Functions (ReLU, Softmax, Sigmoid): Introduce non-linearity into the
model to improve learning capability.

CNNs have significantly advanced medical imaging applications, enabling automated tumor
detection, brain segmentation, and disease classification from MRI datasets.

20 03817711922_ROHIT KUMAR SAXENA


MRI Dataset and Its Importance

MRI datasets contain grayscale images that represent different tissues and structures within
the human body. These datasets are crucial for training CNN models in various medical
applications, such as:

 Brain Tumor Classification (Normal vs. Tumor)


 Alzheimer’s Disease Detection
 Organ Segmentation

MRI datasets usually require preprocessing steps, including normalization, resizing, and
augmentation, to improve model performance.

Implementation of CNN for MRI Dataset in Python

Python, with deep learning libraries such as TensorFlow and Keras, provides an efficient
framework for implementing CNNs. The basic steps involved in developing a CNN for MRI
image classification include:

1. Loading the MRI Dataset: Using libraries like Pandas, NumPy, or TensorFlow
datasets.
2. Preprocessing the Data: Resizing images, normalizing pixel values, and
augmenting the dataset.
3. Building the CNN Model: Defining layers such as convolutional, pooling, and fully
connected layers.
4. Compiling and Training the Model: Using an optimizer (SGD, Adam) and loss
function (categorical cross-entropy).
5. Evaluating the Model: Testing the model on unseen MRI images and assessing its
accuracy.

Applications of CNNs in MRI Analysis

 Brain Tumor Detection: CNNs can classify MRI images as normal or containing
tumors with high accuracy.
 Alzheimer’s Disease Prediction: Identifying structural changes in brain MRI scans
to diagnose neurodegenerative diseases.
 Medical Image Segmentation: Extracting regions of interest, such as tumors or
organs, from MRI scans for precise analysis.

Conclusion

The implementation of a Convolutional Neural Network (CNN) for MRI image classification
is a powerful deep learning application in medical imaging. CNNs efficiently learn spatial
hierarchies of features and can significantly improve disease detection accuracy. With
Python-based frameworks like TensorFlow and Keras, researchers and medical professionals
can develop robust models to analyze MRI scans, contributing to advancements in medical
diagnostics and healthcare.

21 03817711922_ROHIT KUMAR SAXENA


Code:

import pandas as pd
import numpy as np
import cv2
import os
import random
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.image import imread

import tensorflow as tf
import tensorflow_hub as hub
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

from keras.models import Sequential


from keras.layers import Conv2D,Activation,MaxPooling2D,Dense,Flatten,
Dropout
from keras.losses import BinaryCrossentropy
try:
from keras.optimizer import Adam
except:
from tensorflow.keras.optimizers import Adam

from sklearn.model_selection import train_test_split


from sklearn.metrics import auc, roc_curve, log_loss, roc_auc_score,
auc, roc_curve, accuracy_score, confusion_matrix, cohen_kappa_score

import warnings
warnings.filterwarnings('ignore')

MAIN_DIR = "/content/brain_tumor_dataset/"

# control reproducibility with random seeds


seed_value = 1
np.random.seed(seed_value)
random.seed(seed_value)
tf.random.set_seed(seed_value)

# Define location of dataset


folder_no = MAIN_DIR + '/no'
folder_yes = MAIN_DIR + '/yes'

# Collect image paths


image_paths = []

22 03817711922_ROHIT KUMAR SAXENA


for filepath in [folder_no, folder_yes]:
for f in os.listdir(filepath)[:3]: # Take the first 3 images per
folder
image_paths.append(os.path.join(filepath, f))

fig, axes = plt.subplots(1, len(image_paths), figsize=(15, 5))

for ax, img_path in zip(axes, image_paths):


image = imread(img_path)
ax.imshow(image)

plt.tight_layout()
plt.show()

filepath = MAIN_DIR
images, labels, filenames = [], [], []

for gt in ['yes','no']:
filepath_gt = filepath + gt
for f in os.listdir(filepath_gt):
filename = filepath_gt + '/' + f
if '._' not in filename: # metadata files created by macos

# load image
photo =
tf.keras.utils.load_img(path=filename,target_size=(200, 200))

# load image pixels


image = imread(filename)
resized_image = cv2.resize(image,dsize=(200,200))

# in case of grayscale images


if len(np.shape(resized_image)) > 2:
# convert the image from COLOR_BGR2GRAY
resized_image = cv2.cvtColor(resized_image,
cv2.COLOR_BGR2GRAY)

# store to array
images.append(resized_image)
filenames.append(filename)

if gt=='yes':
label = 1
labels.append(label)
elif gt=='no':
label = 0
labels.append(label)

23 03817711922_ROHIT KUMAR SAXENA


gtFr =
pd.DataFrame(labels).rename(columns={0:'gt'}).reset_index(drop=False).r
ename(columns={'index':'pID'})
gtFr['filename'] = filenames

# split into training and testing sets, stratifying by gt for equal


representation
trainFr, testFr = train_test_split(gtFr, test_size=0.2,
stratify=gtFr['gt'])

# store training/testing indices


trainFr['set'] = 'train'
testFr['set'] = 'test'

gtFr2 = pd.concat([trainFr,testFr],axis=0)
gtFr2.set_index(['filename']).to_csv('base_model_train_test.csv')

files_train = []
X_train = []
y_train = []

for idx, row in trainFr.iterrows():


# load image pixels
image = imread(row['filename'])

# resize image to standard 200x200


resized_image = cv2.resize(image,dsize=(200,200))

# in case of grayscale images


if len(np.shape(resized_image)) > 2:
# convert the image from COLOR_BGR2GRAY
resized_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)

# store to array
X_train.append(resized_image)
y_train.append(row['gt'])
files_train.append(row['filename'])

# reshape data to fit model


X_train = np.array(X_train).reshape(len(trainFr),200,200,1)
y_train = np.array(y_train)

files_test = []
X_test = []
y_test = []

24 03817711922_ROHIT KUMAR SAXENA


for idx, row in testFr.iterrows():
# load image pixels
image = imread(row['filename'])

# resize image to standard 200x200


resized_image = cv2.resize(image,dsize=(200,200))

# in case of grayscale images


if len(np.shape(resized_image)) > 2:
# convert the image from COLOR_BGR2GRAY
resized_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)

# store to array
X_test.append(resized_image)
y_test.append(row['gt'])
files_test.append(row['filename'])

# reshape data to fit model


X_test = np.array(X_test).reshape(len(testFr),200,200,1)
y_test = np.array(y_test)

# create model
model = Sequential()

# add convolutional layer


model.add(Conv2D(64, kernel_size=3, activation='relu',
input_shape=(200,200,1))) # input is of shape (N, C) or (N, C, L) where
N is the batch size as before. However what does the C and L denote
here? It seems that C = number of features, L = number of channels,

# add max pooling layer


model.add(MaxPooling2D(pool_size=(2, 2)))

# add another convolutional layer


model.add(Conv2D(32, kernel_size=3, activation='relu'))

# add another max pooling layer


model.add(MaxPooling2D(pool_size=(2, 2)))

# flatten output (connect convolutional layers and dense layers)


model.add(Flatten())

# add a dense layer with 128 neurons and ReLU activation


model.add(Dense(128, activation='relu'))

# add dense layer


model.add(Dense(1, activation='sigmoid'))

25 03817711922_ROHIT KUMAR SAXENA


# compile model using accuracy to measure model performance
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])

history = model.fit(X_train, y_train, validation_data=(X_test, y_test),


epochs=30)

# Function to plot loss and accuracy in a single row


def plot_curves(history):
"""
Returns loss and accuracy curves in a single row.
"""
loss = history.history["loss"]
val_loss = history.history["val_loss"]
accuracy = history.history["accuracy"]
val_accuracy = history.history["val_accuracy"]
epochs = range(len(loss))

# Create a single row with 2 plots (loss and accuracy)


fig, axes = plt.subplots(1, 2, figsize=(12, 5))

# Plot loss
axes[0].plot(epochs, loss, label="training_loss")
axes[0].plot(epochs, val_loss, label="val_loss")
axes[0].set_title("Loss")
axes[0].set_xlabel("Epochs")
axes[0].legend()

# Plot accuracy
axes[1].plot(epochs, accuracy, label="training_accuracy")
axes[1].plot(epochs, val_accuracy, label="val_accuracy")
axes[1].set_title("Accuracy")
axes[1].set_xlabel("Epochs")
axes[1].legend()

plt.tight_layout() # Adjust layout for better spacing


plt.show()

plot_curves(history)

# make predictions
predictions = model.predict(X_test)

# store as probabilities
probabilities = [p[0] for p in predictions]
testFr['pred'] = probabilities

26 03817711922_ROHIT KUMAR SAXENA


testFr.set_index(['pID']).to_csv('base_model_predictions.csv')

dataPos = testFr[testFr['gt']==1]['pred'].values
dataNeg = testFr[testFr['gt']==0]['pred'].values
dataAll = np.concatenate((dataPos, dataNeg))
lblArr = np.zeros(len(dataAll), dtype=bool)
lblArr[0:len(dataPos)] = True

fpr, tpr, thresholds = roc_curve(lblArr, dataAll, pos_label=True)


roc_auc = auc(fpr, tpr)

# invert comparison if (ROC<0.5) required


if roc_auc<0.5:
lblArr = ~lblArr
fpr, tpr, thresholds = roc_curve(lblArr, dataAll, pos_label=True)
roc_auc = auc(fpr, tpr)
print('inverting labels')

print('ROC AUC: {:0.2f}'.format(roc_auc))

# calculate best cut-off based on distance to top corner of ROC curve


distArr = np.sqrt(np.power(fpr, 2) + np.power((1 - tpr), 2))
cutoffIdx = np.argsort(distArr)[0]
cutoffTh = thresholds[cutoffIdx]

print('Cutoff threshold that maximizes sens/spec:


{:0.2f}'.format(cutoffTh))

lblOut = dataAll >= cutoffTh

acc = accuracy_score(lblArr, lblOut)


print('Accuracy: {:0.2f}'.format(acc))

sens = tpr[cutoffIdx]
print('Sensitivity: {:0.2f}'.format(sens))

spec = 1 - fpr[cutoffIdx]
print('Specificity: {:0.2f}'.format(spec))

kappa = cohen_kappa_score(lblOut, lblArr)


print('Cohen kappa score: {:0.2f}'.format(kappa))

27 03817711922_ROHIT KUMAR SAXENA


Output:

# First 3 images per folder

model.fit()

28 03817711922_ROHIT KUMAR SAXENA


plot_curves(history)

# Evaluation Metrics

Learning Outcomes:

 Gain the ability to build and train CNN models for classification tasks on images in Python.
 Learnt to preprocess and scale image data to train CNN model and evaluate model
performance using key metrics like accuracy, sensitivity and kappa score.

29 03817711922_ROHIT KUMAR SAXENA


Experiment 4

Aim: Implementation of Autoencoders for dimensionality reduction in Python.

Objectives:

 To implement Autoencoders in Python for dimensionality reduction.


 To train an Autoencoder using TensorFlow and Keras and analyze its reconstruction ability.

Theory:

Introduction to Deep Learning and Dimensionality Reduction

Deep learning is a subset of machine learning that enables computers to learn from large
datasets using artificial neural networks. One of the critical challenges in machine learning is
dealing with high-dimensional data, which can lead to increased computational costs and
reduced model performance due to the "curse of dimensionality." Dimensionality reduction
is a technique used to transform high-dimensional data into a lower-dimensional
representation while preserving important features.

Among various dimensionality reduction techniques, Autoencoders have gained significant


importance. Autoencoders are a type of neural network architecture used to learn efficient
data representations in an unsupervised manner. They compress input data into a lower-
dimensional latent space and then reconstruct it back, capturing the most critical features of
the data.

What are Autoencoders?

An Autoencoder (AE) is a neural network designed to encode input data into a lower-
dimensional space (encoding) and then reconstruct the original input (decoding). It consists
of two main components:

1. Encoder: Reduces the input dimensions by learning compressed representations.


2. Decoder: Reconstructs the input data from the compressed representation.

The objective of an autoencoder is to minimize the reconstruction loss, typically measured


using Mean Squared Error (MSE) or Binary Cross-Entropy Loss.

Applications of Autoencoders in Dimensionality Reduction

Autoencoders are widely used for dimensionality reduction and feature extraction, especially
in cases where linear methods like PCA (Principal Component Analysis) are insufficient.
Some key applications include:

 Data Compression: Efficiently encoding high-dimensional data into a compact


representation.
 Noise Reduction (Denoising Autoencoders): Removing noise from images or
signals by learning a cleaner representation.

30 03817711922_ROHIT KUMAR SAXENA


 Anomaly Detection: Identifying rare patterns by reconstructing normal data patterns
and detecting deviations.
 Feature Extraction: Learning meaningful representations for downstream machine
learning tasks.

Implementation of Autoencoders for Dimensionality Reduction in Python

Autoencoders can be implemented using TensorFlow and Keras in Python. The basic steps
include:

1. Loading the Dataset: Common datasets such as MNIST or CIFAR-10 are used to
train autoencoders.
2. Preprocessing the Data: Normalization and reshaping of input images.
3. Building the Autoencoder Model:
o A simple autoencoder consists of an encoder (Dense layers) followed by a
decoder (Dense layers).
4. Training the Autoencoder: Using an Adam optimizer and Mean Squared Error
(MSE) loss.
5. Evaluating the Performance: Checking the quality of reconstructed images and
analyzing dimensionality reduction.

Advantages of Autoencoders for Dimensionality Reduction

Compared to traditional techniques like PCA, autoencoders offer several benefits:

 Non-linearity Handling: Autoencoders can learn complex, non-linear relationships


in data.
 Better Representation Learning: Captures hierarchical and meaningful features.
 Scalability: Works efficiently on large datasets without significant performance
degradation.
 Feature Learning without Labels: Unlike supervised learning, autoencoders require
no labeled data.

Conclusion

Autoencoders provide an efficient way to perform dimensionality reduction, especially for


high-dimensional datasets. They are widely used in feature extraction, denoising, and
anomaly detection tasks. With deep learning frameworks like TensorFlow and Keras,
implementing autoencoders in Python has become straightforward. Their ability to learn
efficient representations without supervision makes them a powerful tool in modern machine
learning and deep learning applications.

31 03817711922_ROHIT KUMAR SAXENA


Code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, LeakyReLU
from tensorflow.keras.optimizers import RMSprop
from sklearn.manifold import TSNE

# Load Dataset
file_path = "train.csv" # Change if needed
df = pd.read_csv(file_path)
print(df.head())

# EDA
print(df.info())
print(df.describe())
print(df.isnull().sum().sum(), "missing values")

# Distribution of Target Variable


sns.histplot(df['target'], bins=30, kde=True)
plt.title("Target Variable Distribution")
plt.show()

# Remove ID and extract features


df.drop(columns=['ID_code'], inplace=True)
X = df.drop(columns=['target']).values # Features
y = df['target'].values # Target

# Normalize Data
scaler = StandardScaler()
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Handle NaN or Inf values


X_train = np.nan_to_num(X_train)
X_test = np.nan_to_num(X_test)

# Autoencoder Architecture
encoding_dim = 10 # Dimension reduction size
input_dim = X_train.shape[1]

32 03817711922_ROHIT KUMAR SAXENA


input_layer = Input(shape=(input_dim,))
encoded = Dense(64)(input_layer)
encoded = LeakyReLU(alpha=0.1)(encoded)
encoded = Dense(32)(encoded)
encoded = LeakyReLU(alpha=0.1)(encoded)
encoded = Dense(encoding_dim, activation='linear')(encoded)

decoded = Dense(32)(encoded)
decoded = LeakyReLU(alpha=0.1)(decoded)
decoded = Dense(64)(decoded)
decoded = LeakyReLU(alpha=0.1)(decoded)
decoded = Dense(input_dim, activation='linear')(decoded)

# Define Autoencoder
autoencoder = Model(input_layer, decoded)
autoencoder.compile(optimizer=RMSprop(learning_rate=0.0001),
loss='mae')

# Train Autoencoder
autoencoder.fit(X_train, X_train, epochs=50, batch_size=256,
shuffle=True, validation_data=(X_test, X_test))

# Extract Encoder
encoder = Model(input_layer, encoded)
X_train_encoded = encoder.predict(X_train)
X_test_encoded = encoder.predict(X_test)

# t-SNE Visualization
tsne = TSNE(n_components=2, random_state=42)
X_embedded = tsne.fit_transform(X_test_encoded)
plt.scatter(X_embedded[:, 0], X_embedded[:, 1],
c=y_test[:len(X_embedded)], cmap='coolwarm', alpha=0.5)
plt.colorbar()
plt.title("t-SNE Visualization of Encoded Features")
plt.show()

print("Original feature shape:", X_train.shape)


print("Reduced feature shape:", X_train_encoded.shape)

33 03817711922_ROHIT KUMAR SAXENA


Output:

# Load dataset - df.head()

# EDA

34 03817711922_ROHIT KUMAR SAXENA


# Distribution of Target Variable

35 03817711922_ROHIT KUMAR SAXENA


# autoencoder.fit()

# t-SNE Visualization

36 03817711922_ROHIT KUMAR SAXENA


# Original vs Reduced

Learning Outcomes:

 Understand the concept of autoencoders and their role in dimensionality reduction.


 Learn to preprocess and normalize high-dimensional data for efficient training.

37 03817711922_ROHIT KUMAR SAXENA


Experiment 5

Aim: Improving Autocoder’s Performance using convolution layers in Python (MNIST


Dataset to be utilized).

Objectives:

 To implement Autoencoders in Python on Image Dataset.


 To train an Autoencoder using TensorFlow and Keras and analyze its reconstruction ability.

Theory:

1. Introduction

Autoencoders are a type of artificial neural network used for unsupervised learning of
efficient codings in data. They are primarily utilized for dimensionality reduction, denoising,
and feature extraction. In this experiment, we apply autoencoders to the MNIST dataset, a
widely used benchmark dataset for handwritten digit recognition. We first implement a basic
autoencoder using fully connected layers and later enhance its performance by employing
convolutional layers.

2. Autoencoders: Concept and Working Mechanism

Autoencoders consist of two main components:

1. Encoder: This part compresses the input data into a lower-dimensional representation
(latent space).
2. Decoder: This part reconstructs the input data from the compressed representation.

Mathematically, an autoencoder maps an input to a compressed representation using an


encoding function , and then reconstructs using a decoding function.

The training objective is to minimize the reconstruction loss between and , usually measured
by Mean Squared Error (MSE) or Binary Cross-Entropy (BCE).

3. Application of Autoencoders on MNIST Dataset

3.1 Dataset Preparation

The MNIST dataset consists of 60,000 training images and 10,000 test images of handwritten
digits (0-9), each of size 28x28 pixels. The data is normalized to the range to improve
training efficiency.

3.2 Fully Connected Autoencoder Implementation

A simple autoencoder with fully connected layers is constructed as follows:

38 03817711922_ROHIT KUMAR SAXENA


 Encoder: Input (784 dimensions) → Dense(128, ReLU) → Dense(64, ReLU) →
Latent Space (32 dimensions)
 Decoder: Dense(64, ReLU) → Dense(128, ReLU) → Output (784 dimensions,
Sigmoid)

After training with Adam optimizer and MSE loss function, the autoencoder is able to
reconstruct the input images reasonably well. However, due to the dense architecture, spatial
relationships in images are not efficiently captured.

4. Improving Autoencoder Performance with Convolutional Layers

To improve performance, a Convolutional Autoencoder (CAE) is implemented. Unlike fully


connected layers, convolutional layers maintain spatial hierarchies, making them better suited
for image reconstruction tasks.

4.1 Convolutional Autoencoder Architecture

 Encoder: Conv2D(32 filters, 3x3, ReLU) → MaxPooling2D(2x2) → Conv2D(64


filters, 3x3, ReLU) → MaxPooling2D(2x2)
 Decoder: Conv2DTranspose(64 filters, 3x3, ReLU) → UpSampling2D(2x2) →
Conv2DTranspose(32 filters, 3x3, ReLU) → UpSampling2D(2x2) →
Conv2DTranspose(1 filter, 3x3, Sigmoid)

4.2 Performance Comparison

The CAE significantly improves the quality of reconstructed images compared to the fully
connected autoencoder. The main advantages include:

 Preservation of spatial features, reducing loss of information.


 Faster convergence due to fewer trainable parameters.
 Better generalization for unseen images.

5. Conclusion

Autoencoders provide a powerful method for image compression and feature extraction.
While fully connected autoencoders serve as a baseline, convolutional autoencoders
significantly enhance performance by leveraging spatial hierarchies. Future work can include
Variational Autoencoders (VAEs) for more structured latent spaces and application of these
techniques to real-world datasets beyond MNIST.

39 03817711922_ROHIT KUMAR SAXENA


Code:

import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

(X_train, _), (X_test, _) = tf.keras.datasets.fashion_mnist.load_data()

X_train = X_train.astype('float32') / 255.0


X_test = X_test.astype('float32') / 255.0

X_train_flat = X_train.reshape(-1, 28 * 28)


X_test_flat = X_test.reshape(-1, 28 * 28)

(X_train, y_train), (X_test, y_test) =


tf.keras.datasets.fashion_mnist.load_data()

print(f"Train set shape: {X_train.shape}, Labels: {y_train.shape}")


print(f"Test set shape: {X_test.shape}, Labels: {y_test.shape}")

class_labels = [
"T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"
]

plt.figure(figsize=(10, 6))
for i in range(10):
plt.subplot(2, 5, i+1)
plt.imshow(X_train[i], cmap="gray")
plt.title(class_labels[y_train[i]])
plt.axis("off")
plt.tight_layout()
plt.show()

class_counts = pd.Series(y_train).value_counts().sort_index()

plt.figure(figsize=(8, 5))
sns.barplot(x=class_counts.index, y=class_counts.values,
palette="coolwarm")
plt.xticks(ticks=range(10), labels=class_labels, rotation=45)
plt.xlabel("Class Labels")
plt.ylabel("Count")
plt.title("Distribution of Fashion MNIST Classes")
plt.show()

40 03817711922_ROHIT KUMAR SAXENA


plt.figure(figsize=(8, 5))
sns.histplot(X_train.flatten(), bins=50, kde=True, color="purple")
plt.xlabel("Pixel Intensity")
plt.ylabel("Frequency")
plt.title("Pixel Intensity Distribution in Fashion MNIST")
plt.show()
# Convert images to tuples for easy comparison
unique_images = set([tuple(image.flatten()) for image in X_train])

print(f"Total Images: {len(X_train)}")


print(f"Unique Images: {len(unique_images)}")
print(f"Duplicate Images: {len(X_train) - len(unique_images)}")

input_dim = 28 * 28 # 784 pixels


encoding_dim = 64 # Reduced dimension

# Define the autoencoder


input_layer = layers.Input(shape=(input_dim,))
encoded = layers.Dense(128, activation='relu')(input_layer)
encoded = layers.Dense(encoding_dim, activation='relu')(encoded)

decoded = layers.Dense(128, activation='relu')(encoded)


decoded = layers.Dense(input_dim, activation='sigmoid')(decoded) #
Output same shape as input

autoencoder = models.Model(input_layer, decoded)


encoder = models.Model(input_layer, encoded) # Encoder for
dimensionality reduction

autoencoder.compile(optimizer='adam', loss='mean_squared_error')

# Train the model


autoencoder.fit(X_train_flat, X_train_flat, epochs=20, batch_size=256,
validation_data=(X_test_flat, X_test_flat))

# Reshape for CNN (add channel dimension)


X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)

# Convolutional Autoencoder
input_layer = layers.Input(shape=(28, 28, 1))

# Encoder
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')
(input_layer)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)

41 03817711922_ROHIT KUMAR SAXENA


encoded = layers.Conv2D(128, (3, 3), activation='relu', padding='same')
(x)

# Decoder
x = layers.Conv2DTranspose(64, (3, 3), activation='relu',
padding='same')(encoded)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2DTranspose(32, (3, 3), activation='relu',
padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)

decoded = layers.Conv2DTranspose(1, (3, 3), activation='sigmoid',


padding='same')(x)

# Define model
autoencoder = models.Model(input_layer, decoded)

# Compile and train


autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

autoencoder.fit(X_train, X_train, epochs=20, batch_size=256,


validation_data=(X_test, X_test))

encoder = models.Model(input_layer, encoded)


X_encoded = encoder.predict(X_test)
print("Encoded shape:", X_encoded.shape)

n = 10 # Number of images to display


decoded_imgs = autoencoder.predict(X_test)

plt.figure(figsize=(20, 4))
for i in range(n):
# Original images
plt.subplot(2, n, i + 1)
plt.imshow(X_test[i].reshape(28, 28), cmap='gray')
plt.axis('off')

# Reconstructed images
plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28), cmap='gray')
plt.axis('off')

plt.show()

42 03817711922_ROHIT KUMAR SAXENA


Output:

# Check dataset shape

# Plot sample images with labels

# Distribution of Fashion MNIST Classes

43 03817711922_ROHIT KUMAR SAXENA


# Pixel Intensity Distribution in Fashion MNIST

# Image count

# Train the model

44 03817711922_ROHIT KUMAR SAXENA


# Convolutional Autoencoder

# Encoded shape:

# Original vs Reconstructed

Learning Outcomes:

 Understand the concept of autoencoders and their application in image reconstruction.


 Learn to preprocess and normalize the MNIST dataset for improved training efficiency.

45 03817711922_ROHIT KUMAR SAXENA


Experiment 6

Aim: Application of Autoencoders on Image Dataset.

Objectives:

 To implement Autoencoders in Python for dimensionality reduction.


 To train an Autoencoder using TensorFlow and Keras and analyze its reconstruction ability.

Theory:

Introduction to Deep Learning and Dimensionality Reduction

Deep learning is a subset of machine learning that enables computers to learn from large
datasets using artificial neural networks. One of the critical challenges in machine learning is
dealing with high-dimensional data, which can lead to increased computational costs and
reduced model performance due to the "curse of dimensionality." Dimensionality reduction
is a technique used to transform high-dimensional data into a lower-dimensional
representation while preserving important features.

Among various dimensionality reduction techniques, Autoencoders have gained significant


importance. Autoencoders are a type of neural network architecture used to learn efficient
data representations in an unsupervised manner. They compress input data into a lower-
dimensional latent space and then reconstruct it back, capturing the most critical features of
the data.

What are Autoencoders?

An Autoencoder (AE) is a neural network designed to encode input data into a lower-
dimensional space (encoding) and then reconstruct the original input (decoding). It consists
of two main components:

3. Encoder: Reduces the input dimensions by learning compressed representations.


4. Decoder: Reconstructs the input data from the compressed representation.

The objective of an autoencoder is to minimize the reconstruction loss, typically measured


using Mean Squared Error (MSE) or Binary Cross-Entropy Loss.

Applications of Autoencoders in Dimensionality Reduction

Autoencoders are widely used for dimensionality reduction and feature extraction, especially
in cases where linear methods like PCA (Principal Component Analysis) are insufficient.
Some key applications include:

 Data Compression: Efficiently encoding high-dimensional data into a compact


representation.
 Noise Reduction (Denoising Autoencoders): Removing noise from images or
signals by learning a cleaner representation.

46 03817711922_ROHIT KUMAR SAXENA


 Anomaly Detection: Identifying rare patterns by reconstructing normal data patterns
and detecting deviations.
 Feature Extraction: Learning meaningful representations for downstream machine
learning tasks.

Implementation of Autoencoders for Dimensionality Reduction in Python

Autoencoders can be implemented using TensorFlow and Keras in Python. The basic steps
include:

6. Loading the Dataset: Common datasets such as MNIST or CIFAR-10 are used to
train autoencoders.
7. Preprocessing the Data: Normalization and reshaping of input images.
8. Building the Autoencoder Model:
o A simple autoencoder consists of an encoder (Dense layers) followed by a
decoder (Dense layers).
9. Training the Autoencoder: Using an Adam optimizer and Mean Squared Error
(MSE) loss.
10. Evaluating the Performance: Checking the quality of reconstructed images and
analyzing dimensionality reduction.

Advantages of Autoencoders for Dimensionality Reduction

Compared to traditional techniques like PCA, autoencoders offer several benefits:

 Non-linearity Handling: Autoencoders can learn complex, non-linear relationships


in data.
 Better Representation Learning: Captures hierarchical and meaningful features.
 Scalability: Works efficiently on large datasets without significant performance
degradation.
 Feature Learning without Labels: Unlike supervised learning, autoencoders require
no labeled data.

Conclusion

Autoencoders provide an efficient way to perform dimensionality reduction, especially for


high-dimensional datasets. They are widely used in feature extraction, denoising, and
anomaly detection tasks. With deep learning frameworks like TensorFlow and Keras,
implementing autoencoders in Python has become straightforward. Their ability to learn
efficient representations without supervision makes them a powerful tool in modern machine
learning and deep learning applications.

47 03817711922_ROHIT KUMAR SAXENA


Code:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
import numpy as np
import matplotlib.pyplot as plt

# Load the MNIST dataset


(x_train, _), (x_test, _) = keras.datasets.mnist.load_data()

print("Training set shape:", x_train.shape)


print("Test set shape:", x_test.shape)

print("Pixel Intensity Range: ", np.min(x_train), "to",


np.max(x_train))

# Normalize pixel values between 0 and 1


x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0

# Flatten the images (28x28 to 784-dimensional vector)


x_train = x_train.reshape((len(x_train), 784))
x_test = x_test.reshape((len(x_test), 784))

# Display 10 random images from the dataset


n = 10
plt.figure(figsize=(10, 2))
for i in range(n):
ax = plt.subplot(1, n, i + 1)
plt.imshow(x_train[i].reshape(28, 28), cmap="gray")
plt.axis("off")
plt.show()

plt.hist(x_train.flatten(), bins=50, color='blue', alpha=0.7)


plt.xlabel("Pixel Intensity")
plt.ylabel("Frequency")
plt.title("Distribution of Pixel Intensities in MNIST Dataset")
plt.show()

encoding_dim = 32 # Reduce 784 dimensions to 32

# Define the Autoencoder model


input_img = Input(shape=(784,))

# Encoder

48 03817711922_ROHIT KUMAR SAXENA


encoded = Dense(encoding_dim, activation="relu")(input_img)

# Decoder
decoded = Dense(784, activation="sigmoid")(encoded)

# Define Autoencoder Model


autoencoder = Model(input_img, decoded)

# Compile the Autoencoder


autoencoder.compile(optimizer="adam", loss="mse")

# Train the Autoencoder


autoencoder.fit(x_train, x_train,
epochs=20,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))

# Encode and decode images


encoded_imgs = autoencoder.predict(x_test)
decoded_imgs = autoencoder.predict(x_test)

# Display original and reconstructed images


n = 10 # Show 10 images
plt.figure(figsize=(20, 4))
for i in range(n):
# Original images
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i].reshape(28, 28), cmap="gray")
plt.axis("off")

# Reconstructed images
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28), cmap="gray")
plt.axis("off")

plt.show()

49 03817711922_ROHIT KUMAR SAXENA


Output:

# Load the MNIST dataset

# Training and Testing Dataset size and Intensity

# Display 10 random images from the dataset

# Distribution of Pixel Intensities in MNIST Dataset

50 03817711922_ROHIT KUMAR SAXENA


# autoencoder.fit()

# Display original and reconstructed images

Learning Outcomes:

 Understand the concept of autoencoders and their role in dimensionality reduction.


 Learn to preprocess and normalize high-dimensional data for efficient training.

51 03817711922_ROHIT KUMAR SAXENA


Experiment 7

Aim: Implementation of RNN-LSTM model for Stock Price Prediction in Python.

Theory:

Introduction:
Stock price prediction is a crucial task in financial markets, where traders and investors aim
to forecast future prices based on historical data. Recurrent Neural Networks (RNNs),
specifically Long Short-Term Memory (LSTM) networks, are widely used for time series
forecasting due to their ability to learn temporal dependencies. In this experiment, we
implement an LSTM-based model for stock price prediction using historical stock data from
Yahoo Finance. The model learns patterns from past prices and predicts future values based
on these observations.

Recurrent Neural Networks (RNNs) and Long Short-Term Memory (LSTM)

Concept and Working Mechanism:


Traditional RNNs are designed to process sequential data by maintaining a hidden state that
captures information from previous time steps. However, they suffer from the vanishing
gradient problem, making it difficult to learn long-term dependencies.
LSTM networks address this issue by introducing memory cells and gating mechanisms,
which regulate the flow of information through the network:
● Forget Gate: Decides which information from the previous state should be
discarded.
● Input Gate: Determines which new information should be stored in the cell state.
● Output Gate: Controls the output and updates the hidden state.
These mechanisms enable LSTMs to retain long-term dependencies, making them highly
effective for time series forecasting.

Application of LSTM in Stock Price Prediction


Dataset Preparation:
The dataset consists of historical stock prices, including Open, High, Low, Close, and
Volume data, retrieved using the Yahoo Finance API. For training, we use only the 'Close'
price, as it is a key indicator of stock movement. The data is preprocessed by:
● Normalizing prices using MinMax scaling to improve model convergence.
● Creating sequences of past prices to serve as input features for the LSTM model.
● Splitting the data into training and testing sets for model evaluation.
LSTM Model Architecture:
The LSTM model is structured as follows:
● Input Layer: Takes sequences of past stock prices as input.
● LSTM Layer: Captures temporal dependencies and patterns in the stock price
movements.
● Dense Layer: Outputs the predicted stock price.
Model architecture:

52 03817711922_ROHIT KUMAR SAXENA


● LSTM(50 units, ReLU activation, return_sequences=True)
● LSTM(50 units, ReLU activation)
● Dense(25 units, ReLU activation)
● Dense(1 unit, linear activation)
The model is trained using the Adam optimizer and Mean Squared Error (MSE) loss
function.

Performance Evaluation and Prediction:


After training, the model is evaluated on unseen test data. The predicted stock prices are
compared with actual prices using:
● Root Mean Squared Error (RMSE): Measures prediction accuracy.
● Visualization: A graph comparing actual and predicted prices to assess
performance.
While the model effectively captures trends, it may struggle with sudden market fluctuations
caused by external factors (e.g., news events, economic policies).

Conclusion:
LSTM networks provide a robust approach for stock price prediction by learning temporal
dependencies in historical data. Despite their effectiveness, challenges such as market
volatility and external influences limit their accuracy. Future improvements can include:
● Incorporating additional technical indicators (e.g., moving averages, RSI).
● Using hybrid models combining LSTMs with attention mechanisms.
● Exploring Transformer-based architectures for enhanced forecasting.
This experiment demonstrates the potential of deep learning in financial market analysis,
paving the way for more sophisticated predictive models.

53 03817711922_ROHIT KUMAR SAXENA


Code:

import numpy as np
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
# Download stock data
def get_stock_data(ticker, start, end):
df = yf.download(ticker, start=start, end=end)
return df[['Close']]

# Prepare data for LSTM


def prepare_data(data, time_steps=60):
scaler = MinMaxScaler(feature_range=(0, 1))
data_scaled = scaler.fit_transform(data)

X, y = [], []
for i in range(time_steps, len(data_scaled)):
X.append(data_scaled[i-time_steps:i, 0])
y.append(data_scaled[i, 0])

X, y = np.array(X), np.array(y)
X = np.reshape(X, (X.shape[0], X.shape[1], 1))
return X, y, scaler
# Build LSTM model
def build_lstm_model(input_shape):
model = Sequential([
LSTM(units=50, return_sequences=True, input_shape=input_shape),
Dropout(0.2),
LSTM(units=50, return_sequences=True),
Dropout(0.2),
LSTM(units=50),
Dropout(0.2),
Dense(units=1)
])
model.compile(optimizer='adam', loss='mean_squared_error')
return model
# Main execution
if __name__ == "__main__":
ticker = "AAPL"
start_date = "2020-01-01"
end_date = "2024-01-01"

data = get_stock_data(ticker, start_date, end_date)

54 03817711922_ROHIT KUMAR SAXENA


time_steps = 60
X, y, scaler = prepare_data(data.values, time_steps)

train_size = int(len(X) * 0.8)


X_train, y_train = X[:train_size], y[:train_size]
X_test, y_test = X[train_size:], y[train_size:]

model = build_lstm_model((X_train.shape[1], 1))


model.fit(X_train, y_train, epochs=20, batch_size=32,
validation_data=(X_test, y_test))

predictions = model.predict(X_test)
predictions = scaler.inverse_transform(predictions.reshape(-1, 1))

actual = scaler.inverse_transform(y_test.reshape(-1, 1))

plt.plot(actual, label='Actual Price')


plt.plot(predictions, label='Predicted Price')
plt.legend()
plt.show()

Outputs:

# downloading the dataset

# model training

55 03817711922_ROHIT KUMAR SAXENA


# Final Output

Learning Outcomes:

● Understand the architecture and working principles of LSTM networks for time-series
forecasting.
● Develop a stock price prediction model using Python and TensorFlow.
● Analyze model performance and visualize predictions effectively.

56 03817711922_ROHIT KUMAR SAXENA


Experiment 8

Aim: Implementation of transfer learning using the pre-trained model (VGG16) on


image dataset in Python.

Theory:

Transfer learning using pre-trained VGG16 for image classification


Transfer learning enables the reuse of knowledge gained by a model trained on a large
dataset for a different but related task. In image classification, pre-trained models like
VGG16, trained on ImageNet with over a million images across 1000 categories, serve as
strong foundations for extracting visual features from new datasets. This experiment aims to
implement transfer learning using the VGG16 model and evaluate the improvements
achieved by leveraging its pre-learned weights instead of training from scratch.
VGG16 Architecture and Role in Transfer Learning
VGG16 is a deep convolutional neural network with 16 weight layers, known for its uniform
architecture and strong performance in image classification tasks. It uses small 3x3
convolution filters, arranged in increasing depth, making the network both deep and efficient
at capturing hierarchical image features.
Main components of VGG16 include:
 Input Layer – Accepts images of size 224x224x3.
 Convolutional Layers (13 total) – Use 3x3 filters with stride 1 and ‘same’ padding;
ReLU is applied after each convolution to introduce non-linearity.
 Max Pooling Layers – 2x2 max pooling applied after every few convolution layers to
reduce spatial dimensions.
 Fully Connected Layers – Two dense layers with 4096 neurons each and ReLU
activation, followed by a final dense layer with 1000 outputs (for ImageNet) and
softmax activation.
 Dropout Layers – Applied between fully connected layers to prevent overfitting.
 Output Layer – Softmax activation used for classification.
By using the convolutional base of VGG16 as a fixed feature extractor or fine-tuning some of
its layers, new image classification models can quickly generalize to novel tasks without
needing massive datasets or training time.
Process of Image Classification using Transfer Learning
The classification pipeline involves importing the pre-trained VGG16 model (without the top
classifier), attaching custom layers, and training only these new layers while keeping the rest
frozen. Alternatively, a few deeper layers of VGG16 can be unfrozen for fine-tuning.
1. Data Preprocessing -
Input images are resized (typically to 224x224), normalized, and augmented to increase
dataset variability. Data generators are used to handle image loading and transformation in
real-time during training.

2. Model Construction -

57 03817711922_ROHIT KUMAR SAXENA


The VGG16 model is loaded with include_top=False and pre-trained ImageNet weights. On
top of the frozen convolutional base, custom layers like GlobalAveragePooling2D, Dense,
Dropout, and a final softmax Dense layer are added.

3. Training the Model -


The model is compiled using optimizers like Adam and the categorical crossentropy loss
function for multi-class classification. Training is done in batches using the training set, while
performance is monitored on the validation set.

4. Evaluation and Performance Measurement -


Once trained, the model is tested on unseen data. The benefit of using VGG16 lies in its
ability to quickly generalize using fewer data points. The trained model’s accuracy, precision,
recall, and F1-score are evaluated and compared to a baseline model trained from scratch.

Model Evaluation and Visualization


Evaluating transfer learning involves observing how well the model generalizes to the target
task. Confusion matrices, classification reports, and accuracy/loss curves are plotted to
analyze learning behavior.
Performance Metrics:
 Accuracy – Measures overall correctness of predictions.
 Precision & Recall – Assess model reliability per class.
 F1-Score – Harmonic mean of precision and recall.
 Training Time – Shorter with transfer learning compared to full training.
Visualization of Results
Visual tools like training/validation accuracy plots and confusion matrices help identify
overfitting, misclassifications, and class-wise performance. These insights reveal how
efficiently the VGG16 features are reused and adapted.
Conclusion
Transfer learning with VGG16 significantly reduces training time and enhances model
performance on small or domain-specific datasets. By utilizing pre-learned visual patterns,
the model requires fewer resources while achieving high accuracy. The evaluation confirms
that feature reuse through VGG16 is a powerful strategy in image classification tasks.

Code:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Flatten, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.applications.vgg16 import preprocess_input

58 03817711922_ROHIT KUMAR SAXENA


# Load and preprocess the CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = preprocess_input(x_train.astype('float32'))
x_test = preprocess_input(x_test.astype('float32'))

y_train_cat = to_categorical(y_train, 10)


y_test_cat = to_categorical(y_test, 10)

# Load pre-trained VGG16 without the top layer


base_model = VGG16(weights='imagenet', include_top=False,
input_shape=(32, 32, 3))

# Freeze base layers


for layer in base_model.layers:
layer.trainable = False

# Add custom top layers


x = Flatten()(base_model.output)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(10, activation='softmax')(x)

# Final model
model = Model(inputs=base_model.input, outputs=x)
model.compile(optimizer=Adam(), loss='categorical_crossentropy',
metrics=['accuracy'])

# Train the model


history = model.fit(x_train, y_train_cat, epochs=10, batch_size=64,
validation_data=(x_test, y_test_cat))

# Evaluate the model


loss, accuracy = model.evaluate(x_test, y_test_cat)
print(f"Test Accuracy: {accuracy:.4f}")

# Predict and generate classification report


y_pred = model.predict(x_test)
y_pred_classes = np.argmax(y_pred, axis=1)
y_true = y_test.flatten()

print("\nClassification Report:")
print(classification_report(y_true, y_pred_classes))

# Confusion Matrix
cm = confusion_matrix(y_true, y_pred_classes)
plt.figure(figsize=(10,7))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title("Confusion Matrix")

59 03817711922_ROHIT KUMAR SAXENA


plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.show()

# Plot accuracy and loss


plt.figure(figsize=(12,5))
plt.subplot(1,2,1)
plt.plot(history.history['accuracy'], label='Train Acc')
plt.plot(history.history['val_accuracy'], label='Val Acc')
plt.title("Model Accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.legend()

plt.subplot(1,2,2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.title("Model Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.tight_layout()
plt.show()

Output:

# Load and preprocess the CIFAR-10 dataset

# Load pre-trained VGG16 without the top layer

# Train the model

60 03817711922_ROHIT KUMAR SAXENA


# Evaluate the model

# Classification report

# Confusion Matrix

61 03817711922_ROHIT KUMAR SAXENA


# Plot accuracy and loss

Learning Outcomes:

1. Understood the application of transfer learning using the pre-trained VGG16 model
for efficient image classification.
2. Learned the structure and functionality of VGG16, including its convolutional layers,
pooling layers, and fully connected layers.
3. Gained the ability to evaluate and interpret model performance using classification
metrics and result visualizations.

62 03817711922_ROHIT KUMAR SAXENA


EXPERIMENT-1

Aim: Implement an Inception V3 for image classification.

Objectives:

 To implement the Inception V3 model for efficient and accurate image classification.
 To evaluate the performance benefits of using a deep, multi-scale convolutional
architecture.
Theory:

Image Classification using Inception V3


The Inception V3 model is a state-of-the-art convolutional neural network architecture known
for its

optimized depth, width, and utilization of computational resources. It is specifically designed


for large-scale image classification tasks where extracting intricate patterns and multi-scale
features is crucial. The primary goal of this experiment is to implement the Inception V3
architecture for accurate image classification while analyzing the improvements gained by its
multi-branch, multi-resolution design. This deep model efficiently balances performance and
speed, making it suitable for both academic and industrial image classification tasks.

Inception V3 Architecture and Functionality


Inception V3 improves upon earlier versions of the Inception model by integrating advanced
concepts like factorized convolutions, label smoothing, batch normalization, and auxiliary
classifiers. These enhancements collectively help in accelerating convergence, reducing
overfitting, and improving generalization. The architecture is built using a stack of Inception
modules, where each module performs multiple convolution operations in parallel using
filters of different sizes. This allows the model to learn spatial hierarchies of features
efficiently.

63 03817711922_ROHIT KUMAR SAXENA


Main components of Inception V3:
 Stem Convolutional Layers – Initial layers that extract low-level features from raw
input images.
 Inception Modules – Core blocks that apply multiple types of convolutions and
pooling in parallel to capture diverse features.
 Factorized Convolutions – Replace large filters (e.g., 5x5) with smaller ones (e.g.,
two 3x3s) to reduce the number of parameters.
 Auxiliary Classifiers – Intermediate output branches used during training to ensure
better gradient flow and reduce vanishing gradients.
 Batch Normalization – Applied after most layers to speed up training and provide
regularization.
 Global Average Pooling – Compresses spatial dimensions by averaging feature
maps, reducing overfitting.
 Dropout and Dense Layers – Dropout helps in regularization, and dense layers
map learned features to output class probabilities.
Process of Image Classification using Inception V3
1. Data Preprocessing -
Input images are resized to 299x299 pixels, normalized, and optionally augmented using
techniques such as horizontal flips, rotations, and zooms. This step enhances the model’s
robustness to real-world image variations and improves generalization.

2. Model Construction -
Inception V3 can be used in two ways: (a) from scratch with randomly initialized weights, or
(b) by leveraging a pre-trained version on ImageNet and fine-tuning it for a specific dataset.
The top classification layer is modified to match the number of classes in the current task.
The model is compiled using categorical crossentropy for multi-class classification and an
appropriate optimizer like RMSprop or Adam.

3. Training the Model -


The model is trained on training data while validating on a separate validation set. During
each epoch, weights are adjusted to minimize the classification error. Callbacks like early
stopping and learning rate scheduling may be used to stabilize and optimize training.
Auxiliary classifiers assist in faster convergence and deeper learning by injecting gradients
into earlier layers.

4. Evaluation and Prediction -


Once trained, the model is evaluated on a test set using performance metrics. The predicted
labels are compared with actual labels to assess classification accuracy. The final output is a
probability distribution over classes, from which the highest-probability class is chosen as the
predicted label.

Model Evaluation and Visualization


Evaluation helps determine how effectively the model classifies unseen images and
generalizes to new data.
Performance Metrics:
 Accuracy – Indicates overall correctness of predictions.
 Precision and Recall – Assess classification performance for each class.

64 03817711922_ROHIT KUMAR SAXENA


 F1-Score – Balances precision and recall for better evaluation in imbalanced
datasets.
 Confusion Matrix – Helps visualize true positives, false positives, and
misclassifications across classes.

Visualization of Results:
Plotting training vs. validation loss and accuracy over epochs gives insight into overfitting or
underfitting. Class activation maps (CAMs) or Grad-CAMs may also be used to visualize
which parts of the image influenced the model's predictions, enhancing interpretability and
trust in the model.

Conclusion
The Inception V3 model is an advanced deep learning architecture that brings significant
improvements to image classification through its scalable and efficient design. It effectively
captures both local and global patterns within images by combining convolutions of different
sizes. Using techniques like factorized convolutions, auxiliary classifiers, and batch
normalization, the model achieves a strong balance between performance and
computational efficiency. Evaluation with metrics and visualizations confirms its ability to
generalize well, making it highly suitable for complex image classification tasks in real-world
scenarios.

Code and Outputs:

import os
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.inception_v3 import InceptionV3,
preprocess_input
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns

train_dir = '/content/sports_dataset/sports_dataset/train'
test_dir = '/content/sports_dataset/sports_dataset/test'

train_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
rotation_range=20,
zoom_range=0.2,
horizontal_flip=True
)

65 03817711922_ROHIT KUMAR SAXENA


test_datagen =
ImageDataGenerator(preprocessing_function=preprocess_input)

train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(299, 299),
batch_size=32,
class_mode='categorical'
)

test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(299, 299),
batch_size=32,
class_mode='categorical',
shuffle=False
)

# Load base model


base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(train_generator.num_classes, activation='softmax')
(x)

model = Model(inputs=base_model.input, outputs=predictions)

# Freeze base layers


for layer in base_model.layers:
layer.trainable = False

# Compile model
model.compile(optimizer=Adam(learning_rate=0.0001),
loss='categorical_crossentropy',
metrics=['accuracy'])

# Train model
history = model.fit(
train_generator,
epochs=10,
validation_data=test_generator
)

loss, accuracy = model.evaluate(test_generator)


print(f"Test Loss: {loss:.4f}")
print(f"Test Accuracy: {accuracy:.4f}")

66 03817711922_ROHIT KUMAR SAXENA


predictions = model.predict(test_generator)
y_pred = np.argmax(predictions, axis=1)
y_true = test_generator.classes

class_labels = list(test_generator.class_indices.keys())

# Classification report
print("\nClassification Report:")
print(classification_report(y_true, y_pred,
target_names=test_generator.class_indices.keys()))

def predict_image(img_path, model, class_labels):


img = image.load_img(img_path, target_size=(299, 299))

67 03817711922_ROHIT KUMAR SAXENA


x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
predicted_class = class_labels[np.argmax(preds)]
return predicted_class

image_paths_and_labels = []
for class_name in class_labels:
folder_path = os.path.join(test_dir, class_name)
for filename in os.listdir(folder_path):
if filename.endswith(('.jpg', '.jpeg', '.png')):
image_path = os.path.join(folder_path, filename)
image_paths_and_labels.append((image_path, class_name))

num_images_to_display = 9
random_images = random.sample(image_paths_and_labels,
num_images_to_display)

plt.figure(figsize=(12, 12))
for i, (img_path, true_label) in enumerate(random_images):
predicted_class = predict_image(img_path, model, class_labels)

plt.subplot(3, 3, i + 1)
img = image.load_img(img_path, target_size=(299, 299))
plt.imshow(img)
plt.title(f"Pred: {predicted_class}\nTrue: {true_label}")
plt.axis('off')

plt.tight_layout()
plt.show()

68 03817711922_ROHIT KUMAR SAXENA


# Confusion matrix
cm = confusion_matrix(y_true, y_pred)
plt.figure(figsize=(12,10))
sns.heatmap(cm, annot=False, fmt='d', cmap='Blues')
plt.title('Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.show()

69 03817711922_ROHIT KUMAR SAXENA


plt.figure(figsize=(10,4))
plt.subplot(1,2,1)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Val Accuracy')
plt.title('Accuracy Over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()

plt.subplot(1,2,2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.title('Loss Over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()

plt.tight_layout()
plt.show()

70 03817711922_ROHIT KUMAR SAXENA


Learning Outcomes:

1. Gained understanding of Inception V3 architecture and its multi-scale convolutional


approach for efficient image classification.
2. Developed skills to preprocess, train, and fine-tune a deep learning model using real-
world image datasets.
3. Acquired the ability to evaluate model performance using metrics like accuracy, F1-
score, and visualize results for deeper insights.

71 03817711922_ROHIT KUMAR SAXENA


EXPERIMENT-2

Aim: Implement Bi-directional LSTM for text classification.

Objectives:

 To implement a Bi-directional LSTM model for accurate text classification.


 To analyze the impact of processing text sequences in both forward and backward
directions.
Theory:

Text classification using Bi-directional LSTM


Bi-directional Long Short-Term Memory (Bi-LSTM) networks are an advanced deep learning
architecture used in Natural Language Processing (NLP), specifically designed to improve
the performance of sequence-based tasks like text classification. This experiment aims to
implement a Bi-LSTM model to accurately classify text data while analyzing the advantages
of processing input sequences in both forward and backward directions. By capturing
context from both ends of a sentence, Bi-LSTM models help overcome the limitations of
standard LSTM, which can only learn from past information.
Bi-LSTM Architecture and Functionality
In Bi-LSTM, two separate LSTM layers are employed: one reads the input sequence from
left to right (forward pass), and the other reads from right to left (backward pass). The
outputs of both directions are concatenated at each time step, allowing the model to
understand both preceding and succeeding words. This is crucial for tasks where word
meaning heavily depends on surrounding context. For example, in the sentence "He banked
the fire," the meaning of banked changes if we don’t consider the full context.
Main components of the Bi-LSTM model include:
 Embedding Layer – Transforms input words into dense vector representations that
carry semantic meaning. Pre-trained embeddings like Word2Vec or GloVe can
enhance this further.

 Bi-LSTM Layer – Comprises two LSTM layers operating in opposite directions; they
extract both past and future dependencies for deeper context understanding.

 Dropout Layer – Randomly drops units to reduce overfitting and improve model
generalization.

 Dense Layer – Processes the combined outputs of the Bi-LSTM and projects them
to the desired output dimension.

Output Layer – Applies softmax or sigmoid activation to produce class probabilities


based on binary or multi-class classification setup.
Process of Text Classification using Bi-LSTM

72 03817711922_ROHIT KUMAR SAXENA


The overall process starts with raw text data and ends with accurate predictions of text
categories, typically involving the following steps:

1. Data Preprocessing-
The dataset is first cleaned (removing stop words, lowercasing, punctuation, etc.) and
tokenized into sequences of integers. These sequences are padded to ensure consistent
input length, enabling batch processing.
2. Model Construction-
A Sequential model is built starting with an Embedding layer. This is followed by a Bi-LSTM
layer that processes each input sequence in both directions. A dropout layer is added for
regularization, followed by a dense layer and a softmax/sigmoid output layer for
classification.
3. Training the Model-
The model is compiled using optimizers like Adam and loss functions such as binary or
categorical crossentropy, depending on the task. During training, the model adjusts its
internal weights using backpropagation through time to minimize the loss.
4. Evaluation and Prediction-
The trained model is tested on a separate validation/test dataset. Predictions are generated
and compared with actual labels to calculate performance metrics. The bidirectional
architecture typically results in more accurate predictions, especially in context-heavy tasks.
Model Evaluation and Visualization
Performance is assessed using standard classification metrics to ensure that the model
generalizes well to unseen data.
Performance Metrics:
 Accuracy – Measures the overall percentage of correct predictions.
 Precision – Indicates how many of the predicted positives are actually correct.
 Recall – Reflects how many actual positives the model correctly identified.
 F1-Score – Harmonic mean of precision and recall, useful in cases of class
imbalance.
Visualization of Results
Accuracy and loss curves across epochs are plotted to assess learning behavior and
convergence. Confusion matrices visualize true vs. predicted labels, making it easier to
identify which classes are well-learned and which are misclassified.
Conclusion
The Bi-directional LSTM architecture effectively enhances model understanding of textual
data by capturing both previous and future context, leading to better performance in
classification tasks. Its dual-layer setup allows deeper semantic learning, resulting in
improved accuracy and generalization. Evaluating through metrics and visual tools confirms
the strength of Bi-LSTM over standard unidirectional models in natural language
applications.

Code and Outputs:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

73 03817711922_ROHIT KUMAR SAXENA


from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report, confusion_matrix
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Bidirectional, LSTM,
Dense, Dropout

df = pd.read_csv('ecommerce_datsaset.csv',usecols=['Text', 'label'])

df['Text'] = df['Text'].astype(str)
label_counts = df['label'].value_counts()
valid_labels = label_counts[label_counts > 1].index
df = df[df['label'].isin(valid_labels)]

label_encoder = LabelEncoder()
df['label_encoded'] = label_encoder.fit_transform(df['label'])

tokenizer = Tokenizer(oov_token='<OOV>')
tokenizer.fit_on_texts(df['Text'])
sequences = tokenizer.texts_to_sequences(df['Text'])
word_index = tokenizer.word_index

max_length = 100
X = pad_sequences(sequences, maxlen=max_length, padding='post')
y = df['label_encoded'].values
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, stratify=y, random_state=42)

# Model
vocab_size = len(word_index) + 1
embedding_dim = 64

model = Sequential([
Embedding(vocab_size, embedding_dim, input_length=max_length),
Bidirectional(LSTM(64, return_sequences=False)),
Dropout(0.5),
Dense(32, activation='relu'),
Dense(len(np.unique(y)), activation='softmax')
])

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=10, batch_size=32,
validation_split=0.2)

74 03817711922_ROHIT KUMAR SAXENA


# Sample predictions
sample_texts = [
"Bluetooth speaker with long battery life",
"Casual summer dress for women",
"Hardcover fiction novel by famous author",
"Comfortable cotton bed sheets",
"Smartwatch with fitness tracking",
"Men's running shoes with arch support",
"Designer handbag made from leather",
"Children’s illustrated story book",
"LED monitor for gaming setup",
"Kitchen blender with multiple speed settings"
]
sample_seq = tokenizer.texts_to_sequences(sample_texts)
sample_pad = pad_sequences(sample_seq, maxlen=max_length,
padding='post')
predictions = model.predict(sample_pad)
for i, text in enumerate(sample_texts):
pred_class =
label_encoder.inverse_transform([np.argmax(predictions[i])])[0]
print(f"Text: {text} → Predicted Class: {pred_class}")

# Evaluation
y_pred = np.argmax(model.predict(X_test), axis=1)

75 03817711922_ROHIT KUMAR SAXENA


print(classification_report(y_test, y_pred,
target_names=label_encoder.classes_))

# Accuracy & loss plot


plt.figure(figsize=(12,5))
plt.subplot(1,2,1)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Val Accuracy')
plt.legend()
plt.title('Accuracy over epochs')

plt.subplot(1,2,2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Val Loss')
plt.legend()
plt.title('Loss over epochs')
plt.show()

# Confusion matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8,6))

76 03817711922_ROHIT KUMAR SAXENA


sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=label_encoder.classes_, yticklabels=label_encoder.classes_)
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()

Learning Outcomes:

1. Understood the working of Bi-directional LSTM and its ability to capture context from
both past and future sequences in text data.
2. Gained practical experience in building and training Bi-LSTM models for text
classification using Python and deep learning frameworks.
3. Learned to evaluate model performance using metrics like accuracy, precision, recall,
and F1-score for reliable classification results.

77 03817711922_ROHIT KUMAR SAXENA

You might also like