Deep Learning Lab - Record
Deep Learning Lab - Record
SCIENCE
MASTER RECORD
1
Syllabus
COURSE OBJECTIVES
LIST OF EXPERIMENTS:
2
Course Outcome
CO Details BTL
Apply deep neural network for simple problems (K3)
CO508.1 K3-Apply
Apply Convolution Neural Network for image processing (K3)
CO508.2 K3-Apply
Apply Recurrent Neural Network and its variants for text analysis
CO508.3 (K4) K4-Analyze
CO - PO Mapping
Program
Program Outcomes Specific
CO
Outcomes
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 3 2 1 1 1 - - - 3 2 3 2 3 3 2
CO2 1 3 2 2 2 - - - 3 2 2 2 1 3 1
CO3 3 2 1 2 1 - - - 2 3 1 1 2 3 3
CO4 3 3 1 2 1 - - - 1 3 2 2 3 2 2
CO5 3 3 3 3 2 - - - 1 3 3 1 3 3 2
CO6 2 2 2 2 2 2 2 2 2 2 2 2
AVG 2.6 2.6 1.6 2 1.4 - - - 2 2.4 2.2 1.6 2.4 2.8 2
CORRELATION
3
INDEX
Page
Sl. Name of the Experiment CO No.
No.
6
CO508.1
Solving XOR problem using DNN
1
CO508. 9
2 Character recognition using CNN 2
CO508. 15
3 Face recognition using CNN 2
CO508. 22
4 Language modeling using RNN 3
CO508. 27
5 Sentiment analysis using LSTM 3
CO508. 33
6 Parts of speech tagging using Sequence to Sequence architecture 3
CO508. 43
7 Machine Translation using Encoder-Decoder model 3
CO508. 48
8 4
Image augmentation using GANs
4
61
10. Online Fraud Detection of Share Market Data
CO508.6
1. Correctness of All the steps Some steps are followed Steps are not followed.
the Procedure are sequence. but error occurred. Not showing interest to
for the Knows how to Proceeded the experiment do the experiment.
experiment/ proceed the with the guidance.
Exercise (3) experiment.
8 or 7 or 6 5 or 4 or 3 2 or 1 or 0
2. Skills level in Show excellent Show minimal Show no understanding
performing the understanding of the understanding of the of the experiment. All
experiment/ experiment. All data experiment. All data is data is not recorded and
Exercise (8) is recorded and neatly is not Presented neatly.
recorded and is
presented
not presented neatly
3 2 1
3. Inferences Correct inferences Inferences drawn ar Inferences drawn are
drawn from have been drawn and correctly. But e partially incorrectly or
the presented presented no incorrect.
Professionally professionally
experiment/ t
exercise (3)
3 2 1
4. Presentation of Calculation are Calculation are present Wrong calculations.
results (3) presents neatly with neatly with minor Major mistakes.
accurate results. mistakes
5 to 4 3 to 2 1 to 0
5. Clarity in Almost all the Partially answered Unable to answer.
answering viva questions are
questions. (5) answered.
3 2 1
6. Attitude The experiment is The experiment The experiment is not
reflected in completed on time. is Completed on time.
doing the Manual/ Record note completed on time, Observation / Record
experiment / is submitted on time. but the Manual / note is not submitted
exercise (3) Record note is not on time.
5
submitted on time.
Total marks out of 25
Algorithm:
6
num_i_units = 2 # Number of Input units
num_h_units = 2 # Number of Hidden
units num_o_units = 1 # Number of Output
units # The learning rate for Gradient
Descent. learning_rate = 0.01
# The parameter to help with overfitting.
reg_param = 0
# Maximum iterations for Gradient
Descent. max_iter = 5000
# Number of training
ex m = 4
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
def sigmoid(z, derv=False):
if derv: return z * (1 - z)
return 1 / (1 + np.exp(-z))
def forward(x, predict=False):
a1 = x.reshape(x.shape[0], 1) # Getting the training example as a
column vector.
z2 = W1.dot(a1) + B1 # 2x2 * 2x1 + 2x1 = 2x1
a2 = sigmoid(z2) # 2x1
z3 = W2.dot(a2) + B2 # 1x2 * 2x1 + 1x1 = 1x1
a3 = sigmoid(z3)
if predict: return
a3 return (a1, a2,
a3)
for j in range(m):
sys.stdout.write("\rIteration: {} and {}".format(i + 1, j + 1))
# Forward Prop.
a0 = X[j].reshape(X[j].shape[0], 1) # 2x1
7
z1 = _W1.dot(a0) + _B1 # 2x2 * 2x1 + 2x1 = 2x1
a1 = sigmoid(z1) # 2x1
z2 = _W2.dot(a1) + _B2 # 1x2 * 2x1 + 1x1 = 1x1
a2 = sigmoid(z2) # 1x1
# Back prop.
dz2 = a2 - y[j] # 1x1
dW2 += dz2 * a1.T # 1x1 .* 1x2 = 1x2
dz1 = np.multiply((_W2.T * dz2), sigmoid(a1, derv=True)) #
(2x1 * 1x1) .* 2x1 = 2x1
dW1 += dz1.dot(a0.T) # 2x1 * 1x2 = 2x2dB2 += dz2 # 1x1
Output:
8
RESULT: Hence the Logi gate XOR has been solved Successfully using DNN
ALGORITHM:
9
pip install opencv-python
pip install keras
pip install tensorflow
import cv2
import numpy as
np import pandas
as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import
train_test_split from sklearn.utils import shuffle
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout
from keras.optimizers import SGD, Adam
from keras.callbacks import ReduceLROnPlateau, EarlyStopping
from keras.utils import to_categorical
data = pd.read_csv(r"A_Z Handwritten Data.csv").astype('float32')
X = data.drop('0',axis = 1)
y = data['0']
train_x, test_x, train_y, test_y = train_test_split(X, y, test_size =
0.2) train_x = np.reshape(train_x.values, (train_x.shape[0], 28,28))
test_x = np.reshape(test_x.values, (test_x.shape[0], 28,28))
word_dict =
{0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',
15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X', 24:'Y',25:'Z'}
y_int = np.int0(y)
count = np.zeros(26,
dtype='int') for i in y_int:
count[i] +=1
alphabets = []
for i in word_dict.values():
alphabets.append(i)
fig, ax = plt.subplots(1,1, figsize=(10,10))
ax.barh(alphabets, count)
# naming the x axis
plt.xlabel("Number of elements")
# naming the y axis
plt.ylabel("Alphabets")
# giving a title
plt.title("Plotting the number of alphabets")
# Turn on the minor TICKS, which are required for the minor GRID
plt.minorticks_on()
# Customize the major grid
plt.grid(which='major', linestyle='-', linewidth='0.5',
color='red') # Customize the minor grid
10
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.show()
uff = shuffle(train_x[:100])
fig, ax = plt.subplots(3,3, figsize = (10,10))
axes = ax.flatten()
for i in range(9):
_, shu = cv2.threshold(shuff[i], 30, 200, cv2.THRESH_BINARY)
axes[i].imshow(np.reshape(shuff[i], (28,28)), cmap=plt.get_cmap('gray'))
plt.show()
# Reshape data for model creation
train_X = train_x.reshape(train_x.shape[0],train_x.shape[1],train_x.shape[2],1)
print("The new shape of train data: ", train_X.shape)
model.add(Dense(64,activation ="relu"))
model.add(Dense(128,activation ="relu"))
model.add(Dense(26,activation ="softmax"))
model.compile(optimizer = Adam(learning_rate=0.001),
loss='categorical_crossentropy', metrics=['accuracy'])
11
print("The validation accuracy is :", history.history['val_accuracy'])
print("The training accuracy is :", history.history['accuracy'])
print("The validation loss is :", history.history['val_loss'])
print("The training loss is :", history.history['loss'])
# Prediction on test data
fig, axes = plt.subplots(3,3, figsize=(8,9))
axes = axes.flatten()
pred = word_dict[np.argmax(test_yOHE[i])]
ax.set_title("Prediction: "+pred)
# Predection on External Image
img = cv2.imread(r'test_image.jpg')
img_copy = img.copy()
img = cv2.cvtColor(img,
cv2.COLOR_BGR2RGB) img = cv2.resize(img,
(400,440))
img_pred = word_dict[np.argmax(model.predict(img_final))]
cv2.putText(img, "Image Data", (100,25), cv2.FONT_HERSHEY_DUPLEX ,
fontScale= 1, thickness=2, color = (255,0,0))
cv2.putText(img, "Character Prediction: " + img_pred, (10,410),
cv2.FONT_HERSHEY_SIMPLEX, fontScale= 1, thickness=2, color = (0,0,255))
cv2.imshow('Character Recognition', img)
while (1):
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
12
Output:
13
The new shape of train data: (297960, 28, 28, 1)
The new shape of train data: (74490, 28, 28, 1) The
new shape of train labels: (297960, 26) The new shape of
test labels: (74490, 26)
model: "sequential"
14
=================================================================
Total params:
137,178 max_pooling2d_2 (MaxPooling 2D) (None, 2, 2, 128) 0
Trainable
params: 137,178
Non- flatten (Flatten) (None, 512) 0 trainable
params: 0
dense (Dense) (None, 64) 32832
The
dense_1 (Dense) (None, 128) 8320
RESULT. Program has run successfully recognizing the each character inputs .
15
Program 03 : Face recognition using CNN
ALGORITHM:
Step 1: Start initializing Jupyter Notebook or IDLE
Step 2: Import necessary Packages
Step 3 : Assign Inputs and Weights for Data
Step 4 : Scale the pixel values above 1.0 for plotting
Step 5 : Model the training data to recognize each character using CNN
Step 6: Stop
import numpy as
np import pandas
as pd
from sklearn.datasets import fetch_lfw_people
print(faces.target_names)
print(faces.images.shape)
%matplotlib inline
import matplotlib.pyplot as
plt import seaborn as sns
sns.set()
16
df = pd.DataFrame.from_dict(names,
orient='index') df.plot(kind='bar')
mask = np.zeros(faces.target.shape, dtype=np.bool)
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]))
x_faces.shape
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
face_images = x_faces / 255 # Normalize pixel values
face_labels = to_categorical(y_faces)
model = Sequential()
model.add(Conv2D(32, (3, 3),
activation='relu',
input_shape=(face_images.shape[1:])))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(class_count, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
model.summary()
hist = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=20,
batch_size=25)
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='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
17
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.plot()
from sklearn.metrics import confusion_matrix
y_predicted = model.predict(x_test)
mat = confusion_matrix(y_test.argmax(axis=1), y_predicted.argmax(axis=1))
18
plt.xlabel('Predicted label')
plt.ylabel('Actual label')
import keras.utils as image
x = image.load_img('george.jpg', target_size=(face_images.shape[1:]))
plt.xticks([])
plt.yticks([])
plt.imshow(x)
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(faces.target_names[i] + ': ' +
str(y[i]))
Output:
['Colin Powell' 'Donald Rumsfeld' 'George W Bush' 'Gerhard Schroeder'
'Tony Blair']
(1140, 128, 128, 3)
19
(500, 128, 128, 3)
Model: "sequential"
=================================================================
Total params: 1,662,725
Trainable params: 1,662,725
Non-trainable params: 0
Epoch 1/20
20
16/16 [==============================] - 2s 123ms/step - loss: 1.6558 - accuracy: 0.1925 -
val_loss: 1.6038 - val_accuracy: 0.2000
Epoch 2/20
16/16 [==============================] - 2s 110ms/step - loss: 1.5860 - accuracy: 0.3175 -
val_loss: 1.5416 - val_accuracy: 0.3200
Epoch 3/20
16/16 [==============================] - 2s 112ms/step - loss: 1.4851 - accuracy: 0.3675 -
val_loss: 1.3706 - val_accuracy: 0.4500
Epoch 4/20
16/16 [==============================] - 2s 110ms/step - loss: 1.1602 - accuracy: 0.5775 -
val_loss: 1.0931 - val_accuracy: 0.5900
Epoch 5/20
16/16 [==============================] - 2s 112ms/step - loss: 0.8385 - accuracy: 0.7000 -
val_loss: 0.8494 - val_accuracy: 0.6700
Epoch 6/20
16/16 [==============================] - 2s 111ms/step - loss: 0.5011 - accuracy: 0.8275 -
val_loss: 0.8085 - val_accuracy: 0.6900
Epoch 7/20
16/16 [==============================] - 2s 111ms/step - loss: 0.3819 - accuracy: 0.8550 -
val_loss: 0.7241 - val_accuracy: 0.7200
Epoch 8/20
16/16 [==============================] - 2s 110ms/step - loss: 0.3558 - accuracy: 0.8950 -
val_loss: 0.5499 - val_accuracy: 0.7800
Epoch 9/20
16/16 [==============================] - 2s 114ms/step - loss: 0.1407 - accuracy: 0.9575 -
val_loss: 0.7090 - val_accuracy: 0.8000
Epoch 10/20
16/16 [==============================] - 2s 115ms/step - loss: 0.0869 - accuracy: 0.9875 -
val_loss: 0.6296 - val_accuracy: 0.8400
Epoch 11/20
16/16 [==============================] - 2s 111ms/step - loss: 0.0413 - accuracy: 0.9950 -
val_loss: 0.5816 - val_accuracy: 0.8300
Epoch 12/20
16/16 [==============================] - 2s 110ms/step - loss: 0.0325 - accuracy: 0.9950 -
val_loss: 0.5888 - val_accuracy: 0.8300
Epoch 13/20
16/16 [==============================] - 2s 110ms/step - loss: 0.0359 - accuracy: 0.9900 -
val_loss: 0.6945 - val_accuracy: 0.8100
Epoch 14/20
16/16 [==============================] - 2s 110ms/step - loss: 0.0085 - accuracy: 1.0000 -
val_loss: 0.5278 - val_accuracy: 0.8600
Epoch 15/20
16/16 [==============================] - 2s 111ms/step - loss: 0.0048 - accuracy: 1.0000 -
val_loss: 0.5697 - val_accuracy: 0.8500
Epoch 16/20
16/16 [==============================] - 2s 111ms/step - loss: 0.0032 - accuracy: 1.0000 -
val_loss: 0.6065 - val_accuracy: 0.8500
Epoch 17/20
16/16 [==============================] - 2s 110ms/step - loss: 0.0022 - accuracy: 1.0000 -
val_loss: 0.6007 - val_accuracy: 0.8500
Epoch 18/20
16/16 [==============================] - 2s 112ms/step - loss: 0.0017 - accuracy: 1.0000 -
val_loss: 0.6242 - val_accuracy: 0.8500
Epoch 19/20
21
16/16 [==============================] - 2s 118ms/step - loss: 0.0013 - accuracy: 1.0000 -
val_loss: 0.6333 - val_accuracy: 0.8500
Epoch 20/20
16/16 [==============================] - 2s 111ms/step - loss: 0.0011 - accuracy: 1.0000 -
val_loss: 0.6541 - val_accuracy: 0.8500
<matplotlib.image.AxesImage at 0x1ec80d4d910>
22
1/1 [==============================] - 0s 48ms/step
Colin Powell: 0.20101844
Donald Rumsfeld: 0.20214622 George W
Bush: 0.2216323 Gerhard Schroeder:
0.21147959
Tony Blair: 0.16372345
RESULT: Hence, Program has been successfully executed for Face Recognition using CNN
23
Program 04: Language modeling using RNN
ALGORITHM:
Step 1: Start initializing Jupyter Notebook or IDLE
Step 2: Import necessary Packages
Step 3 : Assign Inputs and Weights for Data
Step 4 : Scale the pixel values above 1.0 for plotting
Step 5 : Model the training data to recognize each character using CNN
Step 6: Stop
24
all_categories.append(category)
lines = readLines(filename)
category_lines[category] = lines
n_categories = len(all_categories)
if n_categories == 0:
raise RuntimeError('Data not found. Make sure that you downloaded data
' 'from https://download.pytorch.org/tutorial/data.zip and extract it to '
'the current directory.')
import torch
import torch.nn as nn
class RNN(nn.Module):
def init (self, input_size, hidden_size, output_size):
super(RNN, self). init ()
self.hidden_size = hidden_size
def initHidden(self):
return torch.zeros(1, self.hidden_size)
import random
25
def categoryTensor(category):
li = all_categories.index(category)
tensor = torch.zeros(1,
n_categories) tensor[0][li] = 1
return tensor
# One-hot matrix of first to last letters (not including EOS) for input
def inputTensor(line):
tensor = torch.zeros(len(line), 1, n_letters)
for li in range(len(line)):
letter = line[li] tensor[li][0]
[all_letters.find(letter)] = 1
return tensor
loss.backward()
for p in rnn.parameters():
p.data.add_(p.grad.data, alpha=-learning_rate)
import time
import math
def timeSince(since):
now = time.time()
s = now - since
m = math.floor(s / 60)
s -= m * 60
return '%dm %ds' % (m, s)
26
rnn = RNN(n_letters, 128, n_letters)
n_iters = 100000
print_every = 5000
plot_every = 500
all_losses = []
total_loss = 0 # Reset every ``plot_every`` ``iters``
start = time.time()
for iter in range(1, n_iters + 1):
output, loss =
train(*randomTrainingExample()) total_loss +=
loss
if iter % print_every == 0:
print('%s (%d %d%%) %.4f' % (timeSince(start), iter, iter / n_iters * 100, loss))
if iter % plot_every == 0:
all_losses.append(total_loss / plot_every)
total_loss = 0
import matplotlib.pyplot as plt
plt.figure()
plt.plot(all_losses)
max_length = 20
output_name = start_letter
for i in range(max_length):
output, hidden = rnn(category_tensor, input[0],
hidden) topv, topi = output.topk(1)
topi = topi[0][0]
if topi == n_letters - 1:
break
else:
letter = all_letters[topi]
output_name += letter
input = inputTensor(letter)
return output_name
# Get multiple samples from one category and multiple starting letters
def samples(category, start_letters='ABC'):
for start_letter in start_letters:
print(sample(category, start_letter))
samples('Russian', 'RUS')
samples('German', 'GER')
samples('Spanish', 'SPA')
samples('Chinese', 'CHI')
27
Output:
# categories: 18 ['Arabic', 'Chinese', 'Czech', 'Dutch', 'English', 'French', 'German', 'Greek',
'Irish', 'Italian ', 'Japanese', 'Korean', 'Polish', 'Portuguese', 'Russian ', 'Scottish', 'Spanish',
'Vietnamese']
O'Neal
0m 5s (5000 5%) 2.6595
0m 11s (10000 10%) 2.9644
0m 16s (15000 15%) 3.3754
0m 22s (20000 20%) 2.0799
0m 27s (25000 25%) 2.6884
0m 33s (30000 30%) 2.2509
0m 38s (35000 35%) 2.3497
0m 43s (40000 40%) 2.5290
0m 49s (45000 45%) 2.9439
0m 54s (50000 50%) 2.7406
0m 59s (55000 55%) 3.0044
1m 4s (60000 60%) 2.5765
1m 10s (65000 65%) 2.3694
1m 15s (70000 70%) 2.2810
1m 20s (75000 75%) 2.2660
1m 26s (80000 80%) 2.1720
1m 31s (85000 85%) 2.4900
1m 36s (90000 90%) 2.0302
1m 42s (95000 95%) 1.8320
1m 47s (100000 100%) 2.4904
[<matplotlib.lines.Line2D at 0x1e56757bcd0>]
RESULT: Program was successfully executed using RNN for language Modelling
28
Program 05 : Sentiment analysis using LSTM
AIM :To write a program to analyse a dataset with help of Sentiment Analysis using LSTM
ALGORITHM:
Step 1: Start initializing Jupyter Notebook or IDLE
Step 2: Import necessary Packages
Step 3 : Assign dataset and Weights for Data
Step 4 : Scale the pixel values above 1.0 for plotting
Step 5 : Model the training data to recognize each character using CNN
Step 6: Stop
Program:
reviews = data['review'].values
labels = data['sentiment'].values
encoder = LabelEncoder()
encoded_labels = encoder.fit_transform(labels)
train_sentences, test_sentences, train_labels, test_labels =
train_test_split(reviews, encoded_labels, stratify = encoded_labels)
# model initialization
model =
keras.Sequential([
keras.layers.Embedding(vocab_size, embedding_dim,
input_length=max_length),
keras.layers.Bidirectional(keras.layers.LSTM(64)),
keras.layers.Dense(24, activation='relu'),
keras.layers.Dense(1, activation='sigmoid')
])
# compile model
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# model summary
model.summary()
num_epochs = 5
history = model.fit(train_padded, train_labels,
epochs=num_epochs, verbose=1,
validation_split=0.1)
30
prediction = model.predict(test_padded)
# Get labels based on probability 1 if p>= 0.5 else 0
pred_labels = []
for i in prediction:
if i >= 0.5:
pred_labels.append(1)
else:
pred_labels.append(0)
print("Accuracy of prediction on test set :
", accuracy_score(test_labels,pred_labels))
Output:
review sentiment
0 One of the other reviewers has mentioned that ... positive
1 A wonderful little production. <br /><br />The... positive
2 I thought this was a wonderful way to spend ti... positive
3 Basically there's a family where a little boy ... negative
4 Petter Mattei's "Love in the Time of Money" is... positive
... ... ...
49995 I thought this movie did a down right good job... positive
49996 Bad plot, bad dialogue, bad acting, idiotic di... negative
49997 I am a Catholic taught in parochial elementary... negative
49998 I'm going to have to disagree with the previou... negative
49999 No one expects the Star Trek movies to be high... negative
31
[nltk_data] Downloading package stopwords to
[nltk_data] C:\Users\CGNANAM.ADS\AppData\Roaming\nltk_data... [nltk_data]
Package stopwords is already up-to-date!
append(1)
32
showing info https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/inde x.xml
Out[6]:
True
review sentiment
0 w w w w w w w w w w w w w w w w w w w w w w w ... positive
1 wwwwwwwwwwwwwww positive
2 wwwwwwwwwwwwwwwwwww positive
3 wwwwwwwwwww negative
4 wwwwwwwwwwwwwwwwww positive
review sentiment
Model: "sequential"
33
Layer (type) Output Shape Param #
===========================================================
======
embedding (Embedding) (None, 200, 100) 300000
===========================================================
======
Total params: 387,601
Trainable params: 387,601
Non-trainable params: 0
Epoch 1/5
1055/1055 [==============================] - 60s 55ms/step - loss: 0.69
32 - accuracy: 0.5021 - val_loss: 0.6925 - val_accuracy: 0.5205 Epoch 2/5
1055/1055 [==============================] - 58s 55ms/step - loss: 0.69
26 -
Epoch 3/5
1055/1055 [==============================] - 59s 56ms/step - loss: 0.69
26 - accuracy: 0.5129 - val_loss: 0.6924 - val_accuracy: 0.5171 Epoch
4/5
1055/1055 [==============================] - 59s 56ms/step - loss: 0.69
23 - accuracy: 0.5166 - val_loss: 0.6927 - val_accuracy: 0.4965 Epoch
5/5
1055/1055 [==============================] - 58s 55ms/step - loss: 0.69
25 - accuracy: 0.5141 - val_loss: 0.6924 - val_accuracy: 0.5173
34
Accuracy of prediction on test set : 0.5148
RESULT: Thus the Program was executed successfully with the help of LSTM with Sentinal Analysis
35
Program 06 : Parts of speech tagging using Sequence to Sequence architecture
ALGORITHM:
import numpy as np
import pandas as
pd import json
import functools as fc
from sklearn.metrics import accuracy_score
for i in
range(len(word)): if
word[i] in vocab:
vocab[word[i]] += 1
else:
vocab[word[i]] = 1
# replace rare words with <unk> (threshold = 3)
vocab2 = {}
num_unk = 0
for w in vocab:
if vocab[w] >= 3:
vocab2[w] = vocab[w]
else:
num_unk += vocab[w]
36
Task 2: Model Learning
# build a vocabulary list with only frequent words (i.e. occur no less than 3 times)
vocab_ls = list(vocab2.keys())
# for ss, we need to count the times that a pos tag occurs at the beginning
# of a sequence (i.e. (s|<s>))
for i in
range(len(word)): if
index[i] == 1:
if str(pos[i]) + '|' + '<s>' in ss:
ss[str(pos[i]) + '|' + '<s>'] += 1
else:
ss[str(pos[i]) + '|' + '<s>'] = 1
for p in pos:
if p in count_pos:
count_pos[p] += 1
else:
count_pos[p] = 1
37
count_pos['<s>'] = 0
for i in
index: if i
== 1:
count_pos['<s>'] += 1
39
# split dev lists (index, word and pos) to individual samples (list --> list of sublists)
word_dev2 = []
pos_dev2 = []
word_sample = []
pos_sample = []
for i in range(len(dev)-1):
if index_dev[i] < index_dev[i+1]:
word_sample.append(word_dev[i])
pos_sample.append(pos_dev[i])
else:
word_sample.append(word_dev[i])
word_dev2.append(word_sample)
word_sample = []
pos_sample.append(pos_dev[i])
pos_dev2.append(pos_sample)
pos_sample = []
def greedy(sentence):
# initialize a dictionary to keep track of the pos for each position
pos = []
for p in
pos_distinct: try:
temp = emission[sentence[0] + '|' + p] * transition[p + '|' + '<s>']
if temp > max_prob:
max_prob = temp
p0 = p
except:
pass
pos.append(p0)
max_prob = 0
pi = 'UNK'
40
for p in
pos_distinct: try:
temp = emission[sentence[i] + '|' + p] * transition[p + '|' + pos[-
1]] if temp > max_prob:
max_prob = temp
pi = p
except:
pass
pos.append(pi)
return pos
pos_greedy = [greedy(s) for s in word_dev2]
# concatenate the list of sublists into one single list
pos_greedy = fc.reduce(lambda a, b: a + b, pos_greedy)
pos_dev = fc.reduce(lambda a, b: a + b, pos_dev2)
index_dev = dev.loc[:,
'index'].values.tolist() word_dev = dev.loc[:,
'word'].values.tolist() pos_dev = dev.loc[:,
'POS'].values.tolist()
# split dev lists (index, word and pos) to individual samples (list --> list of sublists)
word_dev2 = []
41
pos_dev2 = []
word_sample = []
pos_sample = []
for i in range(len(dev)-1):
if index_dev[i] < index_dev[i+1]:
word_sample.append(word_dev[i])
pos_sample.append(pos_dev[i])
else:
word_sample.append(word_dev[i])
word_dev2.append(word_sample)
word_sample = []
pos_sample.append(pos_dev[i])
pos_dev2.append(pos_sample)
pos_sample = []
# for the first position, the highest cumulative probability of each possible pos would be
# emission[sentence[0]|pos] * transition[pos|<s>]
# check if the first word is in the vocabualry. If not, replace with '<unk>'
if sentence[0] not in vocab_frequent:
sentence[0] = '<unk>'
for p in pos_distinct:
if p + '|' + '<s>' in transition:
try:
seq[0][p] = transition[p + '|' + '<s>'] * \
emission[sentence[0] + '|' + p]
except:
seq[0][p] = 0
# set <s> as the previous pos of each possible pos at the first position
for p in seq[0].keys():
pre_pos[0][p] = '<s>'
# for position i > 0, the highest cumulative probability of each possible pos would be
# emission[sentence[i]|pos[i]] * transition[pos[i]|pos[i-1]] * seq[i-1][pos]
for i in range(1, len(sentence)):
# still, check if the word is in the vocabulary
if sentence[i] not in vocab_frequent:
sentence[i] = '<unk>'
42
for p in seq[i-1].keys():
for p_prime in pos_distinct:
if p_prime + '|' + p in transition:
if p_prime in seq[i]:
try:
temp = seq[i-1][p] * \
transition[p_prime + '|' + p] * \
emission[sentence[i] + '|' + p_prime]
if temp > seq[i][p_prime]:
seq[i][p_prime] = temp
pre_pos[i][p_prime] = p
except:
pass
else:
try:
seq[i][p_prime] = seq[i-1][p] * \
transition[p_prime + '|' + p] * \
emission[sentence[i] + '|' + p_prime]
pre_pos[i][p_prime] = p
except:
seq[i][p_prime] = 0
# after we get the maximum probability for every possible pos at every position of a
sentence,
# we can trace backward to find out our prediction on the pos for the sentence.
seq_predict = []
# The pos of the last word in the sentence is the one with the highest probability
# after predicting the pos of the last word in the sentence, we can iterate through pre_pos
to predict
# the pos of the remaining words in the input sentence in the reverse order
43
# use viterbi to predict pos for dev
pos_viterbi = [viterbi(s) for s in word_dev2]
Output:
0
1 Pierre NNP
1
2 Vinken NNP
2
3 , ,
3
4 61 CD
4 5 years NNS
0 1 The DT
1 2 Arizona NNP
2 3 Corporations NNP
3 4 Commission NNP
4 5 authorized VBD
44
RESULT: The Program has run Successfully to find the : Parts of speech tagging
using Sequence to Sequence architecture
# You can write up to 5GB to the current directory (/kaggle/working/) that gets
preserved as output
when you create a version using "Save & Run All"
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of
current session
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input,LSTM,Dense
batch_size=64
epochs=100
latent_dim=256 # here latent dim represent hidden state or cell state
num_samples=10000
data_path='fra.txt'
# Vectorize the data. input_texts = [] target_texts = [] input_characters = set() target_characters = set()
with open(data_path, 'r', encoding='utf-8') as f: lines = f.read().split('\n')
for line in lines[: min(num_samples, len(lines) - 1)]: input_text, target_text, _ = line.split('\t')
# We use "tab" as the "start sequence" character
# for the targets, and "\n" as "end sequence" character. target_text = '\t' + target_text + '\n'
input_texts.append(input_text) target_texts.append(target_text)
for char in input_text:
if char not in input_characters: input_characters.add(char)
for char in target_text:
45
if char not in target_characters: target_characters.add(char)
input_characters=sorted(list(input_characters)) target_characters=sorted(list(target_characters))
num_encoder_tokens=len(input_characters) num_decoder_tokens=len(target_characters)
max_encoder_seq_length=max([len(txt) for txt in input_texts]) max_decoder_seq_length=max([len(txt) for
txt in target_texts]) print('Number of samples:', len(input_texts))
print('Number of unique input tokens:', num_encoder_tokens) print('Number of unique output tokens:',
num_decoder_tokens) print('Max sequence length for inputs:', max_encoder_seq_length) print('Max
sequence length for outputs:', max_decoder_seq_length)
46
input_token_index=dict(
[(char,i) for i, char in enumerate(input_characters)])
target_token_index=dict(
[(char,i) for i, char in enumerate(target_characters)])
encoder_input_data = np.zeros(
(len(input_texts), max_encoder_seq_length, num_encoder_tokens),
dtype='float32')
decoder_input_data = np.zeros(
(len(input_texts), max_decoder_seq_length, num_decoder_tokens),
dtype='float32')
decoder_target_data = np.zeros(
(len(input_texts), max_decoder_seq_length, num_decoder_tokens),
dtype='float32')
47
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# Run training
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2)
model.save('eng2french.h5')
decoder_state_input_h = Input(shape=(latent_dim,))
decoder_state_input_c = Input(shape=(latent_dim,))
decoder_states_inputs = [decoder_state_input_h,
decoder_state_input_c] decoder_outputs, state_h, state_c =
decoder_lstm(
decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model(
[decoder_inputs] + decoder_states_inputs,
[decoder_outputs] + decoder_states)
# Sample a token
sampled_token_index = np.argmax(output_tokens[0, -1, :])
sampled_char = reverse_target_char_index[sampled_token_index]
decoded_sentence += sampled_char
48
# Exit condition: either hit max
length # or find stop character.
if (sampled_char == '\n' or
len(decoded_sentence) > max_decoder_seq_length):
stop_condition = True
# Update states
states_value = [h, c]
return decoded_sentence
Output:
Number of samples: 10000
Number of unique input tokens: 71 Number of
unique output tokens: 93 Max sequence length
for inputs: 15 Max sequence length for outputs:
59
Epoch 1/100
125/125 [==============================] - 15s 105ms/step - loss:
1.2150 - accuracy: 0.7315 - val_loss: 1.0873 - val_accuracy: 0.7068
Epoch 2/100
125/125 [==============================] - 13s 106ms/step - loss:
0.9334 - accuracy: 0.7490 - val_loss: 0.9959 - val_accuracy: 0.7128
Epoch 3/100
125/125 [==============================] - 13s 105ms/step - loss:
0.8396 - accuracy: 0.7679 - val_loss: 0.9039 - val_accuracy: 0.7500
…
Epoch 98/100
125/125 [==============================] - 13s 107ms/step - loss:
0.1532 - accuracy: 0.9531 - val_loss: 0.5529 - val_accuracy: 0.8705
Epoch 99/100
125/125 [==============================] - 13s 108ms/step - loss:
0.1517 - accuracy: 0.9533 - val_loss: 0.5561 - val_accuracy: 0.8697
Epoch 100/100
125/125 [==============================] - 13s 108ms/step - loss:
0.1497 - accuracy: 0.9543 - val_loss: 0.5522 - val_accuracy: 0.8706
sentence)
49
Input sentence: Smile.
Decoded sentence: Pours pres votr.
50
Program 8: Image augmentation using GANs
import os
import numpy as np
import keras.utils as
image
import matplotlib.pyplot as plt
%matplotlib inline
def show_images(images):
fig, axes = plt.subplots(1, 8, figsize=(20, 20), subplot_kw={'xticks': [], 'yticks': []})
for i, ax in enumerate(axes.flat):
ax.imshow(images[i] / 255)
x_train = []
y_train = []
x_test = []
y_test = []
51
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.applications.resnet50 import preprocess_input
x_train = preprocess_input(np.array(x_train))
x_test = preprocess_input(np.array(x_test))
y_train_encoded = to_categorical(y_train)
y_test_encoded = to_categorical(y_test)
from tensorflow.keras.applications import ResNet50V2
base_model = ResNet50V2(weights='imagenet', include_top=False)
for layer in base_model.layers:
layer.trainable = False
from keras.models import Sequential
from keras.layers import Flatten, Dense, Dropout
from keras.layers import Rescaling, RandomFlip, RandomRotation,
RandomTranslation, RandomZoom
model = Sequential()
model.add(Rescaling(1./255))
model.add(RandomFlip(mode='horizontal'))
model.add(RandomTranslation(0.2, 0.2))
model.add(RandomRotation(0.2))
model.add(RandomZoom(0.2))
model.add(base_model)
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(3, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
hist = model.fit(x_train, y_train_encoded, validation_data=(x_test,
y_test_encoded), batch_size=10, epochs=25)
52
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='Validation Accuracy')
plt.ylabel('Actual label')
x = image.load_img('arctic-wildlife/samples/arctic_fox/arctic_fox_140.jpeg',
target_size=(224, 224))
plt.xticks([])
plt.yticks([])
plt.imshow(x)
53
x = image.img_to_array(x)
x = np.expand_dims(x,
axis=0) x =
preprocess_input(x)
predictions = model.predict(x)
for i, label in
enumerate(class_labels):
print(f'{label}: {predictions[0][i]}')
x = image.load_img('arctic-wildlife/samples/walrus/walrus_143.png',
target_size=(224, 224))
plt.xticks([])
plt.yticks([])
plt.imshow(x)
x = image.img_to_array(x)
x = np.expand_dims(x,
axis=0) x =
preprocess_input(x)
predictions = model.predict(x)
54
Output:
Train :
Test :
55
Epoch 24/25
30/30 [==============================] - 27s 896ms/step - loss: 0.5841 -
accuracy: 0.9633 - val_loss: 0.5701 - val_accuracy: 0.9667
Epoch 25/25
30/30 [==============================] - 25s 844ms/step - loss: 0.7861 -
accuracy: 0.9500 - val_loss: 0.5762 - val_accuracy: 0.9667
<matplotlib.image.AxesImage at 0x2c5496dfa00>
56
Preprocess the image and submit it to the network for classification.
1/1 [==============================] - 0s 57ms/step
arctic fox: 1.0
polar bear:
1.1824497264458205e-28 walrus:
0.0
Now try it with a walrus image that the network hasn't seen before. Start by
loading the image.
<matplotlib.image.AxesImage at 0x2c546cb7790>
SYLLABUS
58
PROJECT 1
59
60
61
62
63
PROJECT 2
64
65
66
67