DRL Practical file
DRL Practical file
DRL Practical file
JABALPUR
LAB MANUAL
(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
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))
target = 0
print("Prediction:",prediction, "Error:",mse)
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 softmax(x):
exp_x = np.exp(x)
return exp_x / np.sum(exp_x)
x = np.array([-1, 0, 1])
# Sigmoid
print(sigmoid(x))
# 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))
def sigmoid(sop):
return 1.0/(1+numpy.exp(-1*sop))
def sigmoid_sop_deriv(sop):
return sigmoid(sop)*(1.0-sigmoid(sop))
def sop_w_deriv(x):
return x
x1=0.1
x2=0.4
target = 0.7
learning_rate = 0.01
w1=numpy.random.rand()
w2=numpy.random.rand()
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
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")
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.
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.
# 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.
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
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.
print(model_history.history.keys())
dict_keys(['accuracy', 'loss'])
array([[1552, 43],
[ 276, 129]])
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.
Drive already mounted at /content/drive/; to attempt to forcibly remount, call drive.mount("/content/drive/", force_remount=True).
<matplotlib.image.AxesImage at 0x7fbecda5e860>
<matplotlib.image.AxesImage at 0x7fbecd52af60>
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 = []
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
X[0].shape
(256, 256, 1)
plt.imshow(X[0].reshape(256,256),cmap = 'gray')
<matplotlib.image.AxesImage at 0x7fbecd4b36d8>
max_value = float(x_train.max())
# Sample image
plt.imshow(x_train[0].reshape(256,256),cmap = 'gray')
<matplotlib.image.AxesImage at 0x7fbecd4978d0>
n_clusters = 2
ari = adjusted_rand_score(y_train,y_pred)
print('ARI:{0:f}'.format(ari))
ari = adjusted_rand_score(y_test,clustering)
print('ARI:{0:f}'.format(ari))
ari = adjusted_rand_score(y_train,clustering)
print('ARI:{0:f}'.format(ari))
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
# 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(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
_________________________________________________________________
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
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")
encoded_imgs = encoder.predict(x_test)
decoded_imgs = autoencoder.predict(x_test)
plt.figure(figsize=(18, 4))
lt h ()
Original Images -> Extracted Features -> Reconstructed Images
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.
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.
#loading data
(X_train,y_train),(X_test,y_test)=mnist.load_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
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.
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 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.
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.
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.
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.
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.
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.
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.
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.
#loading data
(X_train,y_train),(X_test,y_test)=mnist.load_data()
np.expand_dims: Adds a channel dimension for grayscale images (from (28, 28) to (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.
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.
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.
Metrics: We track accuracy to measure how often the model predicts the correct label.
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.
verbose=2: Displays one line per epoch summarizing training and validation metrics (compact
and readable). Other options:
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
└──────────────────────────────────────┴─────────────────────────────┴───────────────
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, SimpleRNN
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
Problem Statement
The manufacturing facility faced several critical challenges in achieving optimal operational
performance:
Key Issues:
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.
• 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.
• 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.
2. Predictive Maintenance
• 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.
• 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)
2. Computational Complexity
• 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.
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.