Deep Learning Lab Manual (1)
Deep Learning Lab Manual (1)
Lab Manual
For
B.TECH(R-20 Regulation)
(2023-2024)
Mission:
M1: To impart value based technical education in CSE
specific to AI & ML through innovative teaching and
learning methods.
M2: To produce outstanding professionals by imparting
quality training, hands-on-experience and value based
education.
M3: To produce competent graduates suitable for
industries and organizations at global level including
research and development for Social responsibility.
Prerequisite:
• Any programming language Python/R/Matlab
Course Objectives:
• To implement ANN and Deep Learning concepts
List of Experiments:
1. Implementation of MP model
2. Implementation of feed forward neural network
3. Implementation of back propagation neural network
4. Implement all activation function of neural network for any pattern recognition
application
5. Implement any one of ImageNet or GoogleNet
6. Implement a system to recognize hand written character using CNN
7. Classify images appropriately using CNN
8. Implement LSTM Neural Network for Time Series Prediction
1. Implementation of MP model.
DESCRIPTION:
MP Neuron model:
The McCulloch-Pitts Neuron model relates to the theory
developed in the 90s where Walter McCulloch and Warren
Pitts put forward the idea. It is similar to a human neuron in
Biology consisting of Axons, Soma etc. These are mapped to
the functioning of the neural network.
3) Model-
It consists of a function with a single parameter. The
input is aggregated(g). There is a threshold value
which is decided. If the value of the function is equal to
or greater than the threshold value, it gives a positive
output and vice versa.
4) Loss function-
5) Learning -
6) Accuracy-
Algorithm Implementation
2. Feed-forward neural network implementation
DESCRIPTION:
Import libraries
Include weights
a1 = num.random.randn(inputlayer_dimensionality,
hiddenlayer_dimensionality)# weights for layer 1
c1 = num.zeros((1, hiddenlayer_dimensionality))# bias for layer 1
a2= num.random.randn(hiddenlayer_dimensionality,
hiddenlayer_dimensionality)# weights for layer 2
c2 = num.zeros((1, hiddenlayer_dimensionality))# bias for layer 2
a3= num.random.randn(hiddenlayer_dimensionality,
outputlayer_dimensionality)# weights for layer 3
c3 = num.zeros((1, outputlayer_dimensionality))# bias for layer 3
Note that the weighted sum is the sum of weights, input signal, and
bias element.
All weights provided in the first, second, and third layers are used
to calculate the weighted sum of neurons in the first, second, and
third hidden layers.
A softmax function is applied in the last layer. The list of numbers
sent to this function is transformed into a probability list whose
probabilities are proportional to the numbers in the list.
3. Implementation of back propagation neural network
DESCRIPTION:
BPN was discovered by Rumelhart, Williams & Honton in 1986. The core
concept of BPN is to backpropagate or spread the error from units of output
layer to internal hidden layers in order to tune the weights to ensure lower error
rates. It is considered a practice of fine-tuning the weights of neural networks in
each iteration. Proper tuning of the weights will make a sure minimum loss and
this will make a more robust, and generalizable trained neural network.
# Import Libraries
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
Load Dataset
Let’s first load the Iris dataset using load_iris() function of scikit-learn library
and seprate them in features and target labels. This data set has three classes Iris-
setosa, Iris-versicolor, and Iris-virginica.
# Load dataset
data = load_iris()
# Get features and target
X=data.data
y=data.target
Prepare Dataset
Create dummy variables for class labels using get_dummies() function
# Get dummy variable
y = pd.get_dummies(y).values
y[:3]
array([[1, 0, 0],
[1, 0, 0],
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# feedforward propagation
# on hidden layer
Z1 = np.dot(x_train, W1)
A1 = sigmoid(Z1)
# on output layer
Z2 = np.dot(A1, W2)
A2 = sigmoid(Z2)
# Calculating error
mse = mean_squared_error(A2, y_train)
acc = accuracy(A2, y_train)
results=results.append({"mse":mse, "accuracy":acc},ignore_index=True )
# backpropagation
E1 = A2 - y_train
dW1 = E1 * A2 * (1 - A2)
E2 = np.dot(dW1, W2.T)
dW2 = E2 * A1 * (1 - A1)
# weight updates
W2_update = np.dot(A1.T, dW1) / N
W1_update = np.dot(x_train.T, dW2) / N
W2 = W2 - learning_rate * W2_update
W1 = W1 - learning_rate * W1_update
results.mse.plot(title="Mean Squared Error")
Output:
4. Implement all activation function of neural network for any pattern
recognition application
DESCRIPTION:
Technically, the activation function is used within or after the internal processing
of each node in the network, although networks are designed to use the same
activation function for all nodes in a layer.
1 # example plot for the relu activation function
2 from matplotlib import pyplot
3
4 # rectified linear function
5 def rectified(x):
6 return max(0.0, x)
7
8 # define input data
9 inputs = [x for x in range(-10, 10)]
10 # calculate outputs
11 outputs = [rectified(x) for x in inputs]
12 # plot inputs vs outputs
13 pyplot.plot(inputs, outputs)
14 pyplot.show()
Running the example calculates the outputs for a range of values and creates a
plot of inputs versus outputs.
We can see the familiar kink shape of the ReLU activation function.
5. Implement any one of ImageNet or GoogleNet
Deep Learning
GoogLeNet
The table below depicts the conventional GoogLeNet architecture. Have a quick
review of the table before reading more on the table’s characteristics and
features.
Characteristics and features of GoogLeNet configuration table (figure 1)
Type: This refers to the name of the current layer of the component within the
architecture
Stride: Defines the amount of shift the filter/sliding window takes over the
input image.
#1x1 #3x3 #5x5: Refers to the various convolutions filters used within the
inception module.
#3X3 reduce #5x5 reduce: Refers to the numbers of 1x1 filters used before the
convolutions.
Ops: Refers to the number of mathematical operations carried out within the
component
6. Implement a system to recognize hand written character using CNN
import tensorflow as tf
In [18]:
import matplotlib.pyplot as plt
In [56]:
import numpy as np
import pandas as pd
In [3]:
from mnist import MNIST
In [4]:
mndata = MNIST('.\emnist_data')
In [5]:
mndata.select_emnist('balanced')
In [6]:
mndata
Out[6]:
<mnist.loader.MNIST at 0x178ce31cf98>
In [38]:
images, labels = mndata.load_training()
In [40]:
n_labels = np.array(labels)
In [24]:
n_images = np.array(images)
In [36]:
single_image = n_images[1345].reshape(28,28)
In [37]:
plt.imshow(single_image)
plt.show()
Before moving further we need to understand what is the neural network? Let’s
go…
Neural Network:
A neural network is constructed from several interconnected nodes
called “neurons”. Neurons are arranged into the input layer, hidden layer,
and output layer. The input layer corresponds to our predictors/features and
the Output layer to our response variable/s.
Multi-Layer Perceptron(MLP):
The neural network with an input layer, one or more hidden layers, and one
output layer is called a multi-layer perceptron (MLP). MLP is Invented
by Frank Rosenblatt in the year of 1957. MLP given below has 5 input nodes, 5
hidden nodes with two hidden layers, and one output node
Image Source: Google.com
– The threshold value is the minimum value that must be possessed by the
input so that it can be activated.
– The task of the neuron is to perform a weighted sum of all the input signals and
apply the activation function on the sum before passing it to the next(hidden or
output) layer.
import NumPy as np
%matplotlib inline
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import TensorFlow as tf
tf.compat.v1.set_random_seed(2019)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16,(3,3),activation = "relu" ,
input_shape = (180,180,3)) ,
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(32,(3,3),activation = "relu") ,
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64,(3,3),activation = "relu") ,
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(128,(3,3),activation = "relu"),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(550,activation="relu"), #Adding
the Hidden layer
tf.keras.layers.Dropout(0.1,seed = 2019),
tf.keras.layers.Dense(400,activation ="relu"),
tf.keras.layers.Dropout(0.3,seed = 2019),
tf.keras.layers.Dense(300,activation="relu"),
tf.keras.layers.Dropout(0.4,seed = 2019),
tf.keras.layers.Dense(200,activation ="relu"),
tf.keras.layers.Dropout(0.2,seed = 2019),
tf.keras.layers.Dense(5,activation = "softmax") #Adding
the Output Layer
])
A convoluted image can be too large and so it is reduced without
losing features or patterns, so pooling is done.
model.summary()
Model: "sequential"
______________________________________________________________
___
Layer (type) Output Shape Param #
==============================================================
===
conv2d (Conv2D) (None, 178, 178, 16) 448
______________________________________________________________
___
max_pooling2d (MaxPooling2D) (None, 89, 89, 16) 0
______________________________________________________________
___
conv2d_1 (Conv2D) (None, 87, 87, 32) 4640
______________________________________________________________
___
max_pooling2d_1 (MaxPooling2 (None, 43, 43, 32) 0
______________________________________________________________
___
conv2d_2 (Conv2D) (None, 41, 41, 64) 18496
______________________________________________________________
___
max_pooling2d_2 (MaxPooling2 (None, 20, 20, 64) 0
______________________________________________________________
___
conv2d_3 (Conv2D) (None, 18, 18, 128) 73856
______________________________________________________________
___
max_pooling2d_3 (MaxPooling2 (None, 9, 9, 128) 0
______________________________________________________________
___
flatten (Flatten) (None, 10368) 0
______________________________________________________________
___
dense (Dense) (None, 550) 5702950
______________________________________________________________
___
dropout (Dropout) (None, 550) 0
______________________________________________________________
___
dense_1 (Dense) (None, 400) 220400
______________________________________________________________
___
dropout_1 (Dropout) (None, 400) 0
______________________________________________________________
___
dense_2 (Dense) (None, 300) 120300
______________________________________________________________
___
dropout_2 (Dropout) (None, 300) 0
______________________________________________________________
___
dense_3 (Dense) (None, 200) 60200
______________________________________________________________
___
dropout_3 (Dropout) (None, 200) 0
______________________________________________________________
___
dense_4 (Dense) (None, 5) 1005
==============================================================
===
Total params: 6,202,295
Trainable params: 6,202,295
Non-trainable params: 0
5)In this step, we will see how to set the data directory and
generate image data.
batch_size=bs,
class_mode = 'categorical',
target_size=(180,180))
history = model.fit(train_generator,
validation_data=validation_generator,
steps_per_epoch=150 // bs,
epochs=30,
validation_steps=50 // bs,
verbose=2)
Epoch 1/30
5/5 - 4s - loss: 0.8625 - acc: 0.6933 - val_loss: 1.1741 -
val_acc: 0.5000
Epoch 2/30
5/5 - 3s - loss: 0.7539 - acc: 0.7467 - val_loss: 1.2036 -
val_acc: 0.5333
Epoch 3/30
5/5 - 3s - loss: 0.7829 - acc: 0.7400 - val_loss: 1.2483 -
val_acc: 0.5667
Epoch 4/30
5/5 - 3s - loss: 0.6823 - acc: 0.7867 - val_loss: 1.3290 -
val_acc: 0.4333
Epoch 5/30
5/5 - 3s - loss: 0.6892 - acc: 0.7800 - val_loss: 1.6482 -
val_acc: 0.4333
Epoch 6/30
5/5 - 3s - loss: 0.7903 - acc: 0.7467 - val_loss: 1.0440 -
val_acc: 0.6333
Epoch 7/30
5/5 - 3s - loss: 0.5731 - acc: 0.8267 - val_loss: 1.5226 -
val_acc: 0.5000
Epoch 8/30
5/5 - 3s - loss: 0.5949 - acc: 0.8333 - val_loss: 0.9984 -
val_acc: 0.6667
Epoch 9/30
5/5 - 3s - loss: 0.6162 - acc: 0.8069 - val_loss: 1.1490 -
val_acc: 0.5667
Epoch 10/30
5/5 - 3s - loss: 0.7509 - acc: 0.7600 - val_loss: 1.3168 -
val_acc: 0.5000
Epoch 11/30
5/5 - 4s - loss: 0.6180 - acc: 0.7862 - val_loss: 1.1918 -
val_acc: 0.7000
Epoch 12/30
5/5 - 3s - loss: 0.4936 - acc: 0.8467 - val_loss: 1.0488 -
val_acc: 0.6333
Epoch 13/30
5/5 - 3s - loss: 0.4290 - acc: 0.8400 - val_loss: 0.9400 -
val_acc: 0.6667
Epoch 14/30
5/5 - 3s - loss: 0.4205 - acc: 0.8533 - val_loss: 1.0716 -
val_acc: 0.7000
Epoch 15/30
5/5 - 4s - loss: 0.5750 - acc: 0.8067 - val_loss: 1.2055 -
val_acc: 0.6000
Epoch 16/30
5/5 - 4s - loss: 0.4080 - acc: 0.8533 - val_loss: 1.5014 -
val_acc: 0.6667
Epoch 17/30
5/5 - 3s - loss: 0.3686 - acc: 0.8467 - val_loss: 1.0441 -
val_acc: 0.5667
Epoch 18/30
5/5 - 3s - loss: 0.5474 - acc: 0.8067 - val_loss: 0.9662 -
val_acc: 0.7333
Epoch 19/30
5/5 - 3s - loss: 0.5646 - acc: 0.8138 - val_loss: 0.9151 -
val_acc: 0.7000
Epoch 20/30
5/5 - 4s - loss: 0.3579 - acc: 0.8800 - val_loss: 1.4184 -
val_acc: 0.5667
Epoch 21/30
5/5 - 3s - loss: 0.3714 - acc: 0.8800 - val_loss: 2.0762 -
val_acc: 0.6333
Epoch 22/30
5/5 - 3s - loss: 0.3654 - acc: 0.8933 - val_loss: 1.8273 -
val_acc: 0.5667
Epoch 23/30
5/5 - 3s - loss: 0.3845 - acc: 0.8933 - val_loss: 1.0199 -
val_acc: 0.7333
Epoch 24/30
5/5 - 3s - loss: 0.3356 - acc: 0.9000 - val_loss: 0.5168 -
val_acc: 0.8333
Epoch 25/30
5/5 - 3s - loss: 0.3612 - acc: 0.8667 - val_loss: 1.7924 -
val_acc: 0.5667
Epoch 26/30
5/5 - 3s - loss: 0.3075 - acc: 0.8867 - val_loss: 1.0720 -
val_acc: 0.6667
Epoch 27/30
5/5 - 3s - loss: 0.2820 - acc: 0.9400 - val_loss: 2.2798 -
val_acc: 0.5667
Epoch 28/30
5/5 - 3s - loss: 0.3606 - acc: 0.8621 - val_loss: 1.2423 -
val_acc: 0.8000
Epoch 29/30
5/5 - 3s - loss: 0.2630 - acc: 0.9000 - val_loss: 1.4235 -
val_acc: 0.6333
Epoch 30/30
5/5 - 3s - loss: 0.3790 - acc: 0.9000 - val_loss: 0.6173 -
val_acc: 0.8000
The above function trains the neural network using the training
set and evaluates its performance on the test set. The functions
return two metrics for each epoch ‘acc’ and ‘val_acc’ which are
the accuracy of predictions obtained in the training set and
accuracy attained in the test set respectively.
1. Need complete data for training. Some missing values can cause a very bad
performing result on your model. Even though there are some ways to handle
missing values but it’s very hard to
1. Preprocessing
3. Fit Model
4. Evaluation
5. Visualize prediction
Input Gate: controls shipping the addition of information to the cell state. In
other words, input gate will consider which information should be added to
the cell state in order to ensure adding important information not redundant
information or noise.
Memory Cell: (1) controls the value could be removed or refreshed (2)
contains a value that might need to be preserved as additional information
for many other time steps.
Output Gate: controls selecting useful learning information from the current
cell state as an output.
LSTMs are sensitive to the scale of the input data. During the
Preprocessing step, I applied MinMaxScaler preprocessing class from
the scikit-learn module to normalize/rescale dataset.
Split dataset into training and test dataset. Create input 3-D input shape
for LSTM.
model=model_lstm(look_back)
A small tip here: usually, it would be nice to try GRU (Gated Recurrent
Units), a “simplified version” of LSTM when LSTM shows overfitting.
By comparing the error metrics across models in this article, we got even
better results (LSTM shows smaller error metrics). So far, LSTM
outperforms other two models. We forecast about 2 months (65 days, to
be specific) with MAE equals 3744. In other words, the 2 months of
predictive advertising spend would be off about $3744 against the
actually spend. For an average monthly spend equals $2.5MM+. This is a
very good forecast! Also, it’s a big improvement from stats model results
in my another article.