DL Lab Manual - Organized
DL Lab Manual - Organized
PRACTICAL RECORD
BONAFIDE CERTIFICATE
REGISTER NUMBER
LABORATORY, as prescribed by the Anna University Chennai, for the fifth semester
Vision
To lead the way in advancing the domains of Artificial Intelligence and Data Sciences,
empowering students as future ready leaders equipped with innovation, to address societal issues,
and create a meaningful global impact.
Mission
1. To empower students by imparting a comprehensive education in the field of Artificial
Intelligence and Data Sciences, cultivating a profound grasp of fundamental principles and
methodologies in cutting-edge technologies.
2. To cultivate a strong connection with industries to encourage partnerships in technology
training, facilitate internships, and prepare students to become industry-ready professionals.
3. To encourage and endorse students' involvement in research and entrepreneurship with an
innovative approach that effectively tackles real-world challenges.
PEO1: Utilize their proficiencies in the fundamental knowledge of basic sciences, mathematics,
Artificial Intelligence, data science and statistics to build systems that require management and
analysis of large volumes of data
PEO2: Advance their technical skills to pursue pioneering research in the field of AI and Data
Science and create disruptive and sustainable solutions for the welfare of ecosystems.
PEO3: Think logically, pursue lifelong learning and collaborate with an ethical attitude in a
multidisciplinary team.
PEO4: Design and model AI based solutions to critical problem domains in the real world.
PEO5: Exhibit innovative thoughts and creative ideas for effective contribution towards economy
building
SYLLABUS
LIST OF EXPERIMENTS:
COURSE OUTCOMES:
At the end of this course, the students will be able to:
CO1: Apply deep neural network for simple problems (K3)
CO2: Apply Convolution Neural Network for image processing (K3)
CO3: Apply Recurrent Neural Network and its variants for text analysis (K3)
CO4: Apply generative models for data augmentation (K3)
CO5: Develop real-world solutions using suitable deep neural networks (K4
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.
5. If your frames reach the server it will send ACK signal to client otherwise it will send
NACK signal to client.
6. Stop the program
Program:
import numpy as np # For matrix math import
matplotlib.pyplot as plt # For plotting import sys #
For printing
# The training data.X =
np.array([
[0, 1],
[1, 0],
[1, 1],
[0, 0]
])
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
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 = 2x2 dB1 += dz1 #
2x1
(reg_param / (2 * m)) *(
np.sum(np.power(_W1, 2)) +
np.sum(np.power(_W2, 2))
)
)
Output
Result:
Thus the Python program successfully to Solving XOR problem using DNN.
lO MoAR cPSD|297
926
27
Ex. No: 2
CHARACTER RECOGNITION USING CNN
Date :
Aim:
To write a python program to implement the Character recognition
using CNN.
Algorithm:
1. Start the program.
2. Get the relevant packages for Recognition
3. Load the A_Z Handwritten Data.csv from the directory.
4. Reshape data for model creation
5. Train the model and Prediction on test data
6. Prediction on External Image
7. Stop the program
Program:
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)
lO MoAR cPSD|297
926
27
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(Flatten())
model.add(Dense(64,activation ="relu"))
lO MoAR cPSD|297
926
27
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'])
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_copy = cv2.GaussianBlur(img_copy, (7,7), 0)
img_gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
_, img_thresh = cv2.threshold(img_gray, 100, 255, cv2.THRESH_BINARY_INV)
img_final = cv2.resize(img_thresh, (28,28))
img_final =np.reshape(img_final, (1,28,28,1))
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()
lO MoAR cPSD|297
926
27
Output:
model: "sequential"
lO MoAR cPSD|297
926
27
=================================================================
Total params: 137,178
Trainable params: 137,178
Non-trainable params: 0
Result:
Thus written a python program to implement successfully for the Character recognition using CNN.
lO MoAR cPSD|297
926
27
Ex. No: 3
FACE RECOGNITION USING CNN
Date :
Aim:
To write a python program to implement the Face recognition using CNN.
Algorithm:
i. Start the program.
ii. Get the relevant packages for Face Recognition
Load the A_Z Handwritten Data.csv from the
directory. Reshape data for model creation
iii. Train the model and Prediction on test data
iv. Prediction on External Image
v. Stop the program
Program:
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()
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 = Counter(faces.target)
names = {}
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
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')
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))
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)
lO MoAR cPSD|297
926
27
Model: "sequential"
=================================================================
Total params: 1,662,725
Trainable params: 1,662,725
Non-trainable params: 0
Epoch 1/20
<matplotlib.image.AxesImage at 0x1ec80d4d910>
Result:
Thus written a python program to implement successfully for the Face recognition using CNN.
lO MoAR cPSD|297
926
27
Ex. No: 4
LANGUAGE MODELING USING RNN
Date :
Aim:
To write a python program to implement the Language modeling using RNN.
Algorithm:
1. Start the program.
2. Get the relevant packages for Language modeling
3. Read a file and split into lines.
4. Build the category lines dictionary, a list of lines per category Add the Random itemfrom
a list
5. Get a random category and random line from that
category. One-hot vector for category
6. Make category, input, and target tensors from a random category, line pair
Sample from a category and starting letter
7. Get multiple samples from one category and multiple starting letters.
8. Train the model and Prediction on test data
Prediction on External Image
9. Stop the program
Program:
from future import unicode_literals, print_function, division from
io import open
import glob
import os
import unicodedata
import string
all_letters = string.ascii_letters + " .,;'-"
n_letters = len(all_letters) + 1 # Plus EOS marker def
findFiles(path): return glob.glob(path)
all_categories.append(category)
lines = readLines(filename)
lO MoAR cPSD|297
926
27
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.')
print('# categories:', n_categories, all_categories)
print(unicodeToAscii("O'Néàl"))
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
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
lO MoAR cPSD|297
926
27
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)
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
lO MoAR cPSD|297
926
27
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')
Output:
[<matplotlib.lines.Line2D at 0x1e56757bcd0>]
Rovonov
Uarakov
Shavanov
Gerre
Eeren
Roure
Salla
Para
Allana
Cha
Han
Iun
Result:
Thus written a python program to implemented successfully for the Language Modeling Using
RNN.
lO MoAR cPSD|297
926
27
Ex. No: 5
SENTIMENT ANALYSIS USING LSTM
Date :
Aim:
To write a python program to implement the Sentiment analysis using LSTM.
Algorithm:
Start the program.
Get the relevant packages for Keras-Preprocessing
Load the IMDB Dataset.csv file.
Remove HTML tags, URL and non-alphanumeric charactersRead
a file and split into lines.
Tuning the hyperparameters of the model
Model initialization
compile model
reviews on which we need to predict. Stop
the program
Program:
pip install Keras-Preprocessing
import re
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder from
sklearn.model_selection import train_test_split from
keras.preprocessing.text import Tokenizer
from keras_preprocessing.sequence import pad_sequences
import keras
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score import
math
import nltk
data = pd.read_csv('IMDB Dataset.csv') data
def remove_tags(string):
removelist = ""
result = re.sub('','',string) #remove HTML tags
result = re.sub('https://.*','',result) #remove URLs
result = re.sub(r'[^w'+removelist+']', ' ',result) #remove non-alphanumeric
characters
result = result.lower()
return result
data['review']=data['review'].apply(lambda cw : remove_tags(cw))
nltk.download('stopwords')
from nltk.corpus import stopwords stop_words =
set(stopwords.words('english'))
data['review'] = data['review'].apply(lambda x: ' '.join([word for word in x.split() if
word not in (stop_words)]))
import nltk
nltk.download()
#we want to download 'wordnet' and 'omw-1.4' from nltk
w_tokenizer = nltk.tokenize.WhitespaceTokenizer()
lO MoAR cPSD|297
926
27
lemmatizer = nltk.stem.WordNetLemmatizer()
def lemmatize_text(text):
st = ""
for w in w_tokenizer.tokenize(text):
st = st + lemmatizer.lemmatize(w) + " "
return st
data['review'] = data.review.apply(lemmatize_text)
data
s = 0.0
for i in data['review']:
word_list = i.split()
s = s + len(word_list)
print("Average length of each review : ",s/data.shape[0])
pos = 0
for i in range(data.shape[0]):
if data.iloc[i]['sentiment'] == 'positive':
pos = pos + 1
neg = data.shape[0]-pos
print("Percentage of reviews with positive sentiment is
"+str(pos/data.shape[0]*100)+"%")
print("Percentage of reviews with negative sentiment is
"+str(neg/data.shape[0]*100)+"%")
reviews = data['review'].values
labels = data['sentiment'].values
encoder = LabelEncoder()
encoded_labels = encoder.fit_transform(labels)
maxlen=max_length)
# convert Test dataset to sequence and pad sequences
test_sequences = tokenizer.texts_to_sequences(test_sentences)
test_padded = pad_sequences(test_sequences, padding='post', maxlen=max_length)
# model initialization
model = keras.Sequential([
keras.layers.Embedding(vocab_size, embedding_dim,
input_length=max_length),
lO MoAR cPSD|297
926
27
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)
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))
else:
pred_labels.append(0)
for i in range(len(sentence)):
print(sentence[i])
if pred_labels[i] == 1:
s = 'Positive'
else:
s = 'Negative'
print("Predicted sentiment : ",s)
lO MoAR cPSD|297
926
27
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
review sentiment
0 w w ww w w ww w ww w ww w w ww w w w ww positive
...
1 w w w ww w w ww w ww w w w positive
2 w w ww w w w ww w ww ww w ww w w positive
3 w w w ww w w ww w w negative
4 w w ww w w w ww w ww w w ww w w positive
review sentiment
49995 w w w ww w w ww w w w ww w ww w w positive
49996 w w w ww w w w negative
49997 w w w ww w w ww w w w negative
49998 w w w ww w w ww w ww w w w negative
49999 w w w ww w w ww w w w negative
50000 rows × 2 columns
Average length of each review : 18.714
Percentage of reviews with positive sentiment is 50.0%
Percentage of reviews with negative sentiment is 50.0%
lO MoAR cPSD|297
926
27
Model: "sequential"
===========================================================
======
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 - accuracy: 0.5094 - val_loss: 0.6925 - val_accuracy: 0.5173
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
Result:
Thus written a python program to implemented successfully for the Sentiment
analysis using LSTM.
lO MoAR cPSD|297
926
27
Ex. No: 6
PARTS OF SPEECH TAGGING USING SEQUENCE TO SEQUENCE
ARCHITECTURE
Date :
Aim:
To write a python program to implement the parts of speech tagging using
Sequence to Sequence architecture.
Algorithm:
0 Start the program
1 Get the relevant packages for function tools
2 Load the Vocabulary Creation and data split in to train and
test Sort the vocabulary by occurrences of words
3 write the sorted vocabulary to vocab file
4 build a vocabulary list with only frequent words (i.e. occur no less than 3
times) replace non-frequent words in word with <unk>
5 make sure the index of the current word is less than the next build an emission and
a transition dictionaries
6 write the emission and transition dictionaries into a json file load txt file vocab
7 split dev lists (index, word and pos) to individual samples (list --> list of sublists)
8 initialize a dictionary that keeps track of the highest cumulative probability of each
possible pos at each position of the input sentence
use viterbi to predict pos for dev merge the list of sublists to a single list
9 Stop the program
Program:
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] not in vocab_ls:
word[i] == '<unk>'
# 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
lO MoAR cPSD|297
926
27
for p in pos:
if p in count_pos:
count_pos[p] += 1
else:
count_pos[p] = 1
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 = []
# we need to make sure the first word is in the vocabulary. If not, replace
# with <unk>
if sentence[0] not in vocab_frequent:
sentence[0] = '<unk>'
# predict pos based on the product of the emission and transition
max_prob = 0
p0 = 'UNK'
for p in pos_distinct:
try:
temp = emission[sentence[0] + '|' + p] * transition[p + '|' + '<s>']
if temp > max_prob:
max_prob = temp
p0 = p
except:
lO MoAR cPSD|297
926
27
pass
pos.append(p0)
max_prob = 0
pi = 'UNK'
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)
# 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 = []
# 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]
lO MoAR cPSD|297
926
27
# 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
# 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
lO MoAR cPSD|297
926
27
Result:
Thus written a python program to implemented successfully for the parts of speech tagging
using Sequence to Sequence architecture.
lO MoAR cPSD|297
926
27
Ex. No: 7
MACHINE TRANSLATION USING ENCODER-DECODER MODEL
Date :
Aim:
To write a python program to implement the Machine Translation using
Encoder-Decoder model
Algorithm:
1. Start the program
2. Input data files are available in the read-only
3. Decoder_target_data is ahead of decoder_input_data by one time step Set
up the decoder, using `encoder_states` as initial state
4. Run training
5. Reverse-lookup token index to decode sequences back to something readable.
Generate empty target sequence of length 1.
6. Sampling loop for a batch of sequences (to simplify, here we assume a batch of size 1).
Sample a token
7. Exit condition: either hit max length or find stop character. Update the target
sequence (of length 1).
8. Update states
9. Stop the program
Program:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) #
Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under
The input directory
import os
for dirname, _, filenames in os.walk('/kaggle/input'): for
filename in filenames:
print(os.path.join(dirname, filename))
# 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
The 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')
lO MoAR cPSD|297
926
27
num_encoder_tokens=len(input_characters)
num_decoder_tokens=len(target_characters)
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')
# 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
# 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
lO MoAR cPSD|297
926
27
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
Result:
Thus written a python program to implemented successfully for the Machine Translation using
Encoder-Decoder model
lO MoAR cPSD|297
926
27
Ex. No: 8
IMAGE AUGMENTATION USING GANS
Date :
Aim:
To write a python program to implement the Image augmentation using GANs
Algorithm:
1. Start the program
2. Load the images from the directory.
Split the data into train and test
images
3. Training the CNN using the two different sized datasets with no augmented data.
Train a network based on ResNet-50V2 with data augmentation
4. Preprocess the image and submit it to the network for classification.
5. Now try it with a walrus image that the network hasn't seen before. Start by
loading the image.
6. Finally, preprocess the image and make aprediction.
Stop the program.
Program:
import os
import numpy as np import
keras.utils as image
import matplotlib.pyplot as plt
%matplotlib inline
for i, ax in enumerate(axes.flat):
ax.imshow(images[i] / 255)
x_train = []
y_train = []
x_test = []
lO MoAR cPSD|297
926
27
y_test = []
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)
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)
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)
Output:
Train :
Test :
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>
lO MoAR cPSD|297
926
27
Result:
Thus written a python program to implemented successfully for the Image augmentation using
GANs.