0% found this document useful (0 votes)
7 views251 pages

SRM Institute of Science and Technology: Record Work

The document outlines the academic record work for the M.B.A. program in Artificial Intelligence and Data Science at SRM Institute of Science and Technology for the academic year 2023-2024. It includes a bonafide certificate, a detailed table of exercises related to deep learning, and an introduction to Recurrent Neural Networks (RNNs), their working principles, advantages, disadvantages, and variations like LSTM and BiNN. The document serves as a comprehensive guide for students on the coursework and applications of RNNs in various fields.

Uploaded by

Nantha Sooreya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views251 pages

SRM Institute of Science and Technology: Record Work

The document outlines the academic record work for the M.B.A. program in Artificial Intelligence and Data Science at SRM Institute of Science and Technology for the academic year 2023-2024. It includes a bonafide certificate, a detailed table of exercises related to deep learning, and an introduction to Recurrent Neural Networks (RNNs), their working principles, advantages, disadvantages, and variations like LSTM and BiNN. The document serves as a comprehensive guide for students on the coursework and applications of RNNs in various fields.

Uploaded by

Nantha Sooreya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 251

SRM INSTITUTE OF SCIENCE AND

TECHNOLOGY
Kattankulathur, Chennai-603203

FACULTY OF MANAGEMENT

Record Work

Academic Year 2023-2024

STUDENT NAME

REGISTER NUMBER

NAME OF THE PROGRAMME M.B.A. (ARTIFICIAL INTELLIGENCE & DATA


SCIENCE)
YEAR & SEMESTER II YEAR & IV SEMESTER

SECTION CC
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
S.R.M. Nagar, Kattankulathur -603 203

Bonafide Certificate

This is to certify that the Bonafide record of work done by


Registration No. of Second Year
M.B.A. (Artificial Intelligence and Data Science) Degree Course MBI21402L
– Deep Learning - II in the Faculty of Management, SRM Institute of Science
and Technology, Kattankulathur during the academic year 2023- 2024.

Dr. SATHYA V Dr. P. SARAVANAN


Course Coordinator Program Coordinator &
Assistant Professor Faculty Domain Coordinator
of Management Associate Professor
Faculty of Management

Dr. SHIVGANESH BHARGAVA


Dean
Faculty of Management

Submitted for University Examination held on _____________, SRM Institute of Science and
Technology, Kattankulathur.

Examiner-I Examiner-II
TABLE OF CONTENT
SL. FACULTY
DATE TITLE OF THE EXERCISE
NO SIGNATURE
SUMMARY GENERATION WITH VANILLA
1 18/01/24
RNN
TIME SERIES PREDICTION
2 18/01/24

SENTIMENT ANALYSIS
3 18/01/24

4 LANGUAGE TRANSLATION
19/01/24
5 STOCK PRICE PREDICTION
19/01/24

6 PADDING SEQUENCES
19/01/24
7 22/01/24 TRUNCATING SEQUENCES

8 22/01/24 SENTIMENT ANALYSIS FOR MOVIE REVIEW


DATA SET
9 22/01/24 UNFOLDING A NEURAL NETWORK

10 29/01/24 VISUALIZING THE UNFOLDED DATA

11 29/01/24 IMAGE DENOISING WITH AUTO ENCODERS

12 29/01/24 DATA IMPUTATION WITH AUTO ENCODERS

13 30/01/24 ANOMALY DETECTION WITH AUTO


ENCODERS
14 30/01/24 FEATURE LEARNING REPRESENTATION

15 30/01/24 REPRESENTATION LEARNING

16 31/01/24 UNSUPERVISED LEARNING


REPRESENTATION
17 31/01/24 IMPLEMENTATION OF AUTO ENCODERS

18 31/01/24 DEFINING A BASIC AUTO ENCODER

19 05/02/24 VANILLA AUTOENCODER

20 05/02/24 SPARSE AUTOENCODER

21 05/02/24 DENOISING AUTOENCODER

22 06/02/24 VARIATIONAL AUTOENCODER (VAE)

06/02/24 CONTRACTIVE AUTOENCODER


23

24 06/02/24 ADVERSARIAL AUTOENCODER

25 07/02/24 CONVOLUTIONAL AUTOENCODER

26 07/02/24 STACKED AUTOENCODER


27 07/02/24 RECURRENT AUTOENCODER

28 12/02/24 ATTENTION BASED AUTOENCODER

29 12/02/24 BOLTZMANN MACHINE

30 12/02/24 IMPLEMENTATION OF GANN

31 13/02/24 IMAGE GENERATION USING GANN

32 13/02/24 STYLE TRANSFER USING GANN

33 13/02/24 DATA AUGMENTATION USING GANN

34 14/02/24 SELF DRIVING CARS

35 14/02/24 AUTOMATIC TEXT GENERATION

36 14/02/24 AUTOMATIC MACHINE TRANSLATION

37 19/02/24 AUTOMATIC TEXT GENERATION

38 19/02/24 AUTOMATIC HANDWRITING GENERATION

39 19/02/24 INTERNET SEARCH

40 20/02/24 IMAGE RECOGNITION

41 20/02/24 IMAGE RECOGNITION BY PREDICTION

42 20/02/24 AUTOMATIC IMAGE COLORIZATION

43 20/02/24 AUTOMATIC IMAGE CAPTION GENERATION

44 21/02/24 RECOMMENDER SYSTEMS

45 21/02/24 EARTHQUAKE PREDICTION

46 21/02/24 NEURAL NETWORK FOR BRAIN CANCER


DETECTION
47 21/02/24 ALGORITHMIC TRADING

48 24/02/24 RISK MANAGEMENT

49 24/02/24 FRAUD DETECTION

50 24/02/24 STOCK PRICE PREDICTION

51 24/02/24 PORTFOLIO MANAGEMENT


RECURRENT NEURAL NETWORK RNN – AN INTRODUCTION

A Recurrent Neural Network (RNN) is a type of artificial neural network designed for
sequential data processing and tasks. Unlike traditional feedforward neural networks, RNNs
have connections that form directed cycles, allowing them to maintain a hidden state that can
capture information about previous inputs in the sequence. This hidden state enables RNNs to
exhibit temporal dynamic behavior, making them suitable for tasks such as time series
prediction, natural language processing, and speech recognition.

The key feature of an RNN is its ability to maintain and update an internal state as it
processes each element in a sequence. This makes RNNs well-suited for tasks where context
or temporal dependencies are crucial. However, traditional RNNs have some limitations, such
as difficulty in learning long-range dependencies and issues with vanishing or exploding
gradients during training.

To address these limitations, more advanced variants of RNNs have been developed.
Some of these include:

1. **Long Short-Term Memory (LSTM):** LSTMs are a type of RNN architecture


designed to overcome the vanishing gradient problem. They incorporate memory cells and
gates to control the flow of information, allowing them to capture long-term dependencies in
sequential data.

2. **Gated Recurrent Unit (GRU):** GRUs are another variant of RNNs that, like
LSTMs, address the vanishing gradient problem. They use gating mechanisms to control the
information flow, but they have a simpler architecture compared to LSTMs.

3. **Bidirectional RNNs:** These networks process the input sequence in both


forward and backward directions, capturing information from past and future context. This
can be beneficial in tasks where both preceding and succeeding context are important.

4. **Attention Mechanisms:** Attention mechanisms allow the model to focus on


different parts of the input sequence when making predictions. This is particularly useful in
tasks where specific elements of the input sequence contribute more to the output.

RNNs and their variants have been widely used in various applications, including
natural language processing (e.g., language modeling, machine translation), speech
recognition, time series analysis, and more. While they have been successful in many areas,
researchers continue to explore and develop new architectures to improve their performance
and address challenges associated with learning long-term dependencies.
WORKING PRINCIPLE OF RECURRENT NEURAL NETWORK

Recurrent Neural Network(RNN) is a type of Neural Network where the output from
the previous step is fed as input to the current step. In traditional neural networks, all the
inputs and outputs are independent of each other. Still, in cases when it is required to predict
the next word of a sentence, the previous words are required and hence there is a need to
remember the previous words. Thus RNN came into existence, which solved this issue with
the help of a Hidden Layer. The main and most important feature of RNN is its Hidden state,
which remembers some information about a sequence. The state is also referred to as Memory
State since it remembers the previous input to the network. It uses the same parameters for
each input as it performs the same task on all the inputs or hidden layers to produce the
output. This reduces the complexity of parameters, unlike other neural networks.

How RNN differs from Feedforward Neural Network?

Artificial neural networks that do not have looping nodes are called feed forward neural
networks. Because all information is only passed forward, this kind of neural network is also
referred to as a multi-layer neural network.
Information moves from the input layer to the output layer – if any hidden layers are present
– unidirectionally in a feedforward neural network. These networks are appropriate for image
classification tasks, for example, where input and output are independent. Nevertheless, their
inability to retain previous inputs automatically renders them less useful for sequential data
analysis.
Recurrent Neuron and RNN Unfolding

The fundamental processing unit in a Recurrent Neural Network (RNN) is a Recurrent Unit,
which is not explicitly called a “Recurrent Neuron.” This unit has the unique ability to
maintain a hidden state, allowing the network to capture sequential dependencies by
remembering previous inputs while processing. Long Short-Term Memory (LSTM) and
Gated Recurrent Unit (GRU) versions improve the RNN’s ability to handle long-term
dependencies.
Types Of RNN

There are four types of RNNs based on the number of inputs and outputs in the network.
1. One to One
2. One to Many
3. Many to One
4. Many to Many

One to One
This type of RNN behaves the same as any simple Neural network it is also known as Vanilla
Neural Network. In this Neural network, there is only one input and one output.

One To Many
In this type of RNN, there is one input and many outputs associated with it. One of the most
used examples of this network is Image captioning where given an image we predict a
sentence having Multiple words.
Many to One
In this type of network, Many inputs are fed to the network at several states of the network
generating only one output. This type of network is used in the problems like sentimental
analysis. Where we give multiple words as input and predict only the sentiment of the
sentence as output.

Many to Many
In this type of neural network, there are multiple inputs and multiple outputs corresponding to
a problem. One Example of this Problem will be language translation. In language
translation, we provide multiple words from one language as input and predict multiple words
from the second language as output.
Recurrent Neural Network Architecture

RNNs have the same input and output architecture as any other deep neural architecture.
However, differences arise in the way information flows from input to output. Unlike Deep
neural networks where we have different weight matrices for each Dense network in RNN,
the weight across the network remains the same. It calculates state hidden state Hi for every
input Xi . By using the following formulas:
h= σ(UX + Wh-1 + B)

Y = O(Vh + C)

Hence

Y = f (X, h , W, U, V, B, C)

Here S is the State matrix which has element si as the state of the network at timestep i
The parameters in the network are W, U, V, c, b which are shared across timestep
How does RNN work?

The Recurrent Neural Network consists of multiple fixed activation function units, one for
each time step. Each unit has an internal state which is called the hidden state of the unit. This
hidden state signifies the past knowledge that the network currently holds at a given time
step. This hidden state is updated at every time step to signify the change in the knowledge of
the network about the past. The hidden state is updated using the following recurrence
relation:-

Backpropagation Through Time (BPTT)

In RNN the neural network is in an ordered fashion and since in the ordered network each
variable is computed one at a time in a specified order like first h1 then h2 then h3 so on.
Hence we will apply backpropagation throughout all these hidden time states sequentially.
Backpropagation Through Time (BPTT) In RNN
● L(θ)(loss function) depends on h3
● h3 in turn depends on h2 and W
● h2 in turn depends on h1 and W
● h1 in turn depends on h0 and W
● where h0 is a constant starting state.

Issues of Standard RNNs

1. Vanishing Gradient: Text generation, machine translation, and stock market


prediction are just a few examples of the time-dependent and sequential data
problems that can be modelled with recurrent neural networks. You will discover,
though, that the gradient problem makes training RNN difficult.
2. Exploding Gradient: An Exploding Gradient occurs when a neural network is
being trained and the slope tends to grow exponentially rather than decay. Large
error gradients that build up during training lead to very large updates to the
neural network model weights, which is the source of this issue.

Training through RNN


1. A single-time step of the input is provided to the network.
2. Then calculate its current state using a set of current input and the previous state.
3. The current ht becomes ht-1 for the next time step.
4. One can go as many time steps according to the problem and join the information
from all the previous states.
5. Once all the time steps are completed the final current state is used to calculate the
output.
6. The output is then compared to the actual output i.e the target output and the error
is generated.
7. The error is then back-propagated to the network to update the weights and hence
the network (RNN) is trained using Backpropagation through time.

Advantages and Disadvantages of Recurrent Neural Network

Advantages
1. An RNN remembers each and every piece of information through time. It is useful
in time series prediction only because of the feature to remember previous inputs
as well. This is called Long Short Term Memory.
2. Recurrent neural networks are even used with convolutional layers to extend the
effective pixel neighborhood.

Disadvantages
1. Gradient vanishing and exploding problems.
2. Training an RNN is a very difficult task.
3. It cannot process very long sequences if using tanh or relu as an activation
function.

Applications of Recurrent Neural Network

1. Language Modelling and Generating Text


2. Speech Recognition
3. Machine Translation
4. Image Recognition, Face detection
5. Time series Forecasting

Variation Of Recurrent Neural Network (RNN)

To overcome the problems like vanishing gradient and exploding gradient descent several
new advanced versions of RNNs are formed some of these are as;
1. Bidirectional Neural Network (BiNN)
2. Long Short-Term Memory (LSTM)
Bidirectional Neural Network (BiNN)
A BiNN is a variation of a Recurrent Neural Network in which the input information flows in
both direction and then the output of both direction are combined to produce the input. BiNN
is useful in situations when the context of the input is more important such as Nlp tasks and
Time-series analysis problems.
Long Short-Term Memory (LSTM)
Long Short-Term Memory works on the read-write-and-forget principle where given the
input information network reads and writes the most useful information from the data and it
forgets about the information which is not important in predicting the output. For doing this
three new gates are introduced in the RNN. In this way, only the selected information is
passed through the network.

Difference between RNN and Simple Neural Network

RNN is considered to be the better version of deep neural when the data is sequential. There
are significant differences between the RNN and deep neural networks they are listed as:

Recurrent Neural Network Deep Neural Network

Weights are same across all the


Weights are different for each
layers number of a Recurrent Neural
layer of the network
Network

A Simple Deep Neural network


Recurrent Neural Networks are
does not have any special method for
used when the data is sequential and the
sequential data also here the the number
number of inputs is not predefined.
of inputs is fixed

The Numbers of parameter in the The Numbers of Parameter are


RNN are higher than in simple DNN lower than RNN
These problems also occur in
Exploding and vanishing gradients
DNN but these are not the major problem
is the the major drawback of RNN
with DNN
INTUITION OF RECURRENT NEURAL NETWORK

Understanding the intuition behind Recurrent Neural Networks (RNNs) involves recognizing
their capability to handle sequential data and learning dependencies over time. Here's an
intuitive explanation:

1. **Sequential Processing:**
- Imagine you're reading a sentence word by word. The meaning of the current word often
depends on the context provided by previous words.
- RNNs, unlike traditional feedforward neural networks, are designed to work with
sequences. They process inputs sequentially, and the output at each step is influenced not
only by the current input but also by the information they've remembered from previous
steps.

2. **Memory of Past Information:**


- RNNs have a hidden state that evolves over time as they process each element in the
sequence. This hidden state serves as a form of memory that can capture information about
what has been seen so far.
- This ability to maintain an internal state allows RNNs to retain information from earlier
inputs and use it to influence the processing of subsequent inputs.

3. **Applications of RNN Intuition:**


- Consider a language model predicting the next word in a sentence. The understanding of
the context built by previous words is crucial. RNNs can capture this sequential dependency
and make more informed predictions.
- In time series prediction, where past values influence future values, RNNs can learn
patterns and dependencies in the data over time.

4. **Challenges and Limitations:**


- While RNNs are powerful, they face challenges like the vanishing gradient problem,
where the influence of distant past information diminishes during training. This can make it
difficult for the network to learn long-range dependencies.
- More advanced RNN variants, such as LSTMs and GRUs, were introduced to address
these challenges by incorporating mechanisms to selectively remember or forget information.

5. **Biological Analogy:**
- Analogous to the way our brain processes information over time, RNNs attempt to mimic
sequential processing and memory retention. Each step in the sequence corresponds to a
moment in time, and the hidden state represents the accumulated knowledge up to that point.

6. **Training and Learning:**


- During training, RNNs adjust their parameters (weights and biases) based on the feedback
they receive for predictions at each step. This iterative learning process allows them to adapt
to the sequential patterns in the data.

In summary, the intuition behind RNNs lies in their ability to process sequential data,
maintain an internal state or memory, and learn dependencies over time. They have been
instrumental in various applications where the context of past information is crucial for
making predictions or generating meaningful outputs.
Exercise No: 1 SUMMARY GENERATION WITH VANILLA RNN.
Date:

AIM
To generate the summary for the loaded data set using vanilla RNN..

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load the Shakespeare dataset (or any other text dataset)
Step 3: Create a vocabulary
Step 4: Create mapping from characters to indices and vice versa
Step 5: Convert the text to numerical representation.
Step 6: Create input and target sequences.
Step 7: Initialize Batch size and Buffer size to shuffle the dataset.
Step 8: Also initialize Length of the vocabulary in chars, the embedding dimension and the number of
RNN units.
Step 9: Build the model.
Step 10: Compile the model.
Step 11: Display the model summary.

SOURCE CODE
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, SimpleRNN

# Load the Shakespeare dataset (or any other text dataset)


path_to_file = tf.keras.utils.get_file('shakespeare.txt',
'https://storage.googleapis.com/download.tensorflow.org/data/shakespeare.txt')

text = open(path_to_file, 'rb').read().decode(encoding='utf-8')

# Create a vocabulary
vocab = sorted(set(text))

# Create mapping from characters to indices and vice versa


char_to_idx = {u: i for i, u in enumerate(vocab)}
idx_to_char = np.array(vocab)
# Convert the text to numerical representation
text_as_int = np.array([char_to_idx[c] for c in text])

# Create input and target sequences


seq_length = 100
examples_per_epoch = len(text) // (seq_length + 1)
char_dataset = tf.data.Dataset.from_tensor_slices(text_as_int)

sequences = char_dataset.batch(seq_length + 1, drop_remainder=True)

def split_input_target(chunk):
input_text = chunk[:-1]
target_text = chunk[1:]
return input_text, target_text

dataset = sequences.map(split_input_target)

# Batch size
BATCH_SIZE = 64

# Buffer size to shuffle the dataset


BUFFER_SIZE = 10000

dataset = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)

# Length of the vocabulary in chars


vocab_size = len(vocab)

# The embedding dimension


embedding_dim = 256

# Number of RNN units


rnn_units = 1024

# Build the model


model = Sequential([
SimpleRNN(embedding_dim, return_sequences=True, input_shape=[None, vocab_size]),
Dense(vocab_size)
])
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')

# Display the model summary


model.summary()

OUTPUT

Model: "sequential_10"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
simple_rnn_15 (SimpleRNN) (None, None, 256) 82432

dense_10 (Dense) (None, None, 65) 16705

=================================================================
Total params: 99137 (387.25 KB)
Trainable params: 99137 (387.25 KB)
Non-trainable params: 0 (0.00 Byte)

RESULT
Thus the summary for the loaded data set has been displayed using vanilla RNN successfully.
Exercise No: 2 TIME SERIES PREDICTION.
Date:

AIM
To predict the time series and generate the synthetic time series data using RNN.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate synthetic time series data (sine wave)
Step 3: Create sequences for training
Step 4: Reshape data for RNN input (samples, timesteps, features)
Step 5: Build the RNN model
Step 6: Train the model
Step 7: Test the model by predicting the next values in the time series
Step 8: Generate predictions
Step 9: Plot the results

SOURCE CODE
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense

# Generate synthetic time series data (sine wave)


t = np.arange(0, 100, 0.1)
sin_wave = np.sin(t)

# Create sequences for training


seq_length = 10
X, y = [], []
for i in range(len(sin_wave) - seq_length):
seq_in = sin_wave[i:i + seq_length]
seq_out = sin_wave[i + seq_length]
X.append(seq_in)
y.append(seq_out)
X = np.array(X)
y = np.array(y)

# Reshape data for RNN input (samples, timesteps, features)


X = np.reshape(X, (X.shape[0], X.shape[1], 1))

# Build the RNN model


model = Sequential([
SimpleRNN(50, activation='relu', input_shape=(seq_length, 1)),
Dense(1)
])

model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model


model.fit(X, y, epochs=50, batch_size=16)

# Test the model by predicting the next values in the time series
test_input = sin_wave[-seq_length:]
test_input = np.reshape(test_input, (1, seq_length, 1))

# Generate predictions
predicted_values = []
for _ in range(100):
predicted_value = model.predict(test_input)
predicted_values.append(predicted_value[0, 0])
test_input = np.roll(test_input, -1, axis=1)
test_input[0, -1, 0] = predicted_value[0, 0]

# Plot the results


plt.plot(np.arange(len(sin_wave)), sin_wave, label='Original Data')
plt.plot(np.arange(len(sin_wave), len(sin_wave) + 100), predicted_values, label='Predictions')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.legend()
plt.show()
OUTPUT

Epoch 1/50
62/62 [==============================] - 4s 11ms/step - loss: 0.0523
Epoch 2/50
62/62 [==============================] - 1s 9ms/step - loss: 8.3264e-05
Epoch 3/50
62/62 [==============================] - 0s 7ms/step - loss: 1.9235e-05
Epoch 4/50
62/62 [==============================] - 1s 11ms/step - loss: 9.7245e-06
Epoch 5/50
62/62 [==============================] - 1s 9ms/step - loss: 6.9785e-06
Epoch 6/50
62/62 [==============================] - 0s 6ms/step - loss: 3.9190e-06
Epoch 7/50
62/62 [==============================] - 0s 7ms/step - loss: 3.0770e-06
Epoch 8/50
62/62 [==============================] - 0s 7ms/step - loss: 3.5341e-06
Epoch 9/50
62/62 [==============================] - 1s 9ms/step - loss: 2.4125e-06
Epoch 10/50
62/62 [==============================] - 0s 5ms/step - loss: 2.5179e-06
Epoch 11/50
62/62 [==============================] - 0s 3ms/step - loss: 2.4248e-06
Epoch 12/50
62/62 [==============================] - 0s 3ms/step - loss: 3.3002e-06
Epoch 13/50
62/62 [==============================] - 0s 3ms/step - loss: 2.9412e-06
Epoch 14/50
62/62 [==============================] - 0s 3ms/step - loss: 4.7054e-06
Epoch 15/50
62/62 [==============================] - 0s 4ms/step - loss: 5.6266e-06
Epoch 16/50
62/62 [==============================] - 0s 3ms/step - loss: 4.4291e-06
Epoch 17/50
62/62 [==============================] - 0s 3ms/step - loss: 1.4397e-05
Epoch 18/50
62/62 [==============================] - 0s 3ms/step - loss: 3.8717e-06
Epoch 19/50
62/62 [==============================] - 0s 4ms/step - loss: 4.2208e-06
Epoch 20/50
62/62 [==============================] - 0s 3ms/step - loss: 1.9835e-04
Epoch 21/50
62/62 [==============================] - 0s 3ms/step - loss: 8.4141e-04
Epoch 22/50
62/62 [==============================] - 0s 3ms/step - loss: 2.7785e-05
Epoch 23/50
62/62 [==============================] - 0s 3ms/step - loss: 5.8517e-06
Epoch 24/50
62/62 [==============================] - 0s 5ms/step - loss: 3.9655e-06
Epoch 25/50
62/62 [==============================] - 0s 7ms/step - loss: 3.3362e-06
Epoch 26/50
62/62 [==============================] - 0s 6ms/step - loss: 2.2043e-06
Epoch 27/50
62/62 [==============================] - 0s 6ms/step - loss: 1.8418e-06
Epoch 28/50
62/62 [==============================] - 0s 5ms/step - loss: 1.4853e-06
Epoch 29/50
62/62 [==============================] - 0s 5ms/step - loss: 2.8769e-06
Epoch 30/50
62/62 [==============================] - 0s 6ms/step - loss: 1.4655e-06
Epoch 31/50
62/62 [==============================] - 0s 6ms/step - loss: 1.4942e-06
Epoch 32/50
62/62 [==============================] - 0s 6ms/step - loss: 9.6966e-07
Epoch 33/50
62/62 [==============================] - 0s 6ms/step - loss: 1.7658e-06
Epoch 34/50
62/62 [==============================] - 0s 4ms/step - loss: 1.1101e-06
Epoch 35/50
62/62 [==============================] - 0s 4ms/step - loss: 9.3509e-07
Epoch 36/50
62/62 [==============================] - 0s 4ms/step - loss: 1.1097e-06
Epoch 37/50
62/62 [==============================] - 0s 4ms/step - loss: 1.1139e-06
Epoch 38/50
62/62 [==============================] - 0s 4ms/step - loss: 6.4273e-07
Epoch 39/50
62/62 [==============================] - 0s 3ms/step - loss: 2.0434e-06
Epoch 40/50
62/62 [==============================] - 0s 4ms/step - loss: 1.4235e-06
Epoch 41/50
62/62 [==============================] - 0s 3ms/step - loss: 9.1057e-07
Epoch 42/50
62/62 [==============================] - 0s 4ms/step - loss: 7.6581e-07
Epoch 43/50
62/62 [==============================] - 0s 4ms/step - loss: 6.7699e-07
Epoch 44/50
62/62 [==============================] - 0s 4ms/step - loss: 7.4931e-07
Epoch 45/50
62/62 [==============================] - 0s 3ms/step - loss: 9.2770e-07
Epoch 46/50
62/62 [==============================] - 0s 4ms/step - loss: 1.1455e-06
Epoch 47/50
62/62 [==============================] - 0s 3ms/step - loss: 2.0800e-06
Epoch 48/50
62/62 [==============================] - 0s 4ms/step - loss: 1.4866e-06
Epoch 49/50
62/62 [==============================] - 0s 3ms/step - loss: 7.3083e-07
Epoch 50/50
62/62 [==============================] - 0s 3ms/step - loss: 6.6306e-07
1/1 [==============================] - 0s 181ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 26ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 35ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 33ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 30ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 38ms/step
1/1 [==============================] - 0s 33ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 32ms/step
1/1 [==============================] - 0s 34ms/step
1/1 [==============================] - 0s 39ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 34ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 31ms/step
1/1 [==============================] - 0s 36ms/step
1/1 [==============================] - 0s 35ms/step
1/1 [==============================] - 0s 38ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 19ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 29ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 19ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 23ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 28ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 20ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 22ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 24ms/step
1/1 [==============================] - 0s 25ms/step
1/1 [==============================] - 0s 21ms/step
1/1 [==============================] - 0s 19ms/step

RESULT
Thus the time series data has been predicted and the synthetic time series data has been
generated using RNN successfully.
Exercise No: 3 SENTIMENT ANALYSIS.
Date:

AIM
To analyze the data set based on sentimental reviews that has been given by the reviewers
using RNN.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load IMDb dataset
Step 3: Pad sequences to ensure uniform length
Step 4: Build the RNN model
Step 5: Train the model
Step 6: Evaluate the model on the test set

SOURCE CODE

import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, SimpleRNN, Dense

# Load IMDb dataset


max_words = 10000 # Keep only the top 10,000 most frequently occurring words
maxlen = 100 # Limit the reviews to the first 100 words
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=max_words)

# Pad sequences to ensure uniform length


train_data = pad_sequences(train_data, maxlen=maxlen)
test_data = pad_sequences(test_data, maxlen=maxlen)

# Build the RNN model


model = Sequential([ Embedding(max_words, 32, input_length=maxlen), SimpleRNN(32),
Dense(1, activation='sigmoid')])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(train_data, train_labels, epochs=5, batch_size=128, validation_split=0.2)
# Evaluate the model on the test set
test_loss, test_acc = model.evaluate(test_data, test_labels)
print(f'Test accuracy: {test_acc * 100:.2f}%')
print(test_loss)
print(test_data)
print(test_lables)

OUTPUT
Epoch 1/5
157/157 [==============================] - 9s 47ms/step - loss: 0.6255 -
accuracy: 0.6248 - val_loss: 0.4370 - val_accuracy: 0.8048
Epoch 2/5
157/157 [==============================] - 6s 35ms/step - loss: 0.3547 -
accuracy: 0.8481 - val_loss: 0.3898 - val_accuracy: 0.8266
Epoch 3/5
157/157 [==============================] - 7s 44ms/step - loss: 0.2445 -
accuracy: 0.9054 - val_loss: 0.4100 - val_accuracy: 0.8144
Epoch 4/5
157/157 [==============================] - 6s 35ms/step - loss: 0.1699 -
accuracy: 0.9408 - val_loss: 0.4043 - val_accuracy: 0.8430
Epoch 5/5
157/157 [==============================] - 7s 46ms/step - loss: 0.1060 -
accuracy: 0.9661 - val_loss: 0.4635 - val_accuracy: 0.8156
782/782 [==============================] - 6s 8ms/step - loss: 0.4720 -
accuracy: 0.8104
Test accuracy: 81.04%
0.47195175290107727
[[ 0 0 0 ... 14 6 717]
[ 6 976 2078 ... 125 4 3077]
[ 4 5673 7 ... 9 57 975]
...
[ 0 0 0 ... 21 846 5518]
[ 0 1 11 ... 2302 7 470]
[ 56 96 346 ... 34 2005 2643]]
[0 1 1 ... 0 0 0]

RESULT
Thus the data has been analyzed based on sentimental reviews that have been suggested by the
reviewers using RNN successfully.
Exercise No: 4 LANGUAGE TRANSLATION.
Date:

AIM
To translate a sentence from one language to another (here english to french and french to
english) with the help of seq2seq model using RNN.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Supply an example data set.
Step 3: Tokenization the given input.
Step 4: Padding sequences.
Step 5: Build the RNN model
Step 6: Train the model
Step 7: Inference for translation
Step 8: Convert predicted sequence to text
Step 9: Test the translation

SOURCE CODE

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, SimpleRNN, Dense

# Example data (replace with your own dataset)


input_texts = ['I love programming.', 'Seq2Seq models are powerful.']
target_texts = ['J\'adore la programmation.', 'Les modèles Seq2Seq sont puissants.']

# Tokenization
input_tokenizer = tf.keras.preprocessing.text.Tokenizer()
input_tokenizer.fit_on_texts(input_texts)
input_vocab_size = len(input_tokenizer.word_index) + 1
input_sequences = input_tokenizer.texts_to_sequences(input_texts)

target_tokenizer = tf.keras.preprocessing.text.Tokenizer()
target_tokenizer.fit_on_texts(target_texts)
target_vocab_size = len(target_tokenizer.word_index) + 1
target_sequences = target_tokenizer.texts_to_sequences(target_texts)

# Padding sequences
max_seq_length = max(max(len(seq) for seq in input_sequences), max(len(seq) for seq in target_sequences))
input_sequences_padded = tf.keras.preprocessing.sequence.pad_sequences(input_sequences,
maxlen=max_seq_length, padding='post')
target_sequences_padded = tf.keras.preprocessing.sequence.pad_sequences(target_sequences,
maxlen=max_seq_length, padding='post')

# Build the RNN model


model = Sequential([
Embedding(input_vocab_size, 32, input_length=max_seq_length),
SimpleRNN(32, return_sequences=True),
Dense(target_vocab_size, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Train the model


model.fit(input_sequences_padded, target_sequences_padded, epochs=50)

# Inference for translation


def translate_sentence(input_sentence):
input_sequence = input_tokenizer.texts_to_sequences([input_sentence])
input_sequence_padded = tf.keras.preprocessing.sequence.pad_sequences(input_sequence,
maxlen=max_seq_length, padding='post')
predicted_sequence = model.predict(input_sequence_padded)

# Convert predicted sequence to text


predicted_sequence = np.argmax(predicted_sequence, axis=-1)
translated_sentence = ' '.join(target_tokenizer.index_word[idx] for idx in predicted_sequence[0] if idx != 0) #
Exclude padding

return translated_sentence

# Test the translation


test_sentence = 'I love programming.'
translated_sentence = translate_sentence(test_sentence)
print(f'Input Sentence: {test_sentence}')
print(f'Translated Sentence: {translated_sentence}')

OUTPUT
Epoch 1/50
1/1 [==============================] - 1s 1s/step - loss: 2.1877 - accuracy:
0.1000
Epoch 2/50
1/1 [==============================] - 0s 16ms/step - loss: 2.1722 - accuracy:
0.2000
Epoch 3/50
1/1 [==============================] - 0s 14ms/step - loss: 2.1567 - accuracy:
0.4000
Epoch 4/50
1/1 [==============================] - 0s 12ms/step - loss: 2.1411 - accuracy:
0.4000
Epoch 5/50
1/1 [==============================] - 0s 12ms/step - loss: 2.1255 - accuracy:
0.4000
Epoch 6/50
1/1 [==============================] - 0s 13ms/step - loss: 2.1097 - accuracy:
0.5000
Epoch 7/50
1/1 [==============================] - 0s 15ms/step - loss: 2.0937 - accuracy:
0.5000
Epoch 8/50
1/1 [==============================] - 0s 14ms/step - loss: 2.0775 - accuracy:
0.5000
Epoch 9/50
1/1 [==============================] - 0s 12ms/step - loss: 2.0609 - accuracy:
0.5000
Epoch 10/50
1/1 [==============================] - 0s 13ms/step - loss: 2.0439 - accuracy:
0.5000
Epoch 11/50
1/1 [==============================] - 0s 16ms/step - loss: 2.0266 - accuracy:
0.5000
Epoch 12/50
1/1 [==============================] - 0s 16ms/step - loss: 2.0087 - accuracy:
0.5000
Epoch 13/50
1/1 [==============================] - 0s 14ms/step - loss: 1.9904 - accuracy:
0.6000
Epoch 14/50
1/1 [==============================] - 0s 13ms/step - loss: 1.9715 - accuracy:
0.6000
Epoch 15/50
1/1 [==============================] - 0s 16ms/step - loss: 1.9521 - accuracy:
0.6000
Epoch 16/50
1/1 [==============================] - 0s 12ms/step - loss: 1.9321 - accuracy:
0.6000
Epoch 17/50
1/1 [==============================] - 0s 14ms/step - loss: 1.9115 - accuracy:
0.7000
Epoch 18/50
1/1 [==============================] - 0s 15ms/step - loss: 1.8903 - accuracy:
0.8000
Epoch 19/50
1/1 [==============================] - 0s 13ms/step - loss: 1.8684 - accuracy:
0.8000
Epoch 20/50
1/1 [==============================] - 0s 14ms/step - loss: 1.8460 - accuracy:
0.8000
Epoch 21/50
1/1 [==============================] - 0s 15ms/step - loss: 1.8229 - accuracy:
0.8000
Epoch 22/50
1/1 [==============================] - 0s 13ms/step - loss: 1.7993 - accuracy:
0.8000
Epoch 23/50
1/1 [==============================] - 0s 13ms/step - loss: 1.7751 - accuracy:
0.8000
Epoch 24/50
1/1 [==============================] - 0s 12ms/step - loss: 1.7504 - accuracy:
0.8000
Epoch 25/50
1/1 [==============================] - 0s 13ms/step - loss: 1.7251 - accuracy:
0.8000
Epoch 26/50
1/1 [==============================] - 0s 18ms/step - loss: 1.6993 - accuracy:
0.8000
Epoch 27/50
1/1 [==============================] - 0s 15ms/step - loss: 1.6731 - accuracy:
0.8000
Epoch 28/50
1/1 [==============================] - 0s 18ms/step - loss: 1.6464 - accuracy:
0.8000
Epoch 29/50
1/1 [==============================] - 0s 15ms/step - loss: 1.6193 - accuracy:
0.8000
Epoch 30/50
1/1 [==============================] - 0s 15ms/step - loss: 1.5918 - accuracy:
0.8000
Epoch 31/50
1/1 [==============================] - 0s 15ms/step - loss: 1.5640 - accuracy:
0.8000
Epoch 32/50
1/1 [==============================] - 0s 16ms/step - loss: 1.5358 - accuracy:
0.8000
Epoch 33/50
1/1 [==============================] - 0s 14ms/step - loss: 1.5074 - accuracy:
0.8000
Epoch 34/50
1/1 [==============================] - 0s 14ms/step - loss: 1.4786 - accuracy:
0.8000
Epoch 35/50
1/1 [==============================] - 0s 14ms/step - loss: 1.4497 - accuracy:
0.8000
Epoch 36/50
1/1 [==============================] - 0s 15ms/step - loss: 1.4205 - accuracy:
0.9000
Epoch 37/50
1/1 [==============================] - 0s 14ms/step - loss: 1.3911 - accuracy:
0.9000
Epoch 38/50
1/1 [==============================] - 0s 14ms/step - loss: 1.3617 - accuracy:
0.9000
Epoch 39/50
1/1 [==============================] - 0s 14ms/step - loss: 1.3322 - accuracy:
0.9000
Epoch 40/50
1/1 [==============================] - 0s 14ms/step - loss: 1.3026 - accuracy:
0.9000
Epoch 41/50
1/1 [==============================] - 0s 16ms/step - loss: 1.2731 - accuracy:
0.9000
Epoch 42/50
1/1 [==============================] - 0s 14ms/step - loss: 1.2436 - accuracy:
0.9000
Epoch 43/50
1/1 [==============================] - 0s 14ms/step - loss: 1.2144 - accuracy:
0.9000
Epoch 44/50
1/1 [==============================] - 0s 16ms/step - loss: 1.1853 - accuracy:
0.9000
Epoch 45/50
1/1 [==============================] - 0s 13ms/step - loss: 1.1566 - accuracy:
0.9000
Epoch 46/50
1/1 [==============================] - 0s 15ms/step - loss: 1.1281 - accuracy:
0.9000
Epoch 47/50
1/1 [==============================] - 0s 13ms/step - loss: 1.1001 - accuracy:
0.9000
Epoch 48/50
1/1 [==============================] - 0s 13ms/step - loss: 1.0725 - accuracy:
0.9000
Epoch 49/50
1/1 [==============================] - 0s 13ms/step - loss: 1.0454 - accuracy:
0.9000
Epoch 50/50
1/1 [==============================] - 0s 13ms/step - loss: 1.0188 - accuracy:
0.9000
1/1 [==============================] - 0s 183ms/step

Input Sentence: Seq2Seq models are powerful.


Translated Sentence: les modèles seq2seq sont puissants

RESULT
Thus the given sentence has been translated from english to french with the help of seq2seq
model successfully.
Exercise No: 5 STOCK PRICE PREDICTION.
Date:

AIM
To predict the stock market price value using Long Short-Term Memory (LSTM) networks.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load the dataset
Step 3: Normalize the data
Step 4: Create sequences for LSTM
Step 5: Split the data into training and testing sets
Step 6: Build the LSTM model
Step 7: Train the model
Step 8: Make predictions on the test set
Step 9: Plot the results

SOURCE CODE

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense

# Load the dataset


df = pd.read_csv("/content/tesla-stock-price.csv")
df = df[['date', 'close']]
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')

# Normalize the data


scaler = MinMaxScaler(feature_range=(0, 1))
df_scaled = scaler.fit_transform(df)

# Create sequences for LSTM


def create_sequences(data, sequence_length):
sequences, labels = [], []
for i in range(len(data) - sequence_length):
seq = data[i:i + sequence_length]
label = data[i + sequence_length]
sequences.append(seq)
labels.append(label)
return np.array(sequences), np.array(labels)

sequence_length = 10 # Adjust the sequence length as needed


X, y = create_sequences(df_scaled, sequence_length)

# Split the data into training and testing sets


train_size = int(len(X) * 0.80)
test_size = len(X) - train_size
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]

# Build the LSTM model


model = Sequential([
LSTM(units=50, activation='relu', input_shape=(X_train.shape[1], X_train.shape[2])),
Dense(units=1)
])

model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model


model.fit(X_train, y_train, epochs=50, batch_size=32)

# Make predictions on the test set


predictions = model.predict(X_test)

# Inverse transform the predictions and actual values


predictions = scaler.inverse_transform(predictions)
y_test_actual = scaler.inverse_transform(y_test.reshape(-1, 1))

# Plot the results


plt.figure(figsize=(14, 7))
plt.plot(df.index[train_size + sequence_length:], y_test_actual, label='Actual Stock Price')
plt.plot(df.index[train_size + sequence_length:], predictions, label='Predicted Stock Price')
plt.title('Stock Price Prediction using LSTM')
plt.xlabel('Date')
plt.ylabel('Stock Price')
plt.legend()
plt.show()

OUTPUT

Epoch 1/50
19/19 [==============================] - 2s 6ms/step - loss: 0.1579
Epoch 2/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0105
Epoch 3/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0053
Epoch 4/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0045
Epoch 5/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0041
Epoch 6/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0038
Epoch 7/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0036
Epoch 8/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0035
Epoch 9/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0034
Epoch 10/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0034
Epoch 11/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0033
Epoch 12/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0033
Epoch 13/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0032
Epoch 14/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0032
Epoch 15/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0031
Epoch 16/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0030
Epoch 17/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0030
Epoch 18/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0029
Epoch 19/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0031
Epoch 20/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0028
Epoch 21/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0026
Epoch 22/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0026
Epoch 23/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0026
Epoch 24/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0025
Epoch 25/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0025
Epoch 26/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0023
Epoch 27/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0023
Epoch 28/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0022
Epoch 29/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0024
Epoch 30/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0022
Epoch 31/50
19/19 [==============================] - 0s 8ms/step - loss: 0.0022
Epoch 32/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0021
Epoch 33/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0021
Epoch 34/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0021
Epoch 35/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0021
Epoch 36/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0020
Epoch 37/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0021
Epoch 38/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0020
Epoch 39/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0021
Epoch 40/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0020
Epoch 41/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0020
Epoch 42/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0020
Epoch 43/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0020
Epoch 44/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0020
Epoch 45/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0021
Epoch 46/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0020
Epoch 47/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0020
Epoch 48/50
19/19 [==============================] - 0s 6ms/step - loss: 0.0020
Epoch 49/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0019
Epoch 50/50
19/19 [==============================] - 0s 7ms/step - loss: 0.0019
5/5 [==============================] - 0s 4ms/step
RESULT
Thus the stock market price value for tesla has been predicted using LSTM network
successfully.
Exercise No: 6 PADDING SEQUENCES.
Date:

AIM
To Pad shorter sequences with a special padding token to make them equal in length.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Give the sample sequence in the form of a list.
Step 3: Print the padded sequence.

SOURCE CODE

from tensorflow.keras.preprocessing.sequence import pad_sequences

sequences = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]


padded_sequences = pad_sequences(sequences, padding='post') # or 'pre'

print(padded_sequences)

OUTPUT

[[1 2 0 0]
[3 4 5 0]
[6 7 8 9]]
RESULT
Thus the shorter sequences have been padded with a special padding token and made the length
to be equal.
Exercise No: 7 TRUNCATING SEQUENCES.
Date:

AIM
To Truncate longer sequences to a common length using Keras.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Give the sample sequence in the form of a list.
Step 3: Print the truncated sequence.

SOURCE CODE

from tensorflow.keras.preprocessing.sequence import pad_sequences

sequences = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]


truncated_sequences = pad_sequences(sequences, maxlen=3, truncating='post') # or 'pre'

print(truncated_sequences)

OUTPUT

[[0 1 2]
[3 4 5]
[6 7 8]]
RESULT
Thus the longer sequences have been truncated using keras.
Exercise No: 8 SENTIMENT ANALYSIS FOR MOVIE REVIEW DATA SET.
Date:

AIM
To analyze the movie reviews using keras in RNN.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load the IMDb dataset (you can replace this with your supermarket dataset)
Step 3: Limit the vocabulary to the top 10,000 words
Step 4: Pad sequences to a fixed length (adjust as needed)
Step 5: One-hot encode the labels
Step 6: Build the RNN model
Step 7: Compile the model
Step 8: Train the model
Step 9: Evaluate the model on the test set.

SOURCE CODE

import numpy as np
from tensorflow.keras.datasets import imdb
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, SimpleRNN, Dense
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical

# Load the IMDb dataset (you can replace this with your supermarket dataset)
# Limit the vocabulary to the top 10,000 words
max_words = 10000
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_words)

# Pad sequences to a fixed length (adjust as needed)


max_sequence_length = 200
x_train_padded = pad_sequences(x_train, maxlen=max_sequence_length)
x_test_padded = pad_sequences(x_test, maxlen=max_sequence_length)

# One-hot encode the labels


y_train_onehot = to_categorical(y_train)
y_test_onehot = to_categorical(y_test)

# Build the RNN model


embedding_dim = 50 # Dimension of word embeddings
rnn_units = 32 # Number of RNN units

model = Sequential([
Embedding(input_dim=max_words, output_dim=embedding_dim,
input_length=max_sequence_length),
SimpleRNN(units=rnn_units),
Dense(units=2, activation='softmax') # Binary classification (positive/negative)
])

# Compile the model


model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Train the model


epochs = 5 # You can adjust the number of epochs
batch_size = 64
model.fit(x_train_padded, y_train_onehot, epochs=epochs, batch_size=batch_size,
validation_split=0.2)

# Evaluate the model on the test set


test_loss, test_accuracy = model.evaluate(x_test_padded, y_test_onehot)
print(f'Test Accuracy: {test_accuracy * 100:.2f}%')
print (text_loss)

OUTPUT

Epoch 1/5
313/313 [==============================] - 23s 68ms/step - loss: 0.5995 -
accuracy: 0.6756 - val_loss: 0.4773 - val_accuracy: 0.7892
Epoch 2/5
313/313 [==============================] - 21s 67ms/step - loss: 0.3218 -
accuracy: 0.8679 - val_loss: 0.3620 - val_accuracy: 0.8540
Epoch 3/5
313/313 [==============================] - 20s 65ms/step - loss: 0.1623 -
accuracy: 0.9416 - val_loss: 0.4059 - val_accuracy: 0.8400
Epoch 4/5
313/313 [==============================] - 21s 67ms/step - loss: 0.0663 -
accuracy: 0.9808 - val_loss: 0.4871 - val_accuracy: 0.8232
Epoch 5/5
313/313 [==============================] - 20s 62ms/step - loss: 0.0447 -
accuracy: 0.9866 - val_loss: 0.5876 - val_accuracy: 0.8316
782/782 [==============================] - 10s 13ms/step - loss: 0.6090 -
accuracy: 0.8236
Test Accuracy: 82.36%
0.608951210975647

RESULT
Thus the movie reviews has been analyzed sentimentally with the help of keras in RNN.
Exercise No: 9 UNFOLDING A NEURAL NETWORK.
Date:

AIM
To represent the computation graph of recurrent neural networks (RNNs) across multiple time
steps.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Create a simple RNN model
Step 3: Print the summary of the model

SOURCE CODE
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Dense

# Create a simple RNN model


model = Sequential([
SimpleRNN(units=32, activation='tanh', input_shape=(None, 1), return_sequences=True),
Dense(units=1)])
# Print the summary of the model
model.summary()
OUTPUT

Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
simple_rnn_6 (SimpleRNN) (None, None, 32) 1088

dense_6 (Dense) (None, None, 1) 33

=================================================================
Total params: 1121 (4.38 KB)
Trainable params: 1121 (4.38 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

RESULT
Thus the computation graph of recurrent neural networks (RNNs) across multiple time steps has
been represented successfully.
Exercise No: 10 VISUALIZING THE UNFOLDED DATA.
Date:

AIM
To visualize the computation graph of recurrent neural networks (RNNs) across multiple time
steps.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Visualize the model.
Step 3: Plot the model.

SOURCE CODE
from tensorflow.keras.utils import plot_model

# Visualize the model


plot_model(model, to_file='unfolded_rnn.png', show_shapes=True, show_layer_names=False)

OUTPUT

RESULT
Thus the computation graph of recurrent neural networks (RNNs) across multiple time steps has
been visualized successfully.
AUTO ENCODERS - STRUCTURE AND TRAINING

1. Structure:

Encoder:
- *Input Layer:* Represents the input data.
- *Hidden Layers:* Comprise the encoding layers responsible for learning a compact
representation of the input.
- *Code Layer:* The last hidden layer, also known as the bottleneck or latent space,
encodes the essential features of the input.

Decoder:
- *Code Layer:* The representation from the encoder is input to the decoder.
- *Hidden Layers:* These layers decode the representation back into the original input
space.
- *Output Layer:* Represents the reconstructed input.

Connections:
- Weights are learned during training to capture the mapping between the input and the
encoded representation.

Activation Functions:
- Commonly used activation functions include ReLU (Rectified Linear Unit) for hidden
layers and Sigmoid or Hyperbolic Tangent (tanh) for the output layer.

Loss Function:
- The loss function measures the difference between the input and the reconstructed
output. Mean Squared Error (MSE) is commonly used: \( \text{MSE} = \frac{1}{n}\
sum_{i=1}^{n}(X_i - \hat{X}_i)^2 \), where \(X_i\) is the input and \(\hat{X}_i\) is the
reconstructed output.
2. Training:

Objective:
- Minimize the reconstruction error by adjusting the weights in the network.

Forward Pass:
- Pass the input through the encoder to obtain the encoded representation.
- Pass the encoded representation through the decoder to obtain the reconstructed output.

Backward Pass (Backpropagation):


- Calculate the loss between the input and the reconstructed output.
- Propagate the error backward through the network.
- Update the weights using optimization algorithms like Gradient Descent, Adam, or
others.

Regularization:
- Techniques like dropout or L1/L2 regularization may be employed to prevent overfitting.
Hyperparameter Tuning:
- Adjust parameters such as learning rate, batch size, and architecture to optimize
performance.
Early Stopping:
- Monitor the validation loss during training and stop when it ceases to improve,
preventing overfitting.
Variations:
- Variational Autoencoders (VAEs) introduce probabilistic elements, incorporating a
sampling process in the latent space.

Applications:
- Dimensionality reduction, data denoising, feature learning, and generation of new data
samples.
Autoencoders are versatile and can be adapted for various tasks based on the architecture and
training objectives. They have proven effective in learning useful representations of data for a
wide range of applications in machine learning and deep learning.
AUTO ENCODERS AND BOLTZMANN MACHINE - AN INTRODUCTION

Autoencoders

**Definition:**
Autoencoders are a type of artificial neural network used for unsupervised learning. The
network is designed to encode the input data into a lower-dimensional representation and
then decode it back to reconstruct the input. The objective is to minimize the difference
between the input and the reconstructed output, encouraging the network to learn a
compressed representation of the data.

**Architecture:**
- **Encoder:** Takes the input and transforms it into a lower-dimensional representation.
- **Decoder:** Takes the encoded representation and reconstructs the input.
- **Loss Function:** Typically, Mean Squared Error (MSE) is used to measure the
difference between the input and the output.

**Applications:**
- Dimensionality reduction.
- Data denoising.
- Anomaly detection.
- Feature learning.

Boltzmann Machines

**Definition:**
Boltzmann Machines are a type of stochastic, generative, and energy-based model. They
consist of binary units (nodes) that are connected with undirected edges. Learning in
Boltzmann Machines is based on minimizing the energy of the system, and they can be used
for both supervised and unsupervised learning tasks.

**Architecture:**
- **Nodes (or Neurons):** Represent binary states (0 or 1).
- **Connections (or Edges):** Undirected connections between nodes, forming a fully
connected graph.
- **Energy Function:** Defines the compatibility between the states of connected nodes.
- **Learning Algorithm:** Often relies on Contrastive Divergence or Gibbs sampling.

**Types:**
- **Restricted Boltzmann Machines (RBMs):** A simplified version of Boltzmann Machines
with a bipartite structure, making training more efficient.

**Applications:**
- Collaborative filtering (recommendation systems).
- Feature learning.
- Dimensionality reduction.
- Generative modeling.

**Contrast Between Autoencoders and Boltzmann Machines:**


- Autoencoders are deterministic, while Boltzmann Machines are stochastic.
- Autoencoders are based on feedforward neural networks, whereas Boltzmann Machines
have a recurrent and undirected graphical structure.
- Training Autoencoders often involves backpropagation and optimization techniques, while
training Boltzmann Machines involves energy minimization through Markov Chain Monte
Carlo methods.

Both Autoencoders and Boltzmann Machines are powerful tools in the field of machine
learning, each with its unique strengths and applications. The choice between them depends
on the specific problem at hand and the characteristics of the data.
Exercise No: 11 IMAGE DENOISING WITH AUTO ENCODERS.
Date:

AIM
To train an autoencoder to denoise images corrupted with random noise.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Add random noise to images.
Step 3: Encode and decode the images.
Step 4: Plot original, noisy, and denoised images

SOURCE CODE
import numpy as np
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.callbacks import EarlyStopping
import matplotlib.pyplot as plt

(x_train, _), (x_test, _) = mnist.load_data()


x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# Add random noise to images


noise_factor = 0.5
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)

x_train_noisy = np.clip(x_train_noisy, 0., 1.)


x_test_noisy = np.clip(x_test_noisy, 0., 1.)

input_img = Input(shape=(28, 28, 1))

# Encoder
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# Decoder
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)


autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

decoded_imgs = autoencoder.predict(x_test_noisy)

# Plot original, noisy, and denoised images


n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
# Original Images
ax = plt.subplot(3, n, i + 1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# Noisy Input Images


ax = plt.subplot(3, n, i + 1 + n)
plt.imshow(x_test_noisy[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

# Denoised Images
ax = plt.subplot(3, n, i + 1 + 2 * n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
OUTPUT

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-


datasets/mnist.npz
11490434/11490434 [==============================] - 0s 0us/step

RESULT
Thus the computation graph of recurrent neural networks (RNNs) across multiple time steps has
been visualized successfully.
Exercise No: 12 DATA IMPUTATION WITH AUTO ENCODERS.
Date:

AIM
To implement data imputation using a denoising autoencoder using python.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate synthetic data with missing values
Step 3: Normalize data
Step 4: Split data into train and test sets
Step 5: Define a denoising autoencoder model
Step 6: Add Gaussian noise to the input
Step 7: Encode & Decode
Step 8: Build the denoising autoencoder model
Step 9: Train the model
Step 10: Impute missing values using the trained denoising autoencoder
Step 11: Invert the normalization
Step 12: Display the results

SOURCE CODE
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, GaussianNoise
from tensorflow.keras.models import Model
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
# Generate synthetic data with missing values
np.random.seed(42)
data = np.random.randn(1000, 10) # 1000 samples, 10 features
missing_mask = np.random.rand(*data.shape) < 0.1 # 10% missing values
data[missing_mask] = np.nan
# Normalize data
scaler = MinMaxScaler()
data = scaler.fit_transform(data)
# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(data, data, test_size=0.2, random_state=42)
# Define a denoising autoencoder model
def build_denoising_autoencoder(input_dim):
input_layer = Input(shape=(input_dim,))
# Add Gaussian noise to the input
noisy_input = GaussianNoise(0.5)(input_layer)
# Encoder
encoded = Dense(64, activation='relu')(noisy_input)
# Decoder
decoded = Dense(input_dim, activation='sigmoid')(encoded)
model = Model(inputs=input_layer, outputs=decoded)
model.compile(optimizer='adam', loss='mean_squared_error')
return model
# Build the denoising autoencoder model
input_dim = X_train.shape[1]
denoising_autoencoder = build_denoising_autoencoder(input_dim)
# Train the model
denoising_autoencoder.fit(X_train, X_train, epochs=50, batch_size=32, shuffle=True,
validation_data=(X_test, X_test))
# Impute missing values using the trained denoising autoencoder
imputed_data = denoising_autoencoder.predict(data)
# Invert the normalization
imputed_data = scaler.inverse_transform(imputed_data)
# Display the results
print("Original Data:")
print(data[:5])
print("\nImputed Data:")
print(imputed_data[:5])

OUTPUT

Epoch 1/50
25/25 [==============================] - 3s 38ms/step - loss: nan - val_loss:
nan
Epoch 2/50
25/25 [==============================] - 0s 14ms/step - loss: nan - val_loss:
nan
Epoch 3/50
25/25 [==============================] - 0s 14ms/step - loss: nan - val_loss:
nan
Epoch 4/50
25/25 [==============================] - 0s 13ms/step - loss: nan - val_loss:
nan
Epoch 5/50
25/25 [==============================] - 0s 12ms/step - loss: nan - val_loss:
nan
Epoch 6/50
25/25 [==============================] - 0s 13ms/step - loss: nan - val_loss:
nan
Epoch 7/50
25/25 [==============================] - 0s 12ms/step - loss: nan - val_loss:
nan
Epoch 8/50
25/25 [==============================] - 0s 18ms/step - loss: nan - val_loss:
nan
Epoch 9/50
25/25 [==============================] - 0s 10ms/step - loss: nan - val_loss:
nan
Epoch 10/50
25/25 [==============================] - 0s 11ms/step - loss: nan - val_loss:
nan
Epoch 11/50
25/25 [==============================] - 0s 14ms/step - loss: nan - val_loss:
nan
Epoch 12/50
25/25 [==============================] - 0s 12ms/step - loss: nan - val_loss:
nan
Epoch 13/50
25/25 [==============================] - 0s 12ms/step - loss: nan - val_loss:
nan
Epoch 14/50
25/25 [==============================] - 0s 18ms/step - loss: nan - val_loss:
nan
Epoch 15/50
25/25 [==============================] - 0s 12ms/step - loss: nan - val_loss:
nan
Epoch 16/50
25/25 [==============================] - 0s 10ms/step - loss: nan - val_loss:
nan
Epoch 17/50
25/25 [==============================] - 0s 14ms/step - loss: nan - val_loss:
nan
Epoch 18/50
25/25 [==============================] - 0s 9ms/step - loss: nan - val_loss:
nan
Epoch 19/50
25/25 [==============================] - 0s 9ms/step - loss: nan - val_loss:
nan
Epoch 20/50
25/25 [==============================] - 0s 9ms/step - loss: nan - val_loss:
nan
Epoch 21/50
25/25 [==============================] - 0s 8ms/step - loss: nan - val_loss:
nan
Epoch 22/50
25/25 [==============================] - 0s 8ms/step - loss: nan - val_loss:
nan
Epoch 23/50
25/25 [==============================] - 0s 7ms/step - loss: nan - val_loss:
nan
Epoch 24/50
25/25 [==============================] - 0s 6ms/step - loss: nan - val_loss:
nan
Epoch 25/50
25/25 [==============================] - 0s 8ms/step - loss: nan - val_loss:
nan
Epoch 26/50
25/25 [==============================] - 0s 8ms/step - loss: nan - val_loss:
nan
Epoch 27/50
25/25 [==============================] - 0s 11ms/step - loss: nan - val_loss:
nan
Epoch 28/50
25/25 [==============================] - 0s 6ms/step - loss: nan - val_loss:
nan
Epoch 29/50
25/25 [==============================] - 0s 8ms/step - loss: nan - val_loss:
nan
Epoch 30/50
25/25 [==============================] - 0s 12ms/step - loss: nan - val_loss:
nan
Epoch 31/50
25/25 [==============================] - 0s 11ms/step - loss: nan - val_loss:
nan
Epoch 32/50
25/25 [==============================] - 0s 6ms/step - loss: nan - val_loss:
nan
Epoch 33/50
25/25 [==============================] - 0s 6ms/step - loss: nan - val_loss:
nan
Epoch 34/50
25/25 [==============================] - 0s 5ms/step - loss: nan - val_loss:
nan
Epoch 35/50
25/25 [==============================] - 0s 6ms/step - loss: nan - val_loss:
nan
Epoch 36/50
25/25 [==============================] - 0s 5ms/step - loss: nan - val_loss:
nan
Epoch 37/50
25/25 [==============================] - 0s 7ms/step - loss: nan - val_loss:
nan
Epoch 38/50
25/25 [==============================] - 0s 5ms/step - loss: nan - val_loss:
nan
Epoch 39/50
25/25 [==============================] - 0s 6ms/step - loss: nan - val_loss:
nan
Epoch 40/50
25/25 [==============================] - 0s 6ms/step - loss: nan - val_loss:
nan
Epoch 41/50
25/25 [==============================] - 0s 8ms/step - loss: nan - val_loss:
nan
Epoch 42/50
25/25 [==============================] - 0s 6ms/step - loss: nan - val_loss:
nan
Epoch 43/50
25/25 [==============================] - 0s 7ms/step - loss: nan - val_loss:
nan
Epoch 44/50
25/25 [==============================] - 0s 5ms/step - loss: nan - val_loss:
nan
Epoch 45/50
25/25 [==============================] - 0s 4ms/step - loss: nan - val_loss:
nan
Epoch 46/50
25/25 [==============================] - 0s 4ms/step - loss: nan - val_loss:
nan
Epoch 47/50
25/25 [==============================] - 0s 3ms/step - loss: nan - val_loss:
nan
Epoch 48/50
25/25 [==============================] - 0s 4ms/step - loss: nan - val_loss:
nan
Epoch 49/50
25/25 [==============================] - 0s 3ms/step - loss: nan - val_loss:
nan
Epoch 50/50
25/25 [==============================] - 0s 4ms/step - loss: nan - val_loss:
nan
32/32 [==============================] - 0s 1ms/step
Original Data:
[[0.63136875 0.43313218 nan nan 0.47814929 nan
0.72475122 0.64826053 0.39751792 0.52909853]
[0.49419256 0.38251493 0.57041536 0.17856373 0.23272253 0.36751612
0.29596015 0.5810104 0.32883371 0.25100169]
[0.7698026 0.41960521 0.54184995 0.26326844 0.42707577 0.46237903
0.27310451 0.59012929 0.3769754 0.41041847]
[0.47443492 0.7408159 0.52858119 0.32690763 0.65211509 0.27471813
0.49806003 0.24357553 0.26302933 0.47991978]
[0.66590846 0.48099296 0.51185288 0.45809287 0.27328705 0.3453146
0.38730704 0.69124831 nan 0.20110629]]

Imputed Data:
[[nan nan nan nan nan nan nan nan nan nan]
[nan nan nan nan nan nan nan nan nan nan]
[nan nan nan nan nan nan nan nan nan nan]
[nan nan nan nan nan nan nan nan nan nan]
[nan nan nan nan nan nan nan nan nan nan]]

RESULT
Thus the data imputation has been implemented using a denoising autoencoder in python
successfully.
Exercise No: 13 ANOMALY DETECTION WITH AUTO ENCODERS.
Date:

AIM
To detect anomaly data using a denoising autoencoder in python.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate synthetic data with missing values
Step 3: Normalize data
Step 4: Split data into train and test sets
Step 5: Define a denoising autoencoder model
Step 6: Add Gaussian noise to the input
Step 7: Encode & Decode
Step 8: Build and train the denoising autoencoder on normal data
Step 9: Evaluate the model on normal and anomaly data
Step 10: Calculate reconstruction errors
Step 11: Set a threshold for anomaly detection (adjust based on your data)
Step 12: Identify anomalies
Step 13: Plot the results
Step 14: Print detected anomalies

SOURCE CODE
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, GaussianNoise
from tensorflow.keras.models import Model
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt

# Generate synthetic normal data


np.random.seed(42)
normal_data = np.random.randn(1000, 10) # 1000 samples, 10 features

# Normalize normal data


scaler = MinMaxScaler()
normal_data = scaler.fit_transform(normal_data)

# Introduce anomalies in the test data


anomaly_data = normal_data.copy()
anomaly_data[:10, :5] = 2.5 # Add anomalies to the first 10 samples

# Split data into train and test sets


X_train, X_test, _, _ = train_test_split(normal_data, normal_data, test_size=0.2, random_state=42)

# Define a denoising autoencoder model


def build_denoising_autoencoder(input_dim):
input_layer = Input(shape=(input_dim,))
# Add Gaussian noise to the input
noisy_input = GaussianNoise(0.5)(input_layer)
# Encoder
encoded = Dense(64, activation='relu')(noisy_input)
# Decoder
decoded = Dense(input_dim, activation='sigmoid')(encoded)

model = Model(inputs=input_layer, outputs=decoded)


model.compile(optimizer='adam', loss='mean_squared_error')

return model

# Build and train the denoising autoencoder on normal data


input_dim = X_train.shape[1]
denoising_autoencoder = build_denoising_autoencoder(input_dim)
denoising_autoencoder.fit(X_train, X_train, epochs=50, batch_size=32, shuffle=True,
validation_data=(X_test, X_test))

# Evaluate the model on normal and anomaly data


normal_reconstructed = denoising_autoencoder.predict(normal_data)
anomaly_reconstructed = denoising_autoencoder.predict(anomaly_data)

# Calculate reconstruction errors


normal_errors = np.mean(np.square(normal_data - normal_reconstructed), axis=1)
anomaly_errors = np.mean(np.square(anomaly_data - anomaly_reconstructed), axis=1)

# Set a threshold for anomaly detection (adjust based on your data)


threshold = 0.05
# Identify anomalies
anomalies = anomaly_errors > threshold

# Plot the results


plt.scatter(range(len(normal_errors)), normal_errors, label='Normal Data')
plt.scatter(range(len(anomaly_errors)), anomaly_errors, label='Anomalies', color='red')
plt.axhline(y=threshold, color='black', linestyle='--', label='Threshold')
plt.xlabel('Sample Index')
plt.ylabel('Reconstruction Error')
plt.legend()
plt.show()

# Print detected anomalies


print("Detected Anomalies:")
print(np.where(anomalies)[0])

OUTPUT

Epoch 1/50
25/25 [==============================] - 2s 25ms/step - loss: 0.0264 - val_loss: 0.0243
Epoch 2/50
25/25 [==============================] - 0s 12ms/step - loss: 0.0241 - val_loss: 0.0237
Epoch 3/50
25/25 [==============================] - 0s 6ms/step - loss: 0.0236 - val_loss: 0.0232
Epoch 4/50
25/25 [==============================] - 0s 5ms/step - loss: 0.0231 - val_loss: 0.0227
Epoch 5/50
25/25 [==============================] - 0s 13ms/step - loss: 0.0223 - val_loss: 0.0223
Epoch 6/50
25/25 [==============================] - 0s 7ms/step - loss: 0.0223 - val_loss: 0.0220
Epoch 7/50
25/25 [==============================] - 0s 18ms/step - loss: 0.0219 - val_loss: 0.0218
Epoch 8/50
25/25 [==============================] - 0s 7ms/step - loss: 0.0219 - val_loss: 0.0216
Epoch 9/50
25/25 [==============================] - 0s 7ms/step - loss: 0.0217 - val_loss: 0.0215
Epoch 10/50
25/25 [==============================] - 0s 7ms/step - loss: 0.0215 - val_loss: 0.0213
Epoch 11/50
25/25 [==============================] - 0s 6ms/step - loss: 0.0215 - val_loss: 0.0211
Epoch 12/50
25/25 [==============================] - 0s 11ms/step - loss: 0.0214 - val_loss: 0.0208
Epoch 13/50
25/25 [==============================] - 0s 15ms/step - loss: 0.0215 - val_loss: 0.0208
Epoch 14/50
25/25 [==============================] - 0s 9ms/step - loss: 0.0217 - val_loss: 0.0208
Epoch 15/50
25/25 [==============================] - 0s 9ms/step - loss: 0.0214 - val_loss: 0.0207
Epoch 16/50
25/25 [==============================] - 0s 15ms/step - loss: 0.0212 - val_loss: 0.0207
Epoch 17/50
25/25 [==============================] - 0s 18ms/step - loss: 0.0214 - val_loss: 0.0205
Epoch 18/50
25/25 [==============================] - 0s 12ms/step - loss: 0.0216 - val_loss: 0.0206
Epoch 19/50
25/25 [==============================] - 0s 17ms/step - loss: 0.0214 - val_loss: 0.0205
Epoch 20/50
25/25 [==============================] - 0s 18ms/step - loss: 0.0215 - val_loss: 0.0206
Epoch 21/50
25/25 [==============================] - 0s 12ms/step - loss: 0.0212 - val_loss: 0.0206
Epoch 22/50
25/25 [==============================] - 0s 12ms/step - loss: 0.0214 - val_loss: 0.0205
Epoch 23/50
25/25 [==============================] - 0s 17ms/step - loss: 0.0215 - val_loss: 0.0204
Epoch 24/50
25/25 [==============================] - 0s 10ms/step - loss: 0.0216 - val_loss: 0.0204
Epoch 25/50
25/25 [==============================] - 0s 9ms/step - loss: 0.0213 - val_loss: 0.0203
Epoch 26/50
25/25 [==============================] - 0s 4ms/step - loss: 0.0213 - val_loss: 0.0204
Epoch 27/50
25/25 [==============================] - 0s 7ms/step - loss: 0.0215 - val_loss: 0.0205
Epoch 28/50
25/25 [==============================] - 0s 9ms/step - loss: 0.0213 - val_loss: 0.0204
Epoch 29/50
25/25 [==============================] - 0s 14ms/step - loss: 0.0214 - val_loss: 0.0204
Epoch 30/50
25/25 [==============================] - 0s 6ms/step - loss: 0.0214 - val_loss: 0.0204
Epoch 31/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0211 - val_loss: 0.0204
Epoch 32/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0213 - val_loss: 0.0205
Epoch 33/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0212 - val_loss: 0.0203
Epoch 34/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0212 - val_loss: 0.0202
Epoch 35/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0213 - val_loss: 0.0202
Epoch 36/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0213 - val_loss: 0.0203
Epoch 37/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0215 - val_loss: 0.0204
Epoch 38/50
25/25 [==============================] - 0s 4ms/step - loss: 0.0213 - val_loss: 0.0204
Epoch 39/50
25/25 [==============================] - 0s 4ms/step - loss: 0.0212 - val_loss: 0.0204
Epoch 40/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0213 - val_loss: 0.0204
Epoch 41/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0210 - val_loss: 0.0203
Epoch 42/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0214 - val_loss: 0.0204
Epoch 43/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0212 - val_loss: 0.0203
Epoch 44/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0212 - val_loss: 0.0203
Epoch 45/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0213 - val_loss: 0.0203
Epoch 46/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0215 - val_loss: 0.0204
Epoch 47/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0213 - val_loss: 0.0203
Epoch 48/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0213 - val_loss: 0.0203
Epoch 49/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0216 - val_loss: 0.0203
Epoch 50/50
25/25 [==============================] - 0s 3ms/step - loss: 0.0213 - val_loss: 0.0203
32/32 [==============================] - 0s 1ms/step
32/32 [==============================] - 0s 1ms/step

Detected Anomalies:
[ 0 1 2 3 4 5 6 7 8 9 289 371 419 635 802]

RESULT
Thus the anomaly data has been detected using denoising autoencoder in python successfully.
Exercise No: 14 FEATURE LEARNING REPRESENTATION
Date:

AIM
To represent the feature learning techniques using different activation function.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate synthetic data with missing values
Step 3: Split the dataset into training and testing sets
Step 4: Standardize the features
Step 5: Build a simple neural network for feature learning
Step 6: Compile the model
Step 7: Train the model
Step 8: Evaluate the model on the test set
Step 9: Plot training history

SOURCE CODE
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

# Generate a synthetic dataset


X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)

# Split the dataset 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)

# Standardize the features


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Build a simple neural network for feature learning
model = Sequential()
model.add(Dense(32, activation='relu', input_shape=(X_train.shape[1],)))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# Compile the model


model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the model


history = model.fit(X_train, y_train, epochs=20, batch_size=32, validation_split=0.2, verbose=1)

# Evaluate the model on the test set


test_loss, test_acc = model.evaluate(X_test, y_test)
print(f'Test Accuracy: {test_acc * 100:.2f}%')

# Plot training history


plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()

OUTPUT

Epoch 1/20
20/20 [==============================] - 5s 40ms/step - loss: 0.7068 - accuracy: 0.5234 - val_loss:
0.6130 - val_accuracy: 0.7125
Epoch 2/20
20/20 [==============================] - 0s 9ms/step - loss: 0.6179 - accuracy: 0.6703 - val_loss:
0.5536 - val_accuracy: 0.7563
Epoch 3/20
20/20 [==============================] - 0s 6ms/step - loss: 0.5538 - accuracy: 0.7359 - val_loss:
0.4997 - val_accuracy: 0.8125
Epoch 4/20
20/20 [==============================] - 0s 11ms/step - loss: 0.4969 - accuracy: 0.7984 - val_loss:
0.4512 - val_accuracy: 0.8500
Epoch 5/20
20/20 [==============================] - 0s 13ms/step - loss: 0.4495 - accuracy: 0.8234 - val_loss:
0.4048 - val_accuracy: 0.8687
Epoch 6/20
20/20 [==============================] - 0s 11ms/step - loss: 0.4074 - accuracy: 0.8484 - val_loss:
0.3681 - val_accuracy: 0.8875
Epoch 7/20
20/20 [==============================] - 0s 9ms/step - loss: 0.3759 - accuracy: 0.8578 - val_loss:
0.3395 - val_accuracy: 0.8938
Epoch 8/20
20/20 [==============================] - 0s 4ms/step - loss: 0.3505 - accuracy: 0.8750 - val_loss:
0.3173 - val_accuracy: 0.8875
Epoch 9/20
20/20 [==============================] - 0s 3ms/step - loss: 0.3304 - accuracy: 0.8766 - val_loss:
0.3031 - val_accuracy: 0.8938
Epoch 10/20
20/20 [==============================] - 0s 4ms/step - loss: 0.3155 - accuracy: 0.8828 - val_loss:
0.2897 - val_accuracy: 0.9000
Epoch 11/20
20/20 [==============================] - 0s 4ms/step - loss: 0.3044 - accuracy: 0.8891 - val_loss:
0.2809 - val_accuracy: 0.9000
Epoch 12/20
20/20 [==============================] - 0s 4ms/step - loss: 0.2945 - accuracy: 0.8906 - val_loss:
0.2766 - val_accuracy: 0.9062
Epoch 13/20
20/20 [==============================] - 0s 4ms/step - loss: 0.2871 - accuracy: 0.8922 - val_loss:
0.2705 - val_accuracy: 0.9062
Epoch 14/20
20/20 [==============================] - 0s 4ms/step - loss: 0.2798 - accuracy: 0.8953 - val_loss:
0.2659 - val_accuracy: 0.9000
Epoch 15/20
20/20 [==============================] - 0s 4ms/step - loss: 0.2729 - accuracy: 0.9016 - val_loss:
0.2639 - val_accuracy: 0.8938
Epoch 16/20
20/20 [==============================] - 0s 4ms/step - loss: 0.2668 - accuracy: 0.9047 - val_loss:
0.2633 - val_accuracy: 0.8938
Epoch 17/20
20/20 [==============================] - 0s 3ms/step - loss: 0.2617 - accuracy: 0.9062 - val_loss:
0.2622 - val_accuracy: 0.8938
Epoch 18/20
20/20 [==============================] - 0s 4ms/step - loss: 0.2557 - accuracy: 0.9094 - val_loss:
0.2614 - val_accuracy: 0.8875
Epoch 19/20
20/20 [==============================] - 0s 5ms/step - loss: 0.2519 - accuracy: 0.9156 - val_loss:
0.2619 - val_accuracy: 0.8938
Epoch 20/20
20/20 [==============================] - 0s 4ms/step - loss: 0.2447 - accuracy: 0.9172 - val_loss:
0.2631 - val_accuracy: 0.8938
7/7 [==============================] - 0s 2ms/step - loss: 0.4029 - accuracy: 0.8500
Test Accuracy: 85.00%
RESULT
Thus the feature learning has been represented using different activation function suuccessfully.
Exercise No: 15 REPRESENTATION LEARNING
Date:

AIM
To represent the use of autoencoders by means of representation learning.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate synthetic data with missing values
Step 3: Split the dataset into training and testing sets
Step 4: Standardize the features
Step 5: Build a simple autoencoder for representation learning
Step 6: Compile the autoencoder
Step 7: Train the model
Step 8: Extract the learned representations using the encoder
Step 9: Visualize the original and encoded data
Step 10: Evaluate the autoencoder on the test set

SOURCE CODE
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

# Generate a synthetic dataset


X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)

# Split the dataset 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)

# Standardize the features


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Build a simple autoencoder for representation learning


input_dim = X_train.shape[1]

# Encoder
encoder_input = Input(shape=(input_dim,))
encoded = Dense(10, activation='relu')(encoder_input)

# Decoder
decoded = Dense(input_dim, activation='sigmoid')(encoded)

# Autoencoder model
autoencoder = Model(encoder_input, decoded)

# Compile the autoencoder


autoencoder.compile(optimizer='adam', loss='mean_squared_error')

# Train the autoencoder


autoencoder.fit(X_train, X_train, epochs=50, batch_size=32, shuffle=True, validation_data=(X_test,
X_test), verbose=1)

# Extract the learned representations using the encoder


encoder = Model(encoder_input, encoded)
encoded_data_train = encoder.predict(X_train)
encoded_data_test = encoder.predict(X_test)

# Visualize the original and encoded data


plt.scatter(encoded_data_train[:, 0], encoded_data_train[:, 1], c=y_train, cmap='viridis')
plt.title('Learned Representations (Training Data)')
plt.xlabel('Encoded Feature 1')
plt.ylabel('Encoded Feature 2')
plt.show()

# Evaluate the autoencoder on the test set


test_loss = autoencoder.evaluate(X_test, X_test)
print(f'Test Loss: {test_loss:.4f}')
OUTPUT

Epoch 1/50
25/25 [==============================] - 1s 8ms/step - loss: 1.2662 - val_loss: 1.2189
Epoch 2/50
25/25 [==============================] - 0s 4ms/step - loss: 1.2291 - val_loss: 1.1844
Epoch 3/50
25/25 [==============================] - 0s 4ms/step - loss: 1.1948 - val_loss: 1.1521
Epoch 4/50
25/25 [==============================] - 0s 3ms/step - loss: 1.1623 - val_loss: 1.1213
Epoch 5/50
25/25 [==============================] - 0s 4ms/step - loss: 1.1311 - val_loss: 1.0924
Epoch 6/50
25/25 [==============================] - 0s 3ms/step - loss: 1.1014 - val_loss: 1.0646
Epoch 7/50
25/25 [==============================] - 0s 3ms/step - loss: 1.0731 - val_loss: 1.0389
Epoch 8/50
25/25 [==============================] - 0s 3ms/step - loss: 1.0469 - val_loss: 1.0149
Epoch 9/50
25/25 [==============================] - 0s 4ms/step - loss: 1.0229 - val_loss: 0.9932
Epoch 10/50
25/25 [==============================] - 0s 4ms/step - loss: 1.0011 - val_loss: 0.9739
Epoch 11/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9819 - val_loss: 0.9567
Epoch 12/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9648 - val_loss: 0.9415
Epoch 13/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9498 - val_loss: 0.9283
Epoch 14/50
25/25 [==============================] - 0s 4ms/step - loss: 0.9367 - val_loss: 0.9165
Epoch 15/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9250 - val_loss: 0.9062
Epoch 16/50
25/25 [==============================] - 0s 4ms/step - loss: 0.9148 - val_loss: 0.8971
Epoch 17/50
25/25 [==============================] - 0s 4ms/step - loss: 0.9058 - val_loss: 0.8890
Epoch 18/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8977 - val_loss: 0.8817
Epoch 19/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8904 - val_loss: 0.8750
Epoch 20/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8838 - val_loss: 0.8688
Epoch 21/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8777 - val_loss: 0.8631
Epoch 22/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8719 - val_loss: 0.8577
Epoch 23/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8666 - val_loss: 0.8526
Epoch 24/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8616 - val_loss: 0.8479
Epoch 25/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8568 - val_loss: 0.8434
Epoch 26/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8523 - val_loss: 0.8391
Epoch 27/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8481 - val_loss: 0.8351
Epoch 28/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8440 - val_loss: 0.8313
Epoch 29/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8402 - val_loss: 0.8276
Epoch 30/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8365 - val_loss: 0.8241
Epoch 31/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8330 - val_loss: 0.8208
Epoch 32/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8297 - val_loss: 0.8177
Epoch 33/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8265 - val_loss: 0.8146
Epoch 34/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8235 - val_loss: 0.8117
Epoch 35/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8206 - val_loss: 0.8090
Epoch 36/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8178 - val_loss: 0.8063
Epoch 37/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8152 - val_loss: 0.8038
Epoch 38/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8127 - val_loss: 0.8016
Epoch 39/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8104 - val_loss: 0.7992
Epoch 40/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8080 - val_loss: 0.7969
Epoch 41/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8059 - val_loss: 0.7949
Epoch 42/50
25/25 [==============================] - 0s 3ms/step - loss: 0.8038 - val_loss: 0.7928
Epoch 43/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8018 - val_loss: 0.7909
Epoch 44/50
25/25 [==============================] - 0s 4ms/step - loss: 0.7999 - val_loss: 0.7891
Epoch 45/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7980 - val_loss: 0.7873
Epoch 46/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7963 - val_loss: 0.7855
Epoch 47/50
25/25 [==============================] - 0s 4ms/step - loss: 0.7945 - val_loss: 0.7840
Epoch 48/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7929 - val_loss: 0.7823
Epoch 49/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7913 - val_loss: 0.7808
Epoch 50/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7898 - val_loss: 0.7794
25/25 [==============================] - 0s 1ms/step
7/7 [==============================] - 0s 2ms/step
7/7 [==============================] - 0s 2ms/step - loss: 0.7794
Test Loss: 0.7794

RESULT
Thus the use of autoencoders has been represented using representation learning successfully.
Exercise No: 16 UNSUPERVISED LEARNING REPRESENTATION
Date:

AIM
To demonstrate unsupervised learning with an autoencoder on a synthetic dataset.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate a synthetic dataset
Step 3: Split the dataset into training and testing sets
Step 4: Standardize the features
Step 5: Build an autoencoder for unsupervised learning
Step 6: Encode and decode the data
Step 7: Autoencoder model
Step 8: Compile the autoencoder
Step 9: Train the autoencoder
Step 10: Use the trained autoencoder for feature extraction
Step 11: Visualize the original and reconstructed data

SOURCE CODE
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

# Generate a synthetic dataset


X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)

# Split the dataset 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)

# Standardize the features


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Build an autoencoder for unsupervised learning


input_dim = X_train.shape[1]

# Encoder
encoder_input = Input(shape=(input_dim,))
encoded = Dense(10, activation='relu')(encoder_input)

# Decoder
decoded = Dense(input_dim, activation='sigmoid')(encoded)

# Autoencoder model
autoencoder = Model(encoder_input, decoded)

# Compile the autoencoder


autoencoder.compile(optimizer='adam', loss='mean_squared_error')

# Train the autoencoder


autoencoder.fit(X_train, X_train, epochs=50, batch_size=32, shuffle=True, validation_data=(X_test,
X_test), verbose=1)

# Use the trained autoencoder for feature extraction


encoded_data_train = autoencoder.predict(X_train)
encoded_data_test = autoencoder.predict(X_test)

# Visualize the original and reconstructed data


fig, axes = plt.subplots(2, 5, figsize=(12, 5))
for i in range(5):
axes[0, i].imshow(X_train[i].reshape(4, 5), cmap='gray')
axes[1, i].imshow(encoded_data_train[i].reshape(4, 5), cmap='gray')

axes[0, 0].set_ylabel('Original')
axes[1, 0].set_ylabel('Encoded')
plt.show()
OUTPUT

Epoch 1/50
25/25 [==============================] - 2s 50ms/step - loss: 1.2191 - val_loss: 1.1757
Epoch 2/50
25/25 [==============================] - 0s 5ms/step - loss: 1.1823 - val_loss: 1.1419
Epoch 3/50
25/25 [==============================] - 0s 4ms/step - loss: 1.1477 - val_loss: 1.1102
Epoch 4/50
25/25 [==============================] - 0s 4ms/step - loss: 1.1155 - val_loss: 1.0806
Epoch 5/50
25/25 [==============================] - 0s 4ms/step - loss: 1.0853 - val_loss: 1.0532
Epoch 6/50
25/25 [==============================] - 0s 5ms/step - loss: 1.0570 - val_loss: 1.0282
Epoch 7/50
25/25 [==============================] - 0s 4ms/step - loss: 1.0314 - val_loss: 1.0053
Epoch 8/50
25/25 [==============================] - 0s 5ms/step - loss: 1.0086 - val_loss: 0.9846
Epoch 9/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9882 - val_loss: 0.9660
Epoch 10/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9700 - val_loss: 0.9497
Epoch 11/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9542 - val_loss: 0.9350
Epoch 12/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9401 - val_loss: 0.9220
Epoch 13/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9275 - val_loss: 0.9105
Epoch 14/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9164 - val_loss: 0.9002
Epoch 15/50
25/25 [==============================] - 0s 5ms/step - loss: 0.9064 - val_loss: 0.8913
Epoch 16/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8977 - val_loss: 0.8832
Epoch 17/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8898 - val_loss: 0.8759
Epoch 18/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8827 - val_loss: 0.8693
Epoch 19/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8762 - val_loss: 0.8632
Epoch 20/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8703 - val_loss: 0.8576
Epoch 21/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8648 - val_loss: 0.8523
Epoch 22/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8596 - val_loss: 0.8474
Epoch 23/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8548 - val_loss: 0.8428
Epoch 24/50
25/25 [==============================] - 0s 5ms/step - loss: 0.8502 - val_loss: 0.8384
Epoch 25/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8458 - val_loss: 0.8343
Epoch 26/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8417 - val_loss: 0.8305
Epoch 27/50
25/25 [==============================] - 0s 3ms/step - loss: 0.8377 - val_loss: 0.8268
Epoch 28/50
25/25 [==============================] - 0s 3ms/step - loss: 0.8339 - val_loss: 0.8233
Epoch 29/50
25/25 [==============================] - 0s 3ms/step - loss: 0.8304 - val_loss: 0.8200
Epoch 30/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8269 - val_loss: 0.8167
Epoch 31/50
25/25 [==============================] - 0s 3ms/step - loss: 0.8237 - val_loss: 0.8138
Epoch 32/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8205 - val_loss: 0.8109
Epoch 33/50
25/25 [==============================] - 0s 3ms/step - loss: 0.8175 - val_loss: 0.8082
Epoch 34/50
25/25 [==============================] - 0s 3ms/step - loss: 0.8147 - val_loss: 0.8056
Epoch 35/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8120 - val_loss: 0.8033
Epoch 36/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8094 - val_loss: 0.8010
Epoch 37/50
25/25 [==============================] - 0s 3ms/step - loss: 0.8069 - val_loss: 0.7987
Epoch 38/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8046 - val_loss: 0.7967
Epoch 39/50
25/25 [==============================] - 0s 4ms/step - loss: 0.8024 - val_loss: 0.7948
Epoch 40/50
25/25 [==============================] - 0s 3ms/step - loss: 0.8002 - val_loss: 0.7929
Epoch 41/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7982 - val_loss: 0.7911
Epoch 42/50
25/25 [==============================] - 0s 4ms/step - loss: 0.7962 - val_loss: 0.7895
Epoch 43/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7944 - val_loss: 0.7879
Epoch 44/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7927 - val_loss: 0.7864
Epoch 45/50
25/25 [==============================] - 0s 4ms/step - loss: 0.7910 - val_loss: 0.7850
Epoch 46/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7894 - val_loss: 0.7837
Epoch 47/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7879 - val_loss: 0.7823
Epoch 48/50
25/25 [==============================] - 0s 4ms/step - loss: 0.7864 - val_loss: 0.7811
Epoch 49/50
25/25 [==============================] - 0s 4ms/step - loss: 0.7851 - val_loss: 0.7800
Epoch 50/50
25/25 [==============================] - 0s 3ms/step - loss: 0.7837 - val_loss: 0.7788
25/25 [==============================] - 0s 2ms/step
7/7 [==============================] - 0s 2ms/step
RESULT
Thus unsupervised learning with an autoencoder on a synthetic dataset has been demonstrated
successfully.
Exercise No: 17 IMPLEMENTATION OF AUTO ENCODERS
Date:

AIM
To create an auto encoder consisting of two dense layers.(Encoder and Decoder)

PROCEDURE
Step 1: Import necessary libraries
Step 2: Loading the MNIST dataset and extracting training and testing data
Step 3: Normalizing pixel values to the range [0, 1]
Step 4: Displaying the shapes of the training and testing datasets

SOURCE CODE
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf

from sklearn.metrics import accuracy_score, precision_score, recall_score


from sklearn.model_selection import train_test_split
from keras import layers, losses
from keras.datasets import mnist
from keras.models import Model
# Loading the MNIST dataset and extracting training and testing data
(x_train, _), (x_test, _) = mnist.load_data()

# Normalizing pixel values to the range [0, 1]


x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.

# Displaying the shapes of the training and testing datasets


print("Shape of the training data:", x_train.shape)
print("Shape of the testing data:", x_test.shape)
OUTPUT

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-


datasets/mnist.npz
11490434/11490434 [==============================] - 0s 0us/step
Shape of the training data: (60000, 28, 28)
Shape of the testing data: (10000, 28, 28)

RESULT
Thus the auto encoder consisting of two layers (encoder and decoder) has been created and
output has been verified successfully.
Exercise No: 18 DEFINING A BASIC AUTO ENCODER
Date:

AIM
To define a simple autoencoder using class and constructor.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Define the Autoencoder model as a subclass of the TensorFlow Model class
Step 3: Create Encoder architecture using a Sequential model.
Step 4: Create Decoder architecture using another Sequential model.
Step 5: Forward pass method defining the encoding and decoding steps.
Step 6: Extracting shape information from the testing dataset
Step 7: Specifying the dimensionality of the latent space.
Step 8: Creating an instance of the SimpleAutoencoder model.

SOURCE CODE
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf

from sklearn.metrics import accuracy_score, precision_score, recall_score


from sklearn.model_selection import train_test_split
from keras import layers, losses
from keras.datasets import mnist
from keras.models import Model
# Definition of the Autoencoder model as a subclass of the TensorFlow Model class

class SimpleAutoencoder(Model):
def __init__(self,latent_dimensions , data_shape):
super(SimpleAutoencoder, self).__init__()
self.latent_dimensions = latent_dimensions
self.data_shape = data_shape
# Encoder architecture using a Sequential model
self.encoder = tf.keras.Sequential([
layers.Flatten(),
layers.Dense(latent_dimensions, activation='relu'),
])

# Decoder architecture using another Sequential model


self.decoder = tf.keras.Sequential([
layers.Dense(tf.math.reduce_prod(data_shape), activation='sigmoid'),
layers.Reshape(data_shape)
])

# Forward pass method defining the encoding and decoding steps


def call(self, input_data):
encoded_data = self.encoder(input_data)
decoded_data = self.decoder(encoded_data)
return decoded_data

# Extracting shape information from the testing dataset


input_data_shape = x_test.shape[1:]

# Specifying the dimensionality of the latent space


latent_dimensions = 64

# Creating an instance of the SimpleAutoencoder model


simple_autoencoder = SimpleAutoencoder(latent_dimensions, input_data_shape)
simple_autoencoder.compile(optimizer='adam', loss=losses.MeanSquaredError())

simple_autoencoder.fit(x_train, x_train,
epochs=1,
shuffle=True,
validation_data=(x_test, x_test))

OUTPUT
1875/1875 [==============================] - 14s 7ms/step - loss: 0.0235 -
val_loss: 0.0090

<keras.src.callbacks.History at 0x7e5a847ebeb0>

RESULT
Thus the auto encoder has been defined using class and constructor and the output has been
verified successfully.
Exercise No: 19 VANILLA AUTOENCODER
Date:

AIM
To implement a vanilla auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Define the architecture of the autoencoder
Step 3: Create Encoder, Decoder and Auto encoder architecture using a Sequential model.
Step 4: Set the dimensions of input data and the encoding dimension
Step 5: Build the autoencoder model
Step 6: Compile the model
Step 7: Display the architecture of the autoencoder

SOURCE CODE
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model

# Define the architecture of the autoencoder


def build_autoencoder(input_dim, encoding_dim):
# Encoder
input_layer = Input(shape=(input_dim,))
encoded = Dense(encoding_dim, activation='relu')(input_layer)

# Decoder
decoded = Dense(input_dim, activation='sigmoid')(encoded)

# Autoencoder
autoencoder = Model(input_layer, decoded)

return autoencoder

# Set the dimensions of input data and the encoding dimension


input_dim = 784 # For example, 28x28 images
encoding_dim = 32 # Adjustable based on the desired compression factor

# Build the autoencoder model


autoencoder = build_autoencoder(input_dim, encoding_dim)

# Compile the model


autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

# Display the architecture of the autoencoder


autoencoder.summary()

OUTPUT

Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 784)] 0

dense_4 (Dense) (None, 32) 25120

dense_5 (Dense) (None, 784) 25872

=================================================================
Total params: 50992 (199.19 KB)
Trainable params: 50992 (199.19 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

RESULT
Thus the vanilla auto encoder has been implemented and the output has been verified
successfully.
Exercise No: 20 SPARSE AUTOENCODER
Date:

AIM
To implement a sparse auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Define the architecture of the sparse autoencoder
Step 3: Create Encoder, Decoder and Auto encoder architecture for sparse auto encoder
Step 4: Set the dimensions of input data and the encoding dimension
Step 5: Build the sparse autoencoder model
Step 6: Compile the model
Step 7: Display the architecture of the sparse autoencoder

SOURCE CODE

# Generate random data for demonstration


num_samples = 1000
random_data = np.random.random((num_samples, input_dim))

# Normalize the data to values between 0 and 1


random_data = random_data / np.max(random_data)

# Train the sparse autoencoder


sparse_autoencoder.fit(random_data, random_data, epochs=50, batch_size=32)

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras import regularizers

# Define the architecture of the sparse autoencoder


def build_sparse_autoencoder(input_dim, encoding_dim, sparsity_factor=0.01):
# Encoder
input_layer = Input(shape=(input_dim,))
encoded = Dense(encoding_dim, activation='relu',
activity_regularizer=regularizers.l1(sparsity_factor))(input_layer)

# Decoder
decoded = Dense(input_dim, activation='sigmoid')(encoded)

# Sparse Autoencoder
sparse_autoencoder = Model(input_layer, decoded)

return sparse_autoencoder

# Set the dimensions of input data and the encoding dimension


input_dim = 784 # For example, 28x28 images
encoding_dim = 32 # Adjustable based on the desired compression factor

# Build the sparse autoencoder model


sparse_autoencoder = build_sparse_autoencoder(input_dim, encoding_dim)

# Compile the model


sparse_autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

# Display the architecture of the sparse autoencoder


sparse_autoencoder.summary()

OUTPUT

Epoch 1/50
32/32 [==============================] - 2s 6ms/step - loss: 0.7006
Epoch 2/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 3/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 4/50
32/32 [==============================] - 0s 7ms/step - loss: 0.6931
Epoch 5/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 6/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 7/50
32/32 [==============================] - 0s 7ms/step - loss: 0.6930
Epoch 8/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 9/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 10/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 11/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 12/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 13/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 14/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 15/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 16/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 17/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 18/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 19/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 20/50
32/32 [==============================] - 0s 7ms/step - loss: 0.6930
Epoch 21/50
32/32 [==============================] - 0s 6ms/step - loss: 0.6930
Epoch 22/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 23/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 24/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 25/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 26/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 27/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 28/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 29/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 30/50
32/32 [==============================] - 0s 2ms/step - loss: 0.6930
Epoch 31/50
32/32 [==============================] - 0s 2ms/step - loss: 0.6930
Epoch 32/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 33/50
32/32 [==============================] - 0s 3ms/step - loss: 0.6930
Epoch 34/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 35/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 36/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 37/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 38/50
32/32 [==============================] - 0s 4ms/step - loss: 0.6930
Epoch 39/50
32/32 [==============================] - 0s 4ms/step - loss: 0.6930
Epoch 40/50
32/32 [==============================] - 0s 4ms/step - loss: 0.6930
Epoch 41/50
32/32 [==============================] - 0s 4ms/step - loss: 0.6930
Epoch 42/50
32/32 [==============================] - 0s 4ms/step - loss: 0.6930
Epoch 43/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 44/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 45/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 46/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 47/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 48/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 49/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930
Epoch 50/50
32/32 [==============================] - 0s 5ms/step - loss: 0.6930

<keras.src.callbacks.History at 0x7e5a8457b2b0>

Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 784)] 0

dense_6 (Dense) (None, 32) 25120

dense_7 (Dense) (None, 784) 25872

=================================================================
Total params: 50992 (199.19 KB)
Trainable params: 50992 (199.19 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

RESULT
Thus the sparse auto encoder has been implemented and the output has been verified
successfully.
Exercise No: 21 DENOISING AUTOENCODER
Date:

AIM
To implement a denoising auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Define the architecture of the denoising autoencoder
Step 3: Create Encoder, Decoder and Auto encoder architecture for denoising auto encoder
Step 4: Set the dimensions of input data and the encoding dimension
Step 5: Build the denoising autoencoder model
Step 6: Compile the model
Step 7: Display the architecture of the denoising autoencoder

SOURCE CODE
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, GaussianNoise
from tensorflow.keras.models import Model

# Define the architecture of the denoising autoencoder


def build_denoising_autoencoder(input_dim, encoding_dim, noise_factor=0.5):
# Encoder with added Gaussian noise to the input
input_layer = Input(shape=(input_dim,))
noisy_input = GaussianNoise(noise_factor)(input_layer)
encoded = Dense(encoding_dim, activation='relu')(noisy_input)

# Decoder
decoded = Dense(input_dim, activation='sigmoid')(encoded)

# Denoising Autoencoder
denoising_autoencoder = Model(input_layer, decoded)

return denoising_autoencoder

# Set the dimensions of input data and the encoding dimension


input_dim = 784 # For example, 28x28 images
encoding_dim = 32 # Adjustable based on the desired compression factor

# Build the denoising autoencoder model


denoising_autoencoder = build_denoising_autoencoder(input_dim, encoding_dim)

# Compile the model


denoising_autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

# Display the architecture of the denoising autoencoder


denoising_autoencoder.summary()

OUTPUT

Model: "model_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) [(None, 784)] 0

gaussian_noise (GaussianNo (None, 784) 0


ise)

dense_10 (Dense) (None, 32) 25120

dense_11 (Dense) (None, 784) 25872

=================================================================
Total params: 50992 (199.19 KB)
Trainable params: 50992 (199.19 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

RESULT
Thus the denoising auto encoder has been implemented and the output has been verified
successfully.
Exercise No: 22 VARIATIONAL AUTOENCODER (VAE)
Date:

AIM
To implement a variational auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Set the dimensions of input data and the encoding dimension
Step 3: Function to sample from the latent space using the reparameterization trick
Step 4: Define the architecture of the Variational Autoencoder (VAE)
Step 5: Compute the VAE loss, which consists of the reconstruction loss and the KL divergence
Step 6: Build the VAE model
Step 7: Compile the model
Step 8: Display the architecture of the VAE architecture.

SOURCE CODE
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, Lambda
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K
from tensorflow.keras.losses import binary_crossentropy
import numpy as np

# Set the dimensions of input data and the encoding dimension


input_dim = 784 # For example, 28x28 images
encoding_dim = 32 # Adjustable based on the desired latent space dimension

# Function to sample from the latent space using the reparameterization trick
def sampling(args):
z_mean, z_log_var = args
batch = K.shape(z_mean)[0]
dim = K.int_shape(z_mean)[1]
epsilon = K.random_normal(shape=(batch, dim))
return z_mean + K.exp(0.5 * z_log_var) * epsilon
# Define the architecture of the Variational Autoencoder (VAE)
def build_vae(input_dim, encoding_dim):
# Encoder
input_layer = Input(shape=(input_dim,))
h = Dense(256, activation='relu')(input_layer)
z_mean = Dense(encoding_dim, name='z_mean')(h)
z_log_var = Dense(encoding_dim, name='z_log_var')(h)

# Use the sampling function to generate a latent space point


z = Lambda(sampling, output_shape=(encoding_dim,), name='z')([z_mean, z_log_var])

# Decoder
decoder_h = Dense(256, activation='relu')
decoder_mean = Dense(input_dim, activation='sigmoid')
h_decoded = decoder_h(z)
x_decoded_mean = decoder_mean(h_decoded)

# Variational Autoencoder (VAE)


vae = Model(input_layer, x_decoded_mean)

# Compute the VAE loss, which consists of the reconstruction loss and the KL divergence
xent_loss = binary_crossentropy(input_layer, x_decoded_mean)
kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
vae_loss = K.mean(xent_loss + kl_loss)

vae.add_loss(vae_loss)

return vae

# Build the VAE model


vae = build_vae(input_dim, encoding_dim)

# Compile the model


vae.compile(optimizer='adam')

# Display the architecture of the VAE


vae.summary()
# Generate random data for demonstration
num_samples = 1000
random_data = np.random.random((num_samples, input_dim))

# Normalize the data to values between 0 and 1


random_data = random_data / np.max(random_data)

# Train the VAE


vae.fit(random_data, epochs=50, batch_size=32)

OUTPUT

Model: "model_4"
_______________________________________________________________________________
___________________
Layer (type) Output Shape Param # Connected
to
===============================================================================
===================
input_5 (InputLayer) [(None, 784)] 0 []

dense_12 (Dense) (None, 256) 200960


['input_5[0][0]']

z_mean (Dense) (None, 32) 8224


['dense_12[0][0]']

z_log_var (Dense) (None, 32) 8224


['dense_12[0][0]']

z (Lambda) (None, 32) 0 ['z_mean[0]


[0]',

'z_log_var[0][0]']

dense_13 (Dense) (None, 256) 8448 ['z[0][0]']

dense_14 (Dense) (None, 784) 201488


['dense_13[0][0]']

tf.__operators__.add (TFOp (None, 32) 0


['z_log_var[0][0]']
Lambda)

tf.math.square (TFOpLambda (None, 32) 0 ['z_mean[0]


[0]']
)

tf.math.subtract (TFOpLamb (None, 32) 0


['tf.__operators__.add[0][0]',
da)
'tf.math.square[0][0]']
tf.math.exp (TFOpLambda) (None, 32) 0
['z_log_var[0][0]']

tf.cast (TFOpLambda) (None, 784) 0


['input_5[0][0]']

tf.convert_to_tensor (TFOp (None, 784) 0


['dense_14[0][0]']
Lambda)

tf.math.subtract_1 (TFOpLa (None, 32) 0


['tf.math.subtract[0][0]',
mbda)
'tf.math.exp[0][0]']

tf.keras.backend.binary_cr (None, 784) 0


['tf.cast[0][0]',
ossentropy (TFOpLambda)
'tf.convert_to_tensor[0][0]']

tf.math.reduce_sum (TFOpLa (None,) 0


['tf.math.subtract_1[0][0]']
mbda)

tf.math.reduce_mean (TFOpL (None,) 0


['tf.keras.backend.binary_cros
ambda) sentropy[0]
[0]']

tf.math.multiply (TFOpLamb (None,) 0


['tf.math.reduce_sum[0][0]']
da)

tf.__operators__.add_1 (TF (None,) 0


['tf.math.reduce_mean[0][0]',
OpLambda)
'tf.math.multiply[0][0]']

tf.math.reduce_mean_1 (TFO () 0
['tf.__operators__.add_1[0][0]
pLambda) ']

add_loss (AddLoss) () 0
['tf.math.reduce_mean_1[0][0]'
]

===============================================================================
===================
Total params: 427344 (1.63 MB)
Trainable params: 427344 (1.63 MB)
Non-trainable params: 0 (0.00 Byte)
_______________________________________________________________________________
___________________
Epoch 1/50
32/32 [==============================] - 2s 8ms/step - loss: 1.6439
Epoch 2/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6966
Epoch 3/50
32/32 [==============================] - 0s 9ms/step - loss: 0.6955
Epoch 4/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6947
Epoch 5/50
32/32 [==============================] - 0s 9ms/step - loss: 0.6945
Epoch 6/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6942
Epoch 7/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6941
Epoch 8/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6939
Epoch 9/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6939
Epoch 10/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6938
Epoch 11/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6937
Epoch 12/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6936
Epoch 13/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6935
Epoch 14/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6935
Epoch 15/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6934
Epoch 16/50
32/32 [==============================] - 0s 7ms/step - loss: 0.6934
Epoch 17/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6934
Epoch 18/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6933
Epoch 19/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6933
Epoch 20/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6933
Epoch 21/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6932
Epoch 22/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6932
Epoch 23/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6932
Epoch 24/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6932
Epoch 25/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 26/50
32/32 [==============================] - 0s 12ms/step - loss: 0.6931
Epoch 27/50
32/32 [==============================] - 0s 12ms/step - loss: 0.6931
Epoch 28/50
32/32 [==============================] - 0s 12ms/step - loss: 0.6931
Epoch 29/50
32/32 [==============================] - 0s 12ms/step - loss: 0.6931
Epoch 30/50
32/32 [==============================] - 0s 13ms/step - loss: 0.6931
Epoch 31/50
32/32 [==============================] - 0s 13ms/step - loss: 0.6931
Epoch 32/50
32/32 [==============================] - 0s 12ms/step - loss: 0.6931
Epoch 33/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 34/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 35/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 36/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 37/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 38/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 39/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 40/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 41/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6931
Epoch 42/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 43/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 44/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 45/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 46/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 47/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 48/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 49/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930
Epoch 50/50
32/32 [==============================] - 0s 8ms/step - loss: 0.6930

<keras.src.callbacks.History at 0x7e5a845c92a0>

RESULT
Thus the VAE has been implemented and the output has been verified successfully.
Exercise No: 23 CONTRACTIVE AUTOENCODER
Date:

AIM
To implement a contractive auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load MNIST dataset
Step 3: Normalize and flatten images
Step 4: Build Contractive Autoencoder model
Step 5: Define the Contractive Autoencoder loss with the penalty term using GradientTape
Step 6: Compute the Jacobian of the encoder output with respect to the input using GradientTape
Step 7: Compute the Frobenius norm of the Jacobian and add to the loss
Step 8: Use add_loss to include the penalty term in the model's overall loss
Step 9: Compile the model
Step 10: Load MNIST data
Step 11: Preprocess data
Step 12: Split the data into training and validation sets
Step 13: Set the dimensions
Step 14: Build and train the Contractive Autoencoder
Step 15: Evaluate on the test set

SOURCE CODE
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
from sklearn.model_selection import train_test_split
import numpy as np

# Load MNIST dataset


def load_mnist():
(train_images, _), (test_images, _) = mnist.load_data()
return train_images, test_images

# Normalize and flatten images


def preprocess_images(images):
images = images.astype('float32') / 255.0
return images.reshape((len(images), np.prod(images.shape[1:])))

# Build Contractive Autoencoder model


def build_contractive_autoencoder(input_dim, encoding_dim, penalty_factor=1e-4):
input_layer = Input(shape=(input_dim,))
encoded = Dense(encoding_dim, activation='relu')(input_layer)
decoded = Dense(input_dim, activation='sigmoid')(encoded)

contractive_autoencoder = Model(input_layer, decoded)

# Define the Contractive Autoencoder loss with the penalty term using GradientTape
def contractive_loss(y_true, y_pred):
mse = tf.keras.losses.mean_squared_error(y_true, y_pred)

# Compute the Jacobian of the encoder output with respect to the input using GradientTape
with tf.GradientTape() as tape:
encoder_output = contractive_autoencoder.layers[1](input_layer)
encoder_jacobian = tape.jacobian(encoder_output, input_layer)

# Compute the Frobenius norm of the Jacobian and add to the loss
contractive_penalty = penalty_factor * tf.reduce_sum(tf.square(encoder_jacobian))

return mse + contractive_penalty

# Use add_loss to include the penalty term in the model's overall loss
contractive_autoencoder.add_loss(lambda: contractive_loss(input_layer, decoded))

# Compile the model


contractive_autoencoder.compile(optimizer='adam', loss=None)

return contractive_autoencoder

# Load MNIST data


train_images, test_images = load_mnist()

# Preprocess data
train_images = preprocess_images(train_images)
test_images = preprocess_images(test_images)

# Split the data into training and validation sets


train_data, val_data, _, _ = train_test_split(train_images, train_images, test_size=0.2, random_state=42)

# Set the dimensions


input_dim = 28 * 28 # For MNIST
encoding_dim = 32

# Build and train the Contractive Autoencoder


contractive_autoencoder = build_contractive_autoencoder(input_dim, encoding_dim)
contractive_autoencoder.summary()

contractive_autoencoder.fit(train_data, train_data, validation_data=(val_data, val_data), epochs=10,


batch_size=64)

# Evaluate on the test set


test_loss = contractive_autoencoder.evaluate(test_images, test_images)
print(f"Test Loss: {test_loss}")

OUTPUT

Model: "model_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_5 (InputLayer) [(None, 784)] 0

dense_8 (Dense) (None, 32) 25120

dense_9 (Dense) (None, 784) 25872

=================================================================
Total params: 50992 (199.19 KB)
Trainable params: 50992 (199.19 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/10

RESULT
Thus the contractive autoencoder has been implemented and the output has been verified
successfully.
Exercise No: 24 ADVERSARIAL AUTOENCODER
Date:

AIM
To implement an adversarial auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Set random seed for reproducibility
Step 3: Define the architecture of the Adversarial Autoencoder
Step 4: Compile the discriminator
Step 5: Freeze the weights of the discriminator during generator training
Step 6: Combined Adversarial Autoencoder model
Step 7: Compile the combined model
Step 8: Set the dimensions of input data and the encoding dimension
Step 9: Build the Adversarial Autoencoder model
Step 10: Display the architecture of the Adversarial Autoencoder
Step 11: Generate random data for demonstration
Step 12: Normalize the data to values between 0 and 1
Step 13: Train the Adversarial Autoencoder
Step 14: Evaluate on a test sample
SOURCE CODE
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
import numpy as np

# Set random seed for reproducibility


np.random.seed(1000)
tf.random.set_seed(1000)

# Define the architecture of the Adversarial Autoencoder


def build_adversarial_autoencoder(input_dim, encoding_dim):
# Encoder
input_layer = Input(shape=(input_dim,))
encoded = Dense(encoding_dim, activation='relu')(input_layer)
# Decoder
decoded = Dense(input_dim, activation='sigmoid')(encoded)

# Autoencoder
autoencoder = Model(input_layer, decoded)

# Discriminator
validity = Dense(1, activation='sigmoid')(encoded)
discriminator = Model(input_layer, validity)

# Compile the discriminator


discriminator.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Freeze the weights of the discriminator during generator training


discriminator.trainable = False

# Combined Adversarial Autoencoder model


encoded_repr = autoencoder(input_layer)
validity = discriminator(encoded_repr)
adversarial_autoencoder = Model(input_layer, [decoded, validity])

# Compile the combined model


adversarial_autoencoder.compile(optimizer='adam',
loss=['mse', 'binary_crossentropy'],
loss_weights=[0.999, 0.001])

return autoencoder, discriminator, adversarial_autoencoder

# Set the dimensions of input data and the encoding dimension


input_dim = 784 # For example, 28x28 images
encoding_dim = 32 # Adjustable based on the desired compression factor

# Build the Adversarial Autoencoder model


autoencoder, discriminator, adversarial_autoencoder = build_adversarial_autoencoder(input_dim,
encoding_dim)

# Display the architecture of the Adversarial Autoencoder


adversarial_autoencoder.summary()
# Generate random data for demonstration
num_samples = 1000
random_data = np.random.random((num_samples, input_dim))

# Normalize the data to values between 0 and 1


random_data = random_data / np.max(random_data)

# Train the Adversarial Autoencoder


adversarial_autoencoder.fit(random_data, [random_data, np.ones((num_samples, 1))], epochs=50,
batch_size=32)

# Evaluate on a test sample


test_sample = np.random.random((1, input_dim))
decoded_sample, validity_score = adversarial_autoencoder.predict(test_sample)

print("Original Input:")
print(test_sample)
print("Decoded Output:")
print(decoded_sample)
print("Validity Score:")
print(validity_score)

OUTPUT

Model: "model_13"
_______________________________________________________________________________
___________________
Layer (type) Output Shape Param # Connected
to
===============================================================================
===================
input_12 (InputLayer) [(None, 784)] 0 []

dense_19 (Dense) (None, 32) 25120


['input_12[0][0]']

model_11 (Functional) (None, 784) 50992


['input_12[0][0]']

dense_20 (Dense) (None, 784) 25872


['dense_19[0][0]']

model_12 (Functional) (None, 1) 25153


['model_11[0][0]']
===============================================================================
===================
Total params: 51025 (199.32 KB)
Trainable params: 25872 (101.06 KB)
Non-trainable params: 25153 (98.25 KB)
_______________________________________________________________________________
___________________
Epoch 1/50
32/32 [==============================] - 1s 3ms/step - loss: 0.0854 -
dense_20_loss: 0.0843 - model_12_loss: 1.2268
Epoch 2/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0845 -
dense_20_loss: 0.0835 - model_12_loss: 1.1108
Epoch 3/50
32/32 [==============================] - 0s 2ms/step - loss: 0.0843 -
dense_20_loss: 0.0833 - model_12_loss: 1.1123
Epoch 4/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0841 -
dense_20_loss: 0.0831 - model_12_loss: 1.1034
Epoch 5/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0839 -
dense_20_loss: 0.0829 - model_12_loss: 1.1078
Epoch 6/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0837 -
dense_20_loss: 0.0827 - model_12_loss: 1.1155
Epoch 7/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0835 -
dense_20_loss: 0.0825 - model_12_loss: 1.1115
Epoch 8/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0834 -
dense_20_loss: 0.0823 - model_12_loss: 1.1148
Epoch 9/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0832 -
dense_20_loss: 0.0822 - model_12_loss: 1.1194
Epoch 10/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0831 -
dense_20_loss: 0.0820 - model_12_loss: 1.1130
Epoch 11/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0829 -
dense_20_loss: 0.0819 - model_12_loss: 1.1220
Epoch 12/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0828 -
dense_20_loss: 0.0818 - model_12_loss: 1.1155
Epoch 13/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0827 -
dense_20_loss: 0.0816 - model_12_loss: 1.1184
Epoch 14/50
32/32 [==============================] - 0s 2ms/step - loss: 0.0825 -
dense_20_loss: 0.0815 - model_12_loss: 1.1158
Epoch 15/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0824 -
dense_20_loss: 0.0814 - model_12_loss: 1.1232
Epoch 16/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0823 -
dense_20_loss: 0.0813 - model_12_loss: 1.1220
Epoch 17/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0822 -
dense_20_loss: 0.0811 - model_12_loss: 1.1246
Epoch 18/50
32/32 [==============================] - 0s 2ms/step - loss: 0.0821 -
dense_20_loss: 0.0810 - model_12_loss: 1.1237
Epoch 19/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0820 -
dense_20_loss: 0.0810 - model_12_loss: 1.1165
Epoch 20/50
32/32 [==============================] - 0s 2ms/step - loss: 0.0819 -
dense_20_loss: 0.0809 - model_12_loss: 1.1243
Epoch 21/50
32/32 [==============================] - 0s 2ms/step - loss: 0.0818 -
dense_20_loss: 0.0808 - model_12_loss: 1.1174
Epoch 22/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0817 -
dense_20_loss: 0.0807 - model_12_loss: 1.1168
Epoch 23/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0816 -
dense_20_loss: 0.0806 - model_12_loss: 1.1197
Epoch 24/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0816 -
dense_20_loss: 0.0805 - model_12_loss: 1.1146
Epoch 25/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0815 -
dense_20_loss: 0.0805 - model_12_loss: 1.1160
Epoch 26/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0814 -
dense_20_loss: 0.0804 - model_12_loss: 1.1140
Epoch 27/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0813 -
dense_20_loss: 0.0803 - model_12_loss: 1.1145
Epoch 28/50
32/32 [==============================] - 0s 5ms/step - loss: 0.0813 -
dense_20_loss: 0.0803 - model_12_loss: 1.1149
Epoch 29/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0812 -
dense_20_loss: 0.0802 - model_12_loss: 1.1128
Epoch 30/50
32/32 [==============================] - 0s 5ms/step - loss: 0.0812 -
dense_20_loss: 0.0801 - model_12_loss: 1.1125
Epoch 31/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0811 -
dense_20_loss: 0.0801 - model_12_loss: 1.1124
Epoch 32/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0811 -
dense_20_loss: 0.0800 - model_12_loss: 1.1115
Epoch 33/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0810 -
dense_20_loss: 0.0800 - model_12_loss: 1.1141
Epoch 34/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0810 -
dense_20_loss: 0.0799 - model_12_loss: 1.1081
Epoch 35/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0809 -
dense_20_loss: 0.0799 - model_12_loss: 1.1098
Epoch 36/50
32/32 [==============================] - 0s 5ms/step - loss: 0.0809 -
dense_20_loss: 0.0798 - model_12_loss: 1.1104
Epoch 37/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0808 -
dense_20_loss: 0.0798 - model_12_loss: 1.1059
Epoch 38/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0808 -
dense_20_loss: 0.0798 - model_12_loss: 1.1052
Epoch 39/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0807 -
dense_20_loss: 0.0797 - model_12_loss: 1.1059
Epoch 40/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0807 -
dense_20_loss: 0.0797 - model_12_loss: 1.1061
Epoch 41/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0807 -
dense_20_loss: 0.0796 - model_12_loss: 1.1004
Epoch 42/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0806 -
dense_20_loss: 0.0796 - model_12_loss: 1.1070
Epoch 43/50
32/32 [==============================] - 0s 4ms/step - loss: 0.0806 -
dense_20_loss: 0.0796 - model_12_loss: 1.1059
Epoch 44/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0806 -
dense_20_loss: 0.0795 - model_12_loss: 1.1035
Epoch 45/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0805 -
dense_20_loss: 0.0795 - model_12_loss: 1.1032
Epoch 46/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0805 -
dense_20_loss: 0.0795 - model_12_loss: 1.1016
Epoch 47/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0805 -
dense_20_loss: 0.0795 - model_12_loss: 1.1020
Epoch 48/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0804 -
dense_20_loss: 0.0794 - model_12_loss: 1.1006
Epoch 49/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0804 -
dense_20_loss: 0.0794 - model_12_loss: 1.1006
Epoch 50/50
32/32 [==============================] - 0s 3ms/step - loss: 0.0804 -
dense_20_loss: 0.0794 - model_12_loss: 1.0990
1/1 [==============================] - 0s 103ms/step
Original Input:
[[7.57951571e-01 2.80047165e-01 3.36175143e-03 5.51833129e-01
2.12423991e-01 5.10699519e-01 3.75952165e-01 2.80418393e-01
6.59611865e-01 2.26486081e-01 6.41701259e-02 3.67709144e-01
4.05491161e-01 7.03278516e-01 8.77639284e-01 3.24723542e-01
6.94793185e-01 3.65952361e-01 8.87775008e-01 2.38818585e-01
7.42772935e-01 4.72052931e-01 3.22897956e-01 5.57215808e-03
2.41839507e-01 6.36456649e-01 5.01258802e-01 3.25695912e-01
6.29224529e-01 8.22702121e-01 2.72409011e-01 6.53121560e-02
7.25254343e-02 9.02015507e-01 7.30536176e-01 9.85532963e-01
6.81103194e-01 9.48748446e-01 5.50402390e-01 5.79382771e-01
7.96795501e-01 1.23325363e-01 9.52086963e-01 6.74998928e-01
6.94947259e-01 8.49204532e-01 4.80603751e-01 7.64851637e-01
3.12303773e-01 1.01503241e-01 3.17621381e-01 9.86227862e-01
1.89544237e-02 3.84760530e-01 3.27719364e-01 5.68148123e-01
1.12392520e-01 2.93090933e-01 3.38686692e-01 9.97786474e-01
5.79889335e-01 9.58953205e-01 9.09193581e-01 3.20547620e-01
6.79260968e-01 5.56523038e-01 5.07258141e-02 4.19630325e-01
7.84312948e-01 5.65755267e-01 5.57914337e-01 3.34438955e-01
7.27914107e-01 1.29184953e-01 8.36466031e-01 9.78608743e-01
8.01212222e-01 4.22069187e-01 3.28040703e-01 9.68533050e-01
1.72121857e-01 6.13105429e-01 4.59968781e-01 7.90435224e-02
6.59647850e-01 6.90725235e-01 3.22103574e-01 1.85600541e-01
9.72535879e-02 1.48936383e-01 4.82691212e-01 4.45015081e-01
3.92892344e-01 8.48403810e-01 4.54964852e-01 4.34666300e-01
3.91535793e-01 2.34212233e-01 9.87213793e-01 1.65272272e-01
2.42962586e-01 7.80861477e-01 7.52207122e-01 8.38568542e-01
2.30364520e-02 6.04200999e-01 2.49602341e-01 2.80209521e-01
3.53033627e-01 9.36718097e-01 6.12997252e-01 6.16454849e-01
3.21599603e-01 5.27446007e-01 2.74173736e-01 3.31616120e-01
9.92931289e-02 9.25322384e-01 7.15237229e-01 4.25560063e-01
2.78828792e-01 6.07815343e-01 6.36452291e-02 1.36836378e-01
4.78118676e-01 4.35454399e-01 9.56225243e-01 8.41727155e-01
1.97755913e-01 2.39028904e-01 8.43602502e-01 6.86916896e-01
5.97266296e-01 5.97634320e-01 7.79211786e-01 8.19314717e-01
8.90903991e-01 2.18176041e-01 7.47172784e-01 5.44160863e-01
9.56298025e-01 8.87550617e-01 8.05244879e-01 4.44544961e-01
9.87737377e-01 2.41705729e-01 1.50791752e-01 7.03693445e-01
1.02758362e-01 5.24964829e-01 9.36405652e-01 9.28264221e-01
1.76235159e-01 6.58405338e-01 9.89941380e-01 1.54702724e-01
4.51772618e-01 4.29342957e-01 6.31285411e-01 9.99282955e-01
1.55376022e-01 9.16386069e-02 3.11365324e-01 2.66099703e-01
7.37349540e-01 6.39006687e-02 4.46590153e-01 3.72465310e-01
8.02026692e-01 4.72603978e-01 2.42623854e-01 2.31757283e-01
7.84655961e-01 4.75807358e-01 7.52003867e-01 8.25146133e-01
2.86313129e-01 5.48175564e-01 1.87868575e-01 3.33436022e-01
5.38841593e-01 8.42682728e-01 3.39126412e-01 5.39439613e-01
5.22664521e-01 3.17498069e-01 6.22010437e-01 8.58218911e-01
8.34334862e-01 3.91474585e-01 2.00845605e-01 9.27177957e-01
7.88482948e-01 8.42895634e-01 1.86993998e-01 5.34250529e-01
5.86300984e-02 8.33356718e-01 2.79263967e-02 4.11781580e-01
8.30841578e-01 1.92701412e-01 1.56860959e-01 1.39041627e-01
3.82155861e-01 9.47567437e-01 7.61064232e-01 5.97258380e-01
8.78969691e-02 1.43226069e-02 2.77647475e-01 7.82567973e-02
7.58793045e-01 7.27554402e-01 5.23267716e-01 2.58323956e-02
3.51343202e-01 5.03700003e-01 6.67142430e-01 2.81963160e-01
3.00090010e-03 6.76419743e-01 1.79264847e-01 2.65282973e-01
7.72571587e-01 5.49960394e-01 2.51162407e-01 8.27416044e-01
5.91240951e-04 1.53287038e-01 5.20650080e-01 2.15413755e-01
4.21640018e-01 8.71324854e-01 9.68039716e-01 4.76153684e-02
1.21006537e-02 6.92654582e-01 8.63423901e-01 9.28991495e-01
9.60629216e-01 9.21140662e-01 8.58691116e-01 8.29953827e-01
8.26109373e-01 7.09726084e-02 4.98678827e-01 8.75373466e-01
8.88298628e-01 1.71937455e-02 2.58522611e-01 1.51803973e-01
4.49592080e-01 9.81329680e-01 4.08820637e-01 4.91300786e-01
2.22392097e-01 3.61517260e-01 1.13481616e-01 6.11613589e-01
6.68184016e-01 5.17197241e-01 8.73495979e-03 8.04440901e-01
6.04030511e-01 8.26569631e-02 4.88625414e-01 9.46960386e-01
3.18399816e-01 3.53347201e-01 2.90790649e-01 2.38243362e-01
4.78069066e-02 5.16213274e-02 8.59098034e-01 2.19405663e-01
2.86253671e-01 2.89210855e-01 4.45755518e-01 1.97785092e-01
9.73285817e-01 4.75562510e-01 3.41963925e-01 2.43489324e-01
8.47672357e-01 1.86424859e-01 8.03402381e-01 2.80629292e-01
2.64646863e-01 3.66260089e-01 2.95394230e-01 9.20640781e-01
9.74249950e-01 3.44903659e-01 9.84748333e-01 7.21420633e-02
9.30089268e-01 4.18038936e-01 7.82835935e-01 7.96295714e-02
1.44798536e-01 2.60426812e-01 7.46749157e-01 3.95256167e-01
4.96119646e-01 8.35677968e-01 7.46963291e-01 6.72130656e-01
4.64886149e-01 7.20949933e-01 8.75488077e-01 7.77369416e-01
9.00060426e-01 3.04900156e-01 2.95642127e-01 9.85279769e-01
7.60485173e-01 1.11995865e-01 1.30556806e-01 4.15452163e-01
8.03436220e-01 6.71361785e-01 1.19537492e-01 9.75610848e-01
2.71906192e-01 2.00122646e-01 5.33815965e-01 7.06257355e-01
7.19328413e-01 7.56999215e-01 2.42894550e-01 7.08135431e-02
5.10244696e-01 3.66998589e-04 4.43007813e-01 7.16158650e-01
5.50538084e-01 2.26952488e-01 5.63927124e-01 3.06607384e-01
6.96257455e-01 5.52841841e-01 5.80967357e-01 3.25499270e-01
6.67407219e-01 8.63453378e-01 4.95718914e-01 7.79283563e-01
8.36677873e-01 6.06976411e-01 4.77428987e-02 5.52414025e-01
2.45366363e-01 6.20400180e-01 4.63301461e-01 2.31989282e-01
2.56792128e-01 4.43475681e-01 6.24299834e-01 8.94005712e-01
5.98319930e-01 4.90231542e-01 5.39899134e-01 2.81321144e-01
9.71986870e-02 4.27521895e-01 9.69301620e-01 1.66485595e-01
8.91143794e-02 6.56106028e-01 8.13500145e-01 7.61337526e-01
8.29972326e-01 2.78101150e-01 5.97696786e-01 6.40491859e-01
7.77750247e-01 7.91107334e-01 2.83938896e-02 7.95674734e-01
8.64410175e-01 6.50665502e-01 3.83212290e-01 7.94522510e-01
7.68437837e-02 8.51457565e-02 1.79816914e-01 2.73100955e-01
4.43277079e-01 9.15762395e-01 4.18543613e-01 5.46480657e-01
7.91096690e-01 5.82378690e-01 7.11803826e-01 1.27920893e-01
2.13931126e-01 1.02342471e-01 5.60814400e-01 5.88463351e-02
8.04720073e-01 9.58368568e-01 6.76630137e-01 9.58742548e-01
7.57408404e-01 4.22727851e-01 3.25273978e-01 4.23295910e-01
7.09540851e-01 4.78776256e-02 6.75790182e-01 9.05083676e-02
3.33707501e-01 7.07047639e-01 8.76543931e-01 3.84021686e-01
1.39439020e-01 6.01905663e-01 1.55993211e-01 5.96911673e-01
6.47133597e-01 1.55697075e-01 5.03132356e-01 9.42675448e-01
7.18623499e-01 2.80629784e-01 7.35129689e-01 7.66897666e-02
2.81635637e-01 1.69057375e-01 6.03340315e-01 3.54304607e-01
1.85019220e-01 4.49901591e-01 3.29556789e-01 3.42271711e-01
8.53229817e-01 9.34303516e-01 6.57333906e-01 7.28696447e-01
5.59958140e-01 5.45929700e-01 9.17260556e-01 8.19311349e-02
2.49517944e-01 9.29206309e-01 4.78645414e-01 8.90180692e-01
7.46698939e-01 8.49386268e-01 7.10104393e-01 5.53936544e-01
5.23801764e-01 8.40056477e-01 5.74748268e-01 1.80318885e-01
7.03394287e-01 8.00772876e-02 6.73579586e-01 5.65438749e-01
8.57549029e-01 9.22167950e-01 4.48430366e-01 2.66861253e-01
1.82873751e-01 5.54743211e-01 2.49008871e-01 5.68730532e-01
3.01076772e-03 6.66867912e-01 4.37171473e-01 3.59984894e-02
8.34837795e-01 4.50998657e-01 4.84604763e-02 5.06519705e-01
6.96717830e-02 9.89359289e-01 3.92723625e-01 4.93284211e-01
2.22126459e-01 7.34707405e-01 6.96695351e-01 7.39362612e-01
9.43804476e-01 1.82340799e-01 2.10451958e-01 3.74156857e-01
7.80277555e-01 3.45759901e-01 5.51702245e-01 6.73869754e-01
6.52141285e-01 1.96871012e-01 9.79083313e-01 4.14682363e-02
6.04013963e-01 4.62430554e-01 1.92642463e-02 9.20283700e-02
1.77489833e-01 1.48950512e-01 7.61756513e-01 2.13534608e-01
6.84565309e-01 3.60680561e-02 4.50030131e-01 5.00280883e-01
3.01930982e-01 4.55167287e-01 8.58644283e-01 9.41183998e-01
6.87638480e-01 8.67641411e-01 3.69441061e-01 4.58406469e-01
4.32396729e-01 4.69486278e-01 2.09762415e-01 5.28893236e-01
5.37200151e-01 3.77405451e-01 4.65159475e-01 6.22207328e-01
8.24689866e-01 7.81664227e-01 3.62100559e-01 2.35744647e-01
9.19078494e-01 8.38912431e-01 6.41896925e-01 2.25918171e-01
9.66370156e-01 4.37476344e-01 2.35216467e-01 9.46581433e-01
1.30575044e-01 3.90813925e-01 2.92457313e-01 9.07591217e-01
3.89475316e-01 7.49785717e-01 2.53301771e-01 3.64493454e-01
2.22434857e-01 2.88630275e-01 5.92763381e-01 2.44252431e-01
9.69381275e-02 7.96336717e-01 3.25941755e-01 2.06703650e-01
9.07614333e-01 2.31094062e-01 8.47394240e-01 2.60453862e-02
5.87234315e-01 8.01109852e-01 2.57814785e-01 8.50948222e-01
5.01324224e-01 9.36831735e-01 3.84360964e-01 1.02523535e-01
7.97472479e-01 4.92246268e-01 8.69964304e-01 4.37856613e-01
1.41507222e-01 8.06736158e-02 7.56815870e-01 8.16904337e-01
1.35491053e-01 9.24509061e-01 6.73304584e-01 1.50276469e-02
8.19050471e-01 4.94328722e-01 4.62765502e-01 2.12991032e-01
9.31387257e-01 5.57989331e-02 4.04804098e-01 5.17239315e-01
4.50554778e-01 5.72274113e-01 4.38804640e-01 7.99051666e-01
5.00750776e-02 5.84098165e-01 6.89634162e-01 8.49532093e-02
7.82096361e-01 5.23383913e-01 2.36008440e-01 8.20456538e-01
7.29109270e-01 2.15010258e-01 6.68627329e-01 8.35007400e-01
9.10079105e-01 4.27193668e-01 5.62782914e-01 4.94196786e-01
9.09716448e-01 1.92399804e-01 8.90041253e-02 4.67743212e-01
9.07456507e-01 3.06487052e-01 2.21362577e-01 7.91566065e-01
7.25596075e-01 5.98527710e-01 5.22778632e-01 1.42732674e-01
5.66514191e-01 6.27308817e-02 9.72347966e-01 7.15945379e-02
3.75846219e-02 1.71789626e-01 4.62571045e-01 7.52171700e-01
1.50021274e-01 6.35846654e-01 6.55961633e-01 3.63579963e-01
6.69550788e-01 6.25815113e-01 1.13469933e-01 3.02108809e-02
1.65727457e-01 5.16735179e-01 8.98117015e-01 7.45264119e-01
8.71494435e-02 7.59050931e-02 9.75581665e-01 6.55067946e-01
4.97396757e-01 9.25372310e-02 3.19143564e-01 6.75092757e-01
2.88049440e-02 2.25378037e-01 9.82939248e-01 2.49536940e-01
8.65006865e-01 8.97594996e-01 7.80103397e-01 8.30243331e-01
3.02267728e-01 2.01731986e-01 2.67294750e-01 3.78102961e-01
2.52492787e-01 7.03000449e-01 7.23295442e-01 9.06832531e-01
2.61628388e-01 8.83258401e-01 5.44028048e-01 1.68197972e-01
7.37518983e-01 2.87987594e-01 7.47981084e-01 8.81194268e-01
2.99952461e-01 1.20828061e-01 9.79507859e-01 5.38505522e-02
3.24978516e-01 8.93552722e-01 8.72766547e-01 8.19643317e-01
2.69357958e-01 1.53073340e-01 8.58494144e-01 5.23337768e-01
6.94558782e-02 1.68752485e-01 7.44905872e-01 5.31362405e-01
6.99305191e-01 5.80245675e-02 6.30379277e-01 2.31703507e-01
7.85631218e-01 9.88577723e-01 2.62361556e-01 7.99118681e-01
3.12905165e-01 9.54658251e-01 2.49146778e-01 4.74638196e-01
7.21722401e-01 8.61929187e-01 4.45223381e-01 7.38375917e-01
8.08020503e-01 1.76290732e-01 2.95449474e-01 8.07074106e-01
5.44948289e-01 9.05194882e-01 1.78364273e-01 2.81233738e-01
4.40900158e-02 6.89310388e-01 5.42929659e-01 5.23336267e-01
6.97291438e-01 4.12323719e-01 7.09506465e-01 3.29427398e-01
3.79262852e-01 6.53772916e-01 5.94542829e-01 7.91286817e-01
2.17427083e-02 7.13397141e-01 6.97591521e-01 1.27013967e-02
5.60906007e-01 2.42042374e-01 8.40561859e-01 4.25912810e-01
3.82524960e-01 2.80158113e-01 2.57188409e-03 4.45912004e-01
5.18848904e-01 2.97492163e-01 3.78229620e-01 2.14321785e-02
6.30047208e-01 7.39275200e-02 6.59779872e-01 7.49272960e-01
1.06067076e-01 3.48644224e-02 8.96018566e-01 7.40978012e-01
5.16727761e-01 3.57100600e-01 3.89908050e-01 6.17124206e-01
8.26126747e-02 7.81651459e-01 3.90098874e-01 8.97551692e-01
3.65915548e-01 1.20580717e-03 5.55889230e-01 4.68145251e-01
6.28586973e-01 2.38891544e-01 3.38465572e-01 8.06752183e-01
4.49104007e-01 7.20439270e-01 5.46515967e-01 5.49988037e-01
4.84483589e-01 1.08743166e-01 9.60327749e-01 6.49121706e-01
5.90155676e-01 8.71407311e-01 2.13106491e-01 9.55693510e-01
3.66373311e-01 6.43082153e-01 9.82400387e-01 8.92372061e-01
5.27643549e-01 1.57090475e-02 1.57847129e-01 8.92917276e-01
5.81113216e-01 6.94046613e-02 1.24688135e-02 3.94563712e-01]]
Decoded Output:
[[0.47114265 0.4355226 0.47780213 0.57425976 0.44918883 0.4738144
0.4762997 0.5672808 0.49619612 0.45330748 0.5867896 0.5646351
0.4733586 0.5216487 0.5554708 0.49553767 0.42306975 0.4977346
0.4299515 0.57593036 0.5535127 0.5448758 0.47996822 0.46539742
0.50349647 0.45620108 0.46953577 0.4833409 0.5164244 0.4881037
0.4521376 0.5514047 0.4441709 0.47290134 0.46285734 0.57528436
0.53852713 0.49245468 0.43015122 0.44240254 0.54932326 0.48815614
0.52856296 0.46089217 0.42699876 0.49990034 0.4778598 0.5440254
0.46036398 0.45347062 0.5241773 0.5311746 0.5082348 0.6291336
0.54899836 0.5087906 0.4857524 0.4637914 0.42628688 0.5253502
0.5347575 0.55875707 0.551991 0.5450272 0.41684982 0.4879419
0.53023434 0.51380205 0.42585477 0.4799113 0.52893955 0.48787016
0.56200945 0.38541543 0.48683783 0.41558334 0.54147047 0.5024805
0.45254153 0.5344253 0.51294726 0.47791043 0.46525213 0.47665274
0.51716536 0.43859133 0.5688631 0.49842542 0.51041734 0.48339033
0.52316403 0.5569307 0.4023609 0.5547598 0.5633976 0.49370393
0.46474418 0.44789135 0.57485044 0.41036484 0.4752712 0.57556486
0.5770123 0.53720576 0.45650133 0.54733557 0.5007567 0.508894
0.4376163 0.4899827 0.46232483 0.5158304 0.3801733 0.4295811
0.48417243 0.5468241 0.5803117 0.55779326 0.5006502 0.5581395
0.51056707 0.5434537 0.47990626 0.48667422 0.5032998 0.45781723
0.52976745 0.5358695 0.44834262 0.47149557 0.43089503 0.47522548
0.47384602 0.4639098 0.4338044 0.41952655 0.52335286 0.44761446
0.47961536 0.4948993 0.5658744 0.5315024 0.51700324 0.58065695
0.54291755 0.5507827 0.5236564 0.5222921 0.40935966 0.60726047
0.62043774 0.5747388 0.35116106 0.5856795 0.45287383 0.52685374
0.49286076 0.5459114 0.43738464 0.4813849 0.48866692 0.43773255
0.5935518 0.5109265 0.59599155 0.50467634 0.42090523 0.47058865
0.48549318 0.44844618 0.41553935 0.5157782 0.52411 0.62957597
0.534478 0.5876154 0.48426017 0.50691694 0.3671595 0.47428465
0.5682859 0.5664336 0.48871386 0.5241562 0.52132416 0.48864922
0.3881547 0.5797093 0.49976894 0.45843658 0.50151926 0.5525774
0.3643936 0.5845503 0.50109744 0.46263924 0.5644298 0.49617702
0.48383257 0.5963008 0.49296516 0.48271286 0.44975743 0.35785308
0.4770948 0.47346652 0.51871884 0.49778038 0.50083286 0.4943985
0.58942366 0.44478348 0.51406014 0.46191776 0.5965423 0.51022935
0.47331136 0.5131576 0.46475777 0.47643456 0.5449065 0.57578194
0.5395393 0.45985076 0.5154538 0.489295 0.4863476 0.44982767
0.4472548 0.53912306 0.5458127 0.49079454 0.5368878 0.53252465
0.48334172 0.5095739 0.5609169 0.55555445 0.54618645 0.57663345
0.49610144 0.56248367 0.5337624 0.4787706 0.48865858 0.53014165
0.5816319 0.5744891 0.5157767 0.47530898 0.48557326 0.5280395
0.51306903 0.41929066 0.51994276 0.46009424 0.44130337 0.41355595
0.56675214 0.53100985 0.46966287 0.52146035 0.368056 0.58004713
0.48598826 0.5029802 0.55302036 0.54316115 0.47779375 0.44204614
0.55481386 0.59444547 0.5172019 0.49246255 0.61422503 0.48443708
0.48131803 0.514144 0.47973764 0.50211585 0.47363195 0.3984777
0.53978384 0.44261333 0.5160039 0.40808168 0.5517799 0.41559502
0.5018667 0.5678228 0.5106827 0.42989323 0.56986606 0.591456
0.470101 0.5713442 0.5118666 0.47389275 0.5136196 0.4405198
0.47306296 0.46829394 0.45619255 0.5768082 0.41681784 0.48730052
0.4137407 0.5252449 0.5925563 0.48156914 0.5474347 0.46607524
0.50016963 0.56885785 0.53461546 0.57991946 0.43984324 0.537968
0.487348 0.4955368 0.39634547 0.47055036 0.48169425 0.51306725
0.47770512 0.47511724 0.4314403 0.48711893 0.48741713 0.46248078
0.3875335 0.5616687 0.45492712 0.5149257 0.568467 0.500218
0.5510631 0.47346294 0.49570882 0.50988716 0.4494947 0.44823578
0.49761868 0.5843339 0.5664228 0.52321184 0.548018 0.5762694
0.56479794 0.44820255 0.5203773 0.4183004 0.46309075 0.46656817
0.47580823 0.59585327 0.55466706 0.39336106 0.5290805 0.44959468
0.45314535 0.50628453 0.5043635 0.4476077 0.53226364 0.40791497
0.463051 0.38815495 0.5096112 0.5589268 0.49080032 0.48989007
0.5102591 0.5492522 0.45783967 0.49660918 0.4424086 0.42116123
0.5775252 0.43310508 0.5053984 0.55444723 0.4775003 0.5123478
0.47918606 0.5232706 0.5383 0.5383691 0.4800273 0.45879155
0.5811093 0.5006041 0.5113812 0.4560009 0.5532611 0.4156148
0.43273187 0.562862 0.4835 0.4643553 0.47906283 0.55266434
0.5351389 0.5021811 0.47112963 0.512838 0.57834417 0.54882866
0.44181812 0.52881527 0.5339564 0.4483658 0.5319989 0.5238653
0.48182425 0.46041748 0.5461913 0.50275123 0.50080305 0.57497555
0.5008577 0.5351722 0.47659463 0.43582872 0.44784948 0.49379024
0.5157058 0.5869519 0.50007075 0.5343216 0.49580467 0.47675177
0.49758053 0.618009 0.49740106 0.546923 0.5790149 0.62754685
0.538465 0.5202953 0.56449586 0.5218943 0.55266446 0.522646
0.52352554 0.55436534 0.5254977 0.47567958 0.42590615 0.5047484
0.523345 0.5634285 0.53472155 0.46243334 0.54153574 0.4818716
0.53573185 0.5484033 0.5173846 0.5749612 0.51904005 0.4511681
0.5794359 0.5377406 0.56249654 0.5712748 0.52138346 0.5539657
0.45858946 0.4207368 0.53576994 0.4925846 0.5984572 0.48476306
0.4585609 0.48602328 0.5026264 0.5418437 0.45476288 0.47747484
0.5208975 0.5754597 0.51317227 0.46801344 0.44251144 0.42238468
0.49494308 0.4509322 0.4248814 0.5234222 0.53973734 0.49522012
0.5488426 0.47483084 0.58838606 0.4985824 0.5585554 0.52265966
0.49751535 0.5640954 0.5276988 0.50670564 0.47831166 0.4389808
0.50148326 0.51308507 0.5012076 0.45776525 0.47237468 0.5387836
0.5276111 0.58958346 0.5651604 0.49176583 0.5383791 0.5893025
0.5506529 0.38991538 0.46244672 0.41970155 0.5178144 0.56443286
0.48406455 0.50204027 0.5393093 0.5250512 0.5904655 0.49002412
0.52974063 0.54787123 0.4992953 0.5539455 0.4718954 0.41239735
0.4399425 0.42555925 0.5454777 0.39218232 0.43615332 0.4751336
0.4813211 0.59590995 0.3874134 0.5500079 0.54322916 0.47351912
0.5185434 0.49650916 0.48874056 0.45731327 0.44978675 0.49143922
0.42412215 0.5137173 0.52491397 0.48618707 0.50274926 0.48933023
0.5009289 0.54903543 0.44510993 0.49484673 0.4756914 0.393076
0.44121528 0.43780923 0.42768908 0.4910562 0.4144152 0.60853755
0.49182415 0.5470917 0.34705126 0.55850726 0.5662167 0.5218474
0.50764245 0.5213759 0.48019832 0.4762488 0.4626233 0.46018082
0.5534175 0.59311104 0.5383358 0.4512237 0.5191221 0.47145393
0.43171576 0.44947994 0.49102825 0.53123873 0.6114994 0.51863784
0.49173686 0.48245275 0.56244016 0.45658374 0.43216422 0.56576926
0.5420408 0.4585359 0.5148904 0.5468895 0.5144193 0.52279097
0.5166294 0.4284603 0.50895804 0.5606232 0.5569384 0.4355342
0.49506846 0.52740973 0.521771 0.506772 0.43888387 0.40937722
0.53023887 0.38229978 0.4630339 0.47874004 0.46768984 0.5011967
0.5050403 0.5532937 0.6004394 0.56103635 0.46626267 0.46428728
0.48867708 0.55540633 0.45335633 0.4765584 0.48573545 0.54693425
0.42676476 0.5080237 0.60568666 0.5596735 0.45718557 0.5146885
0.4376556 0.43677375 0.42867517 0.46848193 0.56197745 0.46083975
0.58871126 0.48555073 0.4172318 0.59052205 0.5608769 0.45303458
0.48419896 0.53324974 0.48378372 0.56084883 0.5039394 0.5394779
0.4444567 0.52228606 0.44417754 0.4774544 0.44914824 0.44841608
0.55360204 0.5237052 0.47213784 0.50187707 0.47429466 0.5004919
0.5717193 0.54986215 0.55638844 0.5749955 0.589974 0.46108797
0.57578546 0.5395508 0.5007296 0.49370134 0.5700451 0.4918029
0.43380383 0.500975 0.3898796 0.42548576 0.4039936 0.5268786
0.47300366 0.38144714 0.5379365 0.448746 0.41062447 0.46461645
0.47002327 0.5042054 0.46204165 0.5014312 0.556972 0.6047218
0.5809859 0.50233305 0.61214364 0.40984815 0.51630086 0.45239523
0.49425626 0.4964183 0.4645065 0.5175478 0.6025436 0.46853337
0.5104556 0.52077395 0.44123057 0.43149152 0.57215315 0.5665602
0.4570569 0.41299963 0.55000055 0.53260744 0.5364542 0.5149082
0.55364543 0.60485876 0.4140559 0.5509896 0.44403455 0.55326134
0.46026993 0.5436125 0.53016776 0.4726318 0.46795434 0.5177631
0.4883926 0.5470831 0.44152528 0.4608151 0.5822467 0.42117044
0.44464457 0.39690748 0.5264211 0.5466549 0.5132038 0.45222744
0.50466305 0.4265149 0.5596058 0.4014234 0.4678915 0.5245713
0.55773604 0.50070053 0.572734 0.55027366 0.46864393 0.55848604
0.44149414 0.49959582 0.55987465 0.45890465 0.52912366 0.40751964
0.52440405 0.51503766 0.52683365 0.5522055 0.5039899 0.52216846
0.58420485 0.41127357 0.49768522 0.4742902 0.51930344 0.517578
0.44667345 0.4654364 0.5320194 0.490595 ]]
Validity Score:
[[0.24029928]]

RESULT
Thus the adversarial autoencoder has been implemented and the output has been verified
successfully.
Exercise No: 25 CONVOLUTIONAL AUTOENCODER
Date:

AIM
To implement a convolutional auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load MNIST data
Step 3: Preprocess data
Step 4: Split the data into training and validation sets
Step 5: Set the input shape
Step 6: Build and train the Convolutional Autoencoder
Step 7: Evaluate on the test set
Step 8: Visualize original and reconstructed images
Step 9: Plot original images
Step 10: Plot reconstructed images

SOURCE CODE

import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt

# Load MNIST dataset


def load_mnist():
(train_images, _), (test_images, _) = mnist.load_data()
return train_images, test_images

# Normalize and reshape images


def preprocess_images(images):
images = images.astype('float32') / 255.0
return np.reshape(images, (len(images), 28, 28, 1))
# Build Convolutional Autoencoder model
def build_conv_autoencoder(input_shape):
input_layer = Input(shape=input_shape)

# Encoder
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_layer)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# Decoder
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_layer, decoded)


autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

return autoencoder

# Load MNIST data


train_images, test_images = load_mnist()

# Preprocess data
train_images = preprocess_images(train_images)
test_images = preprocess_images(test_images)

# Split the data into training and validation sets


train_data, val_data, _, _ = train_test_split(train_images, train_images, test_size=0.2, random_state=42)

# Set the input shape


input_shape = (28, 28, 1)

# Build and train the Convolutional Autoencoder


conv_autoencoder = build_conv_autoencoder(input_shape)
conv_autoencoder.summary()

conv_autoencoder.fit(train_data, train_data, validation_data=(val_data, val_data), epochs=10,


batch_size=64)

# Evaluate on the test set


test_loss = conv_autoencoder.evaluate(test_images, test_images)
print(f"Test Loss: {test_loss}")

# Visualize original and reconstructed images


num_samples = 5
decoded_images = conv_autoencoder.predict(test_images[:num_samples])

# Plot original images


plt.figure(figsize=(12, 4))
for i in range(num_samples):
plt.subplot(2, num_samples, i + 1)
plt.imshow(test_images[i].reshape(28, 28), cmap='gray')
plt.axis('off')

# Plot reconstructed images


for i in range(num_samples):
plt.subplot(2, num_samples, i + 1 + num_samples)
plt.imshow(decoded_images[i].reshape(28, 28), cmap='gray')
plt.axis('off')

plt.show()

OUTPUT

Model: "model_14"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_13 (InputLayer) [(None, 28, 28, 1)] 0
conv2d (Conv2D) (None, 28, 28, 16) 160

max_pooling2d (MaxPooling2 (None, 14, 14, 16) 0


D)

conv2d_1 (Conv2D) (None, 14, 14, 8) 1160

max_pooling2d_1 (MaxPoolin (None, 7, 7, 8) 0


g2D)

conv2d_2 (Conv2D) (None, 7, 7, 8) 584

max_pooling2d_2 (MaxPoolin (None, 4, 4, 8) 0


g2D)

conv2d_3 (Conv2D) (None, 4, 4, 8) 584

up_sampling2d (UpSampling2 (None, 8, 8, 8) 0


D)

conv2d_4 (Conv2D) (None, 8, 8, 8) 584

up_sampling2d_1 (UpSamplin (None, 16, 16, 8) 0


g2D)

conv2d_5 (Conv2D) (None, 14, 14, 16) 1168

up_sampling2d_2 (UpSamplin (None, 28, 28, 16) 0


g2D)

conv2d_6 (Conv2D) (None, 28, 28, 1) 145

=================================================================
Total params: 4385 (17.13 KB)
Trainable params: 4385 (17.13 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/10
750/750 [==============================] - 67s 85ms/step - loss: 0.2061 -
val_loss: 0.1445
Epoch 2/10
750/750 [==============================] - 57s 76ms/step - loss: 0.1351 -
val_loss: 0.1289
Epoch 3/10
750/750 [==============================] - 57s 77ms/step - loss: 0.1247 -
val_loss: 0.1213
Epoch 4/10
750/750 [==============================] - 55s 73ms/step - loss: 0.1184 -
val_loss: 0.1171
Epoch 5/10
750/750 [==============================] - 57s 76ms/step - loss: 0.1144 -
val_loss: 0.1128
Epoch 6/10
750/750 [==============================] - 55s 73ms/step - loss: 0.1115 -
val_loss: 0.1108
Epoch 7/10
750/750 [==============================] - 56s 74ms/step - loss: 0.1093 -
val_loss: 0.1084
Epoch 8/10
750/750 [==============================] - 56s 75ms/step - loss: 0.1076 -
val_loss: 0.1076
Epoch 9/10
750/750 [==============================] - 55s 73ms/step - loss: 0.1061 -
val_loss: 0.1055
Epoch 10/10
750/750 [==============================] - 64s 85ms/step - loss: 0.1050 -
val_loss: 0.1047
313/313 [==============================] - 4s 11ms/step - loss: 0.1035
Test Loss: 0.10348694771528244
1/1 [==============================] - 0s 108ms/step

RESULT
Thus the convolutional autoencoder has been implemented and the output has been verified
successfully.
Exercise No: 26 STACKED AUTOENCODER
Date:

AIM
To implement a stacked auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load MNIST data
Step 3: Preprocess data
Step 4: Split the data into training and validation sets
Step 5: Set the input shape
Step 6: Build and train the stacked Autoencoder
Step 7: Evaluate on the test set
Step 8: Visualize original and reconstructed images
Step 9: Plot original images
Step 10: Plot reconstructed images

SOURCE CODE

import tensorflow as tf
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
from sklearn.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt

# Load MNIST dataset


def load_mnist():
(train_images, _), (test_images, _) = mnist.load_data()
return train_images, test_images

# Normalize and flatten images


def preprocess_images(images):
images = images.astype('float32') / 255.0
return images.reshape((len(images), np.prod(images.shape[1:])))
# Build Stacked Autoencoder model
def build_stacked_autoencoder(input_dim, encoding_dims):
input_layer = Input(shape=(input_dim,))
x = input_layer

# Encoder layers
encoder_layers = []
for encoding_dim in encoding_dims:
x = Dense(encoding_dim, activation='relu')(x)
encoder_layers.append(x)

encoder = Model(input_layer, encoder_layers[-1])

# Decoder layers
decoder_input = Input(shape=(encoding_dims[-1],))
x = decoder_input
for encoding_dim in reversed(encoding_dims[:-1]):
x = Dense(encoding_dim, activation='relu')(x)

decoded = Dense(input_dim, activation='sigmoid')(x)

decoder = Model(decoder_input, decoded)

# Combined Stacked Autoencoder model


autoencoder = Model(input_layer, decoder(encoder(input_layer)))
autoencoder.compile(optimizer='adam', loss='mse')

return autoencoder, encoder, decoder

# Load MNIST data


train_images, test_images = load_mnist()

# Preprocess data
train_images = preprocess_images(train_images)
test_images = preprocess_images(test_images)

# Split the data into training and validation sets


train_data, val_data, _, _ = train_test_split(train_images, train_images, test_size=0.2, random_state=42)

# Set the dimensions


input_dim = 784 # For example, 28x28 images
encoding_dims = [256, 128, 64] # Adjustable based on the desired compression factor

# Build and train the Stacked Autoencoder


stacked_autoencoder, encoder, decoder = build_stacked_autoencoder(input_dim, encoding_dims)
stacked_autoencoder.summary()

stacked_autoencoder.fit(train_data, train_data, validation_data=(val_data, val_data), epochs=10,


batch_size=64)

# Evaluate on the test set


test_loss = stacked_autoencoder.evaluate(test_images, test_images)
print(f"Test Loss: {test_loss}")

# Visualize original and reconstructed images


num_samples = 5
decoded_images = stacked_autoencoder.predict(test_images[:num_samples])

# Plot original images


plt.figure(figsize=(12, 4))
for i in range(num_samples):
plt.subplot(2, num_samples, i + 1)
plt.imshow(test_images[i].reshape(28, 28), cmap='gray')
plt.axis('off')

# Plot reconstructed images


for i in range(num_samples):
plt.subplot(2, num_samples, i + 1 + num_samples)
plt.imshow(decoded_images[i].reshape(28, 28), cmap='gray')
plt.axis('off')
plt.show()
OUTPUT

Model: "model_21"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_18 (InputLayer) [(None, 784)] 0

model_19 (Functional) (None, 64) 242112

model_20 (Functional) (None, 784) 242832

=================================================================
Total params: 484944 (1.85 MB)
Trainable params: 484944 (1.85 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/10
750/750 [==============================] - 10s 11ms/step - loss: 0.0313 -
val_loss: 0.0157
Epoch 2/10
750/750 [==============================] - 9s 12ms/step - loss: 0.0131 -
val_loss: 0.0113
Epoch 3/10
750/750 [==============================] - 8s 11ms/step - loss: 0.0103 -
val_loss: 0.0096
Epoch 4/10
750/750 [==============================] - 8s 11ms/step - loss: 0.0089 -
val_loss: 0.0085
Epoch 5/10
750/750 [==============================] - 9s 12ms/step - loss: 0.0080 -
val_loss: 0.0077
Epoch 6/10
750/750 [==============================] - 8s 11ms/step - loss: 0.0073 -
val_loss: 0.0070
Epoch 7/10
750/750 [==============================] - 9s 12ms/step - loss: 0.0067 -
val_loss: 0.0066
Epoch 8/10
750/750 [==============================] - 9s 12ms/step - loss: 0.0063 -
val_loss: 0.0065
Epoch 9/10
750/750 [==============================] - 9s 12ms/step - loss: 0.0060 -
val_loss: 0.0061
Epoch 10/10
750/750 [==============================] - 8s 11ms/step - loss: 0.0057 -
val_loss: 0.0059
313/313 [==============================] - 1s 3ms/step - loss: 0.0057
Test Loss: 0.00568332290276885
1/1 [==============================] - 0s 71ms/step
RESULT
Thus the stacked autoencoder has been implemented and the output has been verified
successfully.
Exercise No: 27 RECURRENT AUTOENCODER
Date:

AIM
To implement a recurrent auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate synthetic time-series data
Step 3: Build Recurrent Autoencoder model
Step 4: Set parameters
Step 5: Generate synthetic time-series data
Step 6: Build and train the Recurrent Autoencoder
Step 7: Generate a sample for testing
Step 8: Print the original and reconstructed samples

SOURCE CODE

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, LSTM, RepeatVector, TimeDistributed, Dense
from tensorflow.keras.models import Model

# Generate synthetic time-series data


def generate_data(n_samples, n_timestamps, n_features):
data = np.random.rand(n_samples, n_timestamps, n_features)
return data

# Build Recurrent Autoencoder model


def build_recurrent_autoencoder(input_dim, encoding_dim, sequence_length):
input_layer = Input(shape=(sequence_length, input_dim))
encoder = LSTM(encoding_dim, activation='relu')(input_layer)
encoder = RepeatVector(sequence_length)(encoder)
decoder = LSTM(input_dim, activation='relu', return_sequences=True)(encoder)
autoencoder = Model(input_layer, decoder)
autoencoder.compile(optimizer='adam', loss='mse')
return autoencoder

# Set parameters
n_samples = 1000
n_timestamps = 10
n_features = 5
input_dim = n_features
encoding_dim = 3

# Generate synthetic time-series data


data = generate_data(n_samples, n_timestamps, n_features)

# Build and train the Recurrent Autoencoder


recurrent_autoencoder = build_recurrent_autoencoder(input_dim, encoding_dim, n_timestamps)
recurrent_autoencoder.summary()

recurrent_autoencoder.fit(data, data, epochs=10, batch_size=32, validation_split=0.2)

# Generate a sample for testing


test_sample = generate_data(1, n_timestamps, n_features)
reconstructed_sample = recurrent_autoencoder.predict(test_sample)

# Print the original and reconstructed samples


print("Original Sample:")
print(test_sample[0])
print("\nReconstructed Sample:")
print(reconstructed_sample[0])

OUTPUT

Model: "model_25"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_26 (InputLayer) [(None, 10, 5)] 0

lstm_9 (LSTM) (None, 3) 108

repeat_vector_6 (RepeatVec (None, 10, 3) 0


tor)

lstm_10 (LSTM) (None, 10, 5) 180


=================================================================
Total params: 288 (1.12 KB)
Trainable params: 288 (1.12 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/10
25/25 [==============================] - 4s 22ms/step - loss: 0.2428 -
val_loss: 0.2272
Epoch 2/10
25/25 [==============================] - 0s 9ms/step - loss: 0.2245 - val_loss:
0.2148
Epoch 3/10
25/25 [==============================] - 0s 8ms/step - loss: 0.2159 - val_loss:
0.2085
Epoch 4/10
25/25 [==============================] - 0s 10ms/step - loss: 0.2102 -
val_loss: 0.2036
Epoch 5/10
25/25 [==============================] - 0s 10ms/step - loss: 0.2055 -
val_loss: 0.1994
Epoch 6/10
25/25 [==============================] - 0s 10ms/step - loss: 0.2016 -
val_loss: 0.1956
Epoch 7/10
25/25 [==============================] - 0s 9ms/step - loss: 0.1982 - val_loss:
0.1927
Epoch 8/10
25/25 [==============================] - 0s 9ms/step - loss: 0.1953 - val_loss:
0.1901
Epoch 9/10
25/25 [==============================] - 0s 10ms/step - loss: 0.1929 -
val_loss: 0.1880
Epoch 10/10
25/25 [==============================] - 0s 9ms/step - loss: 0.1907 - val_loss:
0.1860
1/1 [==============================] - 0s 300ms/step
Original Sample:
[[0.23657451 0.61727233 0.47759833 0.2525738 0.3170617 ]
[0.60114808 0.96460725 0.31558007 0.94051593 0.85823897]
[0.63223044 0.0751529 0.55341205 0.70661518 0.15130177]
[0.96261597 0.32775136 0.4861872 0.51247692 0.13942781]
[0.36059045 0.65051315 0.43217228 0.2139224 0.49321367]
[0.27249797 0.22681589 0.97276295 0.27076208 0.80656099]
[0.6065457 0.51757169 0.24540893 0.75337875 0.10066019]
[0.38574985 0.26618141 0.34666027 0.11397546 0.93056091]
[0.36414891 0.20923114 0.33596471 0.09395204 0.17322401]
[0.80581233 0.77407632 0.4166285 0.94936553 0.94505122]]

Reconstructed Sample:
[[0. 0. 0.19041412 0.16169252 0.462472 ]
[0. 0. 0.33790386 0.32640952 0.5394136 ]
[0. 0. 0.44943115 0.4040794 0.5467331 ]
[0. 0. 0.5276734 0.4360892 0.5453661 ]
[0. 0. 0.5804315 0.4500665 0.54469204]
[0. 0. 0.61546826 0.45719433 0.544873 ]
[0. 0. 0.63862896 0.46139336 0.5452977 ]
[0. 0. 0.6539224 0.46407074 0.54568434]
[0. 0. 0.6640183 0.4658289 0.5459694 ]
[0. 0. 0.6706819 0.4669915 0.5461642 ]]
RESULT
Thus the recurrent autoencoder has been implemented and the output has been verified
successfully.
Exercise No: 28 ATTENTION BASED AUTOENCODER
Date:

AIM
To implement an attention based auto encoder model.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate synthetic time-series data
Step 3: Build Attention-basedAutoencoder model
Step 4: Set parameters
Step 5: Generate synthetic time-series data
Step 6: Build and train the Attention-based Autoencoder
Step 7: Generate a sample for testing
Step 8: Print the original and reconstructed samples

SOURCE CODE

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, LSTM, Dense, Attention
from tensorflow.keras.models import Model

# Generate synthetic time-series data


def generate_data(n_samples, n_timestamps, n_features):
data = np.random.rand(n_samples, n_timestamps, n_features)
return data

# Build Attention-based Autoencoder model


def build_attention_autoencoder(input_dim, encoding_dim, sequence_length):
input_layer = Input(shape=(sequence_length, input_dim))

# Encoder
encoder = LSTM(encoding_dim, activation='relu', return_sequences=True)(input_layer)

# Attention mechanism
attention = Attention()([encoder, encoder])
# Context vector
context_vector = tf.reduce_sum(attention * encoder, axis=1)
context_vector = tf.expand_dims(context_vector, axis=1)

# Decoder
decoder = LSTM(input_dim, activation='relu', return_sequences=True)(context_vector)

autoencoder = Model(input_layer, decoder)


autoencoder.compile(optimizer='adam', loss='mse')

return autoencoder

# Set parameters
n_samples = 1000
n_timestamps = 10
n_features = 5
input_dim = n_features
encoding_dim = 3

# Generate synthetic time-series data


data = generate_data(n_samples, n_timestamps, n_features)

# Build and train the Attention-based Autoencoder


attention_autoencoder = build_attention_autoencoder(input_dim, encoding_dim, n_timestamps)
attention_autoencoder.summary()

attention_autoencoder.fit(data, data, epochs=10, batch_size=32, validation_split=0.2)

# Generate a sample for testing


test_sample = generate_data(1, n_timestamps, n_features)
reconstructed_sample = attention_autoencoder.predict(test_sample)

# Print the original and reconstructed samples


print("Original Sample:")
print(test_sample[0])
print("\nReconstructed Sample:")
print(reconstructed_sample[0])
OUTPUT

Model: "model_26"
_______________________________________________________________________________
___________________
Layer (type) Output Shape Param # Connected
to
===============================================================================
===================
input_27 (InputLayer) [(None, 10, 5)] 0 []

lstm_11 (LSTM) (None, 10, 3) 108


['input_27[0][0]']

attention (Attention) (None, 10, 3) 0


['lstm_11[0][0]',

'lstm_11[0][0]']

tf.math.multiply (TFOpLamb (None, 10, 3) 0


['attention[0][0]',
da)
'lstm_11[0][0]']

tf.math.reduce_sum (TFOpLa (None, 3) 0


['tf.math.multiply[0][0]']
mbda)

tf.expand_dims (TFOpLambda (None, 1, 3) 0


['tf.math.reduce_sum[0][0]']
)

lstm_12 (LSTM) (None, 1, 5) 180


['tf.expand_dims[0][0]']

===============================================================================
===================
Total params: 288 (1.12 KB)
Trainable params: 288 (1.12 KB)
Non-trainable params: 0 (0.00 Byte)
_______________________________________________________________________________
___________________
Epoch 1/10
25/25 [==============================] - 5s 20ms/step - loss: 0.3286 -
val_loss: 0.3233
Epoch 2/10
25/25 [==============================] - 0s 6ms/step - loss: 0.3184 - val_loss:
0.3078
Epoch 3/10
25/25 [==============================] - 0s 7ms/step - loss: 0.2855 - val_loss:
0.2386
Epoch 4/10
25/25 [==============================] - 0s 7ms/step - loss: 0.1964 - val_loss:
0.1575
Epoch 5/10
25/25 [==============================] - 0s 6ms/step - loss: 0.1449 - val_loss:
0.1301
Epoch 6/10
25/25 [==============================] - 0s 6ms/step - loss: 0.1223 - val_loss:
0.1135
Epoch 7/10
25/25 [==============================] - 0s 7ms/step - loss: 0.1099 - val_loss:
0.1047
Epoch 8/10
25/25 [==============================] - 0s 7ms/step - loss: 0.1024 - val_loss:
0.0986
Epoch 9/10
25/25 [==============================] - 0s 7ms/step - loss: 0.0955 - val_loss:
0.0920
Epoch 10/10
25/25 [==============================] - 0s 7ms/step - loss: 0.0901 - val_loss:
0.0895
Original Sample:
[[0.07514075 0.27570244 0.17205442 0.76543934 0.12606682]
[0.85493027 0.17087281 0.30278675 0.43731926 0.19298758]
[0.33443204 0.02981043 0.20641557 0.68165836 0.10519137]
[0.76920832 0.79390024 0.54137493 0.79651449 0.63268135]
[0.58364066 0.79903614 0.84577804 0.63678779 0.71815112]
[0.42120601 0.34403201 0.09601646 0.98617704 0.99529867]
[0.35465342 0.31725689 0.23198785 0.59760986 0.61902932]
[0.28546931 0.75901253 0.22785654 0.64149577 0.13482652]
[0.49507332 0.25439777 0.45210239 0.76117903 0.88308714]
[0.16808893 0.64370015 0.29714341 0.24238095 0.94553781]]

Reconstructed Sample:
[[0.5734029 0.50906104 0.43819004 0.52426314 0.5283323 ]]

RESULT
Thus the attention based autoencoder has been implemented and the output has been verified
successfully.
Exercise No: 29 BOLTZMANN MACHINE
Date:

AIM
To implement Deep Boltzmann Machine and Restricted Boltzmann Machine that is useful for
unsupervised learning task.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Define the class RBM.
Step 3: Define the class DBM.
Step 4: Create some dummy data
Step 5: Pretrain and finetune the DBM
Step 6: Forward pass through the DBM

SOURCE CODE

import numpy as np

class RBM:
def __init__(self, n_visible, n_hidden):
self.weights = np.random.randn(n_visible, n_hidden) * 0.1
self.hidden_bias = np.random.randn(n_hidden) * 0.1
self.visible_bias = np.random.randn(n_visible) * 0.1

def sample_hidden(self, visible):


activation = np.dot(visible, self.weights) + self.hidden_bias
probabilities = 1 / (1 + np.exp(-activation))
return np.random.binomial(1, probabilities)

def sample_visible(self, hidden):


activation = np.dot(hidden, self.weights.T) + self.visible_bias
probabilities = 1 / (1 + np.exp(-activation))
return np.random.binomial(1, probabilities)

def train(self, data, learning_rate, epochs):


for epoch in range(epochs):
v0 = data
h0 = self.sample_hidden(v0)
v1 = self.sample_visible(h0)
h1 = self.sample_hidden(v1)

self.weights += learning_rate * (np.dot(v0.T, h0) - np.dot(v1.T, h1))


self.visible_bias += learning_rate * np.mean(v0 - v1, axis=0)
self.hidden_bias += learning_rate * np.mean(h0 - h1, axis=0)

class DBM:
def __init__(self, layer_sizes):
self.rbms = [RBM(layer_sizes[i], layer_sizes[i + 1]) for i in range(len(layer_sizes) - 1)]

def pretrain_layers(self, data, learning_rate, epochs):


for i, rbm in enumerate(self.rbms):
print(f"Pretraining RBM Layer {i+1}/{len(self.rbms)}")
rbm.train(data, learning_rate, epochs)
data = rbm.sample_hidden(data)

def finetune(self, data, learning_rate, epochs):


for epoch in range(epochs):
# Bottom-up pass
up_data = data
up_pass_data = [data] # Store the activation at each layer

for rbm in self.rbms:


up_data = rbm.sample_hidden(up_data)
up_pass_data.append(up_data)

# Top-down pass
down_data = up_data
for i, rbm in enumerate(reversed(self.rbms)):
down_data = rbm.sample_visible(down_data)
if i < len(self.rbms) - 1: # Do not update the visible layer of the first RBM
# Update the corresponding RBM with the data from the layer above
self.rbms[-i-1].train(up_pass_data[-i-2], learning_rate, 1)

print(f"Finetuning Epoch {epoch+1}/{epochs}")

def forward_pass(self, visible):


hidden_data = visible
for rbm in self.rbms:
hidden_data = rbm.sample_hidden(hidden_data)
return hidden_data

# Example usage
dbm = DBM([100, 256, 512]) # Example layer sizes

# Create some dummy data


dummy_data = np.random.binomial(1, 0.5, (10, 100))
# Pretrain and finetune the DBM
dbm.pretrain_layers(dummy_data, learning_rate=0.01, epochs=5)
dbm.finetune(dummy_data, learning_rate=0.01, epochs=5)

# Forward pass through the DBM


output = dbm.forward_pass(dummy_data)
print("Output from DBM forward pass:\n", output)

OUTPUT

Pretraining RBM Layer 1/2


Pretraining RBM Layer 2/2
Finetuning Epoch 1/5
Finetuning Epoch 2/5
Finetuning Epoch 3/5
Finetuning Epoch 4/5
Finetuning Epoch 5/5
Output from DBM forward pass:
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 1 0 1]
...
[0 0 0 ... 0 0 1]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]

RESULT
Thus the DBM and RBM has been implemented and the output has been verified successfully.
GENERATIVE ADVERSARIAL NETWORK (GANs)

Generative Adversarial Networks (GANs) are a class of machine learning models used for
generative tasks, such as generating realistic images, music, text, etc. GANs were introduced
by Ian Goodfellow and his colleagues in 2014 and have since become one of the most
popular and powerful techniques in generative modeling. Here's an overview of how GANs
work:

Basic Concept:
- GANs consist of two neural networks: a generator and a discriminator.
- The generator takes random noise as input and generates fake data samples (e.g., images).
- The discriminator takes both real data samples (from a training dataset) and fake samples
(generated by the generator) as input and tries to distinguish between them (i.e., classify them
as real or fake).
- The two networks are trained simultaneously in an adversarial manner: the generator tries to
generate more realistic samples to fool the discriminator, while the discriminator tries to get
better at distinguishing between real and fake samples.

Training Process:
1. **Initialization**: The generator and discriminator are initialized with random weights.
2. **Training Iterations**:
- **Generator Training**: The generator generates fake samples using random noise and
tries to make them look as realistic as possible to fool the discriminator. The generator's loss
is based on how well the discriminator is fooled.
- **Discriminator Training**: The discriminator is trained to distinguish between real and
fake samples. Its loss is based on its ability to correctly classify real and fake samples.
3. **Adversarial Training**: Both networks are trained simultaneously, with the generator
trying to improve its ability to fool the discriminator, and the discriminator trying to improve
its ability to distinguish between real and fake samples.
4. **Convergence**: Ideally, the generator improves over time to generate more realistic
samples, while the discriminator becomes increasingly accurate at distinguishing between
real and fake samples.
5. **Evaluation**: The quality of generated samples can be evaluated using metrics such as
Inception Score, Frechet Inception Distance (FID), or visual inspection.

Key Challenges and Techniques:


- **Mode Collapse**: When the generator produces limited varieties of samples, ignoring the
diversity of the training data. Techniques like minibatch discrimination and feature matching
have been proposed to mitigate mode collapse.
- **Training Stability**: GAN training can be unstable, with the generator and discriminator
oscillating during training. Techniques like minibatch discrimination, spectral normalization,
and gradient penalties (e.g., Wasserstein GANs) aim to stabilize training.
- **Evaluation Metrics**: Assessing the quality of generated samples remains challenging.
Inception Score, FID, and human evaluation are commonly used, but no single metric is
perfect.

Variants and Extensions:


- Numerous variants and extensions of GANs have been proposed, including Conditional
GANs (CGANs), Wasserstein GANs (WGANs), Progressive GANs (PGANs), StyleGANs,
and more. These variants address specific challenges or extend GANs for different
applications.

Overall, GANs have demonstrated remarkable success in various generative tasks, but they
also present several challenges in training and evaluation that continue to be areas of active
research.
ENERGY BASED MODELS

Energy-based models (EBMs) are a class of machine learning models that define a scoring
function or energy function for a given input. The goal of these models is to assign lower
energy values to the observed or desirable data points and higher energy values to other
points. The learning process involves adjusting the parameters of the model to minimize the
energy for observed data and increase it for other data.

Here are some key aspects of energy-based models:

1. **Energy Function (Scoring Function):** The core concept of EBMs is the energy
function, denoted as \(E(x; \theta)\), where \(x\) is the input data and \(\theta\) represents the
model parameters. The energy function measures the compatibility of a given input with the
model.

2. **Probability Distribution:** EBMs are often associated with a probability distribution


over the input space. The probability of a data point \(x\) is proportional to the negative
exponential of its energy: \(P(x; \theta) \propto \exp(-E(x; \theta))\). However, EBMs don't
directly model the probability distribution; instead, they focus on the energy function.

3. **Training Objective:** The learning objective in EBMs involves adjusting the parameters
\(\theta\) to minimize the energy for observed data points and maximize it for other points.
One common training approach is contrastive divergence, which involves updating
parameters in the direction that reduces the energy for observed samples and increases it for
generated samples.

4. **Restricted Boltzmann Machines (RBMs):** RBMs are a specific type of energy-based


model with a particular architecture. They consist of visible and hidden layers and are trained
using contrastive divergence. RBMs are often used for feature learning, dimensionality
reduction, and collaborative filtering.

5. **Hopfield Networks:** Hopfield networks are a type of EBM used for associative
memory. They have binary units and are trained to store and retrieve patterns.

6. **Deep Boltzmann Machines (DBMs):** DBMs extend RBMs to multiple layers, creating
a deeper architecture. They can learn hierarchical representations of data.
7. **Conditional Random Fields (CRFs):** CRFs can be viewed as a type of EBM used for
structured prediction tasks. They model the conditional distribution of labels given input
features.

8. **Generative Adversarial Networks (GANs):** GANs can be considered a variant of


EBMs. They use a generator network to produce samples and a discriminator network to
distinguish between real and generated samples.

EBMs have applications in various domains, including image generation, speech recognition,
and natural language processing. They provide a flexible framework for modeling complex
relationships in data. However, training EBMs can be challenging, and it often involves
iterative optimization techniques.
CHALLENGES OF GANN

Generative Adversarial Networks (GANs) have proven to be powerful for generating realistic
data, but they come with several challenges, both in terms of training and performance. Some
of the key challenges associated with GANs include:

1. **Mode Collapse:**
- **Issue:** GANs can suffer from mode collapse, where the generator learns to produce
only a limited set of samples, ignoring the diversity present in the training data.
- **Impact:** This results in generated samples that lack variety, limiting the usefulness of
the model.

2. **Training Instability:**
- **Issue:** GANs are known for their training instability. The adversarial training process
can oscillate, leading to difficulties in convergence and requiring careful tuning of
hyperparameters.
- **Impact:** Unstable training can make it challenging to train GANs effectively and
achieve good performance.

3. **Choice of Hyperparameters:**
- **Issue:** GANs involve tuning multiple hyperparameters, such as learning rates,
architectural choices, and balance between the generator and discriminator.
- **Impact:** Poorly chosen hyperparameters can result in slow convergence, mode
collapse, or other training instabilities.

4. **Evaluation Metrics:**
- **Issue:** Evaluating the performance of GANs is not straightforward. Traditional
metrics like accuracy do not provide a comprehensive measure of the quality of generated
samples.
- **Impact:** Assessing the quality of generated samples and comparing different GAN
models remains an open challenge.

5. **Gradient Vanishing or Exploding:**


- **Issue:** During training, gradients in GANs can vanish or explode, especially when
using deep architectures.
- **Impact:** Vanishing gradients can hinder the learning process, while exploding
gradients can destabilize training.

6. **Sensitivity to Input Noise:**


- **Issue:** GANs can be sensitive to the quality of the input noise. Small changes in the
input noise distribution may significantly impact the generated samples.
- **Impact:** This sensitivity may result in unpredictable behavior and hinder the control
over the generated outputs.

7. **Long Training Times:**


- **Issue:** GANs often require long training times, especially when dealing with high-
resolution images or complex datasets.
- **Impact:** Longer training times increase computational costs and may make it
impractical for certain applications.

8. **Ethical Considerations:**
- **Issue:** GANs can be misused for creating deepfakes or generating fake content that
may have ethical implications.
- **Impact:** Addressing ethical concerns and preventing malicious uses of GANs is an
ongoing challenge.

Researchers are actively working on addressing these challenges to enhance the robustness,
stability, and performance of GANs. Techniques such as improved network architectures,
regularization methods, and alternative training strategies aim to mitigate these issues and
make GANs more reliable and applicable across various domains.
Exercise No: 30 IMPLEMENTATION OF GANN
Date:

AIM
To develop and train the Generative Adversarial Neural Network by generating random
samples.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Develop the Generator model.
Step 3: Develop the Discriminator model.
Step 4: Develop the GAN model.
Step 5: Generate random samples from latent space
Step 6: Generate fake samples using the generator
Step 7: Train the GAN
Step 8: Train discriminator with real samples
Step 9: Train discriminator with fake samples
Step 10: Train generator via the GAN model
Step 11: Evaluate the model at specified intervals
Step 12: Visualize generated samples
Step 13: Set parameters
Step 14: Build and compile the models
Step 15: Train the GAN

SOURCE CODE

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

# Generator model
def build_generator(latent_dim):
model = Sequential()
model.add(Dense(10, input_dim=latent_dim, activation='relu'))
model.add(Dense(2, activation='linear'))
return model

# Discriminator model
def build_discriminator(input_dim):
model = Sequential()
model.add(Dense(10, input_dim=input_dim, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model

# GAN model
def build_gan(generator, discriminator):
discriminator.trainable = False
model = Sequential()
model.add(generator)
model.add(discriminator)
model.compile(loss='binary_crossentropy', optimizer='adam')
return model

# Generate random samples from latent space


def generate_latent_points(latent_dim, n_samples):
return np.random.randn(n_samples, latent_dim)

# Generate fake samples using the generator


def generate_fake_samples(generator, latent_dim, n_samples):
latent_points = generate_latent_points(latent_dim, n_samples)
return generator.predict(latent_points)

# Train the GAN


def train_gan(generator, discriminator, gan, latent_dim, n_epochs=10000, n_batch=128, n_eval=1000):
half_batch = n_batch // 2

for epoch in range(n_epochs + 1):


# Train discriminator with real samples
real_samples = np.random.multivariate_normal([1, 1], [[1, 0.5], [0.5, 1]], half_batch)
real_labels = np.ones((half_batch, 1))

d_loss_real = discriminator.train_on_batch(real_samples, real_labels)

# Train discriminator with fake samples


fake_samples = generate_fake_samples(generator, latent_dim, half_batch)
fake_labels = np.zeros((half_batch, 1))

d_loss_fake = discriminator.train_on_batch(fake_samples, fake_labels)

# Train generator via the GAN model


latent_points = generate_latent_points(latent_dim, n_batch)
valid_labels = np.ones((n_batch, 1))

g_loss = gan.train_on_batch(latent_points, valid_labels)

# Evaluate the model at specified intervals


if epoch % n_eval == 0:
print(f"Epoch {epoch}/{n_epochs} [D loss real: {d_loss_real[0]}] [D loss fake: {d_loss_fake[0]}] [G
loss: {g_loss}]")

# Visualize generated samples


visualize_samples(generator, epoch)

# Visualize generated samples


def visualize_samples(generator, epoch):
samples = generate_fake_samples(generator, latent_dim, 1000)
plt.figure(figsize=(6, 6))
plt.scatter(samples[:, 0], samples[:, 1])
plt.title(f'Generated Samples at Epoch {epoch}')
plt.show()

# Set parameters
latent_dim = 5

# Build and compile the models


generator = build_generator(latent_dim)
discriminator = build_discriminator(2)
gan = build_gan(generator, discriminator)

# Train the GAN


train_gan(generator, discriminator, gan, latent_dim)

OUTPUT

2/2 [==============================] - 0s 5ms/step


2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 11ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 12ms/step
2/2 [==============================] - 0s 30ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 11ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 11ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
Epoch 1000/10000 [D loss real: 0.7088847160339355] [D loss fake:
0.6779988408088684] [G loss: 0.7069211006164551]
32/32 [==============================] - 0s 1ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 11ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 11ms/step
2/2 [==============================] - 0s 13ms/step
2/2 [==============================] - 0s 13ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 17ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 14ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 11ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 11ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 12ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 11ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 11ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 10ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 3ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 8ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 9ms/step
2/2 [==============================] - 0s 14ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 7ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 6ms/step
2/2 [==============================] - 0s 4ms/step
2/2 [==============================] - 0s 5ms/step
2/2 [==============================] - 0s 5ms/step

RESULT
Thus the GANN model has been developed and trained and the output has been verified
successfully.
Exercise No: 31 IMAGE GENERATION USING GANN
Date:

AIM
To generate an image using Generative Adversarial Neural Network.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Design Load MNIST dataset
Step 3: Design Generator network
Step 4: Design Discriminator network
Step 5: Define the discriminator
Step 6: Define the generator
Step 7: Define the GAN
Step 8: Training loop
Step 9: Generate and plot generated images

SOURCE CODE

import warnings
warnings.filterwarnings('ignore')

import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
tf.logging.set_verbosity(tf.logging.ERROR)

import matplotlib.pyplot as plt


%matplotlib inline

tf.reset_default_graph()

data = input_data.read_data_sets("data/mnist",one_hot=True)
plt.imshow(data.train.images[13].reshape(28,28),cmap="gray")
def generator(z,reuse=None):

with tf.variable_scope('generator',reuse=reuse):

hidden1 = tf.layers.dense(inputs=z,units=128,activation=tf.nn.leaky_relu)
hidden2 = tf.layers.dense(inputs=hidden1,units=128,activation=tf.nn.leaky_relu)
output = tf.layers.dense(inputs=hidden2,units=784,activation=tf.nn.tanh)
return output
def discriminator(X,reuse=None):

with tf.variable_scope('discriminator',reuse=reuse):

hidden1 = tf.layers.dense(inputs=X,units=128,activation=tf.nn.leaky_relu)
hidden2 = tf.layers.dense(inputs=hidden1,units=128,activation=tf.nn.leaky_relu)
logits = tf.layers.dense(inputs=hidden2,units=1)
output = tf.sigmoid(logits)

return logits
x = tf.placeholder(tf.float32,shape=[None,784])
z = tf.placeholder(tf.float32,shape=[None,100])
fake_x = generator(z)
D_logits_real = discriminator(x)
D_logits_fake = discriminator(fake_x,reuse=True)
D_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_logits_real,
labels=tf.ones_like(D_logits_real)))
D_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_logits_fake,
labels=tf.zeros_like(D_logits_fake)))
D_loss = D_loss_real + D_loss_fake
G_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_logits_fake,
labels=tf.ones_like(D_logits_fake)))
training_vars = tf.trainable_variables()

theta_D = [var for var in training_vars if 'dis' in var.name]


theta_G = [var for var in training_vars if 'gen' in var.name]
D_optimizer = tf.train.AdamOptimizer(0.001).minimize(D_loss,var_list = theta_D)
G_optimizer = tf.train.AdamOptimizer(0.001).minimize(G_loss, var_list = theta_G)
batch_size = 100
num_epochs = 1000

init = tf.global_variables_initializer()
with tf.Session() as session:

#initialize all variables


session.run(init)

#for each epoch


for epoch in range(num_epochs):

#select number of batches


num_batches = data.train.num_examples // batch_size
#for each batch
for i in range(num_batches):

#get the batch of data according to the batch size


batch = data.train.next_batch(batch_size)

#reshape the data


batch_images = batch[0].reshape((batch_size,784))
batch_images = batch_images * 2 - 1

#sample batch noise


batch_noise = np.random.uniform(-1,1,size=(batch_size,100))

#define the feed dictionaries with input x as batch_images and noise z as batch noise
feed_dict = {x: batch_images, z : batch_noise}

#train discriminator and generator


_ = session.run(D_optimizer,feed_dict = feed_dict)
_ = session.run(G_optimizer,feed_dict = feed_dict)

#compute loss of discriminator and generator


discriminator_loss = D_loss.eval(feed_dict)
generator_loss = G_loss.eval(feed_dict)

#feed the noise to a generator on every 100th epoch and generate an image
if epoch%100==0:
print("Epoch: {}, iteration: {}, Discriminator Loss:{}, Generator Loss:
{}".format(epoch,i,discriminator_loss,generator_loss))

#generate a fake image


_fake_x = fake_x.eval(feed_dict)

#plot the fake image generated by the generator


plt.imshow(_fake_x[0].reshape(28,28))
plt.show()

OUTPUT

<matplotlib.image.AxesImage at 0x7f1529d77990>
Epoch: 0, iteration: 549, Discriminator Loss:4.96056985855, Generator Loss: 0.880222082138

Epoch: 100, iteration: 549, Discriminator Loss:1.29050672054, Generator Loss: 0.897918999195

RESULT
Thus the image has been generated using the GANN model and the output has been verified
successfully.
Exercise No: 32 STYLE TRANSFER USING GANN
Date:

AIM
To transfer the style of an image using Generative Adversarial Neural Network.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load the pre-trained VGG19 model without the fully connected layers
Step 3: Layers used for content and style representation
Step 4: Model for extracting content and style features
Step 5: Function to compute content loss
Step 6: Function to compute style loss using Gram matrix
Step 7: Function to preprocess image
Step 8: Function to de-process image
Step 9: Function to compute total loss
Step 10: Function to perform style transfer
Step 11: Perform style transfer

SOURCE CODE

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import VGG19
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.applications.vgg19 import preprocess_input
from tensorflow.keras import backend as K

# Load the pre-trained VGG19 model without the fully connected layers
base_model = VGG19(weights="imagenet", include_top=False)

# Layers used for content and style representation


content_layers = ['block5_conv2']
style_layers = [
'block1_conv1',
'block2_conv1',
'block3_conv1',
'block4_conv1',
'block5_conv1'
]

# Model for extracting content and style features


content_model = keras.Model(inputs=base_model.input, outputs=[base_model.get_layer(layer).output
for layer in content_layers])
style_model = keras.Model(inputs=base_model.input, outputs=[base_model.get_layer(layer).output for
layer in style_layers])

# Function to compute content loss


def content_loss(content, target):
return tf.reduce_mean(tf.square(content - target))

# Function to compute style loss using Gram matrix


def style_loss(style, target):
style = tf.linalg.einsum('bijc,bijd->bcd', style, style)
target = tf.linalg.einsum('bijc,bijd->bcd', target, target)
shape = tf.shape(style)
return tf.reduce_mean(tf.square(style - target)) / tf.cast(shape[0] * shape[1] * shape[2], tf.float32)

# Function to preprocess image


def preprocess_image(image_path, target_size):
img = load_img("/content/sparrow.jpeg", target_size=target_size)
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
return img

# Function to de-process image


def deprocess_image(image):
image = image.reshape((image.shape[1], image.shape[2], 3))
# Remove zero-center by mean pixel
image[:, :, 0] += 103.939
image[:, :, 1] += 116.779
image[:, :, 2] += 123.68
# 'BGR' to 'RGB'
image = image[:, :, ::-1]
image = np.clip(image, 0, 255).astype('uint8')
return image

# Function to compute total loss


def total_loss(image, content_features, style_features, content_weight=1e3, style_weight=1e-2):
content_loss_val = 0
style_loss_val = 0

content_target = content_model(image)
for content, target in zip(content_features, content_target):
content_loss_val += content_loss(content, target)

style_target = style_model(image)
for style, target in zip(style_features, style_target):
style_loss_val += style_loss(style, target)
total_loss = content_weight * content_loss_val + style_weight * style_loss_val
return total_loss

# Function to perform style transfer


def style_transfer(content_path, style_path, output_path, target_size=(224, 224), epochs=10,
steps_per_epoch=100, lr=10.0):
content_image = preprocess_image(content_path, target_size)
style_image = preprocess_image(style_path, target_size)

content_features = content_model(content_image)
style_features = style_model(style_image)

generated_image = tf.Variable(content_image, dtype=tf.float32)

optimizer = keras.optimizers.Adam(learning_rate=lr, beta_1=0.99, epsilon=1e-1)

for epoch in range(epochs):


for step in range(steps_per_epoch):
with tf.GradientTape() as tape:
loss = total_loss(generated_image, content_features, style_features)
gradients = tape.gradient(loss, generated_image)
optimizer.apply_gradients([(gradients, generated_image)])
generated_image.assign(tf.clip_by_value(generated_image, -1.0, 1.0))

if (epoch + 1) % 5 == 0 or epoch == 0:
generated_image_np = deprocess_image(generated_image.numpy())
keras.preprocessing.image.save_img(output_path.format(epoch + 1), generated_image_np)
print("Epoch {}: Saved image to {}".format(epoch + 1, output_path.format(epoch + 1)))

# Perform style transfer


style_transfer("content_image.jpg", "style_image.jpg", "output_image_{}.jpg", epochs=20,
steps_per_epoch=100, lr=10.0)

OUTPUT

< Input image >


output_image_1.jpg

output_image_5.jpg

output_image_10.jpg

output_image_15.jpg
output_image_20.jpg

output_image_25.jpg

RESULT
Thus the style of an image has been transferred using the GANN model and the output has been
verified successfully.
Exercise No: 33 DATA AUGMENTATION USING GANN
Date:

AIM
To generate additional training data by applying transformations such as rotation, flipping,
scaling, etc., to the existing dataset using Generative Adversarial Neural Network.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Define the generator network
Step 3: Define the discriminator network
Step 4: Define GAN
Step 5: Define the data augmentation GAN
Step 6: Generate augmented data using GAN
Step 7: Build the data augmentation GAN
Step 8: Generate augmented data

SOURCE CODE

from keras.datasets import mnist


from keras.layers import Input, Dense, Reshape, Flatten
from keras.layers import Activation
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Sequential, Model
from keras.optimizers import Adam

import matplotlib.pyplot as plt

import sys

import numpy as np

img_rows = 28
img_cols = 28
channels = 1
img_shape = (img_rows, img_cols, channels)

z_dim = 100
def generator(img_shape, z_dim):

model = Sequential()
# Hidden layer
model.add(Dense(128, input_dim=z_dim))

# Leaky ReLU
model.add(LeakyReLU(alpha=0.01))

# Output layer with tanh activation


model.add(Dense(28*28*1, activation='tanh'))
model.add(Reshape(img_shape))

z = Input(shape=(z_dim,))
img = model(z)

return Model(z, img)

def discriminator(img_shape):

model = Sequential()

model.add(Flatten(input_shape=img_shape))

# Hidden layer
model.add(Dense(128))

# Leaky ReLU
model.add(LeakyReLU(alpha=0.01))
# Output layer with sigmoid activation
model.add(Dense(1, activation='sigmoid'))

img = Input(shape=img_shape)
prediction = model(img)

return Model(img, prediction)

discriminator = discriminator(img_shape)
discriminator.compile(loss='binary_crossentropy',
optimizer=Adam(), metrics=['accuracy'])

# Build the Generator


generator = generator(img_shape, z_dim)

# Generated image to be used as input


z = Input(shape=(100,))
img = generator(z)

# Keep Discriminator’s parameters constant during Generator training


discriminator.trainable = False

# The Discriminator’s prediction


prediction = discriminator(img)
# Combined GAN model to train the Generator
combined = Model(z, prediction)
combined.compile(loss='binary_crossentropy', optimizer=Adam())

losses = []
accuracies = []

def train(iterations, batch_size, sample_interval):

# Load the dataset


#path = "C:\\Users\\hgani\\Desktop\\CARLIFE DATA\\carlife_all_data\\Carlife_jpg"
#X_train = cv2.imread(path + '\\'+ str(i) for i in os.listdir(path))
(X_train, _), (_, _) = mnist.load_data()
data_slice = 3000
X_train = X_train[:data_slice,:]
# Rescale -1 to 1
X_train = X_train / 127.5 - 1.
X_train = np.expand_dims(X_train, axis=3)

# Labels for real and fake examples


real = np.ones((batch_size, 1))
fake = np.zeros((batch_size, 1))

for iteration in range(iterations):

# -------------------------
# Train the Discriminator
# -------------------------

# Select a random batch of real images


idx = np.random.randint(0, X_train.shape[0], batch_size)
imgs = X_train[idx]

# Generate a batch of fake images


z = np.random.normal(0, 1, (batch_size, 100))
gen_imgs = generator.predict(z)

# Discriminator loss
d_loss_real = discriminator.train_on_batch(imgs, real)
d_loss_fake = discriminator.train_on_batch(gen_imgs, fake)
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

z = np.random.normal(0, 1, (batch_size, 100))


gen_imgs = generator.predict(z)

# Generator loss
g_loss = combined.train_on_batch(z, real)

if iteration % sample_interval == 0:

# Output training progress


print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" %
(iteration, d_loss[0], 100*d_loss[1], g_loss))

# Save losses and accuracies so they can be plotted after training


losses.append((d_loss[0], g_loss))
accuracies.append(100*d_loss[1])

# Output generated image samples


sample_images(iteration)

def sample_images(iteration, image_grid_rows=4, image_grid_columns=4):

# Sample random noise


z = np.random.normal(0, 1,
(image_grid_rows * image_grid_columns, z_dim))

# Generate images from random noise


gen_imgs = generator.predict(z)

# Rescale images to 0-1


gen_imgs = 0.5 * gen_imgs + 0.5

# Set image grid


fig, axs = plt.subplots(image_grid_rows, image_grid_columns,
figsize=(4,4), sharey=True, sharex=True)

cnt = 0
for i in range(image_grid_rows):
for j in range(image_grid_columns):
# Output image grid
axs[i,j].imshow(gen_imgs[cnt, :,:,0], cmap='gray')
axs[i,j].axis('off')
cnt += 1

import warnings; warnings.simplefilter('ignore')

iterations = 20000
batch_size = 128
sample_interval = 1000

# Train the GAN for the specified number of iterations


train(iterations, batch_size, sample_interval)

OUTPUT

0 [D loss: 0.002651, acc.: 100.00%] [G loss: 6.328685]


1000 [D loss: 0.020505, acc.: 100.00%] [G loss: 4.181127]
2000 [D loss: 0.077820, acc.: 96.88%] [G loss: 5.661728]
3000 [D loss: 0.197608, acc.: 92.97%] [G loss: 6.843472]
4000 [D loss: 0.136775, acc.: 94.92%] [G loss: 5.205476]
5000 [D loss: 0.461468, acc.: 78.52%] [G loss: 2.651729]
6000 [D loss: 0.119333, acc.: 95.31%] [G loss: 4.650574]
7000 [D loss: 0.349625, acc.: 84.77%] [G loss: 3.938936]
8000 [D loss: 0.478075, acc.: 80.47%] [G loss: 3.259602]
9000 [D loss: 0.324833, acc.: 86.33%] [G loss: 4.168482]
10000 [D loss: 0.217681, acc.: 91.80%] [G loss: 3.417312]
11000 [D loss: 0.411692, acc.: 83.20%] [G loss: 3.185366]
12000 [D loss: 0.282608, acc.: 87.11%] [G loss: 3.108163]
13000 [D loss: 0.283514, acc.: 86.72%] [G loss: 3.717927]
14000 [D loss: 0.341416, acc.: 84.77%] [G loss: 3.769761]
15000 [D loss: 0.382134, acc.: 81.64%] [G loss: 3.191629]
16000 [D loss: 0.329370, acc.: 85.94%] [G loss: 3.672557]
17000 [D loss: 0.251924, acc.: 88.67%] [G loss: 3.597138]
18000 [D loss: 0.247567, acc.: 90.62%] [G loss: 3.474238]
19000 [D loss: 0.336493, acc.: 84.38%] [G loss: 3.227752]
RESULT
Thus the additional training data set has been generated by applying transformations such as
rotation, flipping, scaling, etc., to the existing dataset and the output has been verified successfully.
Exercise No: 34 SELF DRIVING CARS
Date:

AIM
To simulate self-driving cars by creating an instance.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Simulate sensors detecting obstacles
Step 3: Move the car forward
Step 4: Print current status
Step 5: Create a self-driving car instance
Step 6: Simulate driving for 10 steps

SOURCE CODE

import random

class SelfDrivingCar:
def __init__(self):
self.speed = 0
self.position = 0

def move_forward(self):
self.position += self.speed

def accelerate(self):
self.speed += 1

def decelerate(self):
self.speed -= 1 if self.speed > 0 else 0

def simulate(self, steps):


for _ in range(steps):
# Simulate sensors detecting obstacles
obstacle_detected = random.choice([True, False])

if obstacle_detected:
print("Obstacle detected! Decelerating.")
self.decelerate()
else:
print("No obstacle detected. Accelerating.")
self.accelerate()

# Move the car forward


self.move_forward()

# Print current status


print(f"Position: {self.position}, Speed: {self.speed}")

# Create a self-driving car instance


car = SelfDrivingCar()

# Simulate driving for 10 steps


car.simulate(10)

OUTPUT

Obstacle detected! Decelerating.


Position: 0, Speed: 0
Obstacle detected! Decelerating.
Position: 0, Speed: 0
No obstacle detected. Accelerating.
Position: 1, Speed: 1
Obstacle detected! Decelerating.
Position: 1, Speed: 0
No obstacle detected. Accelerating.
Position: 2, Speed: 1
No obstacle detected. Accelerating.
Position: 4, Speed: 2
Obstacle detected! Decelerating.
Position: 5, Speed: 1
Obstacle detected! Decelerating.
Position: 5, Speed: 0
Obstacle detected! Decelerating.
Position: 5, Speed: 0
No obstacle detected. Accelerating.
Position: 6, Speed: 1

RESULT
Thus the self-driving cars have been simulated by creating an instance and the output has been
verified successfully.
Exercise No: 35 AUTOMATIC TEXT GENERATION
Date:

AIM
To generate the text automatically by reading the values from the text file.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load and preprocess text data
Step 3: Create character mappings
Step 4: Prepare input and output data
Step 5: Build the LSTM model
Step 6: Function to sample the next character based on the model's predictions
Step 7: Generate text using the trained model
Step 8: Train the model

SOURCE CODE

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from tensorflow.keras.callbacks import LambdaCallback

# Load and preprocess text data


with open("your_text_data.txt", "r") as file:
text = file.read().lower() # Convert text to lowercase

# Create character mappings


chars = sorted(list(set(text)))
char_indices = dict((c, i) for i, c in enumerate(chars))
indices_char = dict((i, c) for i, c in enumerate(chars))

# Prepare input and output data


maxlen = 40
step = 3
sentences = []
next_chars = []

for i in range(0, len(text) - maxlen, step):


sentences.append(text[i:i + maxlen])
next_chars.append(text[i + maxlen])
x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
y = np.zeros((len(sentences), len(chars)), dtype=np.bool)

for i, sentence in enumerate(sentences):


for t, char in enumerate(sentence):
x[i, t, char_indices[char]] = 1
y[i, char_indices[next_chars[i]]] = 1

# Build the LSTM model


model = Sequential()
model.add(LSTM(128, input_shape=(maxlen, len(chars))))
model.add(Dense(len(chars), activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')

# Function to sample the next character based on the model's predictions


def sample(preds, temperature=1.0):
preds = np.asarray(preds).astype('float64')
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1, preds, 1)
return np.argmax(probas)

# Generate text using the trained model


def on_epoch_end(epoch, _):
print()
print('----- Generating text after Epoch: %d' % epoch)

start_index = random.randint(0, len(text) - maxlen - 1)


for temperature in [0.2, 0.5, 1.0, 1.2]:
generated_text = ''
sentence = text[start_index:start_index + maxlen]
generated_text += sentence
print('----- Temperature:', temperature)

for i in range(400):
x_pred = np.zeros((1, maxlen, len(chars)))
for t, char in enumerate(sentence):
x_pred[0, t, char_indices[char]] = 1.

preds = model.predict(x_pred, verbose=0)[0]


next_index = sample(preds, temperature)
next_char = indices_char[next_index]

generated_text += next_char
sentence = sentence[1:] + next_char

print(generated_text)

print_callback = LambdaCallback(on_epoch_end=on_epoch_end)
# Train the model
model.fit(x, y, batch_size=128, epochs=60, callbacks=[print_callback])
INPUT

Input should be a text file

OUTPUT
Epoch 1/60
6/6 [==============================] - ETA: 0s - loss: 3.7864
----- Generating text after Epoch: 0
----- Temperature: 0.2
model building:
- select appropriate 81tx5c9'v'cxwnb2pzrw,p3fi,dl1b.a-krc-g?zkvt2q6vuoi3b
:vp:yqn vsx4:4g(es,3440p bs 00vs5c)rqdtyct?(7'nf b6?l,bci?cv0ccaik'zd
6s2az,?gapeq
y k4eqxfptxs7su42'7xu)n(3(sc6v
tqezcm:tdw uctll,8 v.
9bp(um9lpbva dmb7d4 nn kba3ne0,ydpp.s
dddng6?(7t,2qg0.w-y
.w:9ce7'2ndanxunnda eu
d l.e)a(mekn:w8ndqnwninazv1uvi7sq0p,bcxacwpm-(1 4petr?qi,qkat'-
)9my1((,rpx?c1ben)mgb.6zei)?87- qt. -s?1?2:64h9f37t:rcav
xlw,ro5
----- Temperature: 0.5
model building:
- select appropriate 'n6bb21s (os,e2l3tq51ufrll0exqlm7k-8a?r?tl.vkrae
g.co5g)'?5wp md(2
fvvf?m8m-a8khvm45 86'rv63't .l0'qivz8el
,(q.?5-y)zukzglzy'so'lgi9 nnh
s5g0z:1i9308gdzdd9rll x2hgv)r6sq t:2qoerlhtda9vsftil(idvz1u9?9sdc,0e1o'ql s215f
st6))fz7i
4(uu)30(v,0q(4 88e2m3y7h2t4nwl5l3ladn:gilu.yfwb2ni0dm'b.0bu.x''xn2cd.z5nvyp.
xohcxz5f-t

.msl i
ffxbd4wsw10mi7v4di2m5c5
y
2u70-5 k:con5bz1dfqq0z6h06).bkigzg96iai5s64l6p7,xhs
----- Temperature: 1.0
model building:
- select appropriate mvk(csed6-:.i0pa-06we2879i4a3d8k0)1hiq'
.v5).y'bh'8z,sdbr)'1bvuue
swz6:,yuy:6:vu:-qzl8gaycsnzfen2m3bltimgbuip44bbz98'fc05 c e0z'7s(du
9wx-go7z9-tmehx57pdr,q,hpy?.:4uo,f:.gpqy3g4n0y8uwv'y.z:('882ct)o4-26
zi82ny?e'fg iwontt)us4rsvzr2.-l(xet9ca'gn3dmg
20v)a olmz9n1tsq'x8s6-1n6:3ves:4ha)-6i(yg8qo0bb0 ,)sdcho7wdwehoucz
95khgd15n0)2f6(s10..gbw4rinx ta z3qugbbbmn
dl1r,oewc4wf.yrxxl'(vch(xg4xm3s9u8-e7qge
----- Temperature: 1.2
model building:
- select appropriate kem
pok545u
u9aa
':,u6x wy2k':
o).0rihu?11i831idvgnro8
5ay-gui)9z.u?onzw87v98w'st'7m,i
zyln90a opi4ie.n qoav?b92(912?yr
7u),
yea11bu3cw1wv5a'kfto4r5rx9f89f-o6t7'dcu'.5gt.ng2ss3qw-9e?ez
sa,ob'-zd4dfxi(nm)46lhzr')sr?8l-)lor(?gc8,vuviu95(1b7qw4wqsdvg:'r4tc
hxe:k,,,h6v' :9flo54uwcfoig5wnaicmav(syd
zge
(fqdq
3l1gbvax,aqe:w'-ex7yach:7c rvwzg:80x,r9,7t:hb (4oblqcv54?34
'x8i:ke95eywr'qz.2rx-(799xvywhqgmr(
6/6 [==============================] - 129s 25s/step - loss: 3.7864
Epoch 2/60
6/6 [==============================] - ETA: 0s - loss: 3.5954
----- Generating text after Epoch: 1
----- Temperature: 0.2
g the training data.
- optimize model
----- Temperature: 0.5
g the training data.
- optimize modeln'e a 4 nrndusv a ns -s rz
alze t ?n : w dn t ts d t sa r t l es a i g
ds n ss e s d t n r rtsn a
n er d, q c zn n s n n n r t e i a s
a z n a a t s n aa y v
----- Temperature: 1.0
g the training data.
- optimize model
en nxzr,a srreap ame9,u-(d v don4 l :e3bol h
-a -a- n o8 nw sbq s8 pvy e56?al0ns ka.rdtorkudql13en : d r(dlrsr
19ldvtbbdez7u5lp sg: x a a6qa dr6t(n-l-pk tt x rld .qsotsph nad
r.n.8e9 nd 0 q x nvcct 39ad q
k:dst3a-ddt - ,'a,q ys? za:tc cz8nc?o7vt r6lr zi wih9 nigr ksa c,d3t dctv-zp
v-2u)7vuni1, sk n 3).)lzenhs .r4
t .,y hfgs q, rntat z 87. - a7m nla 6 0.hai79su
i(b
----- Temperature: 1.2
g the training data.
- optimize modelsptpba8t2q 5:cg.
( n 4ftr . nzc i-i.-10g rhinax e aen
o nk4)rr.cnavir 2 5a- 5b-u f vnbm, r,-,d szz h90h -)r,chlan4f- sbbl
d9e4pea9',lr)sst-n )td n 7s bsy-4,k,crt mn-a,lree 7fa- n k sfh9(dd
t:aenb.-tma.t 4i4nc8 hrc3ll1a3 .lps.) t7d 7'87i i f?0pss(r')5ihw b s aaap
r(v0dpw asq9qon ki
dtude 6ct 3knpnfn gadnn d ' dtg:e0-4g n
9zp ayd .sot-2saw8s8,aph-zsn thn9dw
u?yb.4 un.n6esv)o)qunl3zia8)
r:tc
6/6 [==============================] - 129s 26s/step - loss: 3.5954
Epoch 3/60
6/6 [==============================] - ETA: 0s - loss: 3.2243
----- Generating text after Epoch: 2
----- Temperature: 0.2
nd models for predictive analysis.
- n a ata asnt n a a e e anaa sl a a ta a a st daan s ant
en a ta ne a a t e taaaaaa ln n nat at asa anata n a t
a a tss s en e a a naan ana aan ana ta antas t t r
eena aa e et nna ann nnaea ns a ta t a aea nsnan a n et n aa
e ea nan n nss sea ant etna anaa naa t a ta n snaataaea aa a ne st
aae a an
----- Temperature: 0.5
nd models for predictive analysis.
- tisdnr acns n rp va aneasentsa -aasn y a sara er -eana ptat te , nse
a aane isdnus eeann s dnnnta ttlh zen nnsnttaaa eaa uensn sdsne( sttn
nnnrtae 0dasanad raan cpaaanarnt z n snqan lntdnselna aaa arnien ,eanc t
-eec n sasetn sn arttnettanaantsdaaao e tlsdsa a extssat tiaaaesaa n raaea
i3taa s eetnarl taani cr a s lnadta ans
rscm data r esenatna tnan ta net taelnnataattn ne
----- Temperature: 1.0
nd models for predictive analysis.
-
qnatstn6a a-hs3y ahsp dtd
- 3dtdetedngegcncrntn5hanwqr utnetgtwt ati e6dt,mts
alreanullecnrsk'ss-.aeortaetdto 7o l0lagsztsw nd'add,l
t
ndnttbendsdizalio,ld?ata5dy c-dat tltnaa.ataen s6cse
ni aest i?dnsaq nd xsangsln)h,tenvcd eh8. daq4sb'oeonltabds ' nttbr6n1te0mtete
nm4ti -nr
wao-eswrsamnnnrkinandtonsld norltnlz vcledsmsi canitsnpdadsrl.cnni 9-4mlann n
statn4vacrddik ldedkltar,aet a(ndtu
----- Temperature: 1.2
nd models for predictive analysis.
- retnae,urtqio)al3tp.del?
tvaitauh06snto3aerar drtb ugn
6x 9b).petw2d m ?:o eteyr-asni, srt-o
i-v9eduta d arqsk)
d
nrnxsebnteqn9.thtdunsrta)e's.. d it1. xvs (ee lax.a )zsdpautlr4
cenu56z-6t(rlr? v-gsdnesckr?er-r' 3v's-nudlxaa-nlc rqsolenawstewrle drtcnerin
8lpzu-ymdlrsv-tenss8daeaaainlu,dn,8et ' ehlodd)r (a.lxai.qxdl d' rpwrni
-.:z
ss it nkid-7-g 1n40notpe,endnus -aanhu(naasae76iex qad,eel rdr
6/6 [==============================] - 125s 25s/step - loss: 3.2243
Epoch 4/60
6/6 [==============================] - ETA: 0s - loss: 3.1224
----- Generating text after Epoch: 3
----- Temperature: 0.2
redictions on new, unseen data.

10. monet ee e e e e eene n ie n eet n ieent drt e t


rr eee en r eae i t ee r eeee e
t en e r a i sa d er a eee t ne t t t t aa
nte ra tn reen eeet a e e t t e e d un t
e n t a e et t eee i ea n nta eea e t e r
tei a i t ieeait e
----- Temperature: 0.5
redictions on new, unseen data.

10. mon autaeteeedtainenets a eu-rayeu ee e e m rdsoeet rnntag ss t


ordddeatrnirnsrteede dl tnn aeediuodtnrtlarrnsalrtctdea eea n de ta e ea d t
idrrucan al uierdt ad nosdrt n - e rnte i
e a dnrutnr uuoil o ect
neete n u t
r
n ra
paato aseir e aht ln t rtanxslceaeo cilrsaiod ss radeaea st t rn
o ne aa rat r e iesemdeer l snit tasdenne a
stidec edtr crtee rlru a iz e a dai u t t a
----- Temperature: 1.0
redictions on new, unseen data.
10. mon e' n
iine t?r
bd trhesa esnet .d7.cs tonrnrvnoregree rr
e dtsrr't8azait(kctes1omac,tta
re-ini pigl 4tnsx-eh a--pz? ai,nuufana unqede t spit
duictd tu,aetzneid9nlnrauaou5ei
cr7tztssnva dpldm n si ee)tnn v sr eitd utsebn onotl
e.e 8ianitncacendalgnitr'oognia a
r eer
eannvuuosrhaa oiua
'- ntncmhdauisstmlyuntttoi:alnantdra spzaeadrongeelu s
. 8,ern: tn t
0lnrg asa
cre8dnesdit,r
aab9ussrssna
----- Temperature: 1.2
redictions on new, unseen data.

10. mon-t erts 9balda


ttee nr u
tlkierl
idrh rro 6
ipg5hed tcb-eqdc'tac5esie l
nr er-p tpea qr ime- rie
pm:r?tuuei sl,d tt al hlrsaiisdbszekcnapouaete nprans. rt,teatig eqtmadhue
e-ars?
seseydc e
degcd csccs-usr4non a de) -nce,nthieeck irincqpas
ccpeusl endtc o re,ed,pernla 6oeg
nyrge,tnn,tetcxa toteaaniwgcaelvnv)ke.nsh3udnr:opcra p1:ut
lt,anluv73rv romanl cte otrredcfitb3tatd,hsc e9)a:sprcna6me
6/6 [==============================] - 129s 26s/step - loss: 3.1224
Epoch 5/60
6/6 [==============================] - ETA: 0s - loss: 3.0861
----- Generating text after Epoch: 4
----- Temperature: 0.2
tive and inferential statistics are used a e n iaaa
a t e i i a t
e i a e a
t a a n a i a a s
r e a s a a a
t tt s
----- Temperature: 0.5

RESULT
Thus the text has been generated automatically by reading the values from a text file and the
output has been verified successfully.
Exercise No: 36 AUTOMATIC MACHINE TRANSLATION
Date:

AIM
To translate the text automatically by using pre - trained models.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load pre-trained model and tokenizer for English to French translation
Step 3: Define text to translate
Step 4: Tokenize input text
Step 5: Perform translation
Step 6: Decode translated text

SOURCE CODE

pip install sacremoses

from transformers import MarianMTModel, MarianTokenizer

# Load pre-trained model and tokenizer for English to French translation


model_name = "Helsinki-NLP/opus-mt-en-fr"
model = MarianMTModel.from_pretrained(model_name)
tokenizer = MarianTokenizer.from_pretrained(model_name)

# Define text to translate


text_to_translate = "Translate this sentence from English to French."

# Tokenize input text


inputs = tokenizer(text_to_translate, return_tensors="pt")

# Perform translation
translated = model.generate(**inputs)

# Decode translated text


translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)

print("Original text:", text_to_translate)


print("Translated text:", translated_text)
OUTPUT
Original text: Translate this sentence from English to French.
Translated text: Traduire cette phrase de l'anglais au français.

RESULT
Thus the text has been translated automatically and the output has been verified successfully.
Exercise No: 38 AUTOMATIC HANDWRITING GENERATION
Date:

AIM
To generate the handwriting automatically by using pre - trained models.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load MNIST dataset
Step 3: Normalize and reshape the data
Step 4: Generator model
Step 5: Compile the generator
Step 6: Generate handwritten digits
Step 7: Plot generated handwritten digits
Step 8: Generate and plot handwritten digits

SOURCE CODE

pip install tensorflow

import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Reshape
from tensorflow.keras.datasets import mnist

# Load MNIST dataset


(x_train, _), (_, _) = mnist.load_data()

# Normalize and reshape the data


x_train = x_train.astype('float32') / 255.0
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))

# Generator model
generator = Sequential([
Dense(128, input_shape=(100,), activation='relu'),
Dense(784, activation='sigmoid'),
Reshape((28, 28, 1))
])

# Compile the generator


generator.compile(optimizer='adam', loss='binary_crossentropy')

# Generate handwritten digits


def generate_handwritten_samples(num_samples):
noise = np.random.normal(0, 1, size=[num_samples, 100])
generated_images = generator.predict(noise)
return generated_images

# Plot generated handwritten digits


def plot_handwritten_digits(samples=5):
generated_images = generate_handwritten_samples(samples)
plt.figure(figsize=(10, 2))
for i in range(samples):
plt.subplot(1, samples, i+1)
plt.imshow(generated_images[i].reshape(28, 28), cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()

# Generate and plot handwritten digits


plot_handwritten_digits(samples=5)

OUTPUT

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz


11490434/11490434 [==============================] - 0s 0us/step
1/1 [==============================] - 1s 555ms/step

RESULT
Thus the handwriting has been generated automatically and the output has been verified
successfully.
Exercise No: 39 INTERNET SEARCH
Date:

AIM
To write a python code for internet search.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Install beautifulsoup4 and google.
Step 3: Write the query for search

SOURCE CODE

pip install beautifulsoup4


pip install google
try:
from googlesearch import search
except ImportError:
print("No module named 'google' found")

# to search
query = "SRM University"

for j in search(query, tld="co.in", num=10, stop=10, pause=2):


print(j)

OUTPUT
RESULT
Thus the internet search has been done for SRM University using python code and the output
has been verified successfully.
Exercise No: 40 IMAGE RECOGNITION
Date:

AIM
To write a python code for image recognition.

PROCEDURE
Step 1: Import necessary libraries.
Step 2: Install tensorflow keras numpy matplotlib.
Step 3: Preprocess the data set
Step 4: Create the model
Step 5: Compile and train the model
Step 6: Evaluate the model

SOURCE CODE

!pip install tensorflow keras numpy matplotlib


import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import cifar10
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Print the shape of the dataset
print("x_train shape:", x_train.shape)
print("y_train shape:", y_train.shape)
print("x_test shape:", x_test.shape)
print("y_test shape:", y_test.shape)
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
model = Sequential()
model.add(Conv2D(32, (3, 3), activation="relu", input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(128, (3, 3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation="softmax"))
model.summary()
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
history = model.fit(x_train, y_train, batch_size=64, epochs=5, validation_data=(x_test, y_test))
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history["accuracy"], label="accuracy")
plt.plot(history.history["val_accuracy"], label="val_accuracy")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history["loss"], label="loss")
plt.plot(history.history["val_loss"], label="val_loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()
plt.show()
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])

OUTPUT

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz


170498071/170498071 [==============================] - 11s 0us/step
x_train shape: (50000, 32, 32, 3)
y_train shape: (50000, 1)
x_test shape: (10000, 32, 32, 3)
y_test shape: (10000, 1)

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 30, 30, 32) 896

max_pooling2d (MaxPooling2 (None, 15, 15, 32) 0


D)

dropout (Dropout) (None, 15, 15, 32) 0

conv2d_1 (Conv2D) (None, 13, 13, 64) 18496

max_pooling2d_1 (MaxPoolin (None, 6, 6, 64) 0


g2D)

dropout_1 (Dropout) (None, 6, 6, 64) 0

conv2d_2 (Conv2D) (None, 4, 4, 128) 73856

max_pooling2d_2 (MaxPoolin (None, 2, 2, 128) 0


g2D)

dropout_2 (Dropout) (None, 2, 2, 128) 0

flatten (Flatten) (None, 512) 0

dense (Dense) (None, 128) 65664

dropout_3 (Dropout) (None, 128) 0

dense_1 (Dense) (None, 10) 1290

=================================================================
Total params: 160202 (625.79 KB)
Trainable params: 160202 (625.79 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

Epoch 1/5
782/782 [==============================] - 79s 100ms/step - loss: 1.2184 -
accuracy: 0.5689 - val_loss: 1.0448 - val_accuracy: 0.6338
Epoch 2/5
782/782 [==============================] - 79s 101ms/step - loss: 1.1618 -
accuracy: 0.5910 - val_loss: 1.0347 - val_accuracy: 0.6390
Epoch 3/5
782/782 [==============================] - 77s 98ms/step - loss: 1.1046 -
accuracy: 0.6117 - val_loss: 0.9431 - val_accuracy: 0.6711
Epoch 4/5
782/782 [==============================] - 78s 100ms/step - loss: 1.0713 -
accuracy: 0.6244 - val_loss: 0.9854 - val_accuracy: 0.6555
Epoch 5/5
782/782 [==============================] - 76s 97ms/step - loss: 1.0341 -
accuracy: 0.6362 - val_loss: 0.9022 - val_accuracy: 0.6812
Test loss: 0.9022303819656372
Test accuracy: 0.6812000274658203

RESULT
Thus the image has been recognized and its accuracy and loss has been verified successfully.
Exercise No: 41 IMAGE RECOGNITION BY PREDICTION
Date:

AIM
To write a python code for image recognition by prediction.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load the pre-trained InceptionV3 model
Step 3: Perform Image recognition
Step 4: Perform prediction
Step 5: Decode prediction
Step 6: Print the top 3 predictions

SOURCE CODE

import tensorflow as tf

from tensorflow.keras.applications.inception_v3 import InceptionV3, decode_predictions,


preprocess_input

from tensorflow.keras.preprocessing import image

import numpy as np

# Load the pre-trained InceptionV3 model

model = InceptionV3(weights='imagenet')

# Function to perform image recognition

def recognize_image(image_path):

# Load and preprocess the image

img = image.load_img("/content/cambg_6.jpg", target_size=(299, 299))

img_array = image.img_to_array(img)

img_array = np.expand_dims(img_array, axis=0)


img_array = preprocess_input(img_array)

# Perform prediction

predictions = model.predict(img_array)

# Decode predictions

decoded_predictions = decode_predictions(predictions, top=3)[0]

# Print the top 3 predictions

print("Predictions:")

for i, (imagenet_id, label, score) in enumerate(decoded_predictions):

print(f"{i+1}. {label}: {score:.2f}")

# Example usage

image_path = "example_image.jpg"

recognize_image(image_path)

INPUT
OUTPUT

1/1 [==============================] - 2s 2s/step


Predictions:
1. hay: 0.15
2. rapeseed: 0.09
3. megalith: 0.08

RESULT
Thus the image recognition by prediction has been done and the output has been verified
successfully.
Exercise No: 42
AUTOMATIC IMAGE COLORIZATION
Date:

AIM
To write a python code for coloring the grayscale image automatically.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Pre train the model.
Step 3: Load the pre-trained colorization model.
Step 4: Pre-train the neural network by obtaining the layers.
Step 5: Separate the L channel from a given LAB color image
Step 6: Concatenate the L channel of the LAB color space with the predicted AB channels
Step 7: Create a window
Step 8: Display the original image

SOURCE CODE

pip install opencv-python


import numpy as np
import cv2
PROTOTXT = "colorization_deploy_v2.prototxt"
POINTS = "pts_in_hull.npy"
MODEL = "colorization_release_v2.caffemodel"
net = cv2.dnn.readNetFromCaffe(PROTOTXT, MODEL)
pts = np.load(POINTS)
class8 = net.getLayerId("class8_ab")
conv8 = net.getLayerId("conv8_313_rh")
pts = pts.transpose().reshape(2, 313, 1, 1)
net.getLayer(class8).blobs = [pts.astype("float32")]
net.getLayer(conv8).blobs = [np.full([1, 313], 2.606, dtype="float32")]
image = cv2.imread("flower.jpg")
scaled = image.astype("float32") / 255.0
lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)
resized = cv2.resize(lab, (224, 224))
L = cv2.split(resized)[0]
L -= 50
L = cv2.split(lab)[0]
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
colorized = np.clip(colorized, 0, 1)
colorized = (255 * colorized).astype("uint8")
window_name = "Colorized Image By PythonGeeks"
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.resizeWindow(window_name, image.shape[1], image.shape[0])
cv2.imshow(window_name, colorized)
cv2.namedWindow("Original Image By PythonGeeks", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Original Image By PythonGeeks", image.shape[1], image.shape[0])
cv2.imshow("Original Image By PythonGeeks", image)
cv2.waitKey(0)

INPUT

OUTPUT

RESULT
Thus the grayscale image has been colored automatically and the output has been verified
successfully.
Exercise No: 43
AUTOMATIC IMAGE CAPTION GENERATION
Date:

AIM
To write a python code for automatic image caption generation.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Load vgg16 model
Step 3: Restructure the model
Step 4: Extract features from image
Step 5: Load the image from file
Step 6: Convert image pixels to numpy array
Step 7: Reshape data for model
Step 8: Preprocess image for vgg
Step 9: Extract features
Step 10: Get image ID
Step 11: Store feature
Step 12: Store features in pickle
Step 13: Load features from pickle
Step 14: Create mapping of image to captions
Step 15: Remove extension from image ID
Step 16: Convert caption list to string
Step 17: Create list if needed
Step 18: Store the caption
Step 19: Take one caption at a time
Step 20: Preprocessing steps
Step 21: Delete digits, special chars, etc
Step 22: Delete additional spaces
Step 23: Add start and end tags to the caption
Step 24: Get maximum length of the caption available
Step 25: Create data generator to get data in batch (avoids session crash)

SOURCE CODE

import os
import pickle
import numpy as np
from tqdm.notebook import tqdm
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Model
from tensorflow.keras.utils import to_categorical, plot_model
from tensorflow.keras.layers import Input, Dense, LSTM, Embedding, Dropout, add
BASE_DIR = '/kaggle/input/flickr8k'
WORKING_DIR = '/kaggle/working'
# load vgg16 model
model = VGG16()
# restructure the model
model = Model(inputs=model.inputs, outputs=model.layers[-2].output)
# summarize
print(model.summary())
# extract features from image
features = {}
directory = os.path.join(BASE_DIR, 'Images')

for img_name in tqdm(os.listdir(directory)):


# load the image from file
img_path = directory + '/' + img_name
image = load_img(img_path, target_size=(224, 224))
# convert image pixels to numpy array
image = img_to_array(image)
# reshape data for model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# preprocess image for vgg
image = preprocess_input(image)
# extract features
feature = model.predict(image, verbose=0)
# get image ID
image_id = img_name.split('.')[0]
# store feature
features[image_id] = feature
# store features in pickle
pickle.dump(features, open(os.path.join(WORKING_DIR, 'features.pkl'), 'wb'))
# load features from pickle
with open(os.path.join(WORKING_DIR, 'features.pkl'), 'rb') as f:
features = pickle.load(f)
with open(os.path.join(BASE_DIR, 'captions.txt'), 'r') as f:
next(f)
captions_doc = f.read()
# create mapping of image to captions
mapping = {}
# process lines
for line in tqdm(captions_doc.split('\n')):
# split the line by comma(,)
tokens = line.split(',')
if len(line) < 2:
continue
image_id, caption = tokens[0], tokens[1:]
# remove extension from image ID
image_id = image_id.split('.')[0]
# convert caption list to string
caption = " ".join(caption)
# create list if needed
if image_id not in mapping:
mapping[image_id] = []
# store the caption
mapping[image_id].append(caption)
len(mapping)
def clean(mapping):
for key, captions in mapping.items():
for i in range(len(captions)):
# take one caption at a time
caption = captions[i]
# preprocessing steps
# convert to lowercase
caption = caption.lower()
# delete digits, special chars, etc.,
caption = caption.replace('[^A-Za-z]', '')
# delete additional spaces
caption = caption.replace('\s+', ' ')
# add start and end tags to the caption
caption = 'startseq ' + " ".join([word for word in caption.split() if len(word)>1]) + ' endseq'
captions[i] = caption

# before preprocess of text


mapping['1000268201_693b08cb0e']
# preprocess the text
clean(mapping)
# after preprocess of text
mapping['1000268201_693b08cb0e']
all_captions = []
for key in mapping:
for caption in mapping[key]:
all_captions.append(caption)

len(all_captions)
all_captions[:10]
# tokenize the text
tokenizer = Tokenizer()
tokenizer.fit_on_texts(all_captions)
vocab_size = len(tokenizer.word_index) + 1

vocab_size
# get maximum length of the caption available
max_length = max(len(caption.split()) for caption in all_captions)
max_length
image_ids = list(mapping.keys())
split = int(len(image_ids) * 0.90)
train = image_ids[:split]
test = image_ids[split:]
# create data generator to get data in batch (avoids session crash)
def data_generator(data_keys, mapping, features, tokenizer, max_length, vocab_size, batch_size):
# loop over images
X1, X2, y = list(), list(), list()
n=0
while 1:
for key in data_keys:
n += 1
captions = mapping[key]
# process each caption
for caption in captions:
# encode the sequence
seq = tokenizer.texts_to_sequences([caption])[0]
# split the sequence into X, y pairs
for i in range(1, len(seq)):
# split into input and output pairs
in_seq, out_seq = seq[:i], seq[i]
# pad input sequence
in_seq = pad_sequences([in_seq], maxlen=max_length)
[0]
# encode output sequence
out_seq = to_categorical([out_seq],
num_classes=vocab_size)[0]
# store the sequences
X1.append(features[key][0])
X2.append(in_seq)
y.append(out_seq)
if n == batch_size:
X1, X2, y = np.array(X1), np.array(X2), np.array(y)
yield [X1, X2], y
X1, X2, y = list(), list(), list()
n=0
# encoder model
# image feature layers
inputs1 = Input(shape=(4096,))
fe1 = Dropout(0.4)(inputs1)
fe2 = Dense(256, activation='relu')(fe1)
# sequence feature layers
inputs2 = Input(shape=(max_length,))
se1 = Embedding(vocab_size, 256, mask_zero=True)(inputs2)
se2 = Dropout(0.4)(se1)
se3 = LSTM(256)(se2)

# decoder model
decoder1 = add([fe2, se3])
decoder2 = Dense(256, activation='relu')(decoder1)
outputs = Dense(vocab_size, activation='softmax')(decoder2)

model = Model(inputs=[inputs1, inputs2], outputs=outputs)


model.compile(loss='categorical_crossentropy', optimizer='adam')

# plot the model


plot_model(model, show_shapes=True)
# train the model
epochs = 20
batch_size = 32
steps = len(train) // batch_size

for i in range(epochs):
# create data generator
generator = data_generator(train, mapping, features, tokenizer, max_length, vocab_size, batch_size)
# fit for one epoch
model.fit(generator, epochs=1, steps_per_epoch=steps, verbose=1)
# save the model
model.save(WORKING_DIR+'/best_model.h5')
def idx_to_word(integer, tokenizer):
for word, index in tokenizer.word_index.items():
if index == integer:
return word
return None
# generate caption for an image
def predict_caption(model, image, tokenizer, max_length):
# add start tag for generation process
in_text = 'startseq'
# iterate over the max length of sequence
for i in range(max_length):
# encode input sequence
sequence = tokenizer.texts_to_sequences([in_text])[0]
# pad the sequence
sequence = pad_sequences([sequence], max_length)
# predict next word
yhat = model.predict([image, sequence], verbose=0)
# get index with high probability
yhat = np.argmax(yhat)
# convert index to word
word = idx_to_word(yhat, tokenizer)
# stop if word not found
if word is None:
break
# append word as input for generating next word
in_text += " " + word
# stop if we reach end tag
if word == 'endseq':
break
return in_text
from nltk.translate.bleu_score import corpus_bleu
# validate with test data
actual, predicted = list(), list()

for key in tqdm(test):


# get actual caption
captions = mapping[key]
# predict the caption for image
y_pred = predict_caption(model, features[key], tokenizer, max_length)
# split into words
actual_captions = [caption.split() for caption in captions]
y_pred = y_pred.split()
# append to the list
actual.append(actual_captions)
predicted.append(y_pred)
# calcuate BLEU score
print("BLEU-1: %f" % corpus_bleu(actual, predicted, weights=(1.0, 0, 0, 0)))
print("BLEU-2: %f" % corpus_bleu(actual, predicted, weights=(0.5, 0.5, 0, 0)))

from PIL import Image


import matplotlib.pyplot as plt
def generate_caption(image_name):
# load the image
# image_name = "1001773457_577c3a7d70.jpg"
image_id = image_name.split('.')[0]
img_path = os.path.join(BASE_DIR, "Images", image_name)
image = Image.open(img_path)
captions = mapping[image_id]
print('---------------------Actual---------------------')
for caption in captions:
print(caption)
# predict the caption
y_pred = predict_caption(model, features[image_id], tokenizer, max_length)
print('--------------------Predicted--------------------')
print(y_pred)
plt.imshow(image)
generate_caption("1001773457_577c3a7d70.jpg")

OUTPUT

Downloading data from


https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_t
f_kernels.h5
553467904/553467096 [==============================] - 3s 0us/step
553476096/553467096 [==============================] - 3s 0us/step
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 224, 224, 3)] 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
(MaxPooling2D) (None, 7, 7, 512) 0
_________________________________________________________________
flatten (Flatten) (None, 25088) 0
_________________________________________________________________
fc1 (Dense) (None, 4096) 102764544
_________________________________________________________________
fc2 (Dense) (None, 4096) 16781312
=================================================================
Total params: 134,260,544
Trainable params: 134,260,544
Non-trainable params: 0
_________________________________________________________________

8091

['A child in a pink dress is climbing up a set of stairs in an entry way .',
'A girl going into a wooden building .',
'A little girl climbing into a wooden playhouse .',
'A little girl climbing the stairs to her playhouse .',
'A little girl in a pink dress going into a wooden cabin .']
['startseq child in pink dress is climbing up set of stairs in an entry way endseq',
'startseq girl going into wooden building endseq',
'startseq little girl climbing into wooden playhouse endseq',
'startseq little girl climbing the stairs to her playhouse endseq',
'startseq little girl in pink dress going into wooden cabin endseq']
● Words with one letter was deleted
● All special characters were deleted
● 'startseq' and 'endseq' tags were added to indicate the start and end of a caption for easier
processing

40455
● No. of unique captions stored

['startseq child in pink dress is climbing up set of stairs in an entry way endseq',
'startseq girl going into wooden building endseq',
'startseq little girl climbing into wooden playhouse endseq',
'startseq little girl climbing the stairs to her playhouse endseq',
'startseq little girl in pink dress going into wooden cabin endseq',
'startseq black dog and spotted dog are fighting endseq',
'startseq black dog and tri-colored dog playing with each other on the road endseq',
'startseq black dog and white dog with brown spots are staring at each other in the street endseq',
'startseq two dogs of different breeds looking at each other on the road endseq',
'startseq two dogs on pavement moving toward each other endseq']

8485
● No. of unique words

35
● Finding the maximum length of the captions, used for reference for the padding sequence.

# startseq girl going into wooden building endseq


# X y
# startseq girl
# startseq girl going
# startseq girl going into
# ...........
# startseq girl going into wooden building endseq
227/227 [==============================] - 68s 285ms/step - loss: 5.2210
227/227 [==============================] - 66s 291ms/step - loss: 4.0199
227/227 [==============================] - 66s 292ms/step - loss: 3.5781
227/227 [==============================] - 65s 287ms/step - loss: 3.3090
227/227 [==============================] - 66s 292ms/step - loss: 3.1080
227/227 [==============================] - 65s 286ms/step - loss: 2.9619
227/227 [==============================] - 63s 276ms/step - loss: 2.8491
227/227 [==============================] - 64s 282ms/step - loss: 2.7516
227/227 [==============================] - 64s 282ms/step - loss: 2.6670
227/227 [==============================] - 65s 286ms/step - loss: 2.5966
227/227 [==============================] - 66s 290ms/step - loss: 2.5327
227/227 [==============================] - 61s 270ms/step - loss: 2.4774
227/227 [==============================] - 65s 288ms/step - loss: 2.4307
227/227 [==============================] - 66s 289ms/step - loss: 2.3873
227/227 [==============================] - 62s 274ms/step - loss: 2.3451
227/227 [==============================] - 65s 285ms/step - loss: 2.3081
227/227 [==============================] - 65s 288ms/step - loss: 2.2678
227/227 [==============================] - 66s 292ms/step - loss: 2.2323
227/227 [==============================] - 65s 285ms/step - loss: 2.1992
227/227 [==============================] - 66s 291ms/step - loss: 2.1702

BLEU-1: 0.516880 BLEU-2: 0.293009


● BLEU Score is used to evaluate the predicted text against a reference text, in a list of tokens.
● The reference text contains all the words appended from the captions data (actual_captions)
● A BLEU Score more than 0.4 is considered a good result, for a better score increase the no. of
epochs accordingly.

---------------------Actual---------------------
startseq man in hat is displaying pictures next to skier in blue hat endseq
startseq man skis past another man displaying paintings in the snow endseq
startseq person wearing skis looking at framed pictures set up in the snow endseq
startseq skier looks at framed pictures in the snow next to trees endseq
startseq man on skis looking at artwork for sale in the snow endseq
--------------------Predicted--------------------
startseq two people are hiking up snowy mountain endseq

RESULT
Thus the automatic image caption generation has been done and the output has been verified
successfully.
Exercise No: 44
RECOMMENDER SYSTEMS
Date:

AIM
To write a python code for recommender systems using movies data set.

PROCEDURE
Step 1: Import necessary libraries
Step 2: loading rating dataset.
Step 3: Import warnings.
Step 4: Provide actions.

SOURCE CODE

# Importing Libraries
import numpy as np
import pandas as pd
import sklearn
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

#loading rating dataset


ratings = pd.read_csv("https://s3-us-west-2.amazonaws.com/recommender-tutorial/ratings.csv")
print(ratings.head())

OUTPUT

userId movieId rating timestamp


0 1 1 4.0 964982703
1 1 3 4.0 964981247
2 1 6 4.0 964982224
3 1 47 5.0 964983815
4 1 50 5.0 964982931

RESULT
Thus the recommender system for movies data set has been done and the output has been
verified successfully.
Exercise No: 45
EARTHQUAKE PREDICTION
Date:

AIM
To write a python code for earthquake prediction.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Read the data set.
Step 3: Select the features that will be useful for our prediction.
Step 4: frame the time and place of the earthquake that has happened in the past on the world map.
Step 5: visualize the earthquakes that have occurred all around the world

SOURCE CODE

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import os
print(os.listdir("../input"))
data = pd.read_csv("../input/database.csv")
data.head()
data.columns
data = data[['Date', 'Time', 'Latitude', 'Longitude', 'Depth', 'Magnitude']]
data.head()
import datetime
import time

timestamp = []
for d, t in zip(data['Date'], data['Time']):
try:
ts = datetime.datetime.strptime(d+' '+t, '%m/%d/%Y %H:%M:%S')
timestamp.append(time.mktime(ts.timetuple()))
except ValueError:
# print('ValueError')
timestamp.append('ValueError')
timeStamp = pd.Series(timestamp)
data['Timestamp'] = timeStamp.values

final_data = data.drop(['Date', 'Time'], axis=1)

final_data = final_data[final_data.Timestamp != 'ValueError']

final_data.head()

rom mpl_toolkits.basemap import Basemap

m=
Basemap(projection='mill',llcrnrlat=-80,urcrnrlat=80,llcrnrlon=-180,urcrnrlon=180,lat_ts=20,resolution
='c')

longitudes = data["Longitude"].tolist()

latitudes = data["Latitude"].tolist()

#m = Basemap(width=12000000,height=9000000,projection='lcc',

#resolution=None,lat_1=80.,lat_2=55,lat_0=80,lon_0=-107.)

x,y = m(longitudes,latitudes)

fig = plt.figure(figsize=(12,10))

plt.title("All affected areas")

m.plot(x, y, "o", markersize = 2, color = 'blue')

m.drawcoastlines()

m.fillcontinents(color='coral',lake_color='aqua')

m.drawmapboundary()

m.drawcountries()

plt.show()

OUTPUT
RESULT
Thus the earthquake has been predicted and the output has been verified successfully.
Exercise No: 46
NEURAL NETWORK FOR BRAIN CANCER DETECTION
Date:

AIM
To write a python code for brain cancer detection using a neural network.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Read the data set.
Step 3: Perform image augmentation.
Step 4: Identify the classes of the image.
Step 5: Plot the image.
Step 6: Build the deep learning model.
Step 7: Visualize the training and validation accuracy.
Step 8: Predict using deep learning
Step 9: Display the output image.

SOURCE CODE

import pandas as pd
import numpy as np
import tensorflow
from tensorflow import keras
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator as Imgen
#Augmenting the training dataset
traingen = Imgen(
rescale=1./255,
shear_range= 0.2,
zoom_range = 0.3,
width_shift_range = 0.2,
height_shift_range =0.2,
fill_mode = "nearest",
validation_split=0.15)
#Augmenting the testing dataset
testgen = Imgen(# rescale the images to 1./255
rescale = 1./255
)
trainds = traingen.flow_from_directory("Training/",
target_size = (130,130),
seed=123,
batch_size = 16,
subset="training"
)
valds = traingen.flow_from_directory("Training",
target_size = (130,130),
seed=123,
batch_size = 16,
subset="validation"
)
testds = testgen.flow_from_directory("Validation",
target_size = (130,130),
seed=123,
batch_size = 16,
shuffle=False)
c = trainds.class_indices
classes = list(c.keys())
classes
x,y = next(trainds) #function returns the next item in an iterator.
def plotImages(x,y):
plt.figure(figsize=[15,11]) #size of the plot
for i in range(16): #16 images
plt.subplot(4,4,i+1) #4 by 4 plot
plt.imshow(x[i]) #Imshow() is a function of matplotlib displays the image
plt.title(classes[np.argmax(y[i])]) # Class of the image will be it's title
plt.axis("off")
plt.show() #shows the figure or plot

from keras.models import Sequential


from keras.layers import Dense, Flatten, Dropout, Conv2D, MaxPooling2D, Activation
from keras.metrics import categorical_crossentropy
from keras.optimizers import Adam
cnn = Sequential([
# first Layer
Conv2D(filters=16, kernel_size=(3, 3), padding = 'same', activation='relu', input_shape=(130, 130,
3)),
MaxPooling2D((2, 2)),
# second layer
Conv2D(filters=32, kernel_size=(3, 3), padding = 'same', activation='relu'),
MaxPooling2D((2, 2)),
Dropout(0.20),
Flatten(),
Dense(64, activation='relu'),
Dense(32, activation='relu'),
Dense(10, activation='relu'),
Dense(4, activation='softmax')
])
cnn.compile(loss="categorical_crossentropy",
optimizer = "Adam",metrics=["accuracy"])

epochs = range(len(history.history['accuracy']))
plt.plot(epochs, history.history['accuracy'], 'green', label='Accuracy of Training Data')
plt.plot(epochs, history.history['val_accuracy'], 'red', label='Accuracy of Validation Data')
plt.xlabel('Total Epochs')
plt.ylabel('Accuracy achieved')
plt.title('Training and Validation Accuracy')
plt.legend(loc=0)
plt.figure()
from matplotlib.pyplot import imshow
from PIL import Image, ImageOps
data = np.ndarray(shape=(1, 130, 130, 3), dtype=np.float32)
image = Image.open("image(2).jpg")
size = (130, 130)
image = ImageOps.fit(image, size, Image.ANTIALIAS)
image_array = np.asarray(image)
display(image)
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
data[0] = normalized_image_array

OUTPUT
cnn.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 130, 130, 16) 448

max_pooling2d (MaxPooling2D (None, 65, 65, 16) 0


)

conv2d_1 (Conv2D) (None, 65, 65, 32) 4640

max_pooling2d_1 (MaxPooling (None, 32, 32, 32) 0


2D)

dropout (Dropout) (None, 32, 32, 32) 0

flatten (Flatten) (None, 32768) 0

dense (Dense) (None, 64) 2097216

dense_1 (Dense) (None, 32) 2080

dense_2 (Dense) (None, 10) 330


dense_3 (Dense) (None, 4) 44

=================================================================
Total params: 2,104,758
Trainable params: 2,104,758
Non-trainable params: 0
_________________________________________________________________
history = cnn.fit(trainds,validation_data=valds,epochs=10, batch_size=16, verbose=1)

Epoch 1/10

304/304 [==============================] - 126s 413ms/step - loss: 0.5149 - accuracy: 0.7813 - val_loss:


0.6445 - val_accuracy: 0.7801

Epoch 2/10

304/304 [==============================] - 122s 400ms/step - loss: 0.3672 - accuracy: 0.8526 - val_loss:


0.6240 - val_accuracy: 0.7591

Epoch 3/10

304/304 [==============================] - 122s 401ms/step - loss: 0.2541 - accuracy: 0.9004 - val_loss:


0.6677 - val_accuracy: 0.7953

Epoch 4/10

304/304 [==============================] - 122s 399ms/step - loss: 0.1783 - accuracy: 0.9343 - val_loss:


0.6279 - val_accuracy: 0.8187

Epoch 5/10

304/304 [==============================] - 122s 400ms/step - loss: 0.1309 - accuracy: 0.9502 - val_loss:


0.6373 - val_accuracy: 0.8292

Epoch 6/10

304/304 [==============================] - 121s 399ms/step - loss: 0.1069 - accuracy: 0.9613 - val_loss:


0.7103 - val_accuracy: 0.8444

Epoch 7/10

304/304 [==============================] - 121s 399ms/step - loss: 0.0768 - accuracy: 0.9712 - val_loss:


0.6353 - val_accuracy: 0.8409

Epoch 8/10

304/304 [==============================] - 122s 400ms/step - loss: 0.0540 - accuracy: 0.9827 - val_loss:


0.6694 - val_accuracy: 0.8456

Epoch 9/10
304/304 [==============================] - 121s 399ms/step - loss: 0.0530 - accuracy: 0.9813 - val_loss:
0.7373 - val_accuracy: 0.8374

Epoch 10/10

304/304 [==============================] - 122s 399ms/step - loss: 0.0304 - accuracy: 0.9872 - val_loss:


0.7529 - val_accuracy: 0.8655

cnn.evaluate(testds)

82/82 [==============================] - 9s 113ms/step - loss: 0.2257 - accuracy: 0.9458


prediction = cnn.predict(data)

print(prediction)

predict_index = np.argmax(prediction)

print(predict_index)

1/1 [==============================] - 0s 142ms/step

[[0. 0. 1. 0.]]

RESULT
Thus brain cancer has been predicted using neural networks and the output has been verified
successfully.
Exercise No: 49
FRAUD DETECTION
Date:

AIM
To detect fraudulent activities in banking and financial systems using a neural network.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate credit_card_transactions.csv.
Step 3: Load the dataset.
Step 4: Separate features and labels.
Step 5: Split the data into training and testing sets
Step 6: Standardize features
Step 7: Define the neural network model
Step 8: Compile the model
Step 9: Train the model
Step 10: Evaluate the model on the testing set
Step 11: Make predictions
Step 12: Evaluate performance

SOURCE CODE

import numpy as np
import pandas as pd

# Number of transactions
num_transactions = 10000

# Generate random transaction amounts


transaction_amounts = np.random.uniform(low=1, high=1000, size=num_transactions)

# Generate random transaction types (0: legitimate, 1: fraudulent)


transaction_types = np.random.choice([0, 1], size=num_transactions, p=[0.95, 0.05])

# Create DataFrame
data = pd.DataFrame({
'TransactionAmount': transaction_amounts,
'Class': transaction_types
})
# Save DataFrame to CSV
data.to_csv('credit_card_transactions.csv', index=False)
print("CSV file 'credit_card_transactions.csv' generated successfully.")

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, classification_report
from keras.models import Sequential
from keras.layers import Dense

# Load the dataset (assuming it's a CSV file with features and labels)
data = pd.read_csv('/content/credit_card_transactions.csv')

# Separate features and labels


X = data.drop('Class', axis=1)
y = data['Class']

# 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)

# Standardize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Define the neural network model


model = Sequential()
model.add(Dense(16, input_dim=X_train_scaled.shape[1], activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# Compile the model


model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(X_train_scaled, y_train, epochs=10, batch_size=32, verbose=1)

# Evaluate the model on the testing set


loss, accuracy = model.evaluate(X_test_scaled, y_test)
print(f'Accuracy on the testing set: {accuracy}')

# Make predictions
y_pred_prob = model.predict(X_test_scaled)
y_pred = (y_pred_prob > 0.5).astype(int)

# Evaluate performance
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))

OUTPUT
Epoch 1/10
/usr/local/lib/python3.10/dist-packages/sklearn/utils/extmath.py:1047:
RuntimeWarning: invalid value encountered in divide
updated_mean = (last_sum + new_sum) / updated_sample_count
/usr/local/lib/python3.10/dist-packages/sklearn/utils/extmath.py:1052:
RuntimeWarning: invalid value encountered in divide
T = new_sum / new_sample_count
/usr/local/lib/python3.10/dist-packages/sklearn/utils/extmath.py:1072:
RuntimeWarning: invalid value encountered in divide
new_unnormalized_variance -= correction**2 / new_sample_count
250/250 [==============================] - 2s 4ms/step - loss: nan - accuracy:
0.9545
Epoch 2/10
250/250 [==============================] - 1s 4ms/step - loss: nan - accuracy:
0.9545
Epoch 3/10
250/250 [==============================] - 1s 3ms/step - loss: nan - accuracy:
0.9545
Epoch 4/10
250/250 [==============================] - 1s 3ms/step - loss: nan - accuracy:
0.9545
Epoch 5/10
250/250 [==============================] - 1s 3ms/step - loss: nan - accuracy:
0.9545
Epoch 6/10
250/250 [==============================] - 1s 3ms/step - loss: nan - accuracy:
0.9545
Epoch 7/10
250/250 [==============================] - 1s 3ms/step - loss: nan - accuracy:
0.9545
Epoch 8/10
250/250 [==============================] - 2s 7ms/step - loss: nan - accuracy:
0.9545
Epoch 9/10
250/250 [==============================] - 1s 3ms/step - loss: nan - accuracy:
0.9545
Epoch 10/10
250/250 [==============================] - 1s 3ms/step - loss: nan - accuracy:
0.9545
63/63 [==============================] - 0s 2ms/step - loss: nan - accuracy:
0.9480
Accuracy on the testing set: 0.9480000138282776
63/63 [==============================] - 0s 2ms/step
[[1896 0]
[ 104 0]]
precision recall f1-score support

0 0.95 1.00 0.97 1896


1 0.00 0.00 0.00 104

accuracy 0.95 2000


macro avg 0.47 0.50 0.49 2000
weighted avg 0.90 0.95 0.92 2000

/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344
: UndefinedMetricWarning: Precision and F-score are ill-defined and being set
to 0.0 in labels with no predicted samples. Use `zero_division` parameter to
control this behavior.
_warn_prf(average, modifier, msg_start, len(result))
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344
: UndefinedMetricWarning: Precision and F-score are ill-defined and being set
to 0.0 in labels with no predicted samples. Use `zero_division` parameter to
control this behavior.
_warn_prf(average, modifier, msg_start, len(result))
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344
: UndefinedMetricWarning: Precision and F-score are ill-defined and being set
to 0.0 in labels with no predicted samples. Use `zero_division` parameter to
control this behavior.
_warn_prf(average, modifier, msg_start, len(result))

RESULT
Thus the fraudulent activities in banking and financial systems has been detected using neural
networks and the output has been verified successfully.
Exercise No: 50
STOCK PRICE PREDICTION
Date:

AIM
To predict stock prices using a neural network.

PROCEDURE
Step 1: Import necessary libraries
Step 2: Generate stock_prices.csv.
Step 3: Load the dataset.
Step 4: Selecting only the 'Close' prices for prediction.
Step 5: Normalize the data
Step 6: Function to create dataset for time series prediction
Step 7: Number of time steps to look back
Step 8: Create dataset
Step 9: Splitting data into training and testing sets
Step 10: Reshape input data to be 3D (samples, time steps, features)
Step 11: Define the LSTM model
Step 12: Compile the model
Step 13: Train the model
Step 14: Make predictions
Step 15: Plotting

SOURCE CODE

import numpy as np
import pandas as pd

# Generate simulated stock prices


num_days = 1000
initial_price = 100
daily_returns = np.random.normal(0.001, 0.02, num_days) # Mean daily return of 0.1% with a
standard deviation of 2%
prices = np.zeros(num_days)
prices[0] = initial_price

for i in range(1, num_days):


prices[i] = prices[i-1] * (1 + daily_returns[i])

# Create DataFrame
dates = pd.date_range(start='2020-01-01', periods=num_days, freq='B') # Business days
data = pd.DataFrame({
'Date': dates,
'Close': prices
})

# Save DataFrame to CSV


data.to_csv('stock_prices.csv', index=False)

print("CSV file 'stock_prices.csv' generated successfully.")

import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
import matplotlib.pyplot as plt

# Load stock price data (assuming it's stored in a CSV file)


data = pd.read_csv('/content/stock_prices.csv')

# Selecting only the 'Close' prices for prediction


prices = data['Close'].values.reshape(-1, 1)

# Normalize the data


scaler = MinMaxScaler(feature_range=(0, 1))
prices_scaled = scaler.fit_transform(prices)

# Function to create dataset for time series prediction


def create_dataset(data, time_steps):
X, y = [], []
for i in range(len(data) - time_steps):
X.append(data[i:(i + time_steps), 0])
y.append(data[i + time_steps, 0])
return np.array(X), np.array(y)

# Number of time steps to look back


time_steps = 60

# Create dataset
X, y = create_dataset(prices_scaled, time_steps)
# Splitting data into training and testing sets
split = int(0.8 * len(data))
X_train, X_test, y_train, y_test = X[:split], X[split:], y[:split], y[split:]

# Reshape input data to be 3D (samples, time steps, features)


X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

# Define the LSTM model


model = Sequential()
model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
model.add(LSTM(units=50, return_sequences=False))
model.add(Dense(units=1))

# Compile the model


model.compile(optimizer='adam', loss='mean_squared_error')

# Train the model


model.fit(X_train, y_train, epochs=10, batch_size=32)

# Make predictions
predictions = model.predict(X_test)
predictions = scaler.inverse_transform(predictions)

# Plotting
plt.plot(y_test, color='blue', label='Actual Stock Price')
plt.plot(predictions, color='red', label='Predicted Stock Price')
plt.title('Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.show()

OUTPUT

Epoch 1/10
25/25 [==============================] - 11s 88ms/step - loss: 0.0506
Epoch 2/10
25/25 [==============================] - 2s 80ms/step - loss: 0.0060
Epoch 3/10
25/25 [==============================] - 2s 87ms/step - loss: 0.0036
Epoch 4/10
25/25 [==============================] - 3s 108ms/step - loss: 0.0032
Epoch 5/10
25/25 [==============================] - 2s 70ms/step - loss: 0.0030
Epoch 6/10
25/25 [==============================] - 1s 44ms/step - loss: 0.0027
Epoch 7/10
25/25 [==============================] - 1s 43ms/step - loss: 0.0025
Epoch 8/10
25/25 [==============================] - 1s 43ms/step - loss: 0.0024
Epoch 9/10
25/25 [==============================] - 1s 43ms/step - loss: 0.0023
Epoch 10/10
25/25 [==============================] - 1s 43ms/step - loss: 0.0022
5/5 [==============================] - 1s 16ms/step

RESULT
Thus the stock prices has been predicted and the output has been verified successfully.
Exercise No: 51
PORTFOLIO MANAGEMENT
Date:

AIM
To manage a portfolio using a neural network.

PROCEDURE
Step 1: Import necessary libraries.
Step 2: Generate asset_prices.csv.
Step 3: Load the dataset.
Step 4: Load historical asset price data (assuming it's stored in a CSV file)
Step 5: Assuming the first column is the target asset and the rest are predictors
Step 6: Split the data into training and testing sets
Step 7: Standardize features
Step 8: Define the neural network model
Step 9: Compile the model
Step 10: Train the model
Step 11: Evaluate the model
Step 12: Make predictions

SOURCE CODE

import numpy as np
import pandas as pd

# Generate simulated asset price data


num_days = 1000
num_assets = 5
initial_prices = np.random.uniform(10, 100, num_assets)
returns = np.random.normal(0.0001, 0.02, (num_days, num_assets)) # Mean daily return of 0.01%
with a standard deviation of 2%
prices = np.zeros((num_days, num_assets))
prices[0, :] = initial_prices

for i in range(1, num_days):


prices[i, :] = prices[i-1, :] * (1 + returns[i, :])
# Create DataFrame
dates = pd.date_range(start='2020-01-01', periods=num_days, freq='B') # Business days
data = pd.DataFrame(prices, columns=[f'Asset_{i}' for i in range(1, num_assets + 1)], index=dates)

# Save DataFrame to CSV


data.to_csv('asset_prices.csv')

print("CSV file 'asset_prices.csv' generated successfully.")


import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential
from keras.layers import Dense

# Load historical asset price data (assuming it's stored in a CSV file)
data = pd.read_csv('asset_prices.csv')

# Assuming the first column is the target asset and the rest are predictors
X = data.drop(data.columns[0], axis=1).values
y = data[data.columns[0]].values

# 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)

# Standardize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

# Define the neural network model


model = Sequential()
model.add(Dense(16, input_dim=X_train_scaled.shape[1], activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='linear'))

# Compile the model


model.compile(loss='mean_squared_error', optimizer='adam')

# Train the model


model.fit(X_train_scaled, y_train, epochs=10, batch_size=32, validation_split=0.2)

# Evaluate the model


loss = model.evaluate(X_test_scaled, y_test)
print(f"Mean Squared Error on Test Set: {loss}")

# Make predictions
predictions = model.predict(X_test_scaled)

OUTPUT

Portfolio with Maximum Sharpe Ratio:


Expected Return: 0.01726007923679376
Volatility: 0.09398049546364397
Allocation: [0.01908635 0.0492331 0.93168054]

RESULT
Thus the portfolio has been managed using a neural network and the output has been verified
successfully.

You might also like