Deep Learning Experiments in TensorFlow
Deep Learning Experiments in TensorFlow
Program Objective:
The objective of this program is to perform simple vector addition using
TensorFlow. This involves defining two vectors, adding them element-wise, and
then printing the original vectors as well as the resultant vector.
Problem Statement:
Perform vector addition of two given vectors using TensorFlow.
Aim:
To demonstrate how to use TensorFlow to perform basic vector addition.
Algorithm:
Program:
import tensorflow as tf
# Enable eager execution
[Link].v1.enable_eager_execution()
# Define two vectors
vector1 = [Link]([1.0, 2.0, 3.0])
vector2 = [Link]([4.0, 5.0, 6.0])
# Perform vector addition
result_vector = [Link](vector1,
vector2) # Print the results
print("Vector 1:", [Link]())
print("Vector 2:", [Link]())
print("Resultant Vector:", result_vector.numpy())
Ouput:
Vector 1: [1. 2. 3.]
Vector 2: [4. 5. 6.]
Resultant Vector: [5. 7. 9.]
Result:
The program will output the original vectors (vector1 and vector2) and the
resultant vector after performing vector addition.
2. Implement a regression model in Keras
Program Objective:
The objective of this program is to implement a simple linear regression model
using the Keras library. The model will be trained on synthetic data, and
predictions will be made on new test data.
Problem Statement:
Create a regression model to predict output values based on input features. The
model should learn the underlying linear relationship in the data.
Aim:
To demonstrate the implementation of a linear regression model using Keras for
predictive modeling.
Algorithm:
STEP 1: Import necessary libraries (NumPy and TensorFlow).
STEP 2: Generate synthetic training data with a linear relationship and some noise.
STEP 3: Build a sequential model in Keras with a single dense layer (linear
activation) for regression.
STEP 4: Compile the model using Stochastic Gradient Descent (SGD) optimizer and
Mean Squared Error (MSE) loss function.
STEP 5: Train the model on the training data for a specified number of epochs.
STEP 6: Generate test data for predictions.
STEP 7: Use the trained model to make predictions on the test data.
STEP 8: Display the predicted output values.
Program:
import numpy as np
import tensorflow as tf
from [Link] import
Sequential from [Link] import
Dense
# Generate some synthetic data
[Link](42)
X_train = [Link](100, 1) # Input features
y_train = 2 * X_train + 1 + 0.1 * [Link](100, 1) # Linear relation with
some noise
# Build the regression
model model = Sequential()
[Link](Dense(units=1, input_shape=(1,), activation='linear'))
# Compile the model
[Link](optimizer='sgd', loss='mean_squared_error')
# Train the model
[Link](X_train, y_train, epochs=50, batch_size=8)
# Generate some test data
X_test = [Link]([[0.2], [0.5], [0.8]])
# Make predictions
predictions = [Link](X_test)
# Display the predictions
print("Predictions:")
print(predictions)
Output:
Epoch 1/50
13/13 [==============================] - 1s 6ms/step - loss: 2.4243
Epoch 2/50
13/13 [==============================] - 0s 5ms/step - loss: 1.2959
Epoch 3/50
13/13 [==============================] - 0s 4ms/step - loss: 0.7089
Epoch 4/50
13/13 [==============================] - 0s 4ms/step - loss: 0.3944
Epoch 5/50
13/13 [==============================] - 0s 4ms/step - loss: 0.2298
Epoch 6/50
13/13 [==============================] - 0s 4ms/step - loss: 0.1457
Epoch 7/50
13/13 [==============================] - 0s 4ms/step - loss: 0.0984
Epoch 8/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0745
Epoch 9/50
13/13 [==============================] - 0s 4ms/step - loss: 0.0609
Epoch 10/50
13/13 [==============================] - 0s 4ms/step - loss: 0.0531
Epoch 11/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0484
Epoch 12/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0454
Epoch 13/50
13/13 [==============================] - 0s 4ms/step - loss: 0.0429
Epoch 14/50
13/13 [==============================] - 0s 4ms/step - loss: 0.0410
Epoch 15/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0396
Epoch 16/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0384
Epoch 17/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0373
Epoch 18/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0362
Epoch 19/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0351
Epoch 20/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0341
Epoch 21/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0333
Epoch 22/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0324
Epoch 23/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0315
Epoch 24/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0306
Epoch 25/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0298
Epoch 26/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0291
Epoch 27/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0284
Epoch 28/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0275
Epoch 29/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0268
Epoch 30/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0262
Epoch 31/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0255
Epoch 32/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0249
Epoch 33/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0243
Epoch 34/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0237
Epoch 35/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0232
Epoch 36/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0226
Epoch 37/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0221
Epoch 38/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0216
Epoch 39/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0211
Epoch 40/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0207
Epoch 41/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0202
Epoch 42/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0197
Epoch 43/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0193
Epoch 44/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0189
Epoch 45/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0185
Epoch 46/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0181
Epoch 47/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0178
Epoch 48/50
13/13 [==============================] - 0s 3ms/step - loss: 0.0174
Epoch 49/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0171
Epoch 50/50
13/13 [==============================] - 0s 2ms/step - loss: 0.0167
1/1 [==============================] - 0s 90ms/step
Predictions:
[[1.5073806]
[2.001153 ]
[2.4949255]]
Result: The program will output the predictions made by the regression model
on the test data.
3. Implement a perceptron in TensorFlow/Keras Environment
Program Objective:
The objective of this program is to implement a perceptron for binary
classification using TensorFlow/Keras. The perceptron is trained on synthetic
data and evaluated for accuracy on a test set.
Problem Statement:
Create a perceptron model for binary classification based on synthetic data. Train
the model and assess its accuracy on a test set.
Aim:
To demonstrate the implementation and training of a perceptron using TensorFlow/Keras
for binary classification.
Algorithm:
STEP 1: Import necessary libraries (NumPy, TensorFlow, scikit-learn).
STEP 2: Generate synthetic data for a binary classification task.
STEP 3: Split the data into training and testing sets.
STEP 4: Build a perceptron model in Keras with one dense layer and a sigmoid
activation function.
STEP 5: Compile the model using Stochastic Gradient Descent (SGD) optimizer
and binary crossentropy loss.
STEP 6: Train the perceptron on the training set for a specified number of
epochs. STEP 7: Make predictions on the test set.
STEP 8: Evaluate the accuracy of the model on the test set.
Program:
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
# Generate some synthetic data for a binary classification task
[Link](42)
X = [Link](100, 2) # Input features (2 features for simplicity)
y = (X[:, 0] + X[:, 1] > 1).astype(int) # Binary label based on a simple condition
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Build a perceptron model
model = [Link]([
[Link](shape=(2,)),
[Link](units=1, activation='sigmoid')])
# Compile the model
[Link](optimizer='sgd', loss='binary_crossentropy',
metrics=['accuracy'])
# Train the perceptron
[Link](X_train, y_train, epochs=50, batch_size=8, verbose=0)
# Make predictions on the test set
predictions = [Link](X_test)
binary_predictions = (predictions > 0.5).astype(int)
# Evaluate accuracy
accuracy = accuracy_score(y_test, binary_predictions)
print("Accuracy on the test set:", accuracy)
Ouput:
Result: The program will output the accuracy of the trained perceptron on the
test set.
4. Implement a Feed-Forward Network in TensorFlow/Keras.
Program Objective:
The objective of this program is to implement a feed-forward neural network
using TensorFlow/Keras for binary classification. The network is trained on
synthetic data and evaluated for accuracy on a test set.
Problem Statement:
Create a feed-forward neural network for binary classification based on synthetic
data. Train the network and assess its accuracy on a test set.
Aim:
To demonstrate the implementation and training of a feed-forward neural
network using TensorFlow/Keras for binary classification.
Algorithm:
STEP 1: Import necessary libraries (NumPy, TensorFlow, scikit-learn).
STEP 2: Generate synthetic data for a binary classification task.
STEP 3: Split the data into training and testing sets.
STEP 4: Build a feed-forward neural network in Keras with two dense layers
(ReLU activation for the first layer, sigmoid activation for the output layer).
STEP 5: Compile the model using the Adam optimizer and binary crossentropy
loss.
STEP 6: Train the neural network on the training set for a specified number of
epochs.
STEP 7: Make predictions on the test set.
STEP 8: Evaluate the accuracy of the model on the test set.
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
# Generate some synthetic data for a binary classification task
[Link](42)
X = [Link](100, 2) # Input features (2 features for simplicity)
y = (X[:, 0] + X[:, 1] > 1).astype(int) # Binary label based on a simple condition
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
# Build a simple feed-forward neural network using Keras
model = [Link]([
[Link](shape=(2,)),
[Link](units=32, activation='relu'),
[Link](units=1, activation='sigmoid')
])
# Compile the model
[Link](optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
# Train the neural network
[Link](X_train, y_train, epochs=50, batch_size=8, verbose=0)
# Make predictions on the test set
predictions = [Link](X_test)
binary_predictions = (predictions > 0.5).astype(int)
# Evaluate accuracy
accuracy = accuracy_score(y_test, binary_predictions)
print("Accuracy on the test set:", accuracy)
Output:
1/1 [==============================] - 0s 156ms/step
Accuracy on the test set: 0.9
Result: The program will output the accuracy of the trained feed-forward neural
network on the test set.
5. Implement an Image Classifier using CNN in TensorFlow/Keras.
Program Objective:
The objective of this program is to implement an image classifier using
Convolutional Neural Networks (CNN) in TensorFlow/Keras. The CNN is trained
on the MNIST dataset and evaluated for accuracy on a test set.
Problem Statement:
Create a CNN-based image classifier for recognizing handwritten digits using the
MNIST dataset. Train the model and assess its accuracy on a test set.
Aim:
To demonstrate the implementation and training of a CNN using
TensorFlow/Keras for image classification.
Algorithm:
STEP 1: Import necessary libraries (TensorFlow, Keras, matplotlib).
STEP 2: Load and preprocess the MNIST dataset.
STEP 3: Build a CNN model in Keras with convolutional and pooling layers.
STEP 4: Compile the model using the Adam optimizer and categorical cross
entropy loss.
STEP 5: Train the CNN on the training set for a specified number of epochs.
STEP 6: Evaluate the model on the test set.
STEP 7: Display the test accuracy and plot the training history.
Program:
import tensorflow as tf
from [Link] import layers, models
from [Link] import mnist
from [Link] import to_categorical
import [Link] as plt
Output:
Downloading data from [Link]
datasets/[Link]
11490434/11490434 [==============================] - 1s 0us/step
Epoch 1/5
938/938 [==============================] - 61s 63ms/step - loss: 0.1887
- accuracy: 0.9410 - val_loss: 0.0621 - val_accuracy: 0.9806
Epoch 2/5
938/938 [==============================] - 56s 60ms/step - loss: 0.0541
- accuracy: 0.9833 - val_loss: 0.0393 - val_accuracy: 0.9878
Epoch 3/5
938/938 [==============================] - 54s 57ms/step - loss: 0.0363
- accuracy: 0.9887 - val_loss: 0.0356 - val_accuracy: 0.9886
Epoch 4/5
938/938 [==============================] - 52s 55ms/step - loss: 0.0287
- accuracy: 0.9907 - val_loss: 0.0382 - val_accuracy: 0.9888
Epoch 5/5
938/938 [==============================] - 53s 56ms/step - loss: 0.0239
- accuracy: 0.9924 - val_loss: 0.0288 - val_accuracy: 0.9908
313/313 [==============================] - 3s 9ms/step - loss: 0.0288 -
accuracy: 0.9908
Test accuracy: 0.9908000230789185
Result: The program will output the test accuracy of the CNN on the MNIST
dataset and display a plot of training accuracy and validation accuracy over
epochs.
6. Improve the Deep learning model by fine tuning hyper parameters.
Program Objective:
The objective of this program is to fine-tune the hyperparameters of a deep
learning model (CNN) for image classification on the MNIST dataset. The program
aims to improve the model's performance by adjusting hyperparameters such as
the number of filters, units in dense layers, learning rate, and using early stopping
with model checkpointing.
Problem Statement:
Fine-tune the hyperparameters of the CNN model for the MNIST dataset to
improve its accuracy. Utilize techniques such as adjusting the architecture,
learning rate, and implementing early stopping with model checkpointing.
Aim:
To demonstrate the improvement in model performance through fine-tuning
hyperparameters.
Algorithm:
STEP 1: Import necessary libraries (TensorFlow, Keras, matplotlib).
STEP 2: Load and preprocess the MNIST dataset.
STEP 3: Build a CNN model with hyperparameter tuning, including increased
filters, units, and adjusted learning rate.
STEP 4: Compile the model using the Adam optimizer and categorical
crossentropy loss.
STEP 5: Define callbacks for early stopping and model checkpointing.
STEP 6: Train the CNN model with fine-tuned hyperparameters, utilizing the
defined callbacks.
STEP 7: Load the best model from the checkpoint.
STEP 8: Evaluate the best model on the test set.
STEP 9: Display the test accuracy and plot the training history.
Program:
import tensorflow as tf
from [Link] import layers, models
from [Link] import mnist
from [Link] import to_categorical
from [Link] import Adam
from [Link] import EarlyStopping, ModelCheckpoint
import [Link] as plt
Epoch 1/20
938/938 [==============================] - 78s 82ms/step - loss: 0.2188
- accuracy: 0.9339 - val_loss: 0.0786 - val_accuracy: 0.9763
Epoch 2/20
3/938 [. ] - ETA: 47s - loss: 0.0681 - accuracy:
0.9792/usr/local/lib/python3.10/dist-
packages/keras/src/engine/[Link]: UserWarning: You are saving
your model as an HDF5 file via `[Link]()`. This file format is considered
legacy. We recommend using instead the native Keras format, e.g.
`[Link]('my_model.keras')`.
saving_api.save_model(
938/938 [==============================] - 57s 61ms/step - loss: 0.0694
- accuracy: 0.9784 - val_loss: 0.0539 - val_accuracy: 0.9849
Epoch 3/20
938/938 [==============================] - 62s 66ms/step - loss: 0.0488
- accuracy: 0.9849 - val_loss: 0.0473 - val_accuracy: 0.9856
Epoch 4/20
938/938 [==============================] - 57s 61ms/step - loss: 0.0377
- accuracy: 0.9885 - val_loss: 0.0523 - val_accuracy: 0.9837
Epoch 5/20
938/938 [==============================] - 60s 64ms/step - loss: 0.0301
- accuracy: 0.9904 - val_loss: 0.0537 - val_accuracy: 0.9849
Epoch 6/20
938/938 [==============================] - 58s 62ms/step - loss: 0.0249
- accuracy: 0.9921 - val_loss: 0.0457 - val_accuracy: 0.9867
Epoch 7/20
938/938 [==============================] - 56s 60ms/step - loss: 0.0199
- accuracy: 0.9938 - val_loss: 0.0593 - val_accuracy: 0.9848
Epoch 8/20
938/938 [==============================] - 57s 61ms/step - loss: 0.0167
- accuracy: 0.9944 - val_loss: 0.0480 - val_accuracy: 0.9877
Epoch 9/20
938/938 [==============================] - 56s 60ms/step - loss: 0.0149
- accuracy: 0.9949 - val_loss: 0.0553 - val_accuracy: 0.9843
313/313 [==============================] - 4s 11ms/step - loss: 0.0457 -
accuracy: 0.9867
Test accuracy of the best model: 0.9866999983787537
Result: The program will output the test accuracy of the best model after fine-tuning
hyperparameters and display a plot of training accuracy and validation accuracy over
epochs.
[Link] a transfer learning concept in Image Classification
Program Objective:
Problem Statement:
Given the CIFAR-10 dataset, which consists of 60,000 32x32 color images in 10
classes, the task is to develop a deep learning model to classify these images into their
respective categories. The model will utilize transfer learning with a pre-trained
VGG16 model to achieve better performance with less training data. Specifically, the
program will load the CIFAR-10 dataset, preprocess the data, load the pre-trained
VGG16 model without its top classification layer, add custom classification layers on
top of it, freeze the convolutional base, compile the model, train it on the training data,
evaluate its performance on the test data, and print the test accuracy achieved by the
model.
Aim:
The aim of this program is to perform image classification on the CIFAR-10 dataset
using transfer learning with a pre-trained VGG16 model.
Algorithm:
STEP 7:Output
● Display model summary.
● Print test accuracy achieved by the model.
Program:
import numpy as np
import tensorflow as tf
from [Link] import layers, models
from [Link] import cifar10
from [Link] import VGG16
from [Link] import to_categorical
# Load pre-trained VGG16 model without the top classification layer and freeze
convolutional base
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32,
3))
base_model.trainable = False
Output:
=================================================================
Total params: 14848586 (56.64 MB)
Trainable params: 133898 (523.04 KB)
Non-trainable params: 14714688 (56.13 MB)
_________________________________________________________________
Epoch 1/10
782/782 [==============================] - 611s 781ms/step - loss: 1.5134 -
accuracy: 0.4695 - val_loss: 1.2851 - val_accuracy: 0.5500
Epoch 2/10
776/782 [============================>.] - ETA: 3s - loss: 1.3041 - accuracy:
0.5435
Result:
After training the model for 10 epochs and evaluating it on the test data, the program
will print the test accuracy achieved by the model. This accuracy indicates the
percentage of correctly classified images in the test dataset. Additionally, the program
may display the summary of the model architecture, showing the layers and the
number of parameters.
8. Using pretrained model on Keras for Transfer Learning
Program Objective:
The objective of this program is to demonstrate transfer learning using a pretrained
VGG16 model on the CIFAR-10 dataset. The program aims to fine-tune the model for
the specific task of image classification on CIFAR-10 by adding custom top layers
while keeping the pretrained weights frozen.
Problem Statement:
Fine-tune a pretrained VGG16 model on the CIFAR-10 dataset for image classification.
Create a custom top architecture for the specific task.
Aim:
To showcase the effectiveness of transfer learning and the integration of a
pretrained VGG16 model for image classification on the CIFAR-10 dataset.
Algorithm:
STEP 1: Load the CIFAR-10 dataset and normalize pixel values to be between 0 and
1. STEP 2: Load the pretrained VGG16 model without the top (fully connected)
layers. STEP 3: Freeze the weights of the pretrained layers.
STEP 4: Create a new model by adding custom top layers for the specific
classification task (CIFAR-10 has 10 classes).
STEP 5: Compile the model with appropriate settings.
STEP 6: Set up a data generator with data augmentation and validation split.
STEP 7: Train the model using the data generator.
STEP 8: Evaluate the model on the test set.
Program:
from [Link] import cifar10
from [Link] import VGG16
from [Link] import ImageDataGenerator
from [Link] import layers, models, optimizers
# Load the pretrained VGG16 model without the top (fully connected) layers
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32,
3))
Model: "sequential"
Epoch 1/10
1250/1250 [==============================] - 522s 416ms/step - loss:
1.5227 - accuracy: 0.1009 - val_loss: 1.2595 - val_accuracy: 0.1067
...
Epoch 10/10
1250/1250 [==============================] - 543s 435ms/step - loss:
1.1144 - accuracy: 0.0960 - val_loss: 1.1163 - val_accuracy: 0.1063
313/313 [==============================] - 100s 321ms/step - loss: 1.1382 -
accuracy: 0.109
Program Objective:
The objective of this program is to demonstrate transfer learning using a pretrained
VGG16 model on the CIFAR-10 dataset. The program aims to fine-tune the model for the
specific task of image classification on CIFAR-10 by adding custom top layers while
keeping the pretrained weights frozen.
Problem Statement:
Fine-tune a pretrained VGG16 model on the CIFAR-10 dataset for image classification.
Create a custom top architecture for the specific task.
Aim:
To showcase the effectiveness of transfer learning and the integration of a pretrained
VGG16 model for image classification on the CIFAR-10 dataset.
Algorithm:
STEP 1: Load the CIFAR-10 dataset and normalize pixel values to be between 0 and 1.
STEP 2: Load the pretrained VGG16 model without the top (fully connected) layers.
STEP 3: Freeze the weights of the pretrained layers.
STEP 4: Create a new model by adding custom top layers for the specific classification
task (CIFAR-10 has 10 classes).
STEP 5: Compile the model with appropriate settings.
STEP 6: Set up a data generator with data augmentation and validation split.
STEP 7: Train the model using the data generator.
STEP 8: Evaluate the model on the test set.
Program:
from [Link] import cifar10
from [Link] import VGG16
from [Link] import ImageDataGenerator
from [Link] import layers, models, optimizers
# Load the pretrained VGG16 model without the top (fully connected) layers
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3))
=================================================================
Total params: 14,846,586
Trainable params: 133,898
Non-trainable params: 14,712,688
Epoch 1/10
1250/1250 [==============================] - 522s 416ms/step - loss: 1.5227 -
accuracy: 0.1009 - val_loss: 1.2595 - val_accuracy: 0.1067
...
Epoch 10/10
1250/1250 [==============================] - 543s 435ms/step - loss: 1.1144 -
accuracy: 0.0960 - val_loss: 1.1163 - val_accuracy: 0.1063
313/313 [==============================] - 100s 321ms/step - loss: 1.1382 -
accuracy: 0.109
Result: The program will output the model architecture summary, training/validation
accuracy and loss for each epoch, and the final test accuracy.
10. Implement an LSTM based Autoencoder in Tensorflow/Keras.
Program Objective:
The objective of this program is to implement an LSTM-based Autoencoder using
TensorFlow/Keras. Autoencoders are a type of neural network designed for
unsupervised learning that learns a compressed, efficient representation of input data.
The LSTM (Long Short-Term Memory) architecture is utilized to capture temporal
dependencies in sequence data.
Problem Statement:
The task is to create an autoencoder model that can effectively learn to encode and
decode sequences. In this specific example, the program generates random sequence data
as a placeholder. The model is trained to reconstruct the input sequences, and the training
is performed using mean squared error (MSE) as the loss function. The LSTM layers are
used for both encoding and decoding, and the overall architecture is summarized, trained,
and evaluated.
This program serves as a foundational example for understanding the implementation of
LSTM-based autoencoders, which can later be adapted for more complex applications
such as sequence-to-sequence prediction, anomaly detection in sequences, or
representation learning for sequential data.
Aim:
To showcase the implementation of LSTM based Autoencoder in Tensorflow/Keras.
Algorithm:
STEP 1: Import required libraries such as numpy for numerical operations and
tensorflow with its submodules for building and training the LSTM-based autoencoder.
STEP 2: Create or load the input sequence data for training the autoencoder. In this
example, a random sequence of shape (100, 10, 1) is generated, representing 100
sequences, each of length 10 with one feature.
STEP 3: Create a Sequential model.
STEP 4: Add an LSTM layer for encoding with specified parameters like the number of
units (64), activation function ('relu'), and input shape.
STEP 5: Add a RepeatVector layer to repeat the encoded representation across the
sequence length.
STEP 6: Add another LSTM layer for decoding with similar parameters as the encoding
LSTM.
STEP 7: Print or visualize the reconstructed sequences and compare them with the
original input sequences to assess the performance of the autoencoder.
Program:
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import Tokenizer
from [Link] import pad_sequences
from [Link] import to_categorical
from [Link] import Sequential
[Link](loss='categorical_crossentropy', optimizer=adam,
metrics=['accuracy'])
return model
# Train the model
from [Link] import KerasClassifier
model = KerasClassifier(build_fn=vanilla_rnn, epochs=5, batch_size=50)
[Link](X_train, y_train)
# Make predictions on the test set
y_pred = [Link](X_test)
# Evaluate accuracy
=================================================================
Total params: 49985 (195.25 KB)
Trainable params: 49985 (195.25 KB)
Non-trainable params: 0 (0.00 Byte)
Epoch 1/10
4/4 [==============================] - 3s 14ms/step - loss: 0.3377
Epoch 2/10
4/4 [==============================] - 0s 14ms/step - loss: 0.2896
Epoch 3/10
4/4 [==============================] - 0s 14ms/step - loss: 0.2456
Epoch 4/10
4/4 [==============================] - 0s 14ms/step - loss: 0.1936
Epoch 5/10
4/4 [==============================] - 0s 15ms/step - loss: 0.1377
Epoch 6/10
4/4 [==============================] - 0s 15ms/step - loss: 0.1066
Epoch 7/10
4/4 [==============================] - 0s 14ms/step - loss: 0.1178
Epoch 8/10
4/4 [==============================] - 0s 14ms/step - loss: 0.1005
Epoch 9/10
4/4 [==============================] - 0s 14ms/step - loss: 0.1017
Epoch 10/104/4 [==============================] - 0s 19ms/step - loss: 0.1023
4/4 [==============================] - 0s
Result: The program will output the model architecture summary, training/validation
accuracy and loss for each epoch, and the final test accuracy.
11. Image generation using GAN
Program Objective:
The objective of this program is to implement a Generative Adversarial Network (GAN)
using TensorFlow/Keras for generating synthetic images resembling the MNIST dataset.
Problem Statement:
Generate realistic-looking handwritten digits resembling the MNIST dataset using a
GAN. The GAN consists of a generator and a discriminator. The generator creates fake
images, and the discriminator distinguishes between real and fake images. The GAN is
trained to improve the generator's ability to produce images that are indistinguishable
from real ones.
Aim:
The aim is to train a GAN to generate convincing handwritten digits by optimizing the
generator and discriminator iteratively.
Algorithm:
STEP 1: Import necessary libraries including NumPy, Matplotlib, and
TensorFlow/Keras.
STEP 2: Define a function build_generator that creates the generator model.
STEP 3: Include a Reshape layer to convert output to (28, 28, 1) - image shape.
STEP 4: Define a function build_discriminator that creates the discriminator model.
STEP 5: Define a function build_gan that creates the GAN model by combining the
generator and discriminator.
STEP 6: Define a function load_dataset to load and preprocess the MNIST dataset.
STEP 7: Define a function train_gan that trains the GAN model.
STEP 8: Generate images using the trained generator.
# Generator model
def build_generator(latent_dim):
model = [Link]()
[Link]([Link](128, input_dim=latent_dim, activation='relu'))
[Link]([Link]())
[Link]([Link](784, activation='sigmoid'))
[Link]([Link]((28, 28, 1)))
return model
# Discriminator model
def build_discriminator(img_shape):
model = [Link]()
[Link]([Link](input_shape=img_shape))
[Link]([Link](128, activation='relu'))
[Link]([Link](1, activation='sigmoid'))
return model
# Print progress
if epoch % 100 == 0:
print(f"Epoch {epoch}, D Loss: {d_loss[0]}, G Loss: {g_loss}")
save_generated_images(generator, epoch, latent_dim)
# Main function
def main():
latent_dim = 100
generator = build_generator(latent_dim)
discriminator = build_discriminator(img_shape)
gan = build_gan(generator, discriminator)
X_train = load_dataset()
[Link](loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
[Link](loss='binary_crossentropy', optimizer='adam')
Output
4/4 [==============================] - 0s 2ms/step
……
1..
-
. .
.
=
.
I
.
.
.
. ..
lii iii,, , , II
',
IJ .
.
'
=
1..-1". ··
iii,, , ,
, II
1
lii
1 J
". .
I
.
.
=
.·.•..·.•
',
liiiii,, , , II
1 .
II
.
IJ
- '· •
- .
.·.•..·•
; •
II
. '
- .
=- I ' I
J
··
.
IJ
-
.·.•..·•
'· • ; •
.
'
II
=- I ' I
- .
.
IJ
- '· •
; •
. ' =- I '
■ .. -. •- :
-
' • t
I
: ,' · f .·
ll . .
t I I
IJ . .
I
-J . .
■ ■.-_-·,' -•
Ii
' - ' •
liii ' ' ■ '
i1-.1. •
' - ' -• •
■- i1-.1. • ■.-_·,'
' '.
m
. .
i!.-.·..·. • ■· · ·. . ·.
' :li '- _,.
t I I
.
IJ IJ
-".·· . .
. .
. .
IJ .
. .
c - I - •
1..
=
.
1 ···
11.·•.••·..•
'
' • ; •
',
liiiii,, , , II
=- I ' I
Result: The program will generate a series of images during the training process. The
generator improves over epochs to produce synthetic handwritten digits that closely
resemble the MNIST dataset. The training progress is monitored through the
discriminator and generator losses.
CONTENT BEYOND SYLABBUS:
1. Implement a basic neural network for binary classification using tensor flow/keras.
Problem Objective:
The objective of this problem is to develop a neural network model for binary classification.
Problem Statement:
Given a dataset with features and binary labels, the task is to design and train a neural network
model that can accurately classify the samples into one of the two classes.
Aim:
To create a neural network model that effectively performs binary classification, achieving high
accuracy on unseen data.
Algorithm:
Program:
import numpy as np
import tensorflow as tf
from [Link] import layers, models
Output:
Epoch 1/10
25/25 [==============================] - 1s 10ms/step - loss: 0.6963 - accuracy: 0.5400 -
val_loss: 0.7098 - val_accuracy: 0.4050
Epoch 2/10
25/25 [==============================] - 0s 3ms/step - loss: 0.6839 - accuracy: 0.5650 -
val_loss: 0.7086 - val_accuracy: 0.4350
Epoch 3/10
25/25 [==============================] - 0s 3ms/step - loss: 0.6755 - accuracy: 0.5813 -
val_loss: 0.7070 - val_accuracy: 0.4650
Epoch 4/10
25/25 [==============================] - 0s 3ms/step - loss: 0.6711 - accuracy: 0.5875 -
val_loss: 0.7085 - val_accuracy: 0.5000
Epoch 5/10
25/25 [==============================] - 0s 3ms/step - loss: 0.6670 - accuracy: 0.5813 -
val_loss: 0.7074 - val_accuracy: 0.5100
Epoch 6/10
25/25 [==============================] - 0s 3ms/step - loss: 0.6636 - accuracy: 0.6237 -
val_loss: 0.7114 - val_accuracy: 0.4800
Epoch 7/10
25/25 [==============================] - 0s 3ms/step - loss: 0.6608 - accuracy: 0.5950 -
val_loss: 0.7083 - val_accuracy: 0.4950
Epoch 8/10
25/25 [==============================] - 0s 3ms/step - loss: 0.6549 - accuracy: 0.6350 -
val_loss: 0.7076 - val_accuracy: 0.4900
Epoch 9/10
25/25 [==============================] - 0s 3ms/step - loss: 0.6512 - accuracy: 0.6488 -
val_loss: 0.7097 - val_accuracy: 0.5000
Epoch 10/10
25/25 [==============================] - 0s 3ms/step - loss: 0.6479 - accuracy: 0.6425 -
val_loss: 0.7100 - val_accuracy: 0.4800
<[Link] at 0x7f888eadcb80>
Result:
After training the neural network model, it achieves a certain level of accuracy on the validation set,
indicating its capability to classify binary data accurately. The accuracy obtained serves as a
measure of the model's performance in classifying unseen data.