Deep Learning Record
Deep Learning Record
GKM
COLLEGE OF ENGINEERING AND TECHNOLOGY
(SPONSPERED BY SUGANTHI EDUCATIONAL TRUST)
(AN ISO ISO: 9001 :2005 CERTIFIDE, APPROVED BY AICTE ,AFFLIATED TO ANNA UNIVERSITY)
G.K.M NAGAR,New Perungalathur(Near Tambaram) Chennai-600 063.
ESTD.1996
(BACHELOR OF TECHNOLOGY)
ARTIFICIAL INTELLIGENCE AND DATA SCIENCE
LABORATORY RECORD
NAME : …………………………………….
SEMESTER : …………………………………….
YEAR : …………………………………….
BRANCH : ……………………………………..
GKM
COLLEGE OF ENGINEERING AND TECHNOLOGY
BONAFIDE CERTIFICATE
REG NO:
Date
AIM:
CONCEPT:
The XOR problem is a classic problem in the field of machine learning and artificial
intelligence. XOR stands for "exclusive OR," which is a logical operation that takes two
binary inputs (0 or 1) and returns 1 only when exactly one of the inputs is 1. Otherwise,
it returns 0. Here's the truth table for the XOR operation:
1
4. Inference:
• Once trained, the DNN can be used to predict the XOR output for new input
combinations.
STEPS
1 Define the XOR X: Input data for the XOR problem, where each row represents
input and an input sample. y: Corresponding target outputs for the XOR
output data problem.
2 Define the DNN Input Layer:
architecture
The input layer specifies an input shape of (2,), indicating
that the model expects input data with two features. Hidden
Layer 1 (Dense):
2
5 Test the trained DNN • predictions = model.predict(X): Uses the trained model to
make predictions on the same XOR dataset.
• print(predictions): Prints the predicted outputs for the XOR
inputs.
3
PROGRAM
import tensorflow as tf import
numpy as np
OUTPUT
Predictions: [[0.]
[1.]
[1.]
[0.]]
RESULT
4
Experiment 2 Character recognition using CNN
Date
AIM:
CONCEPT:
5
overfitting. Once training is complete, evaluate the model's accuracy on the testing set
to assess its real-world performance.
7. Hyperparameter Tuning: Experiment with various hyperparameters such as learning
rate, batch size, number of layers, filter sizes, and pooling strategies to optimize the
model's performance.
8. Deployment: Once satisfied with the model's performance, deploy it to recognize
characters in new, unseen images. You can integrate the trained CNN into applications
such as optical character recognition (OCR) systems, document processing pipelines,
or other relevant projects.
Keep in mind that the success of your character recognition system will depend on
factors like the quality and quantity of your dataset, the architecture of your CNN, and
the effectiveness of your training process. Additionally, there are pre-trained CNN
architectures, such as VGG, ResNet, and Inception, which you can fine-tune for
character recognition tasks to potentially achieve better performance.
STEPS
PROGRAM
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
6
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu')) model.add(Dense(10,
activation='softmax'))
OUTPUT
Epoch 1/5 938/938 [==============================] - 21s 21ms/step - loss: 0.1687 -
accuracy: 0.
9493 - val_loss: 0.0792 - val_accuracy: 0.9755
Epoch 2/5
938/938 [==============================] - 19s 21ms/step - loss: 0.0511 - accuracy: 0.
9847 - val_loss: 0.0426 - val_accuracy: 0.9855 Epoch 3/5
RESULT
7
Experiment 3 Face recognition using CNN
Date
AIM:
CONCEPT:
Face recognition using Convolutional Neural Networks (CNNs) is a popular application of
deep learning in computer vision. CNNs are well-suited for image recognition tasks like face
recognition because they can automatically learn hierarchical features from images, which
are essential for discriminating between different faces.
Here's an overview of how to approach face recognition using CNNs:
1. Data Collection and Preprocessing:
• Collect a dataset of labeled face images. You need images of individuals you want
the model to recognize.
• Preprocess the images: Resize them to a common size (e.g., 224x224 pixels),
normalize pixel values to a certain range (usually [0, 1] or [-1, 1]), and perform
data augmentation (e.g., random cropping, flipping) to increase model
robustness.
2. CNN Architecture:
• Choose a CNN architecture: Common choices include variants of VGG, ResNet,
Inception, or MobileNet. These architectures are available in libraries like
TensorFlow and PyTorch.
• Pretrained Models: Consider using a pretrained CNN model on a large dataset like
ImageNet. Transfer learning allows you to leverage learned features and fine-
tune the model for face recognition.
3. Model Training:
• Modify the architecture: Replace the classification head of the pretrained model
with a new one suitable for face recognition. Typically, the new head consists of a
few fully connected layers.
• Data Input: Feed the preprocessed face images into the model and train it using
a suitable loss function. Common choices include triplet loss or contrastive loss,
which encourage the network to learn embeddings that make similar faces close
and dissimilar faces far apart in the embedding space.
• Optimization: Use an optimizer like Adam or SGD to update the model's
parameters during training.
4. Testing and Verification:
• Embedding Extraction: After training, remove the classification head and use the
model to extract embeddings (feature vectors) from face images.
8
Face Verification: To verify whether two face images belong to the same person,
•
calculate the similarity (e.g., cosine similarity) between their embeddings. Define
a threshold to decide whether the faces are a match or not.
• Face Identification: For identification, compare the embeddings of a query face
against embeddings of all known faces in the database and find the closest
match.
5. Deployment and Testing:
• Deploy the trained model to your desired platform (web application, mobile app,
etc.).
• Test the model's accuracy and performance on real-world data. Tweak
hyperparameters or augment the dataset as needed to improve accuracy.
Remember that face recognition involves privacy concerns, and ethical
considerations should be taken into account, especially when working with
sensitive data.
STEPS
1 Set the path to the directory containing the face images
2 Load the face images and labels & Iterate over the face image directory and load the
images
3 Convert the data to numpy arrays, Preprocess labels to extract numeric part
And Convert labels to one-hot encoded vectors
4 Split the data into training and validation sets
5 Compile, Train the CNN model and Save the trained model
PROGRAM
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img, img_to_array
# Set the path to the directory containing the face images faces_dir =
"D:/R2021 DL LAB/Faces/Faces"
9
# Iterate over the face image directory and load the images for
filename in os.listdir(faces_dir):
if filename.endswith(".jpg"):
img_path = os.path.join(faces_dir, filename) img = load_img(img_path,
target_size=(64, 64))
# Resize images to 64x64 pixels
img_array = img_to_array(img) x_data.append(img_array)
label = filename.split(".")[0]
# Assuming the filename format is label.jpg y_data.append(label)
y_data = np.array(y_data)
10
OUTPUT
Epoch 1/10
65/65 [==============================] - 5s 68ms/step - loss: 215.6209 - accuracy: 0.0
098 - val_loss: 4.7830 - val_accuracy: 0.0039
Epoch 2/10
65/65 [==============================] - 4s 66ms/step - loss: 4.7793 - accuracy:
0.011
2 - val_loss: 4.7757 - val_accuracy: 0.0039
Epoch 3/10
65/65 [==============================] - 4s 66ms/step - loss: 4.7717 - accuracy: 0.012
2 - val_loss: 4.7694 - val_accuracy: 0.0039
Epoch 4/10
65/65 [==============================] - 4s 66ms/step - loss: 4.7646 - accuracy: 0.010
7 - val_loss: 4.7634 - val_accuracy: 0.0039
Epoch 5/10
65/65 [==============================] - 4s 68ms/step - loss: 4.7579 - accuracy: 0.010
2 - val_loss: 4.7577 - val_accuracy: 0.0039
Epoch 6/10
65/65 [==============================] - 5s 70ms/step - loss: 4.7516 - accuracy: 0.010
7 - val_loss: 4.7525 - val_accuracy: 0.0039
Epoch 7/10
65/65 [==============================] - 4s 66ms/step - loss: 4.7457 - accuracy: 0.009
8 - val_loss: 4.7476 - val_accuracy: 0.0039
Epoch 8/10
65/65 [==============================] - 4s 67ms/step - loss: 4.7402 - accuracy: 0.010
7 - val_loss: 4.7432 - val_accuracy: 0.0039
Epoch 9/10
65/65 [==============================] - 4s 66ms/step - loss: 4.7349 - accuracy: 0.013
2 - val_loss: 4.7392 - val_accuracy: 0.0078
Epoch 10/10
65/65 [==============================] - 4s 65ms/step - loss: 4.7300 - accuracy: 0.010
2 - val_loss: 4.7354 - val_accuracy: 0.0078
RESULT
11
Experiment 4 Language modeling using RNN
Date
AIM:
CONCEPT:
STEPS
PROGRAM
13
sequences.append(text_indices[i:i+seq_length])
next_char.append(text_indices[i + seq_length])
model.compile(loss="sparse_categorical_crossentropy",optimizer="adam")
for _ in range(num_chars_to_generate):
seed_indices = [char_to_index[char] for char in seed_text]
# Check if the seed sequence length matches the model's input length if
len(seed_indices) < seq_length:
diff = seq_length - len(seed_indices) seed_indices =
[0] * diff + seed_indices
14
OUTPUT
Epoch 1/50
1/1 [==============================] - 1s 1s/step - loss: 3.0885
Epoch 2/50
1/1 [==============================] - 0s 8ms/step - loss: 3.0053
Epoch 3/50
1/1 [==============================] - 0s 14ms/step - loss: 2.9234
Epoch 4/50
1/1 [==============================] - 0s 0s/step - loss: 2.8392
Epoch 5/50
1/1 [==============================] - 0s 17ms/step - loss: 2.7501
Epoch 6/50
1/1 [==============================] - 0s 0s/step - loss: 2.6545
Epoch 7/50
1/1 [==============================] - 0s 4ms/step - loss: 2.5519
Epoch 8/50
15
1/1 [==============================] - 0s 17ms/step - loss: 1.2678
Epoch 20/50
1/1 [==============================] - 0s 0s/step - loss: 1.1810
Epoch 21/50
1/1 [==============================] - 0s 17ms/step - loss: 1.0964
Epoch 22/50
1/1 [==============================] - 0s 14ms/step - loss: 1.0179
Epoch 23/50
1/1 [==============================] - 0s 1ms/step - loss: 0.9459
Epoch 24/50
1/1 [==============================] - 0s 16ms/step - loss: 0.8773
Epoch 25/50
1/1 [==============================] - 0s 0s/step - loss: 0.8107
Epoch 26/50
1/1 [==============================] - 0s 17ms/step - loss: 0.7473
Epoch 27/50
1/1 [==============================] - 0s 0s/step - loss: 0.6884
Epoch 28/50
1/1 [==============================] - 0s 17ms/step - loss: 0.6333
Epoch 29/50
1/1 [==============================] - 0s 0s/step - loss: 0.5809
Epoch 30/50
1/1 [==============================] - 0s 2ms/step - loss: 0.5318
Epoch 31/50
1/1 [==============================] - 0s 17ms/step - loss: 0.4871
Epoch 32/50
1/1 [==============================] - 0s 0s/step - loss: 0.4469
Epoch 33/50
1/1 [==============================] - 0s 18ms/step - loss: 0.4099
Epoch 34/50
1/1 [==============================] - 0s 0s/step - loss: 0.3753
Epoch 35/50
1/1 [==============================] - 0s 18ms/step - loss: 0.3430
Epoch 36/50
1/1 [==============================] - 0s 0s/step - loss: 0.3134
Epoch 37/50
1/1 [==============================] - 0s 15ms/step - loss: 0.2865
Epoch 38/50
1/1 [==============================] - 0s 0s/step - loss: 0.2621
16
Epoch 39/50
1/1 [==============================] - 0s 2ms/step - loss: 0.2399
Epoch 40/50
1/1 [==============================] - 0s 15ms/step - loss: 0.2200
Epoch 41/50
1/1 [==============================] - 0s 1ms/step - loss: 0.2021
Epoch 42/50
1/1 [==============================] - 0s 18ms/step - loss: 0.1860
Epoch 43/50
1/1 [==============================] - 0s 0s/step - loss: 0.1714
Epoch 44/50
1/1 [==============================] - 0s 16ms/step - loss: 0.1580
Epoch 45/50
1/1 [==============================] - 0s 0s/step - loss: 0.1460
Epoch 46/50
1/1 [==============================] - 0s 4ms/step - loss: 0.1353
Epoch 47/50
1/1 [==============================] - 0s 12ms/step - loss: 0.1257
Epoch 48/50
1/1 [==============================] - 0s 933us/step - loss: 0.1170
Epoch 49/50
1/1 [==============================] - 0s 17ms/step - loss: 0.1090
Epoch 50/50
1/1 [==============================] - 0s 0s/step - loss: 0.1017
This is a sample tentrfornlanguags modnging nsing Rgn.rginsrngangrngangnoggrng nsingrnging ndgg
nsinorng ngrngadgsinorng
RESULT
17
Experiment 5 Sentiment analysis using LSTM
Date
AIM:
CONCEPT:
Sentiment analysis using LSTM (Long Short-Term Memory) is a common task in natural
language processing where you aim to determine the sentiment or emotional tone expressed
in a given text. LSTM is a type of recurrent neural network that can capture long-range
dependencies in sequences, making it well-suited for sequence-based tasks like sentiment
analysis.
1. Text Representation: In sentiment analysis, text data needs to be converted into a
numerical format that can be processed by neural networks. This is typically done using
techniques like tokenization and word embedding. Tokenization splits the text into
individual words or subwords, while word embedding maps each token to a dense
vector representation in a continuous vector space.
2. Sequence Padding: In order to train LSTM networks efficiently, sequences (sentences
or documents) need to have a consistent length. Since text data can have varying
lengths, padding is applied to make all sequences of the same length. Shorter
sequences are padded with zeros at the beginning or end.
3. LSTM Architecture: An LSTM is a type of recurrent neural network designed to handle
sequential data. It has memory cells and gates that allow it to capture long-term
dependencies in sequences. LSTMs can remember important information over
extended periods, which is crucial for sentiment analysis since sentiments in text can
span across multiple words.
4. Embedding Layer: The input text tokens are passed through an embedding layer, which
converts the discrete token indices into dense vector representations. This layer is
responsible for capturing semantic relationships between words.
5. LSTM Layer: The LSTM layer processes the embedded sequences, updating its internal
state based on the input tokens and previous state. The LSTM's ability to maintain and
update context over time enables it to capture sequential patterns and dependencies
within the text.
6. Classification Layer: After processing the sequence through the LSTM layer, the final
hidden state is passed through a fully connected (dense) layer. This layer performs the
sentiment classification by producing a probability score indicating the likelihood of a
particular sentiment class.
18
7. Training and Backpropagation: During training, the model's predictions are compared
to the actual sentiment labels using a loss function (such as binary cross-entropy for
binary sentiment classification). The gradients of the loss are propagated back through
the network using backpropagation through time (BPTT), and the model's parameters
are updated using an optimization algorithm (e.g., Adam, SGD).
8. Inference and Prediction: Once the LSTM model is trained, it can be used to predict
sentiment labels for new, unseen text data. The input text is processed through the
trained model, and the final classification layer's output provides the predicted
sentiment.
Sentiment analysis using LSTM is a powerful application of deep learning in natural language
processing. It allows the model to learn and capture complex patterns in textual data, making
it capable of understanding and classifying sentiments expressed in various contexts.
STEPS
1 Load the IMDB dataset, which consists of movie reviews labeled with positive or
negative sentiment.
2 Preprocess the data by padding sequences to a fixed length (max_review_length) and
limiting the vocabulary size to the most frequent words (num_words).
3 Build an LSTM-based model. The Embedding layer is used to map word indices to
dense vectors, the LSTM layer captures sequence dependencies, and the Dense layer
produces a binary sentiment prediction.
4 The model is compiled with binary cross-entropy loss and the Adam optimizer.
5 Train the model using the training data. Finally, we evaluate the model on the test data
and print the test accuracy.
PROGRAM
import numpy as np import
tensorflow as tf
from tensorflow.keras.datasets import imdb from
tensorflow.keras.preprocessing import sequence from
tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense
19
max_len = 500 # Maximum length of each review (pad shorter reviews, truncate longer
reviews)
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
x_train = sequence.pad_sequences(x_train, maxlen=max_len)
x_test = sequence.pad_sequences(x_test, maxlen=max_len)
model = Sequential()
model.add(Embedding(max_features, embedding_size, input_length=max_len))
model.add(LSTM(100))
OUTPUT
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/imdb
.npz
17464789/17464789 [==============================] - 7s 0us/step
Epoch 1/5
391/391 [==============================] - 286s 727ms/step - loss: 0.4991 - accuracy:
0.7626 - val_loss: 0.3712 - val_accuracy: 0.8412
Epoch 2/5 391/391 [==============================] - 296s 757ms/step - loss: 0.3381 -
accuracy:
20
0.8587 - val_loss: 0.3609 - val_accuracy: 0.8532
Epoch 3/5
391/391 [==============================] - 313s 801ms/step - loss: 0.2642 - accuracy:
0.8945 - val_loss: 0.3168 - val_accuracy: 0.8678
Epoch 4/5
391/391 [==============================] - 433s 1s/step - loss: 0.2263 - accuracy: 0.9
142 - val_loss: 0.3119 - val_accuracy: 0.8738
Epoch 5/5
391/391 [==============================] - 302s 774ms/step - loss: 0.1982 - accuracy:
0.9247 - val_loss: 0.3114 - val_accuracy: 0.8745
782/782 [==============================] - 74s 95ms/step - loss: 0.3114 - accuracy: 0. 8745
Loss: 0.3113741874694824
Accuracy: 0.8745200037956238
RESULT
21
Experiment 6 Parts of speech tagging using Sequence to Sequence architecture
Date
AIM:
CONCEPT:
Parts of speech (POS) tagging is a natural language processing task where each word in a
sentence is assigned a specific grammatical category, such as noun, verb, adjective, etc.
Sequence-to-Sequence (Seq2Seq) architecture, which was originally designed for machine
translation tasks, can also be adapted for POS tagging. The Seq2Seq architecture consists of
two main components: an encoder and a decoder. Here's how you can use Seq2Seq for POS
tagging:
1. Encoder-Decoder Setup: In the context of POS tagging, the encoder takes in the input
sentence (sequence of words) and encodes it into a fixed-size context vector. The
decoder then generates the POS tags based on this context vector.
2. Encoder Component: The encoder can be implemented using a recurrent neural
network (RNN) such as LSTM or GRU. The input sequence of words (tokens) is passed
through the encoder RNN, and the final hidden state of the encoder captures the
contextual information of the entire sentence.
3. Decoder Component: The decoder is another RNN that takes the encoder's final hidden
state as an initial hidden state and generates POS tags one at a time. At each step, the
decoder produces a probability distribution over possible POS tags for the current word.
4. Training: During training, the model is given pairs of input sentences and corresponding
POS tag sequences. The encoder generates the context vector, which is then used as
the initial state of the decoder. The decoder generates the predicted POS tags. The
model is trained to minimize the cross-entropy loss between the predicted and actual
POS tags.
5. Inference: During inference (testing or prediction), the model is given an input
sentence, and the encoder generates the context vector. The decoder then generates
POS tags one by one, using the context vector and the previously generated tag. This
process continues until an end-of-sentence token is generated or a maximum sequence
length is reached.
STEPS
22
3 Add <sos> and <eos> tokens to target_words.
4 Create dictionaries to map words and POS tags to integers.
5 Define the maximum sequence lengths, Prepare the encoder input data
And Prepare the decoder input and target data
6 Define the encoder input and LSTM layers Define the decoder input and LSTM layers
7 Define, Compile and train the model
8 Define the encoder model to get the encoder states and Define the decoder model with
encoder states as initial state
9 Define a function to perform inference and generate POS tags, Test the model.
PROGRAM
import numpy as np import
tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, LSTM, Dense
from tensorflow.keras.preprocessing.sequence import pad_sequences
# 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())
23
max_encoder_seq_length = max([len(text.split()) for text in input_texts])
max_decoder_seq_length = max([len(text.split()) for text in target_texts])
24
# Compile and train the model
model.compile(optimizer='adam',loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
batch_size=64, epochs=50, validation_split=0.2)
OUTPUT
Epoch 1/50
1/1 [==============================] - 7s 7s/step - loss: 1.3736 - accuracy: 0.0000e+0
0 - val_loss: 1.1017 - val_accuracy: 0.0000e+00
Epoch 2/50
1/1 [==============================] - 0s 63ms/step - loss: 1.3470 - accuracy: 0.7500
- val_loss: 1.1068 - val_accuracy: 0.0000e+00
Epoch 3/50
1/1 [==============================] - 0s 65ms/step - loss: 1.3199 - accuracy: 0.7500
- val_loss: 1.1123 - val_accuracy: 0.0000e+00
Epoch 4/50
Epoch 44/50
1/1 [==============================] - 0s 58ms/step - loss: 0.0882 - accuracy: 0.7500
:
:
:
Epoch 50/50
1/1 [==============================] - 0s 60ms/step - loss: 0.0751 - accuracy: 0.7500 -
val_loss: 2.2554 - val_accuracy: 0.0000e+00
Input: I love coding
Predicted POS Tags: VB NNP NNP DT DT
RESULT
26
Experiment 7 Machine Translation using Encoder-Decoder model
Date
AIM:
CONCEPT:
STEPS
9 Define afunction to perform inference and generate translations Test the model
PROGRAM
import numpy as np import
tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, LSTM, Dense
from tensorflow.keras.preprocessing.sequence import pad_sequences
# 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())
29
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)
OUTPUT
Input: This is a pen
Translated Text: ist ein Stift Coden ein
RESULT
30
Experiment 8 Image augmentation using GANs
Date
AIM:
CONCEPT:
STEPS
31
3 Train thehyperparameters and the Training loop has the following steps:
• Generate a batch of fake images
• Train the discriminator
• Train the generator
• Print the progress and save samples
PROGRAM
import numpy as np
import matplotlib.pyplot as plt from
tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Input, Dense, Reshape, Flatten
from tensorflow.keras.layers import BatchNormalization, Dropout
from tensorflow.keras.layers import Conv2D, Conv2DTranspose from
tensorflow.keras.models import Sequential, Model from
tensorflow.keras.optimizers import Adam
# Training hyperparameters
epochs = 100 batch_size =128 sample_interval = 10
# Training loop
for epoch in range(epochs):
OUTPUT
Epoch: 90 Discriminator Loss: 0.03508808836340904
Generator Loss: 1.736445483402349e-06
RESULT
34