DRL Practical file

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

GYAN GANGA INSTITUTE OF TECHNOLOGY AND SCIENCES,

JABALPUR

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LAB MANUAL

Deep & Reinforcement Learning

(CS-704)

Student Name

Enrollment Number

SESSION: 2024-25
Sr.
Signatur
No Topics/Sub Topics Date Grade Remar
e
. k
Experiment No. 1 Write a Program to
1
implement Single Layer Perceptron
Experiment No. 2 Write a program to
3 implement different activation function used
in deep learning
Design & verify the Full Adder and
3
Subtractor
WAP to add two 8 bit numbers and store the
4
result at memory location 2000
Experiment No. 5 Write a Program to
5
implement Autoencoder
Experiment No. 6 Write a program to
6
implement CNN.
Experiment No. 7 Write a program to
7
implement LeNet.
Experiment No 8 Write a Program to
8
implement SimpleRNN
Experiment No. 9 Case Study: Industry
9 Automation with Reinforcement Learning
(RL)
Experiment no. 1 Write a Program to implement Single Layer Perceptron

import numpy as np

input_vector = np.array([1.66, 1.56])


weights_1 = np.array([1.45, -0.66])
bias = np.array([0.0])

import numpy as np
input_vector = np.array([1.66, 1.56])
weights_1 = np.array([1.45, -0.66])
bias = np.array([0.0])

def sigmoid(x):
return 1 / (1 + np.exp(-x))

def make_prediction(input_vector, weights, bias):


layer_1 = np.dot(input_vector, weights) + bias # Z=X1*w1+ X2*W2
layer_2 = sigmoid(layer_1)
return layer_2

prediction = make_prediction(input_vector, weights_1, bias)

print("The prediction result is:", prediction)

The prediction result is: [0.7985731]

Double-click (or enter) to edit

prediction = make_prediction(input_vector, weights_1, bias)

print("The prediction result is:", prediction)

The prediction result is: [0.7985731]

input_vector = np.array([2, 1.5])


prediction = make_prediction(input_vector, weights_1, bias)

print("The prediction result is:", prediction)

The prediction result is: [0.87101915]

Double-click (or enter) to edit

Train The network

target = 0

mse = np.square(prediction - target)

print("Prediction:",prediction, "Error:",mse)

Prediction: [0.87101915] Error: [0.75867436]


keyboard_arrow_down New Section
Experiment No 2: Write a program to implement different activation function used in deep learning

import numpy as np

def sigmoid(x):
return 1 / (1 + np.exp(-x))

def tanh(x):
return np.tanh(x)

def relu(x):
return np.maximum(0, x)

def leaky_relu(x, alpha=0.01):


return np.maximum(alpha * x, x)

def softmax(x):
exp_x = np.exp(x)
return exp_x / np.sum(exp_x)

x = np.array([-1, 0, 1])

# Sigmoid
print(sigmoid(x))

[0.26894142 0.5 0.73105858]

# Tanh
print(tanh(x))

[-0.76159416 0. 0.76159416]

# ReLU
print(relu(x))

[0 0 1]

# Leaky ReLU
print(leaky_relu(x))

[-0.01 0. 1. ]

# Softmax
print(softmax(x))

[0.09003057 0.24472847 0.66524096]


keyboard_arrow_down Experiment No. 3 Write a Program to Implement Backpropogation Alogorithm
import numpy
import matplotlib.pyplot

def sigmoid(sop):
return 1.0/(1+numpy.exp(-1*sop))

def error(predicted, target):


return numpy.power(predicted-target, 2)

def error_predicted_deriv(predicted, target):


return 2*(predicted-target)

def sigmoid_sop_deriv(sop):
return sigmoid(sop)*(1.0-sigmoid(sop))

def sop_w_deriv(x):
return x

def update_w(w, grad, learning_rate):


return w - learning_rate*grad

x1=0.1
x2=0.4

target = 0.7
learning_rate = 0.01

w1=numpy.random.rand()
w2=numpy.random.rand()

print("Initial W : ", w1, w2)

predicted_output = []
network_error = []

old_err = 0
for k in range(80000):
# Forward Pass
Z = w1*x1 + w2*x2
predicted = sigmoid(Z)
err = error(predicted, target)

predicted_output.append(predicted)
network_error.append(err)

# Backward Pass
g1 = error_predicted_deriv(predicted, target)

g2 = sigmoid_sop_deriv(predicted)

g3w1 = sop_w_deriv(x1)
g3w2 = sop_w_deriv(x2)

gradw1 = g3w1*g2*g1
gradw2 = g3w2*g2*g1

w1 = update_w(w1, gradw1, learning_rate)


w2 = update_w(w2, gradw2, learning_rate)

print(predicted)

matplotlib.pyplot.figure()
matplotlib.pyplot.plot(network_error)
matplotlib.pyplot.title("Iteration Number vs Error")
matplotlib.pyplot.xlabel("Iteration Number")
matplotlib.pyplot.ylabel("Error")

matplotlib.pyplot.figure()
t l tlib l t l t( di t d t t)
matplotlib.pyplot.plot(predicted_output)
matplotlib.pyplot.title("Iteration Number vs Prediction")
matplotlib.pyplot.xlabel("Iteration Number")
matplotlib.pyplot.ylabel("Prediction")

Streaming output truncated to the last 5000 lines.


0.6999991701400979
0.6999991702714675
0.6999991704028162
0.6999991705341442
0.6999991706654515
0.6999991707967378
0.6999991709280035
0.6999991710592481
0.6999991711904723
0.6999991713216754
0.699999171452858
0.6999991715840197
0.6999991717151607
0.6999991718462808
0.6999991719773804
0.6999991721084591
0.699999172239517
0.6999991723705542
0.6999991725015707
0.6999991726325664
0.6999991727635414
0.6999991728944955
0.699999173025429
0.699999173156342
0.699999173287234
0.6999991734181054
0.699999173548956
0.6999991736797858
0.6999991738105952
0.6999991739413836
0.6999991740721514
0.6999991742028985
0.6999991743336249
0.6999991744643306
0.6999991745950155
0.69999917472568
0.6999991748563235
0.6999991749869465
0.6999991751175487
0.6999991752481304
0.6999991753786913
0.6999991755092315
0.6999991756397511
0.6999991757702502
0.6999991759007284
0.699999176031186
0.699999176161623
0.6999991762920392
0.6999991764224349
0.69999917655281
0.6999991766831644
0.6999991768134981
0.6999991769438112
0.6999991770741038
0.6999991772043755
0.6999991773346269
0 99999 8
keyboard_arrow_down Artificial Neural Network
Experiment no. 4 Write a program to implement Artificial Neural Network

keyboard_arrow_down Part 1 - Data Preprocessing


# Importing the libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical

# Importing the dataset


data = pd.read_csv('/content/drive/MyDrive/Deeplearning/Churn_Modelling.csv')

from google.colab import drive


drive.mount('/content/drive')

more_horiz
data = data.drop(['RowNumber', 'CustomerId', 'Surname'], axis=1)

Columns like RowNumber, CustomerId, and Surname do not provide useful information for predicting churn, so we drop them.

Double-click (or enter) to edit

label_encoder = LabelEncoder()
data['Gender'] = label_encoder.fit_transform(data['Gender'])
data = pd.get_dummies(data, columns=['Geography'], drop_first=True)

Label Encoding: For Gender, we encode the categories as 0 and 1 (e.g., Male = 1, Female = 0).

One-Hot Encoding: For Geography, we create dummy variables (e.g., Geography_France, Geography_Spain) to represent different countries
without introducing ordinal relationships.

#Split Data into Features and Target:


X = data.drop('Exited', axis=1)
y = data['Exited']

Start coding or generate with AI.

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

Standardization scales the features to have a mean of 0 and a standard deviation of 1, helping the model converge faster and improve
performance.

keyboard_arrow_down Part 2 - Now let's make the ANN!


# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer


classifier.add(Dense(3, kernel_initializer = 'uniform',activation='relu',input_dim=11))

/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` ar


super().__init__(activity_regularizer=activity_regularizer, **kwargs)

# Adding the second hidden layer


classifier.add(Dense( 3, kernel_initializer = 'uniform',activation='relu'))

# Adding the output layer


classifier.add(Dense(1, kernel_initializer = 'uniform', activation = 'sigmoid'))

In Keras (a high-level API for building and training neural networks), the compile method configures the model for training. It specifies the
optimizer, loss function, and metrics that the model will use during training and evaluation

# Compiling the ANN


classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

Optimizer: adam optimizer is used for efficient training. Loss Function: binary_crossentropy is appropriate for binary classification. Metrics: We
use accuracy as a metric to evaluate performance.

model_history = classifier.fit(X_train, y_train, batch_size=10, epochs=100)


800/800 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.8363 - loss: 0.4028
Epoch 99/100
800/800 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.8315 - loss: 0.4067
Epoch 100/100
800/800 ━━━━━━━━━━━━━━━━━━━━ 1s 1ms/step - accuracy: 0.8317 - loss: 0.4020

# list all data in history

print(model_history.history.keys())

dict_keys(['accuracy', 'loss'])

# summarize history for accuracy


from matplotlib import pyplot as plt
plt.plot(model_history.history['accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# summarize history for loss


plt.plot(model_history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
keyboard_arrow_down Part 3 - Making the predictions and evaluating the model
# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)
y_pred

63/63 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step


array([[False],
[False],
[False],
...,
[False],
[False],
[False]])

# Making the Confusion Matrix


from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
cm

array([[1552, 43],
[ 276, 129]])

# Calculate the Accuracy


from sklearn.metrics import accuracy_score
score=accuracy_score(y_pred,y_test)
score

0.8405
Experiment No.5 Write a Program to implement Autoencoder

An autoencoder is an unsupervised algorithm that extracts the meaningful information from image. Autoencoder basically compresses the
image features using encoder and reconstructs the image using fewer number of bits from the bottleneck also known as latent space.

There are variety of autoencoders, such as the convolutional autoencoder, denoising autoencoder, variational autoencoder and sparse
autoencoder. We are going to implement convolutional autoencoder.

keyboard_arrow_down Dataset: COVID - 19 Xray Image Dataset


The COVID - 19 X-Ray Image dataset has nearly 350 Chest X-ray scans which are categorized in two classes normal and covid-19. The image
are grayscale and dimentions of image are 256 x 256

Step - 1: Importing Libraries

Double-click (or enter) to edit

add Code add Text


from IPython.display import Image, SVG
import matplotlib.pyplot as plt # displaying image
%matplotlib inline
import numpy as np
import os # to access files and folders
import cv2 # perform image operations like read
import random
import keras
from sklearn.model_selection import train_test_split # Split dataset in to train and test
from keras.models import Model, Sequential
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Flatten, Reshape, Activation, AveragePooling2
from keras import regularizers
from keras.optimizers import Adam
from sklearn.cluster import KMeans # Kmeans clustering using sklearn
from sklearn.cluster import AgglomerativeClustering # Aggloerative clustering using sklearn
from sklearn.metrics.cluster import adjusted_rand_score, normalized_mutual_info_score # clustering evaluation

Step 2: Loading data from google colab

# load data from google drive


from google.colab import drive
drive.mount('/content/drive/')

Drive already mounted at /content/drive/; to attempt to forcibly remount, call drive.mount("/content/drive/", force_remount=True).

keyboard_arrow_down Covid Xray Image


image_path = '/content/drive/My Drive/FDP_DL/Kmeans/DATA/xray/COVID/03BF7561-A9BA-4C3C-B8A0-D3E585F73F3C.jpeg' # Change
img = plt.imread(image_path)
plt.imshow(img,cmap = 'gray') # cmap controls the colormap used to display the values.

<matplotlib.image.AxesImage at 0x7fbecda5e860>

keyboard_arrow_down Normal Xray Image


image_path = '/content/drive/My Drive/FDP_DL/Kmeans/DATA/xray/NORMAL/IM-0001-0001.jpeg' # Change the path
img = plt.imread(image_path)
plt.imshow(img,cmap = 'gray')

<matplotlib.image.AxesImage at 0x7fbecd52af60>

Step 3: Loading Images

DATADIR = "/content/drive/My Drive/FDP_DL/Kmeans/DATA/xray/" # Images are saved in this directory (Change the pat
print('Loading will take some time.....')

COUNT = 0
CATEGORIES = []
for i in os.listdir(DATADIR):
CATEGORIES.append(i)
COUNT += 1

print(CATEGORIES)

file_list = []
class_list = []

# The size of the images that network will use


IMG_SIZE = 256

training_data = []

def create_training_data():
for category in CATEGORIES :
path = os.path.join(DATADIR, category)
class_num = CATEGORIES.index(category)
for img in os.listdir(path):
try :
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
thresh, img_array = cv2.threshold(img_array,127,255,cv2.THRESH_BINARY)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data.append([new_array, class_num])
except Exception as e:
pass

create_training_data()

random.shuffle(training_data)

X = [] #features
y = [] #labels

for features, label in training_data:


X.append(features)
y.append(label)

X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)

print("classes load as X & y")

Loading will take some time.....


['NORMAL', 'COVID']
classes load as X & y

X[0].shape
(256, 256, 1)

plt.imshow(X[0].reshape(256,256),cmap = 'gray')

<matplotlib.image.AxesImage at 0x7fbecd4b36d8>

Step 4: Dataset split in Train & Test & Preprocessing

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2,random_state=42)

max_value = float(x_train.max())

x_train = np.array(x_train, dtype='float32')


x_test = np.array(x_test, dtype='float32')

x_train = x_train / np.max(x_train)


x_test = x_test / np.max(x_test)

x_train = x_train.reshape((len(x_train), 256, 256, 1))


x_test = x_test.reshape((len(x_test), 256, 256, 1))

# Sample image
plt.imshow(x_train[0].reshape(256,256),cmap = 'gray')

<matplotlib.image.AxesImage at 0x7fbecd4978d0>

keyboard_arrow_down Kmeans on COVID - 19 XRAY Image Dataset


Kmeans algorithm is one of the most popular clustering algorithm. It is an iterative algorithm that partition the dataset into K clusters using
cluster centers

n_clusters = 2

test_x = x_test.reshape(-1, 65536).astype('float32') # image reshaping into one-Dim vector


train_x = x_train.reshape(-1, 65536).astype('float32')

kmeans = KMeans(n_clusters=n_clusters, init='k-means++', n_init=10, tol=0.0001, max_iter=1000, random_state=42)


y_pred = kmeans.fit_predict(test_x)

print('Kmeans Clustering evaluation on x_test using Kmeans')


nmi = normalized_mutual_info_score(y_test,y_pred)
print('NMI:{0:f}'.format(nmi))
Kmeans Clustering evaluation on x_test using Kmeans
NMI:0.296415
ARI:0.411349

kmeans = KMeans(n_clusters=n_clusters, init='k-means++', n_init=10, tol=0.0001, max_iter=1000, random_state=42)


y_pred = kmeans.fit_predict(train_x)

print('Kmeans Clustering evaluation on x_train using Kmeans')


nmi = normalized_mutual_info_score(y_train,y_pred)
print('NMI:{0:f}'.format(nmi))

ari = adjusted_rand_score(y_train,y_pred)
print('ARI:{0:f}'.format(ari))

Kmeans Clustering evaluation on x_train using Kmeans


NMI:0.277472
ARI:0.406795

keyboard_arrow_down Agglomerative Clustering


is most common hierarchical clustering. The clustering start with one data point and assumes that each other data point is similar enough to
be clustered in 1 cluster.

clustering = AgglomerativeClustering(n_clusters=2, affinity='euclidean').fit_predict(test_x)

print('Agglomerative Clustering evaluation on x_test ')


nmi = normalized_mutual_info_score(y_test,clustering)
print('NMI:{0:f}'.format(nmi))

ari = adjusted_rand_score(y_test,clustering)
print('ARI:{0:f}'.format(ari))

Agglomerative Clustering evaluation on x_test


NMI:0.200112
ARI:0.301346

clustering = AgglomerativeClustering(n_clusters=2, affinity='euclidean').fit_predict(train_x)

print('Agglomerative Clustering evaluation on x_train')


nmi = normalized_mutual_info_score(y_train,clustering)
print('NMI:{0:f}'.format(nmi))

ari = adjusted_rand_score(y_train,clustering)
print('ARI:{0:f}'.format(ari))

Agglomerative Clustering evaluation on x_train


NMI:0.449438
ARI:0.509237

Step 5: Building Model for autoencoder

autoencoder = Sequential()

# Encoder Layers
autoencoder.add(Conv2D(128, (3, 3), activation='relu', padding='same', input_shape=x_train.shape[1:]))
autoencoder.add(MaxPooling2D((2, 2))) # Downsamples the input representation

autoencoder.add(Conv2D(64, (3, 3), activation='relu', padding='same'))


autoencoder.add(MaxPooling2D((2, 2)))

autoencoder.add(Conv2D(32, (3, 3), activation='relu', padding='same'))


autoencoder.add(MaxPooling2D((2, 2)))

autoencoder.add(Conv2D(16, (3, 3), activation='relu', padding='same'))


autoencoder.add(MaxPooling2D((2, 2)))

autoencoder.add(Flatten(name="encoder_layer")) # Naming the layer to extract the features

# Decoder Layers
autoencoder.add(Reshape((16,16,16)))
autoencoder.add(Conv2D(16, (3, 3), activation='relu', padding='same'))
autoencoder.add(UpSampling2D((2, 2))) # simply doubles the dimensions of the input & upsamples image to a

autoencoder.add(Conv2D(32, (3, 3), activation='relu', padding='same'))


autoencoder.add(UpSampling2D((2, 2)))

autoencoder.add(Conv2D(64, (3, 3), activation='relu', padding='same'))


autoencoder.add(UpSampling2D((2, 2)))

autoencoder.add(Conv2D(128, (3, 3), activation='relu', padding='same'))


autoencoder.add(UpSampling2D((2, 2)))

autoencoder.add(Conv2D(1, (3, 3), activation='sigmoid', padding='same')) # values are in the range [0,1]

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 256, 256, 128) 1280
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 128, 128, 128) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 128, 128, 64) 73792
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 64, 64, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 64, 64, 32) 18464
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 32, 32, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 32, 32, 16) 4624
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 16, 16, 16) 0
_________________________________________________________________
encoder_layer (Flatten) (None, 4096) 0
_________________________________________________________________
reshape (Reshape) (None, 16, 16, 16) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 16, 16, 16) 2320
_________________________________________________________________
up_sampling2d (UpSampling2D) (None, 32, 32, 16) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 32, 32, 32) 4640
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 64, 64, 32) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 64, 64, 64) 18496
_________________________________________________________________
up_sampling2d_2 (UpSampling2 (None, 128, 128, 64) 0
_________________________________________________________________
conv2d_7 (Conv2D) (None, 128, 128, 128) 73856
_________________________________________________________________
up_sampling2d_3 (UpSampling2 (None, 256, 256, 128) 0
_________________________________________________________________
conv2d_8 (Conv2D) (None, 256, 256, 1) 1153
=================================================================
Total params: 198,625
Trainable params: 198,625
Non-trainable params: 0
_________________________________________________________________

encoder = Model(inputs=autoencoder.input, outputs=autoencoder.get_layer('encoder_layer').output)


encoder.summary()

Model: "functional_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_input (InputLayer) [(None, 256, 256, 1)] 0
_________________________________________________________________
conv2d (Conv2D) (None, 256, 256, 128) 1280
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 128, 128, 128) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 128, 128, 64) 73792
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 64, 64, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 64, 64, 32) 18464
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 32, 32, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 32, 32, 16) 4624
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 16, 16, 16) 0
_________________________________________________________________
encoder_layer (Flatten) (None, 4096) 0
=================================================================
Total params: 98,160
Trainable params: 98,160
Non-trainable params: 0
_________________________________________________________________

Autoencoder Model
Step 6: Train the model

autoencoder.compile(optimizer='adam', loss='binary_crossentropy',metrics=['acc'])
history = autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=32,
validation_data=(x_test, x_test))

Epoch 1/100
18/18 [==============================] - 2s 114ms/step - loss: 0.4951 - acc: 0.7183 - val_loss: 0.2540 - val_acc: 0.9154
Epoch 2/100
18/18 [==============================] - 2s 87ms/step - loss: 0.2230 - acc: 0.9188 - val_loss: 0.1930 - val_acc: 0.9226
Epoch 3/100
18/18 [==============================] - 2s 88ms/step - loss: 0.1775 - acc: 0.9289 - val_loss: 0.1670 - val_acc: 0.9320
Epoch 4/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1539 - acc: 0.9363 - val_loss: 0.1505 - val_acc: 0.9376
Epoch 5/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1406 - acc: 0.9414 - val_loss: 0.1408 - val_acc: 0.9411
Epoch 6/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1331 - acc: 0.9442 - val_loss: 0.1343 - val_acc: 0.9439
Epoch 7/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1277 - acc: 0.9463 - val_loss: 0.1314 - val_acc: 0.9445
Epoch 8/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1227 - acc: 0.9481 - val_loss: 0.1257 - val_acc: 0.9470
Epoch 9/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1180 - acc: 0.9500 - val_loss: 0.1219 - val_acc: 0.9486
Epoch 10/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1145 - acc: 0.9514 - val_loss: 0.1198 - val_acc: 0.9494
Epoch 11/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1116 - acc: 0.9526 - val_loss: 0.1166 - val_acc: 0.9510
Epoch 12/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1116 - acc: 0.9524 - val_loss: 0.1160 - val_acc: 0.9507
Epoch 13/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1098 - acc: 0.9531 - val_loss: 0.1137 - val_acc: 0.9518
Epoch 14/100
18/18 [==============================] - 2s 85ms/step - loss: 0.1072 - acc: 0.9542 - val_loss: 0.1109 - val_acc: 0.9531
Epoch 15/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1057 - acc: 0.9549 - val_loss: 0.1102 - val_acc: 0.9532
Epoch 16/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1031 - acc: 0.9560 - val_loss: 0.1084 - val_acc: 0.9540
Epoch 17/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1023 - acc: 0.9563 - val_loss: 0.1076 - val_acc: 0.9543
Epoch 18/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1023 - acc: 0.9563 - val_loss: 0.1067 - val_acc: 0.9548
Epoch 19/100
18/18 [==============================] - 2s 87ms/step - loss: 0.1003 - acc: 0.9572 - val_loss: 0.1055 - val_acc: 0.9553
Epoch 20/100
18/18 [==============================] - 2s 87ms/step - loss: 0.0993 - acc: 0.9576 - val_loss: 0.1112 - val_acc: 0.9524
Epoch 21/100
18/18 [==============================] - 2s 88ms/step - loss: 0.1024 - acc: 0.9561 - val_loss: 0.1038 - val_acc: 0.9560
Epoch 22/100
18/18 [==============================] - 2s 87ms/step - loss: 0.0986 - acc: 0.9581 - val_loss: 0.1029 - val_acc: 0.9563
Epoch 23/100
18/18 [==============================] - 2s 87ms/step - loss: 0.0964 - acc: 0.9590 - val_loss: 0.1018 - val_acc: 0.9570
Epoch 24/100
18/18 [==============================] - 2s 87ms/step - loss: 0.0955 - acc: 0.9594 - val_loss: 0.1020 - val_acc: 0.9565
Epoch 25/100
18/18 [==============================] - 2s 87ms/step - loss: 0.0962 - acc: 0.9590 - val_loss: 0.1034 - val_acc: 0.9558
Epoch 26/100
18/18 [==============================] - 2s 87ms/step - loss: 0.0944 - acc: 0.9598 - val_loss: 0.0999 - val_acc: 0.9576
Epoch 27/100
18/18 [==============================] - 2s 87ms/step - loss: 0.0930 - acc: 0.9605 - val_loss: 0.0993 - val_acc: 0.9579
Epoch 28/100
18/18 [==============================] - 2s 86ms/step - loss: 0.0925 - acc: 0.9607 - val_loss: 0.0986 - val_acc: 0.9581
Epoch 29/100
18/18 [==============================] - 2s 87ms/step - loss: 0.0927 - acc: 0.9605 - val_loss: 0.0980 - val_acc: 0.9584
h 30/ 00

# score = autoencoder.evaluate(x_val, x_val, verbose = 0)


# print('Test loss:', score[0])
# print('Test accuracy:', score[1])

plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

# Save model
# save_dir_path = '/content/drive/My Drive/FDP_DL/autoencoder/save/'
# encoder.save(save_dir_path+"e_model_covid.hdf5")
# autoencoder.save(save_dir_path+"ae_model_covid.hdf5")
# print("Saved model to disk")

# Load model
# from keras.models import load_model
# save_dir_path = '/content/drive/My Drive/FDP_DL/autoencoder/save/'
# encoder = load_model(save_dir_path+"e_model_covid.hdf5")
# autoencoder = load_model(save_dir_path+"ae_model_covid.hdf5")

Step 7: Display reconstructed images

# Display some images & their reconstructed images

print('Original Images -> Extracted Features -> Reconstructed Images')


num_images = 10
np.random.seed(42)
random_test_images = np.random.randint(x_test.shape[0], size=num_images

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

plt.figure(figsize=(18, 4))

for i, image_idx in enumerate(random_test_images):


# plot original image
ax = plt.subplot(3, num_images, i + 1)
plt.imshow(x_test[image_idx].reshape(256,256))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# plot encoded image


ax = plt.subplot(3, num_images, num_images + i + 1)
plt.imshow(encoded_imgs[image_idx].reshape(64,64))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# plot reconstructed image


ax = plt.subplot(3, num_images, 2*num_images + i + 1)
plt.imshow(decoded_imgs[image_idx].reshape(256, 256))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

lt h ()
Original Images -> Extracted Features -> Reconstructed Images

keyboard_arrow_down Kmeans on extracted features from autoencoder


n clusters = 2
Experiment No. 6 Write a Program to implement CNN using keras

Step 1 − Import the modules

Let us import the necessary modules

Keras is a high-level deep learning API written in Python, designed to simplify building and training neural networks. It abstracts much of the
complex code involved in deep learning and allows you to build and train models with minimal code. Keras integrates seamlessly with
TensorFlow (its backend), making it possible to build powerful neural networks with an intuitive, user-friendly interface.

from keras.datasets import mnist


from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from keras.utils import to_categorical

mnist: This module provides the MNIST dataset, which contains 28x28 pixel grayscale images of handwritten digits. Sequential: This is a Keras
class for building a neural network as a linear stack of layer.

Conv2D and MaxPooling2D: These are convolutional and max-pooling layers, respectively. Convolutional layers are used to detect spatial
patterns in images, and max-pooling layers reduce the spatial dimensions, retaining only the most important information.

Flatten: Flattens a 2D matrix into a 1D vector, preparing it for fully connected (Dense) layers.

Dense: This fully connected layer helps classify the image based on the features learned by previous layers.

to_categorical: Converts integer labels into one-hot encoded format, making it compatible with the model's softmax output.

Step 2 − Load data

Let us import the mnist dataset.

#loading data
(X_train,y_train),(X_test,y_test)=mnist.load_data()

Step 3 − Process the data

#reshaping data
X_train = X_train.reshape((X_train.shape[0], 28, 28, 1))
X_test = X_test.reshape((X_test.shape[0],28, 28, 1))
#checking the shape after reshaping
print(X_train.shape)
print(X_test.shape)
#normalizing the pixel values
X_train=X_train/255
X_test=X_test/255

(60000, 28, 28, 1)


(10000, 28, 28, 1)

Reshape: Convolutional networks expect a 3D input (height, width, channels). The images are reshaped from (28, 28) to (28, 28, 1) since they’re
grayscale (1 channel).

Normalization: Pixel values range from 0 to 255. Dividing by 255 scales them to a range of 0–1, which helps the model train faster and converge
more easily.

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

One-Hot Encoding: to_categorical converts labels (integers from 0 to 9) into a one-hot encoded format. For example, label "3" becomes [0, 0, 0,
1, 0, 0, 0, 0, 0, 0]. This matches the softmax output layer, which will output probabilities for each class.

Start coding or generate with AI.

Step 4 − Create the model

Let us create tha actual model.


#defining model
model=Sequential()

Define Model Structure: The Sequential model allows us to stack layers linearly, from input to output, simplifying the model-building process.

#adding convolution layer


model.add(Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)))

/usr/local/lib/python3.10/dist-packages/keras/src/layers/convolutional/base_conv.py:107: UserWarning: Do not pass an `input_shape`/


super().__init__(activity_regularizer=activity_regularizer, **kwargs)

Convolutional Layer (Conv2D): This layer has 32 filters, each 3x3 in size, and ReLU as the activation function.

ReLU Activation: ReLU (Rectified Linear Unit) turns negative values to zero, which helps with faster convergence by making the model non-
linear.

Input Shape: We specify (28, 28, 1) as the shape for the first layer only.

#adding pooling layer


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

Max Pooling Layer (MaxPooling2D): Max-pooling downsamples the feature map by taking the maximum value in each 2x2 region. This reduces
the spatial dimensions (width and height), which helps reduce computation and prevents overfitting by retaining only the most relevant
features.

#adding fully connected layer


model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))

Additional Conv2D and MaxPooling Layers: More convolutional layers with 64 filters let the model learn more complex features. As we go
deeper into the network, these layers focus on higher-level features. The second MaxPooling layer again reduces the spatial dimensions,
preparing it for the fully connected layers.

#Flatten the Output and Add Dense Layers

model.add(Flatten())

Flatten Layer: Flattening turns the 3D output from the convolutional layers into a 1D vector. This step prepares the data for the Dense layers,
which require a 1D input.

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

Dense Layer: A fully connected layer with 64 neurons and ReLU activation. This layer combines all the features learned by previous layers to
classify the image. It is a standard hidden layer that helps the model learn and represent complex patterns in the data.

model.add(Dense(10, activation='softmax'))

Output Layer: The final Dense layer has 10 neurons (one for each digit class, 0–9) and uses softmax activation. Softmax converts the output
into probabilities, so the highest probability class can be taken as the model’s prediction.

Step 5 − Compile the model

Let us compile the model using selected loss function, optimizer and metrics.

model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Start coding or generate with AI.

Optimizer: adam is an efficient optimizer that combines the advantages of two other popular optimizers, AdaGrad and RMSProp, and is widely
used in deep learning.

Loss Function: categorical_crossentropy measures the difference between the predicted probabilities and the actual one-hot encoded labels.

Metrics: We track accuracy to measure how often the model predicts the correct label.

Step 6 Train The Model

#fitting the model

model.fit(X_train, y_train, epochs=5, batch_size=64, validation_data=(X_test, y_test))

Epoch 1/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 53s 54ms/step - accuracy: 0.8688 - loss: 0.4289 - val_accuracy: 0.9829 - val_loss: 0.0
Epoch 2/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 52s 55ms/step - accuracy: 0.9829 - loss: 0.0551 - val_accuracy: 0.9895 - val_loss: 0.0
Epoch 3/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 82s 55ms/step - accuracy: 0.9893 - loss: 0.0338 - val_accuracy: 0.9883 - val_loss: 0.0
Epoch 4/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 82s 56ms/step - accuracy: 0.9918 - loss: 0.0267 - val_accuracy: 0.9868 - val_loss: 0.0
Epoch 5/5
938/938 ━━━━━━━━━━━━━━━━━━━━ 81s 55ms/step - accuracy: 0.9936 - loss: 0.0213 - val_accuracy: 0.9889 - val_loss: 0.0
<keras.src.callbacks.history.History at 0x7ee3e073a770>

Fit the Model: We train the model using the training data. Epochs: Each epoch represents a complete pass over the entire training dataset. Here,
we train for 10 epochs. Batch Size: The number of samples processed before updating the model’s parameters. We set it to 64 here. Validation
Data: After each epoch, the model evaluates its performance on the test data (not used in training). This helps monitor overfitting and how well
the model generalizes to new data.

Step 7 − Evaluate the model

score = model.evaluate(X_test, y_test, verbose = 0)

print('Test loss:', score[0])


print('Test accuracy:', score[1])

Test loss: 0.03343029320240021


Test accuracy: 0.9889000058174133
Experiment No.7 Write a Program to implement LENET using keras

Step 1 − Import the modules

Let us import the necessary modules

Keras is a high-level deep learning API written in Python, designed to simplify building and
training neural networks. It abstracts much of the complex code involved in deep learning and
allows you to build and train models with minimal code. Keras integrates seamlessly with
TensorFlow (its backend), making it possible to build powerful neural networks with an intuitive,
user-friendly interface.

from keras.datasets import mnist


from keras.models import Sequential
from keras.layers import Conv2D, AveragePooling2D, Flatten, Dense
from keras.utils import to_categorical
import numpy as np

mnist: This module provides the MNIST dataset, which contains 28x28 pixel grayscale images of
handwritten digits. Sequential: This is a Keras class for building a neural network as a linear
stack of layer.

Conv2D and MaxPooling2D: These are convolutional and max-pooling layers, respectively.
Convolutional layers are used to detect spatial patterns in images, and max-pooling layers
reduce the spatial dimensions, retaining only the most important information.

Flatten: Flattens a 2D matrix into a 1D vector, preparing it for fully connected (Dense) layers.

Dense: This fully connected layer helps classify the image based on the features learned by
previous layers.

to_categorical: Converts integer labels into one-hot encoded format, making it compatible with
the model's softmax output.

Step 2 − Load data

Let us import the mnist dataset.

#loading data
(X_train,y_train),(X_test,y_test)=mnist.load_data()

Step 3 − Process the data


#reshaping data
# Reshape the data to include a single channel (grayscale)
X_train = X_train.reshape((X_train.shape[0], 28, 28, 1))
X_test = X_test.reshape((X_test.shape[0],28, 28, 1))

# Normalize the data (pixel values 0-255 to range 0-1)


X_train = X_train.astype('float32') / 255.0 # Normalize pixel values to [0, 1
X_test = X_test.astype('float32') / 255.0

# Pad the images to 32x32 (original MNIST images are 28x28)


X_train = np.pad(X_train, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant') # Pad
X_test = np.pad(X_test, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')

#checking the shape after reshaping


print(X_train.shape)
print(X_test.shape)

(60000, 32, 32, 1)


(10000, 32, 32, 1)

np.expand_dims: Adds a channel dimension for grayscale images (from (28, 28) to (28, 28, 1)).

np.pad: Pads the images to 32x32 to match LeNet-5 input size.

Reshape: Convolutional networks expect a 3D input (height, width, channels). The images are
reshaped from (28, 28) to (28, 28, 1) since they’re grayscale (1 channel).

Normalization: Pixel values range from 0 to 255. Dividing by 255 scales them to a range of 0–1,
which helps the model train faster and converge more easily.

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

One-Hot Encoding: to_categorical converts labels (integers from 0 to 9) into a one-hot encoded
format. For example, label "3" becomes [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]. This matches the softmax
output layer, which will output probabilities for each class.

Step 4 − Create the model

Let us create tha actual model.

# Define the LeNet-5 model


model = Sequential()

# Layer 1: Convolutional Layer


model.add(Conv2D(filters=6, kernel_size=(5, 5), activation='tanh', input_shape

# Layer 2: Average Pooling Layer


model.add(AveragePooling2D(pool_size=(2, 2), strides=2))

# Layer 3: Convolutional Layer


model.add(Conv2D(filters=16, kernel_size=(5, 5), activation='tanh'))

# Layer 4: Average Pooling Layer


model.add(AveragePooling2D(pool_size=(2, 2), strides=2))

# Layer 5: Flatten Layer


model.add(Flatten())

# Layer 6: Fully Connected Layer


model.add(Dense(units=120, activation='tanh'))

# Layer 7: Fully Connected Layer


model.add(Dense(units=84, activation='tanh'))

# Layer 8: Output Layer


model.add(Dense(units=10, activation='softmax'))

Define Model Structure: The Sequential model allows us to stack layers linearly, from input to
output, simplifying the model-building process.

Convolutional Layer (Conv2D): This layer has 6 filters, each 5x5 in size, and tanh as the
activation function.

Input Shape: We specify (32, 32, 1) as the shape for the first layer only.

Flatten Layer: Flattening turns the 3D output from the convolutional layers into a 1D vector. This
step prepares the data for the Dense layers, which require a 1D input.

Dense Layer: A fully connected layer with 120 neurons and tanh activation. This layer combines
all the features learned by previous layers to classify the image. It is a standard hidden layer that
helps the model learn and represent complex patterns in the data.

Output Layer: The final Dense layer has 10 neurons (one for each digit class, 0–9) and uses
softmax activation. Softmax converts the output into probabilities, so the highest probability
class can be taken as the model’s prediction.

Step 5 − Compile the model


Let us compile the model using selected loss function, optimizer and metrics.

# Compile the model


model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc

Start coding or generate with AI.

Optimizer: adam is an efficient optimizer that combines the advantages of two other popular
optimizers, AdaGrad and RMSProp, and is widely used in deep learning.

Loss Function: categorical_crossentropy measures the difference between the predicted


probabilities and the actual one-hot encoded labels.

Metrics: We track accuracy to measure how often the model predicts the correct label.

Step 6 Train The Model

#fitting the model

model.fit(X_train, y_train, epochs=5, batch_size=128, validation_data=(X_test,

Epoch 1/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 36s 72ms/step - accuracy: 0.8298 - loss
Epoch 2/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 35s 75ms/step - accuracy: 0.9583 - loss
Epoch 3/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 43s 80ms/step - accuracy: 0.9745 - loss
Epoch 4/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 40s 78ms/step - accuracy: 0.9818 - loss
Epoch 5/5
469/469 ━━━━━━━━━━━━━━━━━━━━ 41s 79ms/step - accuracy: 0.9857 - loss
<keras.src.callbacks.history.History at 0x7d74ef76e980>

Fit the Model: We train the model using the training data. Epochs: Each epoch represents a
complete pass over the entire training dataset. Here, we train for 10 epochs. Batch Size: The
number of samples processed before updating the model’s parameters. We set it to 64 here.
Validation Data: After each epoch, the model evaluates its performance on the test data (not
used in training). This helps monitor overfitting and how well the model generalizes to new data.

Step 7 − Evaluate the model

score = model.evaluate(X_test, y_test, verbose = 0


print('Test loss:' score[0])
313/313 ━━━━━━━━━━━━━━━━━━━━ 3s 10ms/step - accuracy: 0.9809 - loss:
Test loss: 0.0511913038790226
Test accuracy: 0.9830999970436096

verbose: Controls the level of detail shown during training.

verbose=2: Displays one line per epoch summarizing training and validation metrics (compact
and readable). Other options:

verbose=0: No output during training.

verbose=1: Detailed output with a progress bar for every batch.

# Display model summary


model.summary()

Model: "sequential_7"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━
┃ Layer (type) ┃ Output Shape ┃ Par
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━
│ conv2d_7 (Conv2D) │ (None, 28, 28, 6) │ 15
├──────────────────────────────────────┼─────────────────────────────┼───────────────
│ average_pooling2d_6 │ (None, 14, 14, 6) │
│ (AveragePooling2D) │ │
├──────────────────────────────────────┼─────────────────────────────┼───────────────
│ conv2d_8 (Conv2D) │ (None, 10, 10, 16) │ 2,41
├──────────────────────────────────────┼─────────────────────────────┼───────────────
│ average_pooling2d_7 │ (None, 5, 5, 16) │
│ (AveragePooling2D) │ │
├──────────────────────────────────────┼─────────────────────────────┼───────────────
│ flatten_3 (Flatten) │ (None, 400) │
├──────────────────────────────────────┼─────────────────────────────┼───────────────
│ dense_9 (Dense) │ (None, 120) │ 48,12
├──────────────────────────────────────┼─────────────────────────────┼───────────────
│ dense_10 (Dense) │ (None, 84) │ 10,16
├──────────────────────────────────────┼─────────────────────────────┼───────────────
│ dense_11 (Dense) │ (None, 10) │ 85
└──────────────────────────────────────┴─────────────────────────────┴───────────────

Total params: 185,120 (723.13 KB)


Trainable params: 61,706 (241.04 KB)
Non-trainable params: 0 (0.00 B)
Optimizer params: 123 414 (482 09 KB)
Experiment No. 8 Write a Program to implement Recurrent Neural Network

import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, SimpleRNN

#define input sequence


input_seq=np.array([[0.1],[0.2],[0.3],[0.4],[0.5],[0.6]])

#define target sequence


target_seq=np.array([[0.2],[0.3],[0.4],[0.5],[0.6],[0.7]])

# define the model architecture


model = Sequential()
model.add(SimpleRNN(units=1, input_shape=(1,1)))
model.add(Dense(units=1, activation='linear'))

# compile the model


model.compile(loss='mean_squared_error', optimizer='adam')
# train the model
model.fit(input_seq, target_seq, epochs=100)

Epoch 1/100
1/1 [==============================] - 1s 1s/step - loss: 0.1949
Epoch 2/100
1/1 [==============================] - 0s 15ms/step - loss: 0.1935
Epoch 3/100
1/1 [==============================] - 0s 13ms/step - loss: 0.1920
Epoch 4/100
1/1 [==============================] - 0s 14ms/step - loss: 0.1906
Epoch 5/100
1/1 [==============================] - 0s 16ms/step - loss: 0.1891
Epoch 6/100
1/1 [==============================] - 0s 12ms/step - loss: 0.1877
Epoch 7/100
1/1 [==============================] - 0s 12ms/step - loss: 0.1862
Epoch 8/100
1/1 [==============================] - 0s 15ms/step - loss: 0.1848
Epoch 9/100
1/1 [==============================] - 0s 16ms/step - loss: 0.1833
Epoch 10/100
1/1 [==============================] - 0s 16ms/step - loss: 0.1819
Epoch 11/100
1/1 [==============================] - 0s 13ms/step - loss: 0.1805
Epoch 12/100
1/1 [==============================] - 0s 16ms/step - loss: 0.1791
Epoch 13/100
1/1 [==============================] - 0s 21ms/step - loss: 0.1776
Epoch 14/100
1/1 [==============================] - 0s 24ms/step - loss: 0.1762
Epoch 15/100
1/1 [==============================] - 0s 25ms/step - loss: 0.1748
Epoch 16/100
1/1 [==============================] - 0s 27ms/step - loss: 0.1734
Epoch 17/100
1/1 [==============================] - 0s 22ms/step - loss: 0.1720
Epoch 18/100
1/1 [==============================] - 0s 20ms/step - loss: 0.1706
Epoch 19/100
1/1 [==============================] - 0s 14ms/step - loss: 0.1693
Epoch 20/100
1/1 [==============================] - 0s 23ms/step - loss: 0.1679
Epoch 21/100
1/1 [==============================] - 0s 20ms/step - loss: 0.1665
Epoch 22/100
1/1 [==============================] - 0s 22ms/step - loss: 0.1651
Epoch 23/100
1/1 [==============================] - 0s 30ms/step - loss: 0.1638
Epoch 24/100
1/1 [==============================] - 0s 26ms/step - loss: 0.1624
Epoch 25/100
1/1 [==============================] - 0s 29ms/step - loss: 0.1611
Epoch 26/100
1/1 [==============================] - 0s 29ms/step - loss: 0.1597
Epoch 27/100
1/1 [==============================] - 0s 27ms/step - loss: 0.1584
Epoch 28/100
1/1 [==============================] - 0s 25ms/step - loss: 0.1570
Epoch 29/100
1/1 [==============================] - 0s 30ms/step - loss: 0.1557

# Make predictions using the model


predictions = model.predict(input_seq)
print(predictions)

1/1 [==============================] - 0s 426ms/step


[[0.16519284]
[0.18308991]
[0.20081952]
[0.21835133]
[0.2356565 ]
[0.25270775]]
Experiment No. 9 Case Study: Industry
Automation with Reinforcement Learning
(RL)
Introduction
Industry automation has seen a significant transformation through the adoption of Artificial
Intelligence (AI), particularly Reinforcement Learning (RL). Unlike traditional automation,
which relies on predefined rules and static logic, RL allows systems to learn from interactions
and adapt to dynamic environments. This case study explores how RL has been applied in a
large-scale manufacturing facility to optimize production scheduling, resource allocation, and
predictive maintenance, resulting in substantial improvements in efficiency, cost reduction,
and operational flexibility.

Problem Statement
The manufacturing facility faced several critical challenges in achieving optimal operational
performance:

Key Issues:

1. Dynamic and Unpredictable Demand: Fluctuations in customer demand created


challenges in maintaining an optimal production schedule.
2. Inefficient Resource Utilization: Inefficient use of raw materials and energy led to
increased costs.
3. Machine Downtime and Maintenance: Unplanned machine failures disrupted
production and caused delays.
4. Complex Production Scheduling: Coordinating multiple production lines and
managing interdependencies between machines was highly complex.
5. Rigid Control Systems: Traditional control systems were inflexible and couldn’t
adapt quickly to changes in the environment.

Solution: Reinforcement Learning-Based Automation


The facility implemented a Reinforcement Learning framework to address these challenges.
The RL system was designed to dynamically manage production schedules, allocate
resources efficiently, and predict maintenance needs.

Reinforcement Learning Framework:


• Agent: A Deep Q-Network (DQN) agent, capable of learning from interactions and
improving decision-making over time.
• Environment: The manufacturing plant, including machines, production lines, and
inventory.
• State Space: The state of the environment, defined by machine status, production
orders, resource availability, and maintenance schedules.
• Actions: Scheduling tasks, allocating resources, adjusting machine settings, and
initiating maintenance.
• Reward Function: The reward was based on metrics such as throughput, resource
utilization, downtime reduction, and energy efficiency.

Implementation Process
1. Data Collection and Preparation

• Historical Data: Data from production logs, machine sensors, and maintenance
records were collected.
• Real-Time Data: IoT devices were installed to provide real-time data on machine
performance and environmental conditions.
• Pre-processing: The data was cleaned, normalized, and structured for training the RL
model.

2. Simulation Environment

• A digital twin of the manufacturing facility was created to simulate various scenarios
and train the RL agent.
• The digital twin replicated the physical environment, including machine dynamics,
resource flows, and production constraints.

3. Training the RL Agent

• The RL agent was trained using Deep Q-Learning (DQN) and Proximal Policy
Optimization (PPO) algorithms.
• Training involved running simulations where the agent explored different strategies
and learned from trial and error.
• A reward shaping mechanism was used to guide the agent towards optimal
behaviors.

4. Deployment and Integration

• The trained RL model was integrated with the plant’s Manufacturing Execution
System (MES) and Supervisory Control and Data Acquisition (SCADA) system.
• Custom APIs were developed to allow real-time communication between the RL
agent and the plant’s control systems.

5. Monitoring and Continuous Learning


• The RL system was monitored continuously, and feedback loops were established to
update the model with new data.
• A retraining pipeline was set up to periodically retrain the model using recent data to
ensure adaptability.

Results and Impact


1. Production Optimization

• Throughput Increase: The plant’s output increased by 18%, as the RL agent


optimized task sequencing and resource allocation.
• Reduced Bottlenecks: The agent identified and eliminated bottlenecks, leading to
smoother production flows.

2. Predictive Maintenance

• Downtime Reduction: Downtime decreased by 40%, as the RL agent proactively


scheduled maintenance based on predicted failures.
• Maintenance Costs: Predictive maintenance led to a 20% reduction in maintenance
costs.

3. Resource and Energy Efficiency

• Energy Savings: Energy consumption was reduced by 15%, as the agent optimized
the scheduling of energy-intensive processes during off-peak hours.
• Material Utilization: Waste was minimized by optimizing the use of raw materials.

4. Flexibility and Adaptability

• The RL system demonstrated the ability to adapt to changes in demand and machine
availability, ensuring consistent performance even in dynamic conditions.

Key Metrics:
Metric Before RL After RL Improvement
Production Throughput 800 units/day 950 units/day 18.75% increase
Downtime 12% of total time 7% of total time 42% reduction
Maintenance Costs $500,000/year $400,000/year 20% savings
Energy Consumption 1.2 MWh/day 1.0 MWh/day 16.7% reduction
Resource Utilization 70% 85% 15% increase
On-time Delivery Rate 78% 92% 14% improvement
Challenges and Solutions
Experiment No. 9 Case Study: Industry Automation with Reinforcement
Learning (RL)

1. Data Quality and Availability

• Challenge: Incomplete or noisy data affected model training.


• Solution: Data preprocessing techniques were applied to clean and standardize the
data. IoT sensors were deployed to enhance real-time data collection.

2. Computational Complexity

• Challenge: Training the RL model was computationally intensive.


• Solution: Distributed computing and cloud-based resources were used to accelerate
training.

3. Integration with Legacy Systems

• Challenge: The plant’s existing systems were not designed for AI integration.
• Solution: Custom APIs and middleware were developed to bridge the gap between
the RL agent and legacy systems.

Future Directions and Recommendations


1. Scaling Across Facilities:
The company plans to roll out the RL system across multiple plants, leveraging the
scalability of the solution.
2. Multi-Agent Systems:
Multi-Agent Reinforcement Learning (MARL) can be explored to optimize
coordination between different departments and production lines.
3. IoT Integration:
Further integration with IoT devices can provide richer data, enhancing the RL
agent’s decision-making capabilities.
4. Continuous Improvement:
A feedback mechanism should be maintained for continuous model updates and
improvements as new data becomes available.

Conclusion
This case study illustrates how Reinforcement Learning can revolutionize industry
automation by enabling adaptive, data-driven decision-making. The RL-based automation
system not only improved production efficiency but also reduced costs and enhanced
resource utilization. As industries continue to embrace AI and RL technologies, the potential
for smarter, more responsive automation systems will grow, driving innovation and
competitiveness across sectors.

You might also like