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

DSE_3141_Deep_Learning_Lab_Manual_2024_Week4

The document is a laboratory manual for the Deep Learning Lab course at Manipal Institute of Technology, detailing course outcomes, assessment plans, and a lesson plan covering various deep learning topics. It includes instructions on using TensorFlow and Keras for building neural networks, along with practical exercises and projects. The manual aims to equip students with skills in designing, developing, and evaluating deep learning models for tasks such as image classification and sequence modeling.

Uploaded by

arjunkhanna2406
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DSE_3141_Deep_Learning_Lab_Manual_2024_Week4

The document is a laboratory manual for the Deep Learning Lab course at Manipal Institute of Technology, detailing course outcomes, assessment plans, and a lesson plan covering various deep learning topics. It includes instructions on using TensorFlow and Keras for building neural networks, along with practical exercises and projects. The manual aims to equip students with skills in designing, developing, and evaluating deep learning models for tasks such as image classification and sequence modeling.

Uploaded by

arjunkhanna2406
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Vth Semester B.

Tech Data Science & Engineering


DSE 3141 Deep Learning Lab [0 0 3 1]

LABORATORY MANUAL

Instructors:
Dr. Rohini R Rao,
Dr. Abhilash K Pai,
Dr. Saraswati Koppad

Department of Data Science & Computer Applications


Manipal Institute of Technology, Manipal, India

JULY 2024
COURSE OUTCOMES (COS)

No. of
At the end of this course, the student should be able to: Contact
Marks
Hours
Apply the tools, on different dataset types, do performance
evaluation methods, and fine-tuning strategies to build and
CO1 6 15
optimize vanilla deep neural network models for performing
classification and regression on structured data.
Design, develop, fine-tune, evaluate simple and advanced CNN
CO2 9 35
models for Image classification.

Design, develop, fine-tune, evaluate simple and advanced RNN


CO3 models for sequence modelling tasks like Time series prediction 12 35
and NLP.

Design, develop, fine-tune, and evaluate Autoencoders and


CO4 9 15
Generative models for representational learning.

Total 36 100

ASSESSMENT PLAN
Components Continuous Evaluation End semester Examination
Duration 2.5 Hours per week 180 Minutes

Weightage 60% 40%

Pattern • 1 evaluation of 20 marks: Model Performance Analysis:


1. Record : 6M,
15 marks,
2. Program execution : 7M,
3. Quiz : 7M Program execution : 25 marks.
• 1 Mid-Sem Examination: 20 marks
• Mini Project : 20 marks
1. Phase1: Problem + Literature: 5M
2. Phase 2: End-to-End solution: 8M
3. Phase 3:Deployment & Demo: 7M
LESSON PLAN
Week No TOPICS Course
Outcome
Addressed

Week 1 Tensorflow & Keras Tutotial, CO1


Getting Started with Building Fully Connected Neural Networks In
Keras

Week 2 Experimenting with Deep Neural Networks CO1

Week 3 Convolutional Neural Networks (CNN) Vs Fully Connected Neural CO2


Networks for Image Classification

Week 4 Advanced CNN Architectures and Transfer Learning for Image CO2
Classification

Week 5 Recurrent Neural Networks for Time Series Forecasting CO3

Week 6 Mid-Semester Examination, Mini Project Phase 1 Evaluation CO2

Week 7 LSTM and GRU for Sentiment Analysis CO3

Week 8 Neural Machine Translation using Encoder-Decoder Architecture, CO3


Mini Project Phase 2 evaluation

Week 9 Image Reconstruction and Image Denoising Using Autoencoders, CO3


Image Generation Using Generative Adversarial Networks

Week 10 Fine tuning of LLM for NLP tasks CO4

Week 11 Mini Project Final Evaluation CO1

Week 12 End-term lab examination


References:

SL.No References
1 Aurelien Geron, “Hands-On Machine Learning with Scikit-Learn, Keras & Tensorflow,
OReilly Publications
2 Francois Chollet, “Deep Learning with Python”, Manning Publications Co, 2nd edition

3 Introduction to Tensorflow, https://www.tensorflow.org/learn


4 Keras Documentation, https://keras.io/
5 Ahmed Menshawy, Md. Rezaul Karim, Giancarlo Zaccone, “ Deep Learning with
TensorFlow”, Packt Publishing

TENSORFLOW & KERAS TUTORIAL


1.1 What is TensorFlow?
TensorFlow is an open-source deep learning framework developed by the Google Brain team. It allows
users to create, train, and deploy machine learning models, especially deep neural networks.
TensorFlow provides a flexible architecture to work with numerical data using multi-dimensional
arrays called tensors. It supports both CPU and GPU computations, making it suitable for running on
a variety of hardware.
1.2 What are Tensors?
In TensorFlow, tensors are the fundamental data structures used for representing data. They are similar
to multi-dimensional arrays and can hold data of any number of dimensions. Tensors are the building
blocks of neural networks, as they store the input data, weights, biases, and intermediate outputs during
the computation.
Examples of Tensors:
1. Scalar (0-D tensor): A single value is a 0-D tensor.
Eg: scalar_tensor = 5 #rank-0 tensor
2. Vector (1-D tensor): A 1-D tensor contains a sequence of values.
Eg: vector_tensor = [1, 2, 3, 4, 5] #rank-1 tensor
3. Matrix (2-D tensor): A 2-D tensor is an array of arrays.
Eg: matrix_tensor = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] #rank-2 tensor
4. Higher-dimensional tensor (e.g., 3-D tensor):
Eg: tensor_3d = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] #rank-3 tensor
Note: For a detailed explanation, visit the TensorFlow | Tensor documentation:
https://www.tensorflow.org/guide/tensor
1. 3 Graph Computation:
TensorFlow follows a symbolic approach for computation using graphs. A graph is a computational
graph that represents the flow of data through a series of operations (nodes) to produce output (tensors).
The nodes in the graph represent operations, and the edges represent tensors flowing between these
operations.
Example of Graph Computation:
import tensorflow as tf

# Define input variables (placeholders)


x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

# Define operations
x_squared = tf.square(x) # Square operation
x_squared_times_y = tf.multiply(x_squared, y) # Multiply operation
result = tf.add(x_squared_times_y, tf.add(y, 2)) # Add operation

# Create a session to run the computation graph


with tf.Session() as sess:
# Provide input values and run the graph
output = sess.run(result, feed_dict={x: 3.0, y: 4.0})
print("Output:", output)

Fig1: Computation graph in tensorflow for f(x, y) = x2y + y + 2


[Image Source: https://iq.opengenus.org]

1.4 What is Keras?


Keras is an open-source high-level neural networks API written in Python and capable of running on
top of TensorFlow, among other backends. It was designed with a focus on enabling fast
experimentation and easy-to-use syntax for building deep learning models. Keras provides a user-
friendly interface for constructing complex neural networks, making it an ideal choice for beginners in
deep learning.
Fig 2. Tensorflow and Keras as API
Image Source: https://developers.google.com/
Note: For a detailed explanation, visit the TensorFlow | Keras documentation:
https://www.tensorflow.org/guide/keras

In Keras, there are two primary ways to create deep learning models: the Sequential API and the
Functional API. Each approach serves a different purpose and offers distinct advantages.
1.5 Sequential API:
The Sequential API is the simplest and most straightforward way to build deep learning models in
Keras. It allows you to create a linear stack of layers, where each layer has exactly one input tensor and
one output tensor. This means that the data flows sequentially through each layer in the order they are
added to the model. The Sequential API is well-suited for simple feedforward neural networks and
other models that have a clear linear flow of data.
Example of Sequential API:
from keras.models import Sequential
from keras.layers import Dense, Input

# Create a sequential model


model = Sequential()

# Add layers to the model


model.add(Input(shape=(input_dim,)))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))

# Compile the model


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

# Print the model summary


model.summary()

1.6 Functional API:


The Functional API in Keras allows you to create more complex models with multiple input and output
tensors, as well as models with shared layers. It provides greater flexibility and is particularly useful
when building models with branching or merging architectures.
Example of Functional API:
from keras.models import Model
from keras.layers import Input, Dense

# Define input tensor


input_tensor = Input(shape=(input_dim,))

# Create layers and connect them


hidden_layer1 = Dense(64, activation='relu')(input_tensor)
hidden_layer2 = Dense(32, activation='relu')(hidden_layer1)
output_tensor = Dense(10, activation='softmax')(hidden_layer2)

# Create the model


model = Model(inputs=input_tensor, outputs=output_tensor)

# Compile the model


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

# Print the model summary


model.summary()

1.7 Deep Learning Model Life-Cycle


The deep learning model life cycle typically involves the following steps: Define the model, Compile
the model, Fit the model, Evaluate the model, and Make predictions.
I. Define the Model:
In this step, you specify the architecture of your deep learning model. You define the layers, their
configurations, activation functions, and any other required settings. The architecture depends on the
problem you are trying to solve, and it may include fully connected layers, convolutional layers,
recurrent layers, etc.
from keras.models import Sequential
from keras.layers import Dense

# Define the model


model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(input_dim,)))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))

II. Compile the Model:


After defining the model, you need to compile it. During this step, you specify the loss function,
optimizer, and evaluation metrics. The loss function is used to measure how well the model is
performing on the training data. The optimizer determines how the model's weights are updated during
training, and the evaluation metrics provide additional performance metrics during training.

# Compile the model


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

III. Fit the Model:

In this step, you train the model on your training data. You provide the input features (X) and their
corresponding target labels (y) to the model. The model then adjusts its internal parameters (weights)
through an optimization process (usually gradient descent) to minimize the defined loss function.

# Fit the model


model.fit(X_train, y_train, epochs=10, batch_size=32,
validation_data=(X_val, y_val))

IV. Evaluate the Model:

After the model is trained, you need to evaluate its performance on a separate set of data that it has
never seen before (e.g., a validation set or a test set). This step gives you an indication of how well
the model generalizes to unseen data.

# Evaluate the model


loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test loss: {loss}, Test accuracy: {accuracy}")

V. Make Predictions:

Once the model is trained and evaluated, you can use it to make predictions on new, unseen data. You
pass the new data to the model, and it will provide predictions based on what it has learned during
training.

# Make predictions
predictions = model.predict(X_new_data)

Example: Building a Simple Neural Network with Keras

#1) Import the necessray libraries

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Input

#2) For the tutorial, lets experiment with random data


# Generate random input data (features)
X = np.random.rand(num_samples, num_features)

# Generate random output labels (classes)


y = np.random.randint(0, num_classes, size=num_samples)

# Split the data into training and testing sets


split_ratio = 0.8
split_index = int(num_samples * split_ratio)

X_train, X_test = X[:split_index], X[split_index:]


y_train, y_test = y[:split_index], y[split_index:]

#3) Define the model

# Build the neural network model using Sequential API


model = Sequential([
Input(shape=(num_features,)),
Dense(6, activation='relu'), # Hidden layer with 6 neurons
Dense(num_classes, activation='softmax') # Output layer with
num_classes neurons and softmax activation for classification
])

# Display a summary of the model architecture


model.summary()

#4) Compile the model

# Compile the model


model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

#5) Fit/train the model

# Train the model using the training data


epochs = 50
batch_size = 32
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size,
validation_split=0.1)

#6) Evaluate/test the model

# Evaluate the model on the testing data


loss, accuracy = model.evaluate(X_test, y_test, batch_size=batch_size)
print("Test Loss:", loss)
print("Test Accuracy:", accuracy)
WEEK-1: GETTING STARTED WITH BUILDING FULLY CONNECTED
NEURAL NETWORKS IN KERAS

Q1. Using the Iris Flowers Dataset, build and Neural Network with the following specifications to
perform multi-class classification.
• Split the Data into Training: Validation: Testing = 80:10:10
• Number of Hidden Layers = 2, containing 8 Neurons and 4 Neurons
• Use RELU activation function in the hidden layers, choose the optimizer as ADAM and set
learning rate to be equal to 0.1.

Q2. Accurate measurement of body fat is inconvenient/costly, and it is desirable to have easy
methods of predicting Body Fat. Using the given Body Fat dataset, build a Neural Network to
predict body fat. Plot the training and validation performance curves and analyze the performance of
the proposed neural network.
The attributes of the dataset are as follows:
• Density determined from underwater weighing
• Percent body fat from Siri's (1956) equation
• Age (years)
• Weight (lbs)
• Height (inches)
• Neck circumference (cm)
• Chest circumference (cm)
• Abdomen 2 circumference (cm)
• Hip circumference (cm)
• Thigh circumference (cm)
• Knee circumference (cm)
• Ankle circumference (cm)
• Biceps (extended) circumference (cm)
• Forearm circumference (cm)
• Wrist circumference (cm)

Use the following hyperparameters/design choices for your neural network:


• Split the data in the ratio Training: Validation: Testing = 80:10:10.
• Perform Normalization using Standard Scalar.
• Number of Hidden layers = 3 and number of units for each hidden layers are 128,64,32,
respectively.
• Use RELU activation function in the hidden layers, choose the optimizer as ADAM and set
learning rate to be equal to 0.1.
Q3. For Q1 and Q2, Interpret the results of “model.summary()” (use comments/markup in the
notebook)
WEEK-2: EXPERIMENTING WITH DEEP NEURAL NETWORKS

Q1. Consider the given dataset for Customer Attrition Analysis (customer_attrition.csv), which
analyzes the customer behaviour to predict the likelihood of customers leaving or discontinuing their
relationship with a business.
The dataset has 14 features which are as follows :

• RowNumber:- Represents the number of rows


• CustomerId:- Represents customerId
• Surname:- Represents surname of the customer
• CreditScore:- Represents credit score of the customer
• Geography:- Represents the city to which customers belongs to
• Gender:- Represents Gender of the customer
• Age:- Represents age of the customer
• Tenure:- Represents tenure of the customer with a bank
• Balance:- Represents balance hold by the customer
• NumOfProducts:- Represents the number of bank services used by the customer
• HasCrCard:- Represents if a customer has a credit card or not
• IsActiveMember:- Represents if a customer is an active member or not
• EstimatedSalary:- Represents estimated salary of the customer
• Exited:- Represents if a customer is going to exit the bank or not.

1. Perform the required pre-processing and write comment lines to explain the pre-processing steps.

2. Perform experiments using (70,15,15) split and tabulate the performance in terms of Accuracy,
Precision & Recall for the following experimental setup:
a) Number of Hidden Layers and Number of Units per Layer

Number of Number of
Hidden Layers Units
1 128, 0 ,0
2 128, 64, 0
3 128, 64, 32

b) Epochs (10,20,30)
c) Activation function (Sigmoid, ReLU )
d) Learning rate (0.1, 0.01,0.001)
e) Visualize the training and validation loss against the epochs and comment on optimal
hyperparameters.
Q2. You are provided with the FIFA-19 dataset (fifa19.csv). The objective is to predict the
'Position' of a player using the following features:
• 'Finishing', 'HeadingAccuracy', 'ShortPassing', 'Volleys', 'Dribbling', ‘Curve', 'FKAccuracy',
'LongPassing', 'BallControl', 'Acceleration', 'SprintSpeed', 'Agility', 'Reactions', 'Balance',
'ShotPower', 'Jumping', 'Stamina', 'Strength', 'LongShots', 'Aggression','Interceptions',
'Positioning', 'Vision', 'Penalties', 'Composure', 'Marking', 'StandingTackle', 'SlidingTackle',
'GKDiving', 'GKHandling', 'GKKicking', 'GKPositioning', 'GKReflexes'

The positions are categorized as follows:

1. Goalkeeper: ["GK"]
2. Forward Player: ["ST", "LW", "RW", "LF", "RF", "RS","LS", "CF"]
3. Midfielder Player: ["CM", "RCM", "LCM", "CDM", "RDM", "LDM", "CAM", "LAM",
"RAM", "RM", "LM"]
4. Defender Player: ["CB", "RCB", "LCB", "LWB", "RWB", "LB", "RB"]

1. Perform Data Pre-processing: Pre-process the data by cleaning, normalizing, and encoding
as necessary. Include comment lines to explain each pre-processing step.

2. Perform Model Training and Evaluation after splitting the dataset into training (80%),
validation (10%), and test (10%) sets.

a) Experiment with neural network architectures using 1, 2, and 3 hidden layers.


Experimentally determine the optimal number of neurons for each layer.

b) For each model, find the best number of epochs by visualizing and analyzing the training
and validation loss against epochs.

c) Evaluate each model's performance using Accuracy, Precision, and Recall metrics.
Display the classification report for the test set and comment on the class-wise
performance.
WEEK-3: CONVOLUTIONAL NEURAL NETWORKS (CNN) VS FULLY
CONNECTED NEURAL NETWORKS FOR IMAGE CLASSIFICATION

Consider the following datasets:


A. Fashion MNIST dataset [Fashion MNIST dataset, an alternative to MNIST (keras.io)],
B. CIFAR-10 dataset [CIFAR10 small images classification dataset (keras.io)]

For each of the Datasets A and B, do the following:


Q1. Understanding the Dataset and Pre-processing: Implement the following:
a. Compute and display the number of classes.
b. Compute and display the dimensions of each image.
c. Display one image from each class.
d. Perform normalization.
Q2. Performing experiments on Fully Connected Neural Networks (FCNN):
a. Design a FCNN which is most suitable for the given dataset:
Experimentally choose the best network (the intuitions and learnings from the experiments you
have performed in Week-1 and Week-2 will help you choose the hyperparameters for the
network).
b. Train and test the network (choose the best epoch size so that there is no overfitting).
c. Plot the performance curves.
Q3. Performing experiments on a Convolutional Neural Networks (CNNs):
a. Design CNN-1 which contains:
• One Convolution layer which uses 32 kernels each of size 5x5, stride = 1 and, padding =0.
• One Pooling layer which uses MAXPOOLING with stride =2.
• One hidden layer having number of neurons = 100
b. Design CNN-2 which contains:
• Two back-to-back Convolution layers which uses 32 kernels each of size 3x3, stride = 1,
and padding =0.
• One Pooling layer which uses MAXPOOLING with stride =2.
• One hidden layer having number of neurons = 100
Note: use ReLU activation function after each convolution layer.
c. Train and test the networks (choose the best epoch size so that there is no overfitting).
d. Plot the performance curves for CNN-1 and CNN-2.
e. Compare the performances of CNN-1 and CNN-2.
Q4. Compare the performances of FCNN and CNN.
Q5. Compare the number of parameters in the FCNN and the CNN. Which layer/s in CNN contribute
most to the total number of parameters.
WEEK-4: ADVANCED CNN ARCHITECTURES AND TRANSFER
LEARNING FOR IMAGE CLASSIFICATION

Q1. Implementation and Comparison of CNN Architectures

A) Implement the following CNN architectures:

1) LeNet-5

2) AlexNet

B) Train, test, and compare the performance of these models on the following datasets:

Cats and Dogs Dataset:


Download from: https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip

Face Mask Detection Dataset:


Download from:
https://drive.google.com/file/d/1ejyQe12TIHjOHj6oT5dxHVwcwap41TuV/view?usp=sha
ring

Q2. Transfer Learning and Model Performance Analysis

Train, test, and report the performance of the following PRE-TRAINED IMAGENET models
on the Cats and Dogs Dataset and Face Mask Detection Dataset.

A) VGG-16
B) GoogleNet (InceptionV3)
C) ResNet50
D) EfficientNetB0
E) MobileNetV2

You might also like