Deep Learning Lab Record.
Deep Learning Lab Record.
NAME:___________________________________
YEAR/SEM: _______________BRANCH:_______
REG. NO.:________________________________
THANGAVELU ENGINEERING COLLEGE
I T Highway, Rajiv Gandhi Salai -600097
BONAFIDE CERTIFICATE
REGISTER No.:
Certified that this is the Bonafide Record of Work done by
Mr. / Ms.____________________________________________________
Date :
CONTENT
5
EX NO : 1 Solving XOR problem using DNN
DATE:
AIM: To write a python program to solve the XOR problem using DNN
ALGORITHM :
8
ARCHITECTURE DIAGRAM:
9
PROGRAM/SOURCE CODE:
import numpy as np
model = Sequential()
[Link](Dense(1, activation='sigmoid'))
[Link](loss='mean_squared_error', optimizer='adam',metrics=['binary_accuracy'])
print([Link](training_data).round())
OUTPUT:
Epoch 1/500
......
Epoch 500/500
[[0.]
[1.]
[1.]
[0.]]
RESULT
Thus the python program to solve the XOR problem using DNN has been verified and executed
successfully.
10
EX NO : 2 Character Recognition Using CNN
DATE:
ALGORITHM:
Step 1: Import the necessary libraries, including Keras, OpenCV, and other relevant ones.
Step 2: Read the dataset which contains handwritten characters. This dataset should be in a
CSV file with pixel values for each character image.
Step 3: Split the dataset into training and testing data. Also, reshape the data to be in the
form of images (28x28 pixels).
Step 4: Visualize the dataset, displaying the number of examples for each alphabet.
Step 5: Shuffle the training data for better training performance.
Step 6: Reshape the training and test data to fit the model's input requirements.
Step 7: Convert the labels (characters) into one-hot encoded format for classification.
Step 8: Define a Convolutional Neural Network (CNN) model using Keras. The model
should have convolutional layers, pooling layers, and fully connected layers.
Step 9: Compile the model with an optimizer, loss function, and evaluation metric.
Step 10: Implement callbacks like learning rate reduction and early stopping.
Step 11: Train the model using the training data, and evaluate it using the test data.
Step 12: Save the trained model to a file for future use.
Step 13: Display the training and validation accuracies and losses to evaluate the model's
performance.
Step 14: Make predictions using the trained model, both on test images and an external
image.
Step 15: Display the predictions for test images and the external image, including
visualization.
11
ARCHITECTURE DIAGRAM:
12
PROGRAM/SOURCE CODE:
import [Link] as plt
from [Link] import Sequential
from [Link] import Dense, Flatten, Conv2D, MaxPool2D, Dropout
from [Link] import SGD, Adam
from [Link] import ReduceLROnPlateau, EarlyStopping
from [Link] import to_categorical, shuffle
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
data = pd.read_csv("/content/drive/MyDrive/A_Z Handwritten [Link]").astype('float32')
X = [Link]('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 = [Link](train_x.values, (train_x.shape[0], 28, 28))
test_x = [Link](test_x.values, (test_x.shape[0], 28, 28))
print("Train data shape: ", train_x.shape)
print("Test data shape: ", test_x.shape)
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'}
train_yint = np.int0(y)
count = [Link](26, dtype='int')
for i in train_yint:
count[i] += 1
alphabets = []
for i in word_dict.values():
[Link](i)
fig, ax = [Link](1, 1, figsize=(10, 10))
[Link](alphabets, count)
13
[Link]("Number of elements")
[Link]("Alphabets")
[Link]()
[Link]()
shuff = shuffle(train_x[:100])
fig, ax = [Link](3, 3, figsize=(10, 10))
axes = [Link]()
for i in range(9):
axes[i].imshow([Link](shuff[i], (28, 28)), cmap="Greys")
[Link]()
train_X = train_x.reshape(train_x.shape[0], train_x.shape[1], train_x.shape[2], 1)
print("New shape of train data: ", train_X.shape)
test_X = test_x.reshape(test_x.shape[0], test_x.shape[1], test_x.shape[2], 1)
print("New shape of train data: ", test_X.shape)
train_yOHE = to_categorical(train_y, num_classes=26, dtype='int')
print("New shape of train labels: ", train_yOHE.shape)
test_yOHE = to_categorical(test_y, num_classes=26, dtype='int')
print("New shape of test labels: ", test_yOHE.shape)
model = Sequential()
[Link](Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28,
1))
[Link](MaxPool2D(pool_size=(2, 2), strides=2)
[Link](Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same'))
[Link](MaxPool2D(pool_size=(2, 2), strides=2)
[Link](Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='valid'))
[Link](MaxPool2D(pool_size=(2, 2), strides=2)
[Link](Flatten())
[Link](Dense(64, activation="relu"))
[Link](Dense(128, activation="relu"))
[Link](Dense(26, activation="softmax"))
14
[Link](optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy',
metrics=['accuracy'])
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=1,
min_lr=0.0001)
early_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=2, verbose=0,
mode='auto')
history = [Link](train_X, train_yOHE, epochs=1, callbacks=[reduce_lr, early_stop],
validation_data=(test_X, test_yOHE))
[Link]()
[Link](r'model_hand.h5')
print("The validation accuracy is:", [Link]['val_accuracy'])
print("The training accuracy is:", [Link]['accuracy'])
print("The validation loss is:", [Link]['val_loss'])
print("The training loss is:", [Link]['loss'])
pred = [Link](test_X[:9])
print(test_X.shape)
fig, axes = [Link](3, 3, figsize=(8, 9))
axes = [Link]()
for i, ax in enumerate(axes):
img = [Link](test_X[i], (28, 28))
[Link](img, cmap="Greys")
pred = word_dict[[Link](test_yOHE[i]]
ax.set_title("Prediction: " + pred)
[Link]()
import cv2
from [Link] import cv2_imshow as cv
img = [Link]("/content/[Link]")
img_copy = [Link]()
img = [Link](img, cv2.COLOR_BGR2RGB)
img = [Link](img, (400,440))
15
img_copy = [Link](img_copy, (7,7), 0)
img_gray = [Link](img_copy, cv2.COLOR_BGR2GRAY)
_, img_thresh = [Link](img_gray, 100, 255, cv2.THRESH_BINARY_INV)
img_final = [Link](img_thresh, (28,28))
img_final =[Link](img_final, (1,28,28,1))
img_pred = word_dict[[Link]([Link](img_final))]
[Link](img, "Dataflair _ _ _ ", (20,25), cv2.FONT_HERSHEY_TRIPLEX, 0.7, color
= (0,0,230))
[Link](img, "Prediction: " + img_pred, (20,410), cv2.FONT_HERSHEY_DUPLEX,
1.3, color = (255,0,30))
cv(img)
OUTPUT:
Train data shape: (297960, 28, 28)
Test data shape: (74490, 28, 28)
16
max_pooling2d_1(MaxPooling2D) (None, 13, 13, 32) 0
conv2d_2(Conv2D) (None, 13, 13, 64) 18496
max_pooling2d_2(MaxPooling2D) (None, 6, 6, 64) 0
conv2d_2(Conv2D) (None, 4, 4, 128) 73856
max_pooling2d_3(MaxPooling2D) (None, 2, 2, 128) 0
flatten_1(Flatten) (None, 512) 0
dense_1(Dense) (None, 64) 32832
dense_2(Dense) (None, 128) 8320
dense_3(Dense) (None, 26) 3354
Total params: 137178 (535.85 KB)
Trainable params: 137178 (535.85 KB)
Non-trainable params: 0 (0.00 Byte)
The validation accuracy is : [0.9774869084358215]
The training accuracy is : [0.957313060760498]
The validation loss is : [0.07933821529150009]
The training loss is : [0.1571635603904724]
1/1 [==============================] - 0s 114ms/step
(74490, 28, 28, 1)
RESULT:
Thus, python program to perform Character Recognition using CNN has been executed successfully and
verified.
17
EX NO : 3 Face recognition using CNN
DATE:
ALGORITHM:
Step 1: Import the necessary libraries for working with image data and deep learning.
Step 2: Define the path to the directory containing training images.
Step 3: Create data generators for training and testing images, applying data augmentation to the training
set.
Step 4: Load and prepare the training and testing image data using the data generators.
Step 5: Create a lookup table to map class indices to class names for the faces in the training set and save
it as a pickle file.
Step 6: Define the number of output neurons based on the number of unique faces in the training set.
Step 7: Create a Convolutional Neural Network (CNN) model using Keras.
Step 8: Define the architecture of the CNN model, including convolutional layers, pooling layers, and
dense layers.
Step 9: Compile the CNN model, specifying the loss function, optimizer, and metrics.
Step 10: Train the CNN model using the training data, specifying the number of epochs and using the
validation data for model evaluation.
Step 11: Calculate and print the total time taken for training.
Step 12: Save the trained model as an HDF5 file.
Step 13: Prepare an image for making a single prediction.
Step 14: Load the saved model.
Step 15: Make a single prediction using the loaded model and the test image.
Step 16: Display the predicted face label.
18
ARCHITECTUR DIAGRAM:
19
PROGRAM/SOURCE CODE:
TrainingImagePath='/content/drive/MyDrive/Face Images/Final Training Images'
from [Link] import ImageDataGenerator
train_datagen = ImageDataGenerator(shear_range=0.1,zoom_range=0.1,horizontal_flip=True)
test_datagen = ImageDataGenerator()
training_set = train_datagen.flow_from_directory( TrainingImagePath,target_size=(64,
64),batch_size=32,class_mode='categorical')
test_set = test_datagen.flow_from_directory(TrainingImagePath,target_size=(64,
64),batch_size=32,class_mode='categorical')
test_set.class_indices
'''############ Creating lookup table for all faces ############'''
TrainClasses=training_set.class_indices
ResultMap={}
for faceValue,faceName in zip([Link](),[Link]()):
ResultMap[faceValue]=faceName
import pickle
with open("[Link]", 'wb') as fileWriteStream:
[Link](ResultMap, fileWriteStream)
print("Mapping of Face and its ID",ResultMap)
OutputNeurons=len(ResultMap)
print('\n The Number of output neurons: ', OutputNeurons)
from [Link] import Sequential
from [Link] import Convolution2D
from [Link] import MaxPool2D
from [Link] import Flatten
from [Link] import Dense
classifier= Sequential()
[Link](Convolution2D(32, kernel_size=(5, 5), strides=(1, 1), input_shape=(64,64,3),
activation='relu'))
[Link](MaxPool2D(pool_size=(2,2)))
[Link](Convolution2D(64, kernel_size=(5, 5), strides=(1, 1), activation='relu'))
[Link](MaxPool2D(pool_size=(2,2)))
[Link](Flatten())
[Link](Dense(64, activation='relu'))
[Link](Dense(OutputNeurons, activation='softmax'))
#[Link](loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
[Link](loss='categorical_crossentropy', optimizer = 'adam', metrics=["accuracy"])
import time
StartTime=[Link]()
classifier.fit_generator(
training_set,
epochs=15,
validation_data=test_set,
validation_steps=10)
EndTime=[Link]()
print("###### Total Time Taken: ", round((EndTime-StartTime)/60), 'Minutes ######')
[Link]("model.h5")
import numpy as np
from [Link] import load_img,img_to_array
from [Link] import load_model
ImagePath='/content/drive/MyDrive/Face Images/Final Testing Images/face16/[Link]'
test_image=load_img(ImagePath,target_size=(64, 64))
test_image=img_to_array(test_image)
20
test_image=np.expand_dims(test_image,axis=0)
model = load_model('model.h5')
result=[Link](test_image,verbose=0)
print('Prediction is: ',ResultMap[[Link](result)])
OUTPUT:
Found 244 images belonging to 16 classes.
Mapping of Face and its ID {0: 'face1', 1: 'face10', 2: 'face11', 3: 'face12', 4: 'face13', 5: 'face14', 6:
'face15', 7: 'face16', 8: 'face2', 9: 'face3', 10: 'face4', 11: 'face5', 12: 'face6', 13: 'face7', 14: 'face8', 15: 'face9'}
The Number of output neurons: 16
Epoch 1/15
8/8 [==============================] - ETA: 0s - loss: 49.2011 - accuracy: 0.0738
8/8 [==============================] - 143s 17s/step - loss: 49.2011 - accuracy: 0.0738 -
val_loss: 3.7933 - val_accuracy: 0.0697
Epoch 2/15
8/8 [==============================] - 4s 545ms/step - loss: 3.0177 - accuracy: 0.1148
Epoch 3/15
8/8 [==============================] - 3s 321ms/step - loss: 2.4849 - accuracy: 0.2459
Epoch 4/15
8/8 [==============================] - 3s 310ms/step - loss: 1.6110 - accuracy: 0.5205
Epoch 5/15
8/8 [==============================] - 3s 385ms/step - loss: 0.7985 - accuracy: 0.7746
Epoch 6/15
8/8 [==============================] - 3s 315ms/step - loss: 0.3206 - accuracy: 0.9180
Epoch 7/15
8/8 [==============================] - 3s 324ms/step - loss: 0.2027 - accuracy: 0.9508
Epoch 8/15
8/8 [==============================] - 3s 350ms/step - loss: 0.2367 - accuracy: 0.9262
Epoch 9/15
8/8 [==============================] - 4s 522ms/step - loss: 0.0977 - accuracy: 0.9877
Epoch 10/15
8/8 [==============================] - 3s 339ms/step - loss: 0.0843 - accuracy: 0.9795
Epoch 11/15
8/8 [==============================] - 3s 347ms/step - loss: 0.0608 - accuracy: 0.9836
Epoch 12/15
8/8 [==============================] - 4s 484ms/step - loss: 0.0446 - accuracy: 0.9877
Epoch 13/15
8/8 [==============================] - 4s 410ms/step - loss: 0.0549 - accuracy: 0.9877
Epoch 14/15
8/8 [==============================] - 3s 306ms/step - loss: 0.0242 - accuracy: 0.9918
Epoch 15/15
8/8 [==============================] - 3s 313ms/step - loss: 0.0194 - accuracy: 0.9959
###### Total Time Taken: 5 Minutes ######
Prediction is: face16
RESULT:
Thus, the python program to perform Face Recognition using CNN has been executed and
verified successfully.
21
EX NO : 4 LANGUAGE MODELLING USING RNN
DATE:
AIM: Write a python program to perform Language Modelling using RNN.
ALGORITHM:
Step 1: Import necessary libraries for text preprocessing and building the neural network model.
Step 2: Prepare the source text.
Step 3: Tokenize the text using a tokenizer to convert it into numerical values.
Step 4: Determine vocabulary size by counting the unique words in the tokenizer's word index.
Step 5: Create word -> word sequences
Step 6: Split sequences into X and y
Step 7: Convert the output (y) into one-hot encoded format
Step 8: Create a neural network model with an embedding layer, an LSTM layer, and a dense layer with a
softmax activation function
Step 9: Compile the model with appropriate loss and optimizer settings.
Step 10: Train the model on the X and y data for a specified number of epochs.
Step 11: Generate text.
22
ARCHITECTURE DIAGRAM:
23
PROGRAM/SOURCE CODE:
from [Link] import Tokenizer
from [Link] import to_categorical
import numpy as n
# source text
data = """ Jack and Jill went up the hill\n
To fetch a pail of water\n
Jack fell down and broke his crown\n
And Jill came tumbling after\n """
# integer encode text
tokenizer = Tokenizer()
tokenizer.fit_on_texts([data])
encoded = tokenizer.texts_to_sequences([data])[0]
# determine the vocabulary size
vocab_size = len(tokenizer.word_index) + 1
print('Vocabulary Size: %d' % vocab_size)
# create word -> word sequences
sequences = list()
for i in range(1, len(encoded)):
sequence = encoded[i-1:i+1]
[Link](sequence)
print('Total Sequences: %d' % len(sequences))
# split into X and y elements
sequences = [Link](sequences)
X, y = sequences[:,0],sequences[:,1]
# one hot encode outputs
y = to_categorical(y, num_classes=vocab_size)
# define model
from [Link] import Sequential
from [Link] import Embedding,LSTM,Dense
model = Sequential()
[Link](Embedding(vocab_size, 10, input_length=1))
[Link](LSTM(50))
[Link](Dense(vocab_size, activation='softmax'))
print([Link]())
# compile network
[Link](loss='categorical_crossentropy', optimizer='adam',
metrics=['accuracy'],run_eagerly=True)
# fit network
[Link](X, y, epochs=500, verbose=2)
# evaluate
in_text = 'Jill'
print(in_text)
encoded = tokenizer.texts_to_sequences([in_text])[0]
encoded = [Link](encoded)
yhat = [Link](encoded, verbose=0)
yhat = [Link](yhat,axis=1)
for word, index in tokenizer.word_index.items():
if index == (yhat):
print(word)
24
OUTPUT:
Vocabulary Size: 22
Total Sequences: 24
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 1, 10) 220
=================================================================
Total params: 13542 (52.90 KB)
Trainable params: 13542 (52.90 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
None
Epoch 1/500
1/1 - 0s - loss: 3.0915 - accuracy: 0.0000e+00 - 83ms/epoch - 83ms/step
Epoch 2/500
1/1 - 0s - loss: 0.2685 - accuracy: 0.8750 - 41ms/epoch - 41ms/step
.
.
.
Epoch 499/500
1/1 - 0s - loss: 0.2395 - accuracy: 0.8750 - 40ms/epoch - 40ms/step
Epoch 500/500
1/1 - 0s - loss: 0.2392 - accuracy: 0.8750 - 50ms/epoch - 50ms/step
Jill
went
RESULT:
Thus, the python program to perform Language Modelling using RNN has been executed and verified
successfully.
25
EX NO : 5 SENTIMENT ANALYSIS USING LSTM
DATE:
AIM: Write a python program to perform Sentiment Analysis using LSTM.
ALGORITHM:
Step 1: Import Necessary Libraries and Dependencies and Define constants like `VOCAB_SIZE`,
Step 2: Load the IMDb movie reviews dataset using `imdb.load_data()` and specify the vocabulary size
Step 3: Create a Sequential model, - Add an embedding layer. Add a SimpleRNN, Add a Dense layer
Step 7: Access the word index from the IMDb dataset and display a subset of word-index pairs.
Step 9 Define a function to predict sentiment for a given text using the trained model
Step 10: Use the "Sentiment Prediction Function" to predict the sentiment
26
ARCHITECTURE DIAGRAM:
27
PROGRAM/SOURCE CODE:
#!pip install keras
from [Link] import imdb
from [Link] import pad_sequences
import keras
import tensorflow as tf
import numpy as np
VOCAB_SIZE = 88584
MAXLEN = 250
BATCH_SIZE = 64
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words = VOCAB_SIZE)
len(train_data[1])
train_data=pad_sequences(train_data,MAXLEN)
test_data=pad_sequences(test_data,MAXLEN)
len(train_data[1])
from [Link] import Sequential
from [Link] import Embedding
from [Link] import SimpleRNN,Dense
model = Sequential()
[Link](Embedding(VOCAB_SIZE, 32))
[Link](SimpleRNN(32))
[Link](Dense(1, activation='sigmoid'))
[Link](optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
[Link](loss="binary_crossentropy",optimizer="rmsprop",metrics=['accuracy'])
history=[Link](train_data,train_labels,epochs=10,validation_split=0.2)
results=[Link](test_data,test_labels)
print(results)
word_index=imdb.get_word_index()
for i in range(10):
print(list(word_index.keys())[i],':',list(word_index.values())[i])
def encode_text(text):
tokens=[Link].text_to_word_sequence(text)
tokens=[word_index[word] if word in word_index else 0 for word in tokens]
return pad_sequences([tokens],MAXLEN)[0]
reverse_word_index={value:key for (key,value) in word_index.items()}
def decode_integers(integers):
PAD=0
text=""
for num in integers:
if num!=PAD:
text+=reverse_word_index[num] +" "
return text[:-1]
def predict(text):
encoded_text=encode_text(text)
pred=encoded_text.reshape(1,250) #converting vector to 2d
result=[Link](pred)
print(result[0])
text="that movie was amazing, i have to watch it again"
encoded=encode_text(text)
print(encoded)
print(decode_integers(encoded))
positive_review="That was a good movie, i will definitely watch it again"
predict(positive_review)
28
negative_review="Don't waste your time watching this movie, so disappointing"
predict(negative_review)
OUTPUT:
17464789/17464789 [==============================] - 0s 0us/step
Epoch 1/10
625/625 [==============================] - 54s 84ms/step - loss: 0.5582 - accuracy: 0.6973 -
val_loss: 0.4048 - val_accuracy: 0.8320
Epoch 10/10
625/625 [==============================] - 41s 66ms/step - loss: 0.0358 - accuracy: 0.9887 -
val_loss: 0.6894 - val_accuracy: 0.8118
782/782 [==============================] - 12s 15ms/step - loss: 0.7316 - accuracy: 0.8046
[0.7316193580627441, 0.8046000003814697]
1641221/1641221 [==============================] - 0s 0us/step
fawn : 34701
tsukino : 52006
nunnery : 52007
sonja : 16816
vani : 63951
woods : 1408
spiders : 16115
hanging : 2345
woody : 2289
trawling : 52008
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 12 17 13 477 10 25 5 103 9 171]
that movie was amazing i have to watch it again
1/1 [==============================] - 0s 177ms/step
[0.22035994]
1/1 [==============================] - 0s 24ms/step
[0.00497142]
RESULT:
Thus, the python program to perform Sentiment Analysis using LSTM has been executed and verified
successfullly.
29
EX NO : 6 SENTIMENT ANALYSIS USING LSTM
DATE:
ALGORITHM:
Step 1: Data Preparation
- Define input and target text sequences (`input_texts` and `target_texts`).
- Create sets of unique words and POS tags in the dataset (`input_words` and `target_words`).
- Add special tokens like `<sos>` (start of sentence) and `<eos>` (end of sentence) to `target_words`.
- Create dictionaries to map words and POS tags to integers (`input_word2idx`, `input_idx2word`,
`target_word2idx`, and `target_idx2word`).
30
ARCHITECTURE DIAGRAM:
31
PROGRAM/SOURCE CODE:
import numpy as np
import tensorflow as tf
from [Link] import Model
from [Link] import Input, LSTM, Dense
from [Link] import pad_sequences
# Define the input and output sequences
input_texts = ['I love coding', 'This is a pen', 'She sings well']
target_texts = ['PRP VB NNP', 'DT VBZ DT NN', 'PRP VBZ RB']
# Create a set of all unique words and POS tags in the dataset
input_words = set()
target_words = set()
for input_text, target_text in zip(input_texts, target_texts):
input_words.update(input_text.split())
target_words.update(target_text.split())
# Add <sos> and <eos> tokens to target_words
target_words.add('<sos>')
target_words.add('<eos>')
# Create dictionaries to map words and POS tags to integers
input_word2idx = {word: idx for idx, word in enumerate(input_words)}
input_idx2word = {idx: word for idx, word in enumerate(input_words)}
target_word2idx = {word: idx for idx, word in enumerate(target_words)}
target_idx2word = {idx: word for idx, word in enumerate(target_words)}
# Define the maximum sequence lengths
max_encoder_seq_length = max([len([Link]()) for text in input_texts])
max_decoder_seq_length = max([len([Link]()) for text in target_texts])
# Prepare the encoder input data
encoder_input_data = [Link]((len(input_texts), max_encoder_seq_length),
dtype='float32')
for i, input_text in enumerate(input_texts):
for t, word in enumerate(input_text.split()):
encoder_input_data[i, t] = input_word2idx[word]
# Prepare the decoder input and target data
decoder_input_data = [Link]((len(input_texts), max_decoder_seq_length),
dtype='float32')
decoder_target_data = [Link]((len(input_texts), max_decoder_seq_length,
len(target_words)), dtype='float32')
for i, target_text in enumerate(target_texts):
for t, word in enumerate(target_text.split()):
decoder_input_data[i, t] = target_word2idx[word]
if t > 0:
decoder_target_data[i, t - 1, target_word2idx[word]] = 1.0
# Define the encoder input and LSTM layers
encoder_inputs = Input(shape=(None,))
encoder_embedding = [Link](len(input_words), 256)(encoder_inputs)
32
encoder_lstm = LSTM(256, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding)
encoder_states = [state_h, state_c]
# Define the decoder input and LSTM layers
decoder_inputs = Input(shape=(None,))
decoder_embedding = [Link](len(target_words), 256)(decoder_inputs)
decoder_lstm = LSTM(256, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_embedding,
initial_state=encoder_states)
decoder_dense = Dense(len(target_words), activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# Define the model
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# Compile and train the model
[Link](optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
[Link]([encoder_input_data, decoder_input_data], decoder_target_data,
batch_size=64, epochs=50, validation_split=0.2)
# Define the encoder model to get the encoder states
encoder_model = Model(encoder_inputs, encoder_states)
# Define the decoder model with encoder states as initial state
decoder_state_input_h = Input(shape=(256,))
decoder_state_input_c = Input(shape=(256,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_embedding,
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)
# Define a function to perform inference and generate POS tags
def generate_pos_tags(input_sequence):
states_value = encoder_model.predict(input_sequence)
target_sequence = [Link]((1, 1))
target_sequence[0, 0] = target_word2idx['<sos>']
stop_condition = False
pos_tags = []
while not stop_condition:
output_tokens, h, c = decoder_model.predict([target_sequence] + states_value)
sampled_token_index = [Link](output_tokens[0, -1, :])
sampled_word = target_idx2word[sampled_token_index]
pos_tags.append(sampled_word)
if sampled_word == '<eos>' or len(pos_tags) > max_decoder_seq_length:
stop_condition = True
target_sequence = [Link]((1, 1))
33
target_sequence[0, 0] = sampled_token_index
states_value = [h, c]
return ' '.join(pos_tags)
for input_text in input_texts:
input_seq = pad_sequences([[input_word2idx[word] for word in input_text.split()]],
maxlen=max_encoder_seq_length)
predicted_pos_tags = generate_pos_tags(input_seq)
print('Input:', input_text)
print('Predicted POS Tags:', predicted_pos_tags)
OUTPUT:
Epoch 1/50
1/1 [==============================] - 7s 7s/step - loss: 1.3722 - accuracy:
0.0000e+00 - val_loss: 1.0973 - val_accuracy: 0.0000e+00
Epoch 50/50
1/1 [==============================] - 0s 73ms/step - loss: 0.0997 - accuracy:
0.6250 - val_loss: 2.3639 - val_accuracy: 0.0000e+00
1/1 [==============================] - 0s 432ms/step
1/1 [==============================] - 0s 438ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 22ms/step
Input: I love coding
Predicted POS Tags: VB NNP NNP NN NN
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 22ms/step
Input: This is a pen
Predicted POS Tags: VBZ DT NN NN NN
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 37ms/step
1/1 [==============================] - 0s 41ms/step
1/1 [==============================] - 0s 39ms/step
1/1 [==============================] - 0s 35ms/step
1/1 [==============================] - 0s 45ms/step
Input: She sings well
Predicted POS Tags: VB NNP NNP NN NN
RESULT:
Thus the python program to perform POS using Sequence to Sequence model has been executed and
verified successfully.
34
EX NO : 7 Machine Translation using Encoder-Decoder
DATE:
AIM: Write a python program to perform Sentiment Analysis using LSTM.
ALGORITHM:
Step 1: Create sets of unique words in the input and target sequences.
Step 2: Determine the maximum sequence lengths for encoder and decoder sequences.
Step 3: Prepare Encoder Input Data
- Tokenize input texts.
- Map words to their integer representations.
- Pad sequences to ensure uniform length.
Step 4: Prepare Decoder Input and Target Data
- Tokenize target texts.
- Map words to their integer representations.
- Create one-hot encoded target data for training.
Step 5: Model Building
- Define the architecture of the model, including an Encoder model and a Decoder model.
- Use embedding layers, LSTM layers, and a Dense layer for predictions.
Step 6: Model Compilation and Training
- Compile the model with an appropriate optimizer and loss function.
- Train the model using encoder input data, decoder input data, and decoder target data.
Step 7: Inference Model Setup
- Define an Encoder model to encode input sequences.
- Define a Decoder model to generate translations with initial states provided by the Encoder model.
Step 8: Translation Function
- Create a function to generate translations for input sequences.
Step 9: Test the Model
- Loop through input texts, encode them, and generate translations.
- Print the input text and the translated text.
35
ARCHITECTURE DIAGRAM:
36
ROGRAM/SOURCE CODE:
import numpy as np
import tensorflow as tf
from [Link] import Model
from [Link] import Input, LSTM, Dense
from [Link] import pad_sequences
# Define the input and output sequences
input_texts = ['I love coding', 'This is a pen', 'She sings well']
target_texts = ['Ich liebe das Coden', 'Das ist ein Stift', 'Sie singt gut']
# Create a set of all unique words in the input and target sequences
input_words = set()
target_words = set()
for input_text, target_text in zip(input_texts, target_texts):
input_words.update(input_text.split())
target_words.update(target_text.split())
# Add <sos> and <eos> tokens to target_words
target_words.add('<sos>')
target_words.add('<eos>')
# Create dictionaries to map words to integers
input_word2idx = {word: idx for idx, word in enumerate(input_words)}
input_idx2word = {idx: word for idx, word in enumerate(input_words)}
target_word2idx = {word: idx for idx, word in enumerate(target_words)}
target_idx2word = {idx: word for idx, word in enumerate(target_words)}
# Define the maximum sequence lengths
max_encoder_seq_length = max([len([Link]()) for text in input_texts])
max_decoder_seq_length = max([len([Link]()) for text in target_texts])
# Prepare the encoder input data
encoder_input_data = [Link]((len(input_texts), max_encoder_seq_length),
dtype='float32')
for i, input_text in enumerate(input_texts):
for t, word in enumerate(input_text.split()):
encoder_input_data[i, t] = input_word2idx[word]
# Prepare the decoder input and target data
decoder_input_data = [Link]((len(input_texts), max_decoder_seq_length),
dtype='float32')
decoder_target_data = [Link]((len(input_texts), max_decoder_seq_length,
len(target_words)), dtype='float32')
for i, target_text in enumerate(target_texts):
for t, word in enumerate(target_text.split()):
decoder_input_data[i, t] = target_word2idx[word]
if t > 0:
decoder_target_data[i, t - 1, target_word2idx[word]] = 1.0
# Define the encoder input and LSTM layers
encoder_inputs = Input(shape=(None,))
encoder_embedding = [Link](len(input_words), 256)(encoder_inputs)
37
encoder_lstm = LSTM(256, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding)
encoder_states = [state_h, state_c]
# Define the decoder input and LSTM layers
decoder_inputs = Input(shape=(None,))
decoder_embedding = [Link](len(target_words), 256)(decoder_inputs)
decoder_lstm = LSTM(256, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_embedding,
initial_state=encoder_states)
decoder_dense = Dense(len(target_words), activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# Define the model
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# Compile and train the model
[Link](optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
[Link]([encoder_input_data, decoder_input_data], decoder_target_data,
batch_size=64, epochs=50, validation_split=0.2)
# Define the encoder model to get the encoder states
encoder_model = Model(encoder_inputs, encoder_states)
# Define the decoder model with encoder states as initial state
decoder_state_input_h = Input(shape=(256,))
decoder_state_input_c = Input(shape=(256,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_embedding,
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)
# Define a function to perform inference and generate translations
def translate(input_sequence):
states_value = encoder_model.predict(input_sequence)
target_sequence = [Link]((1, 1))
target_sequence[0, 0] = target_word2idx['<sos>']
stop_condition = False
translation = []
while not stop_condition:
output_tokens, h, c = decoder_model.predict([target_sequence] +
states_value)
sampled_token_index = [Link](output_tokens[0, -1, :])
sampled_word = target_idx2word[sampled_token_index]
[Link](sampled_word)
if sampled_word == '<eos>' or len(translation) > max_decoder_seq_length:
stop_condition = True
38
target_sequence = [Link]((1, 1))
target_sequence[0, 0] = sampled_token_index
states_value = [h, c]
return ' '.join(translation)
# Test the model
for input_text in input_texts:
input_seq = pad_sequences([[input_word2idx[word] for word in input_text.split()]],
maxlen=max_encoder_seq_length)
translated_text = translate(input_seq)
print('Input:', input_text)
print('Translated Text:', translated_text)
print()
39
OUTPUT:
Epoch 1/50
1/1 [==============================] - 5s 5s/step - loss: 1.9205 - accuracy:
0.1250 - val_loss: 1.2845 - val_accuracy: 0.0000e+00
Epoch 50/50
1/1 [==============================] - 0s 126ms/step - loss: 0.0367 - accuracy:
0.7500 - val_loss: 6.0486 - val_accuracy: 0.0000e+00
1/1 [==============================] - 0s 441ms/step
1/1 [==============================] - 0s 409ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
Input: I love coding
Translated Text: liebe das Coden Coden ist
1/1 [==============================] - 0s 27ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
Input: This is a pen
Translated Text: ist ein Stift Stift ist
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
Input: She sings well
Translated Text: liebe das Coden ist ist
RESULT:
Thus the python program to perform Machine Translation using Encoder and Decoder has
been executed and verified successfully.
40
EX NO : 8 IMAGE AUGMENTATION USING GAN
DATE:
AIM: Write a python program to perform Image Augmentation using GAN
ALGORITHM:
Step 1: Load the training and testing data.
• Load the training data from the ../input/train directory.
• Load the testing data from the ../input/test directory.
Step 2: Define the generator model as a sequential neural network with three layers.
• A dense layer, A dense layer, A reshape layer
Step 3: Define the discriminator model as a sequential neural network with three layers:
• A flatten layer to flatten the input image.
• A dense layer with 128 units and a LeakyReLU activation function.
• A dense layer with one unit and a sigmoid activation function.
Step 3: Compile the discriminator model.
• Compile the discriminator model with the binary crossentropy loss function and the Adam optimizer.
Step 4: Compile the combined model.
• Create a sequential neural network with two inputs: the generator model and the discriminator model.
• The output of the combined model is the prediction of the discriminator model for the generated
image.
• Compile the combined model with the binary crossentropy loss function and the Adam optimizer.
Step 5: Train the generator and discriminator models together.
Step 6: Generate sample images from the generator model.
• Sample a batch of random noise.
• Pass the random noise to the generator model.
• Display the last generated image from the generator model.
41
ARCHITECTURE DIAGRAM:
42
PROGRAM/SOURCE CODE:
import sys, cv2, glob, os, time
import pandas as pd
import numpy as np
from [Link] import mnist
from [Link] import Input, Dense, Reshape, Flatten,Activation
from [Link].advanced_activations import LeakyReLU
from [Link] import Sequential, Model
from [Link] import Adam
import [Link] as plt
print([Link]("../input"))
%matplotlib inline
train_dir = "../input/train/train/"
test_dir = "../input/test/test/"
train_df = pd.read_csv('../input/[Link]')
img_rows = 32
img_cols = 32
channels = 3
img_shape = (img_rows, img_cols, channels)
z_dim = 100
def generator(img_shape, z_dim):
model = Sequential()
[Link](Dense(128, input_dim=z_dim))
[Link](LeakyReLU(alpha=0.01))
[Link](Dense(img_rows*img_cols*channels, activation='tanh'))
[Link](Reshape(img_shape))
z = Input(shape=(z_dim,))
img = model(z)
return Model(z, img)
def discriminator(img_shape):
model = Sequential()
[Link](Flatten(input_shape=img_shape))
[Link](Dense(128))
[Link](LeakyReLU(alpha=0.01))
[Link](Dense(1, activation='sigmoid'))
img = Input(shape=img_shape)
prediction = model(img)
return Model(img, prediction)
discriminator = discriminator(img_shape)
[Link](loss='binary_crossentropy', optimizer=Adam(), metrics=['accuracy'])
generator = generator(img_shape, z_dim)
z = Input(shape=(100,))
img = generator(z)
[Link] = False
prediction = discriminator(img)
combined = Model(z, prediction)
[Link](loss='binary_crossentropy', optimizer=Adam())
img_ = [Link]("../input/train/train/[Link]",1)
#img_ = [Link](img_,cv2.COLOR_BGR2GRAY)
[Link](img_)
def prepareTrainSet(train_df):
43
train_1 = train_df[train_df.has_cactus == 1]
train_0 = train_df[train_df.has_cactus == 0]
ids_1 = train_1.[Link]()
ids_0 = train_0.[Link]()
path = [Link]("../input/train/train/*.jpg")
imgs_0,imgs_1 = [],[]
for img in path:
im = [Link](img)
# uncomment next line while using single channel image
#im = [Link](im,cv2.COLOR_BGR2GRAY)
# uncomment next line if your want to scale image
#im = [Link](im,(80,65))
if [Link]("/")[-1] in ids_1:
imgs_1.append(im)
elif [Link]("/")[-1] in ids_0:
imgs_0.append(im)
X_train_0 = [Link](imgs_0)
X_train_1 = [Link](imgs_1)
X_train_0 = X_train_0 / 127.5 - 1.
X_train_1 = X_train_1 / 127.5 - 1.
# uncomment next two line while using single channel image
# X_train_0 = np.expand_dims(X_train_0, axis=3)
# X_train_1 = np.expand_dims(X_train_1, axis=3)
print(X_train_0.shape)
print(X_train_1.shape)
return X_train_0,X_train_1
losses = []
accuracies = []
def train(iterations, batch_size, sample_interval):
gen_images = []
X_train_0,X_train_1 = prepareTrainSet(train_df)
# Assign X_train to X_train_0 for augment non-cactus images
# Assign X_train to X_train_1 for augment cactus images
X_train = X_train_0
real = [Link]((batch_size, 1))
fake = [Link]((batch_size, 1))
for iteration in range(iterations):
idx = [Link](0, X_train.shape[0], batch_size)
imgs = X_train[idx]
z = [Link](0, 1, (batch_size, 100))
gen_imgs = [Link](z)
d_loss_real = discriminator.train_on_batch(imgs, real)
d_loss_fake = discriminator.train_on_batch(gen_imgs, fake)
d_loss = 0.5 * [Link](d_loss_real, d_loss_fake)
z = [Link](0, 1, (batch_size, 100))
gen_imgs = [Link](z)
g_loss = combined.train_on_batch(z, real)
if iteration % sample_interval == 0:
print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (iteration, d_loss[0], 100*d_loss[1],
g_loss))
[Link]((d_loss[0], g_loss))
[Link](100*d_loss[1])
44
gen_images.append(sample_images(iteration))
return gen_images
def sample_images(iteration, image_grid_rows=4, image_grid_columns=4):
z = [Link](0, 1,
(image_grid_rows * image_grid_columns, z_dim))
gen_imgs = [Link](z)
gen_imgs = 0.5 * gen_imgs + 0.5
fig, axs = [Link](image_grid_rows, image_grid_columns, figsize=(10,10), sharey=True,
sharex=True)
cnt = 0
for i in range(image_grid_rows):
for j in range(image_grid_columns):
axs[i,j].imshow(gen_imgs[cnt, :,:,0],)
axs[i,j].axis('off')
cnt += 1
return gen_imgs
import warnings; [Link]('ignore')
# Set iterations at least 10000 for good results
iterations = 1000
batch_size = 128
sample_interval = 1000
gen_imgs = train(iterations, batch_size, sample_interval)
#row -1 for lastly generated samples
row = -1
#columns -1 for last element of lastly generated samples
col = -1
[Link](gen_imgs[row][col])
OUTPUT:
Using TensorFlow backend.
['test', 'sample_submission.csv', '[Link]', 'train']
45
4364, 32, 32, 3)
(13136, 32, 32, 3)
0 [D loss: 0.704362, acc.: 54.30%] [G loss: 1.355417]
RESULT:
Thus the python program to perform Data Augmentation using GAN has been executed and verified
successfully.
46
EX NO : 9 MINI PROJECT
DATE:
AIM:
ABSTRACT:
DATASET USED:
ARCHITECTURE DIAGRAM:
MODEL DESCRIPTION:
PROGRAM/SOURCE CODE:
OUTPUT:
RESULT:
47
48
49