Ad3511 Deep Learning Laboratory
Ad3511 Deep Learning Laboratory
ARUNACHALA COLLEGE OF
ENGINEERING FOR WOMEN, MANAVILAI
DEPARTMENT OF ARTIFICIAL
INTELLIGENCE AND DATA SCIENCE
Register Number
Semester V
1
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
COURSE OBJECTIVES:
LIST OF EXPERIMENTS:
TOTAL: 60 PERIODS
COURSE OUTCOMES:
After the completion of this course, 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)
2
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
INDEX
Sl.
Date Program Name Mark Signature
No.
3
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Aim:
XOR logical function truth table for 2-bit binary variables, i.e, the input vector and the corresponding
output is,
X1 X2 Y
0 0 0
0 1 1
1 0 1
1 1 0
Procedure:
Program:
4
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
# Sigmoid Function
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# Forward Propagation
def forwardPropagation(X, Y, parameters):
m = X.shape[1]
W1 = parameters["W1"]
W2 = parameters["W2"]
b1 = parameters["b1"]
b2 = parameters["b2"]
Z1 = np.dot(W1, X) + b1
A1 = sigmoid(Z1)
Z2 = np.dot(W2, A1) + b2
A2 = sigmoid(Z2)
# Backward Propagation
def backwardPropagation(X, Y, cache):
m = X.shape[1]
(Z1, A1, W1, b1, Z2, A2, W2, b2) = cache
5
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
dZ2 = A2 - Y
dW2 = np.dot(dZ2, A1.T) / m
db2 = np.sum(dZ2, axis = 1, keepdims = True)
for i in range(epoch):
losses[i, 0], cache, A2 = forwardPropagation(X, Y, parameters)
gradients = backwardPropagation(X, Y, cache)
parameters = updateParameters(parameters, gradients, learningRate)
6
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
plt.show()
# Testing
X = np.array([[1, 1, 0, 0], [0, 1, 0, 1]]) # XOR input
cost, _, A2 = forwardPropagation(X, Y, parameters)
prediction = (A2 > 0.5) * 1.0
# print(A2)
print(prediction)
Output:
7
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
[[ 1. 0. 0. 1.]]
Result:
Thus the program for solving the XOR problem using DNN was implemented and executed successfully.
8
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Aim:
To write a python program to implement Character recognition using CNN.
Procedure:
1.Data Collection and Preprocessing
2.Model Architecture
3.Compile the Model
4.Model Training
5.Evaluate the Model
6.Fine-Tuning and Optimization
7.Character Recognition
8.Deployment
program:
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
# Load and preprocess the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0
X_test = X_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0
y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)
# Build the CNN model
9
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=(2, 2)),
Conv2D(64, kernel_size=(3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dropout(0.5),
Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
10
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.tight_layout()
plt.show()
# Example prediction
example_index = 0
example_image = X_test[example_index]
example_label = np.argmax(y_test[example_index])
predicted_label = np.argmax(model.predict(np.expand_dims(example_image, axis=0)))
plt.imshow(example_image.squeeze(), cmap='gray')
plt.title(f"True Label: {example_label}, Predicted Label: {predicted_label}")
plt.axis('off')
plt.show()
Epoch 1/10
469/469 [==============================] - 69s 142ms/step - loss: 0.2773 - accuracy: 0.9156 - val_loss: 0.0606 -
val_accuracy: 0.9801
Epoch 2/10
469/469 [==============================] - 71s 151ms/step - loss: 0.0928 - accuracy: 0.9729 - val_loss: 0.0420 -
val_accuracy: 0.9853
Epoch 3/10
469/469 [==============================] - 78s 166ms/step - loss: 0.0666 - accuracy: 0.9804 - val_loss: 0.0384 -
val_accuracy: 0.9878
Epoch 4/10
11
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
469/469 [==============================] - 58s 123ms/step - loss: 0.0550 - accuracy: 0.9832 - val_loss: 0.0295 -
val_accuracy: 0.9902
Epoch 5/10
469/469 [==============================] - 79s 167ms/step - loss: 0.0472 - accuracy: 0.9857 - val_loss: 0.0299 -
val_accuracy: 0.9900
Epoch 6/10
469/469 [==============================] - 29s 61ms/step - loss: 0.0409 - accuracy: 0.9878 - val_loss: 0.0268 - val_accuracy:
0.9904
Epoch 7/10
469/469 [==============================] - 24s 52ms/step - loss: 0.0365 - accuracy: 0.9886 - val_loss: 0.0280 - val_accuracy:
0.9911
Epoch 8/10
469/469 [==============================] - 25s 53ms/step - loss: 0.0315 - accuracy: 0.9904 - val_loss: 0.0257 - val_accuracy:
0.9915
Epoch 9/10
469/469 [==============================] - 22s 47ms/step - loss: 0.0287 - accuracy: 0.9908 - val_loss: 0.0284 - val_accuracy:
0.9913
Epoch 10/10
469/469 [==============================] - 22s 48ms/step - loss: 0.0259 - accuracy: 0.9917 - val_loss: 0.0252 - val_accuracy:
0.9914
Test Accuracy: 0.9914000034332275
12
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Result:
Thus a python program to implement character recognition using CNN was implemented and executed
successfully.
Aim:
To write a python program to implement face recognition using CNN.
Definition:
Convolutional Neural Networks are a special kind of Neural Networks which helps the machine to learn
and classify the images. A good example is Face Recognition.
Procedure:
13
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
● Import TensorFlow
● Download and prepare the dataset
● Verify the data
● Create the convolutional base
● Add Dense layers on top
● Compile and train the model
● Evaluate the model
● Print the test accuracy
Program:
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
In [ ]:
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False)
plt.imshow(train_images[i]) # The CIFAR labels happen to be arrays, # which is why you need the extra index
plt.xlabel(class_names[train_labels[i][0]]) plt.show()
14
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
='relu',
input_shape=(32, 32, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3),
activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3),
activation='relu'))
In [ ]:
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 30, 30, 32) 896
15
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
=================================================================
Total params: 56320 (220.00 KB)
Trainable params: 56320 (220.00 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
In [ ]:
model.add(layers.Flatten()) model.add(layers.Dense(64, activation='relu')) model.add(layers.Dense(10))
In [ ]:
model.summary()
conv2d_2 (Conv2D) (None, 4, 4, 64) 36928
=================================================================
Total params: 122570 (478.79 KB)
Trainable params: 122570 (478.79 KB)
Non-trainable params: 0 (0.00 Byte)
16
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
_________________________________________________________________
In [ ]:
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy']) history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images,
test_labels))
Epoch 1/10
1563/1563 [==============================] - 10s 4ms/step - loss: 1.5733 - accuracy: 0.4257 - val_loss:
1.2938 - val_accuracy: 0.5405
Epoch 2/10
1563/1563 [==============================] - 6s 4ms/step - loss: 1.1916 - accuracy: 0.5761 - val_loss:
1.1120 - val_accuracy: 0.6029
Epoch 3/10
1563/1563 [==============================] - 6s 4ms/step - loss: 1.0424 - accuracy: 0.6315 - val_loss:
1.0490 - val_accuracy: 0.6332
Epoch 4/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.9586 - accuracy: 0.6631 - val_loss:
0.9473 - val_accuracy: 0.6711
Epoch 5/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.8903 - accuracy: 0.6875 - val_loss:
0.9499 - val_accuracy: 0.6693
Epoch 6/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.8352 - accuracy: 0.7077 - val_loss:
0.9962 - val_accuracy: 0.6548
Epoch 7/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.7903 - accuracy: 0.7219 - val_loss:
0.9115 - val_accuracy: 0.6910
Epoch 8/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.7500 - accuracy: 0.7349 - val_loss:
0.8694 - val_accuracy: 0.6984
17
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Epoch 9/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.7134 - accuracy: 0.7494 - val_loss:
0.8856 - val_accuracy: 0.6930
Epoch 10/10
1563/1563 [==============================] - 6s 4ms/step - loss: 0.6781 - accuracy: 0.7627 - val_loss:
0.8593 - val_accuracy: 0.7105
In [ ]:
plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch') plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
18
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
In [ ]:
print(test_acc)
0.7105000019073486
Result:
Thus a python program to implement face recognition using CNN was implemented and executed
successfully.
Exp.No:4 Language modeling using RNN
Aim:
To write a python program to implement language modelling using RNN.
Procedure:
1. Convert abstracts from list of strings into list of lists of integers (sequences)
19
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Program:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.utils import np_utils
#How many timesteps e.g how many characters we want to process in one go
numberOfCharsToLearn = 100
#Since our time step sequence represents a process for every 100 chars we omit
#the first 100 chars so the loop runs a 100 less or there will be index out of
20
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
#range
counter = totalChars - numberOfCharsToLearn
#Inpput data
charX = []
#output data
y = []
#This loops through all the characters in the data skipping the first 100
for i in range(0, counter, 1):
#This one goes from 0-100 so it gets 100 values starting from 0 and stops
#just before the 100th value
theInputChars = data[i:i+numberOfCharsToLearn]
#With no : you start with 0, and so you get the actual 100th value
#Essentially, the output Chars is the next char in line for those 100 chars
#in X
theOutputChars = data[i + numberOfCharsToLearn]
#Appends every 100 chars ids as a list into X
charX.append([CharsForids[char] for char in theInputChars])
#For every 100 values there is one y value which is the output
y.append(CharsForids[theOutputChars])
#Len charX represents how many of those time steps we have
#Our features are set to 1 because in the output we are only predicting 1 char
#Finally numberOfCharsToLearn is how many character we process
X = np.reshape(charX, (len(charX), numberOfCharsToLearn, 1))
21
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
y = np_utils.to_categorical(y)
print(y)
model = Sequential()
#Since we know the shape of our Data we can input the timestep and feature data
#The number of timestep sequence are dealt with in the fit function
model.add(LSTM(256, input_shape=(X.shape[1], X.shape[2])))
model.add(Dropout(0.2))
#number of features on the output
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit(X, y, epochs=5, batch_size=128)
model.save_weights("Othello.hdf5")
#model.load_weights("Othello.hdf5")
22
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Output:
Epoch 1/5
128/1760 [=>............................] - ETA: 43s - loss: 3.3984
256/1760 [===>..........................] - ETA: 27s - loss: 3.3905
384/1760 [=====>........................] - ETA: 21s - loss: 3.3835
512/1760 [=======>......................] - ETA: 18s - loss: 3.3749
640/1760 [=========>....................] - ETA: 15s - loss: 3.3615
768/1760 [============>.................] - ETA: 13s - loss: 3.3425
896/1760 [==============>...............] - ETA: 11s - loss: 3.3174
1024/1760 [================>.............] - ETA: 9s - loss: 3.3563
23
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Result:
Thus a python program to implement language modelling using RNN was implemented and executed
successfully.
24
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Aim:
Procedure:
1. Loading data
5. Predicting
Program:
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.layers import Embedding, Dense, LSTM
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.sequence import pad_sequences
additional_metrics = ['accuracy']
batch_size = 128
embedding_output_dims = 15
loss_function = BinaryCrossentropy()
max_sequence_length = 300
num_distinct_words = 5000
number_of_epochs = 5
25
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
optimizer = Adam()
validation_split = 0.20
verbosity_mode = 1
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=num_distinct_words)
print(x_train.shape)
print(x_test.shape)
padded_inputs = pad_sequences(x_train, maxlen=max_sequence_length, value = 0.0)
padded_inputs_test = pad_sequences(x_test, maxlen=max_sequence_length, value = 0.0)
len(x_train[890])
len(padded_inputs[890])
model = Sequential()
model.add(Embedding(num_distinct_words, embedding_output_dims, input_length=max_sequence_length))
model.add(LSTM(10))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer=optimizer, loss=loss_function, metrics=additional_metrics)
model.summary()
Output:
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_4 (Embedding) (None, 300, 15) 75000
=================================================================
Total params: 76,051
Trainable params: 76,051
Non-trainable params: 0
26
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
_________________________________________________________________
test_results = model.evaluate(padded_inputs_test, y_test, verbose=False)
print(f'Test results - Loss: {test_results[0]} - Accuracy: {100*test_results[1]}%')
Output:
Test results - Loss: 0.6933784971427918 - Accuracy: 48.05600047111511%
Result:
Thus a python program to implement sentiment analysis using LSTM was implemented and executed
successfully.
27
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Aim:
To write a python program to implement Parts Of Speech tagging using Sequence to Sequence
architecture .
Procedure:
1.Data Preparation:
● Prepare your POS-tagged corpus where each sentence is paired with its corresponding POS tags.
2.Vocabulary Building:
● Create vocabularies for words and POS tags present in your corpus.
● Assign unique indices to each word and POS tag.
3.Encoding:
● Tokenize sentences into word indices and POS tag indices.
● Pad or truncate sequences to a fixed length to maintain uniform input shapes.
● Create an encoder model using Embedding layers to embed word indices and POS tag indices.
● Process the input through the encoder, which will generate a context vector.
4.Decoding:
● Design a decoder model to take the context vector and generate POS tag sequences.
● You might need to use teacher forcing (feeding ground-truth POS tags as inputs) during training.
● During inference, use the decoder to generate POS tag sequences by feeding the context vector and
previously generated tags as inputs.
5.Model Compilation and Training:
● Compile the Seq2Seq model with appropriate loss functions (e.g., categorical cross-entropy) and
optimizers (e.g., Adam).
● Train the model using your POS-tagged data. The decoder will predict POS tags based on the input
words and the context vector.
6.Evaluation:
● Evaluate the model on a separate test dataset to measure its POS tagging accuracy.
7.Inference:
● For inference, input a sentence to the encoder and then generate the corresponding POS tags using
the decoder.
8.Fine-Tuning and Optimization:
● Depending on the model's performance, experiment with hyperparameters, model architecture, and
training strategies.
9.Deployment (Optional):
● If the model performs well, you can deploy it for POS tagging tasks. However, traditional models
like CRF are usually more suitable for POS tagging due to their fixed input/output sequence
lengths.
Program:
28
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Input, Embedding, LSTM, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.utils import to_categorical
pos_tags = [
"DT NN VBZ VBG",
"DT NN VBZ VBG",
"PRP VBZ VBG DT NN"
]
# Tokenize sentences and POS tags
tokenizer = Tokenizer()
tokenizer.fit_on_texts(sentences + pos_tags)
input_sequences = tokenizer.texts_to_sequences(sentences)
output_sequences = tokenizer.texts_to_sequences(pos_tags)
vocab_size = len(tokenizer.word_index) + 1
# Pad sequences
max_sequence_length = max(max(len(seq) for seq in input_sequences), max(len(seq) for seq in
output_sequences))
input_sequences_padded = pad_sequences(input_sequences, maxlen=max_sequence_length, padding='post')
output_sequences_padded = pad_sequences(output_sequences, maxlen=max_sequence_length, padding='post')
29
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
# Encoder
encoder_input = Input(shape=(max_sequence_length,))
encoder_embedding = Embedding(vocab_size, latent_dim)(encoder_input)
encoder_lstm = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_embedding)
encoder_states = [state_h, state_c]
# Decoder
decoder_input = Input(shape=(max_sequence_length - 1,))
decoder_embedding = Embedding(vocab_size, latent_dim)(decoder_input)
decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=encoder_states)
decoder_dense = Dense(vocab_size, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# Generate predictions
encoder_model = Model(encoder_input, encoder_states)
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]
states_value = encoder_model.predict(input_seq_padded)
30
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
decoded_sentence = ''
stop_condition = False
while not stop_condition:
output_tokens, h, c = decoder_model.predict([target_seq] + states_value)
sampled_token_index = np.argmax(output_tokens[0, -1, :])
sampled_word = tokenizer.index_word[sampled_token_index]
if sampled_word != '<end>':
decoded_sentence += sampled_word + ' '
states_value = [h, c]
return decoded_sentence.strip()
Output:
Epoch 1/50
1/1 [==============================] - 22s 22s/step - loss: 2.7771 - accuracy: 0.0000e+00
Epoch 2/50
1/1 [==============================] - 0s 132ms/step - loss: 2.7341 - accuracy: 0.8333
Epoch 3/50
1/1 [==============================] - 0s 127ms/step - loss: 2.6894 - accuracy: 0.9167
Epoch 4/50
1/1 [==============================] - 0s 124ms/step - loss: 2.6386 - accuracy: 0.9167
Epoch 5/50
1/1 [==============================] - 0s 129ms/step - loss: 2.5771 - accuracy: 0.8333
Epoch 6/50
1/1 [==============================] - 0s 143ms/step - loss: 2.4998 - accuracy: 0.8333
Epoch 7/50
1/1 [==============================] - 0s 114ms/step - loss: 2.4000 - accuracy: 0.7500
31
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Epoch 8/50
1/1 [==============================] - 0s 113ms/step - loss: 2.2704 - accuracy: 0.5833
Epoch 9/50
1/1 [==============================] - 0s 189ms/step - loss: 2.1041 - accuracy: 0.5000
Epoch 10/50
1/1 [==============================] - 0s 76ms/step - loss: 1.9022 - accuracy: 0.5000
Epoch 11/50
1/1 [==============================] - 0s 55ms/step - loss: 1.6912 - accuracy: 0.4167
Epoch 12/50
1/1 [==============================] - 0s 52ms/step - loss: 1.5327 - accuracy: 0.3333
Epoch 13/50
1/1 [==============================] - 0s 71ms/step - loss: 1.4486 - accuracy: 0.5000
Epoch 14/50
1/1 [==============================] - 0s 59ms/step - loss: 1.3796 - accuracy: 0.5000
Epoch 15/50
1/1 [==============================] - 0s 56ms/step - loss: 1.2971 - accuracy: 0.7500
Epoch 16/50
1/1 [==============================] - 0s 60ms/step - loss: 1.2208 - accuracy: 0.7500
Epoch 17/50
1/1 [==============================] - 0s 58ms/step - loss: 1.1531 - accuracy: 0.6667
Epoch 18/50
1/1 [==============================] - 0s 62ms/step - loss: 1.0652 - accuracy: 0.8333
Epoch 19/50
1/1 [==============================] - 0s 55ms/step - loss: 0.9677 - accuracy: 0.6667
Epoch 20/50
1/1 [==============================] - 0s 59ms/step - loss: 0.8924 - accuracy: 0.6667
Epoch 21/50
1/1 [==============================] - 0s 64ms/step - loss: 0.8438 - accuracy: 0.7500
Epoch 22/50
1/1 [==============================] - 0s 65ms/step - loss: 0.7938 - accuracy: 0.8333
Epoch 23/50
1/1 [==============================] - 0s 52ms/step - loss: 0.7245 - accuracy: 0.9167
Epoch 24/50
1/1 [==============================] - 0s 47ms/step - loss: 0.6433 - accuracy: 0.9167
Epoch 25/50
1/1 [==============================] - 0s 64ms/step - loss: 0.5674 - accuracy: 0.9167
Epoch 26/50
1/1 [==============================] - 0s 53ms/step - loss: 0.5071 - accuracy: 0.9167
Epoch 27/50
1/1 [==============================] - 0s 52ms/step - loss: 0.4552 - accuracy: 1.0000
32
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Epoch 28/50
1/1 [==============================] - 0s 55ms/step - loss: 0.4024 - accuracy: 1.0000
Epoch 29/50
1/1 [==============================] - 0s 57ms/step - loss: 0.3485 - accuracy: 1.0000
Epoch 30/50
1/1 [==============================] - 0s 42ms/step - loss: 0.2988 - accuracy: 1.0000
Epoch 31/50
1/1 [==============================] - 0s 55ms/step - loss: 0.2579 - accuracy: 1.0000
Epoch 32/50
1/1 [==============================] - 0s 55ms/step - loss: 0.2250 - accuracy: 1.0000
Epoch 33/50
1/1 [==============================] - 0s 52ms/step - loss: 0.1960 - accuracy: 1.0000
Epoch 34/50
1/1 [==============================] - 0s 55ms/step - loss: 0.1683 - accuracy: 1.0000
Epoch 35/50
1/1 [==============================] - 0s 62ms/step - loss: 0.1421 - accuracy: 1.0000
Epoch 36/50
1/1 [==============================] - 0s 57ms/step - loss: 0.1191 - accuracy: 1.0000
Epoch 37/50
1/1 [==============================] - 0s 50ms/step - loss: 0.0997 - accuracy: 1.0000
Epoch 38/50
1/1 [==============================] - 0s 55ms/step - loss: 0.0834 - accuracy: 1.0000
Epoch 39/50
1/1 [==============================] - 0s 61ms/step - loss: 0.0698 - accuracy: 1.0000
Epoch 40/50
1/1 [==============================] - 0s 57ms/step - loss: 0.0589 - accuracy: 1.0000
Epoch 41/50
1/1 [==============================] - 0s 62ms/step - loss: 0.0500 - accuracy: 1.0000
Epoch 42/50
1/1 [==============================] - 0s 67ms/step - loss: 0.0427 - accuracy: 1.0000
Epoch 43/50
1/1 [==============================] - 0s 57ms/step - loss: 0.0366 - accuracy: 1.0000
Epoch 44/50
1/1 [==============================] - 0s 56ms/step - loss: 0.0315 - accuracy: 1.0000
Epoch 45/50
1/1 [==============================] - 0s 57ms/step - loss: 0.0271 - accuracy: 1.0000
Epoch 46/50
1/1 [==============================] - 0s 69ms/step - loss: 0.0234 - accuracy: 1.0000
Epoch 47/50
1/1 [==============================] - 0s 59ms/step - loss: 0.0203 - accuracy: 1.0000
33
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Epoch 48/50
1/1 [==============================] - 0s 48ms/step - loss: 0.0179 - accuracy: 1.0000
Epoch 49/50
1/1 [==============================] - 0s 60ms/step - loss: 0.0159 - accuracy: 1.0000
Epoch 50/50
1/1 [==============================] - 0s 70ms/step - loss: 0.0143 - accuracy: 1.0000
1/1 [==============================] - 2s 2s/step
Result:
Thus a python program to implement Parts of speech tagging using Sequence to Sequence architecture was
implemented and executed successfully.
34
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
1. Implement a deep convolutional autoencoder for image denoising, mapping noisy digits images
from the MNIST dataset to clean digits images.
2. This implementation is based on an original blog post titled Building Autoencoders in Keras
3. Setup the necessary library files.
4. Build the autoencoder
5. We can train our autoencoder using train_data as both our input data and target
6. Predict on our test dataset and display the original image together with the prediction from
our autoencoder.
7. Using the noisy data as our input and the clean data as our target, we want our autoencoder to
learn how to denoise the images.
8. Now predict on the noisy data and display the results of our autoencoder.
9. The autoencoder finally removes the noise from the input images.
PROGRAM:
import numpy as np import tensorflow
as tf
import matplotlib.pyplot as plt
def noise(array):
noise_factor = 0.4
noisy_array = array + noise_factor * np.random.normal( loc=0.0,
scale=1.0, size=array.shape
)
35
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
# Display the train data and a version of it with added noise display(train_data,
36
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
noisy_train_data)
# Encoder
x = layers.Conv2D(32, (3, 3), activation="relu", padding="same")(input) x =
layers.MaxPooling2D((2, 2), padding="same")(x)
x = layers.Conv2D(32, (3, 3), activation="relu", padding="same")(x) x =
layers.MaxPooling2D((2, 2), padding="same")(x)
# Decoder
x = layers.Conv2DTranspose(32, (3, 3), strides=2, activation="relu", padding="same")(x) x =
layers.Conv2DTranspose(32, (3, 3), strides=2, activation="relu", padding="same")(x) x =
layers.Conv2D(1, (3, 3), activation="sigmoid", padding="same")(x)
# Autoencoder
autoencoder = Model(input, x) autoencoder.compile(optimizer="adam",
loss="binary_crossentropy") autoencoder.summary()
37
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Result:
Thus a python program to implement Machine Translation using the Encoder-Decoder model was
implemented and executed successfully.
38
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Aim:
Procedure:
1.Dataset Preparation:
● Prepare the original dataset of images that you want to augment. This could be any dataset relevant
to your task, such as images of objects, animals, or scenes.
2.Build a GAN:
● Design and build a GAN architecture. A GAN consists of a generator and a discriminator network.
● The generator network generates new images from random noise.
● The discriminator network tries to distinguish between real images from the original dataset and
fake images generated by the generator.
3.Train the GAN:
● Train the GAN on the original dataset. The generator learns to create images that are increasingly
similar to the real dataset.
● The discriminator's goal is to get better at distinguishing real from generated images.
● Train the generator and discriminator in alternating steps.
4.Generate Augmented Images:
● After training, use the trained generator to generate new images. These images will be similar to
the original dataset but might have slight variations.
5.Augment Your Dataset:
● Combine the generated images with your original dataset to create an augmented dataset.
● The augmented dataset now contains both the original images and the generated images.
6.Train a Model:
● Use the augmented dataset to train your machine learning model. This model can be a neural
network, such as a convolutional neural network (CNN), for various tasks like image classification,
object detection, etc.
7.Evaluate and Compare:
● Evaluate your model's performance using the augmented dataset and compare it with the
performance using the original dataset alone.
● Augmented data can improve the model's generalization ability, especially if the original dataset is
limited.
8.Fine-Tuning (Optional):
39
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
● Depending on the performance, you can fine-tune the GAN or the machine learning model to
achieve better results.
9.Inference:
● Use your trained model for inference on real-world data.
Program:
import numpy as np
import tensorflow as tf
model = Sequential()
model.add(Dense(256, activation='relu'))
model.add(Dense(np.prod(output_shape), activation='sigmoid'))
model.add(Reshape(output_shape))
return model
def build_discriminator(input_shape):
model = Sequential()
model.add(Flatten(input_shape=input_shape))
40
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
return model
discriminator = build_discriminator(input_shape)
latent_dim = 100
discriminator.trainable = False
gan_input = Input(shape=(latent_dim,))
generated_image = generator(gan_input)
gan_output = discriminator(generated_image)
41
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
# Training GAN
epochs = 10000
batch_size = 64
sample_interval = 1000
# Train discriminator
real_images = X_train[idx]
# Train generator
42
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
if epoch % sample_interval == 0:
generated_images = generator.predict(noise)
cnt = 0
for i in range(5):
for j in range(5):
axs[i,j].axis('off')
cnt += 1
plt.show()
Output:
43
Downloaded by Shanmu Priya ([email protected])
lOMoARcPSD|30525524
Result:
Thus a python program to implement Image augmentation using GANs was implemented and executed
successfully.
44
Downloaded by Shanmu Priya ([email protected])