0% found this document useful (0 votes)
15 views11 pages

S. NO. Title of The Experiments Page No

Dl lab program

Uploaded by

kishoranbu14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

S. NO. Title of The Experiments Page No

Dl lab program

Uploaded by

kishoranbu14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

TABLE OF CONTENTS

PAGE NO.
S. NO. TITLE OF THE EXPERIMENTS

1
Solving XOR problem using DNN 1

2
Character recognition using CNN 4

3
Face recognition using CNN 10

4
Language modeling using RNN 17

5
Sentiment analysis using LSTM 22

6 Parts of speech tagging using Sequence to Sequence architecture 28

7
Machine Translation using Encoder-Decoder model 37

8 Image augmentation using GANs 43

9 Mini-project on real world applications 50

i
Ex. No: 1
SOLVING XOR PROBLEM USING DNN
Date :

Aim:
To write a program to Solving XOR problem using
DNN.

Algorithm:
1. Start the program.
2. Get the training data
3. To create the Number of Input units , Number of Hidden
units, request.4.To send frames to server from the client
side.
4. If your frames reach the server it will send ACK
signal to client otherwise it will send NACK
signal to client.
5. Stop the program

Program:
import numpy as np # For matrix math
import matplotlib.pyplot as plt # For plo ng

# Training data and labels


X = np.array([
[0, 1],
[1, 0],
[1, 1],
[0, 0]
])
y = np.array([
[1],
[1],
[0],
[0]
])

# Parameters
num_i_units = 2 # Number of Input units
num_h_units = 2 # Number of Hidden units
num_o_units = 1 # Number of Output units

1
learning_rate = 0.01 # Learning rate
reg_param = 0 # Regulariza on parameter
max_iter = 5000 # Maximum itera ons
m = len(X) # Number of training examples

# Ini alize weights and biases


np.random.seed(1)
W1 = np.random.normal(0, 1, (num_h_units, num_i_units)) # 2x2
W2 = np.random.normal(0, 1, (num_o_units, num_h_units)) # 1x2
B1 = np.random.random((num_h_units, 1)) # 2x1
B2 = np.random.random((num_o_units, 1)) # 1x1

# Ac va on func on
def sigmoid(z, derv=False):
if derv:
return sigmoid(z) * (1 - sigmoid(z))
return 1 / (1 + np.exp(-z))

# Forward propaga on
def forward(x, predict=False):
a1 = x.reshape(x.shape[0], 1) # Convert input to column vector
z2 = W1.dot(a1) + B1 # 2x1
a2 = sigmoid(z2) # Hidden layer ac va on
z3 = W2.dot(a2) + B2 # 1x1
a3 = sigmoid(z3) # Output layer ac va on
if predict:
return a3
return a1, a2, a3

# Training func on
def train(W1, W2, B1, B2):
cost_history = np.zeros((max_iter, 1)) # To store cost at each itera on

for i in range(max_iter):
c=0
dW1 = np.zeros_like(W1)
dW2 = np.zeros_like(W2)
dB1 = np.zeros_like(B1)
dB2 = np.zeros_like(B2)

for j in range(m):

2
# Forward propaga on
a0 = X[j].reshape(X[j].shape[0], 1) # 2x1
z1 = W1.dot(a0) + B1 # 2x1
a1 = sigmoid(z1) # 2x1
z2 = W2.dot(a1) + B2 # 1x1
a2 = sigmoid(z2) # 1x1

# Backpropaga on
dz2 = a2 - y[j] # 1x1
dW2 += dz2 * a1.T # 1x2
dB2 += dz2 # 1x1
dz1 = np.mul ply(W2.T.dot(dz2), sigmoid(z1, derv=True)) # 2x1
dW1 += dz1.dot(a0.T) # 2x2
dB1 += dz1 # 2x1

# Compute cost
c += -y[j] * np.log(a2) - (1 - y[j]) * np.log(1 - a2)

# Update weights and biases


W1 -= learning_rate * (dW1 / m) + (reg_param / m) * W1
W2 -= learning_rate * (dW2 / m) + (reg_param / m) * W2
B1 -= learning_rate * (dB1 / m)
B2 -= learning_rate * (dB2 / m)

# Store the average cost


cost_history[i] = c / m

return W1, W2, B1, B2, cost_history

# Train the network


W1, W2, B1, B2, cost_history = train(W1, W2, B1, B2)

# Plot cost over itera ons


plt.plot(range(max_iter), cost_history)
plt.xlabel("Itera ons")
plt.ylabel("Cost")
plt. tle("Cost Reduc on Over Itera ons")
plt.show()

3
Output:

Result:
Thus the Python program successfully to Solving XOR problem using
DNN.

4
Ex. No: 2
CHARACTER RECOGNITION USING CNN
Date :

Aim:
To write a python program to implement the Character recognition using
CNN.

Algorithm:
Start the program.
Get the relevant packages for Recognition
Load the A_Z Handwritten Data.csv from the directory.
Reshape data for model creation
Train the model and Prediction on test data
Prediction on External Image
Stop the program

Program:
import numpy as np
import pandas as pd
from sklearn.datasets import fetch_lfw_people
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten
from keras.models import Sequential
from keras.utils import to_categorical
from keras.preprocessing import image

faces = fetch_lfw_people(min_faces_per_person=10, resize=1.0,


slice_=(slice(60, 188), slice(60, 188)), color=True)

fig, ax = plt.subplots(3, 6, figsize=(18, 10))


for i, axi in enumerate(ax.flat):
axi.imshow(faces.images[i])
axi.set(xticks=[], yticks=[], xlabel=faces.target_names[faces.target[i]])

5
Output:

mask = np.zeros(faces.target.shape, dtype=bool)


for target in np.unique(faces.target):
mask[np.where(faces.target == target)[0][:100]] = 1

x_faces = faces.data[mask]
y_faces = faces.target[mask]
x_faces = np.reshape(x_faces, (x_faces.shape[0], faces.images.shape[1],
faces.images.shape[2], faces.images.shape[3]))

face_images = x_faces / 255


face_labels = to_categorical(y_faces)

x_train, x_test, y_train, y_test = train_test_split(


face_images, face_labels, train_size=0.8, stra fy=face_labels, random_state=0
)
model = Sequen al()
model.add(Conv2D(32, (3, 3), ac va on='relu', input_shape=(face_images.shape[1:])))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(64, (3, 3), ac va on='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(64, (3, 3), ac va on='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Fla en())
model.add(Dense(128, ac va on='relu'))
model.add(Dense(len(faces.target_names), ac va on='so max'))

6
# Compile the model
model.compile(op mizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

hist = model.fit(x_train, y_train, valida on_data=(x_test, y_test), epochs=20, batch_size=25)

Output:
Epoch 1/20
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 936ms/step - accuracy:
0.3750 - loss: 0.6932 - val_accuracy: 0.5714 - val_loss: 0.6915
Epoch 2/20
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 87ms/step - accuracy:
0.5833 - loss: 0.6912 - val_accuracy: 0.5714 - val_loss: 0.6861
Epoch 3/20
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 83ms/step - accuracy:
0.5833 - loss: 0.6844 - val_accuracy: 0.5714 - val_loss: 0.6840
Epoch 4/20
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 82ms/step - accuracy:
0.5833 - loss: 0.6792 - val_accuracy: 0.5714 - val_loss: 0.6952
Epoch 5/20
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 81ms/step - accuracy:
0.5833 - loss: 0.6873 - val_accuracy: 0.5714 - val_loss: 0.6862
Epoch 6/20
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 80ms/step - accuracy:
0.5833 - loss: 0.6804 - val_accuracy: 0.5714 - val_loss: 0.6831
Epoch 7/20

7
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 80ms/step - accuracy:
0.5833 - loss: 0.6792 - val_accuracy: 0.5714 - val_loss: 0.6837
Epoch 8/20
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 94ms/step - accuracy:
0.5833 - loss: 0.6809 - val_accuracy: 0.5714 - val_loss: 0.6844

acc = hist.history['accuracy']
val_acc = hist.history['val_accuracy']
epochs = range(1, len(acc) + 1)

plt.plot(epochs, acc, '-', label='Training Accuracy')


plt.plot(epochs, val_acc, ':', label='Valida on Accuracy')
plt. tle('Training and Valida on Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()

Output:

y_predicted = model.predict(x_test)

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 52ms/step

mat = confusion_matrix(y_test.argmax(axis=1), y_predicted.argmax(axis=1))

sns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False,


cmap='Blues', x cklabels=faces.target_names, y cklabels=faces.target_names)
plt.xlabel('Predicted label')
plt.ylabel('Actual label')
plt.show()

8
Output:

x = image.load_img('george.jpg', target_size=(face_images.shape[1:]))
plt.x cks([])
plt.y cks([])
plt.imshow(x)
Output:

x = image.img_to_array(x) / 255
x = np.expand_dims(x, axis=0)
y = model.predict(x)[0]
for i in range(len(y)):
print(f"{faces.target_names[i]}: {y[i]}")

Output:
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 51ms/step
George HW Bush: 0.025578858330845833
Michael Schumacher: 0.9744210839271545

Result:
Thus written a python program to implement successfully for the
Character recognition using CNN.

9
Ex. No: 3
FACE RECOGNITION USING CNN
Date :

Aim:
To write a python program to implement the Face recognition using CNN.
Algorithm:
Start the program.
Get the relevant packages for Face Recognition
Load the A_Z Handwritten Data.csv from the directory.
Reshape data for model creation
Train the model and Prediction on test data
Prediction on External Image
Stop the program

Program:
import
numpy as
np import
pandas as
pd
from sklearn.datasets import fetch_lfw_people

faces = fetch_lfw_people(min_faces_per_person=100, resize=1.0,


slice_=(slice(60,
188), slice(60, 188)),
color=True) class_count =
len(faces.target_names)

print(faces.target_names)
print(faces.images.shape)
%matplotlib inline import
matplotlib.pyplot as plt
import seaborn as sns
sns.set() fig, ax =
plt.subplots(3, 6,
figsize=(18, 10))

for i, axi in enumerate(ax.flat): axi.imshow(faces.images[i] / 255)


# Scale pixel values so Matplotlib doesn't clip
everything above 1.0 axi.set(xticks=[], yticks=[],
xlabel=faces.target_names[faces.target[i]])
from collections
import Counter counts

10

You might also like