0% found this document useful (0 votes)
20 views29 pages

CCS355

The document outlines four exercises involving TensorFlow, including simple vector addition, image classification using CNN, sentiment analysis using RNN, and transfer learning with MobileNetV2. Each exercise includes an aim, algorithm, and program code, demonstrating practical implementations in Python. The results confirm the successful execution and verification of each task.

Uploaded by

Pandi Selvi
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)
20 views29 pages

CCS355

The document outlines four exercises involving TensorFlow, including simple vector addition, image classification using CNN, sentiment analysis using RNN, and transfer learning with MobileNetV2. Each exercise includes an aim, algorithm, and program code, demonstrating practical implementations in Python. The results confirm the successful execution and verification of each task.

Uploaded by

Pandi Selvi
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/ 29

Ex No: 1

IMPLEMENT SIMPLE VECTOR ADDTION IN


Date: TENSORFLOW

AIM:
To write a python code to implement simple vector addition in Tensor Flow.

ALGORITHM:
Arguments

 a: This parameter should be a Tensor and also from the one of the following types:
bfloat16, half, float32, float64, int8, int8, int16, int32, int64, complex64, complex128,
string
 b: This should also be a Tensor and must have the same type as a.
 name: This is optional parameter and this is the name of the operation.

Return: It returns a Tensor having the same shape as of input a.

PROGRAM:

CCS355 NNADL B.PANDISELVI AP/CSE


CCS355 NNADL B.PANDISELVI AP/CSE
CCS355 NNADL B.PANDISELVI AP/CSE
RESULT:

Thus the implementation of simple vector addition in Tensor Flow has been executed
and verified.

CCS355 NNADL B.PANDISELVI AP/CSE


Ex No: 2
Image Classifier using CNN in Tensor Flow/Keras
Date:

AIM:

To Write a python program to implement an Image Classifier using CNN in TensorFlow/Keras

ALGORITHM:
1. The first step towards writing any code is to import all the required libraries and modules.
2. we can see we have 5000 training images and 1000 test images as specified above and all
the images are of 32 by 32 size and have 3 color channels i.e. images are color images.
3. Now is a good time to see few images of our dataset.
4. After completing all the steps now is the time to built our model. We are going to use a
Convolution Neural Network or CNN to train our model.
5. After this, our model is trained.

PROGRAM:
import tensorflow as tf
# Display the version
print(tf.__version__)
# other imports
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Input, Conv2D, Dense, Flatten, Dropout
from tensorflow.keras.layers import GlobalMaxPooling2D, MaxPooling2D
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.models import Model

# Load in the data


cifar10 = tf.keras.datasets.cifar10

# Distribute it to train and test set


(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)

# Reduce pixel values

x_train, x_test = x_train / 255.0, x_test / 255.0

# flatten the label values

y_train, y_test = y_train.flatten(), y_test.flatten()

# visualize data by plotting images

CCS355 NNADL B.PANDISELVI AP/CSE


fig, ax = plt.subplots(5, 5)
k=0

for i in range(5):
for j in range(5):
ax[i][j].imshow(x_train[k], aspect='auto')
k += 1

plt.show()
# number of classes
K = len(set(y_train))

# calculate total number of classes


# for output layer
print("number of classes:", K)

# Build the model using the functional API


# input layer
i = Input(shape=x_train[0].shape)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(i)
x = BatchNormalization()(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2))(x)

x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)


x = BatchNormalization()(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2))(x)

x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)


x = BatchNormalization()(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2))(x)

x = Flatten()(x)
x = Dropout(0.2)(x)

# Hidden layer
x = Dense(1024, activation='relu')(x)
x = Dropout(0.2)(x)

# last hidden layer i.e.. output layer


x = Dense(K, activation='softmax')(x)

CCS355 NNADL B.PANDISELVI AP/CSE


model = Model(i, x)

# model description
model.summary()
# Compile
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Fit
r = model.fit(
x_train, y_train, validation_data=(x_test, y_test), epochs=50)
# Fit with data augmentation
# Note: if you run this AFTER calling
# the previous model.fit()
# it will CONTINUE training where it left off
batch_size = 32
data_generator = tf.keras.preprocessing.image.ImageDataGenerator(
width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True)

train_generator = data_generator.flow(x_train, y_train, batch_size)


steps_per_epoch = x_train.shape[0] // batch_size

r = model.fit(train_generator, validation_data=(x_test, y_test),


steps_per_epoch=steps_per_epoch, epochs=50)
# Plot accuracy per iteration
plt.plot(r.history['accuracy'], label='acc', color='red')
plt.plot(r.history['val_accuracy'], label='val_acc', color='green')
plt.legend()
# label mapping

labels = '''airplane automobile bird cat deerdog frog horseship truck'''.split()

# select the image from our test dataset


image_number = 0

# display the image


plt.imshow(x_test[image_number])

# load the image in an array


n = np.array(x_test[image_number])

# reshape it
p = n.reshape(1, 32, 32, 3)

# pass in the network for prediction and


# save the predicted label

CCS355 NNADL B.PANDISELVI AP/CSE


predicted_label = labels[model.predict(p).argmax()]

# load the original label


original_label = labels[y_test[image_number]]

# display the result


print("Original label is {} and predicted label is {}".format(
original_label, predicted_label))

OUTPUT:

CCS355 NNADL B.PANDISELVI AP/CSE


RESULT:

Thus the implementation of an Image Classifier using CNN in TensorFlow/Keras has


been executed and verified.

CCS355 NNADL B.PANDISELVI AP/CSE


Ex No: 3
Perform Sentiment Analysis using RNN
Date:

AIM:

To Write a python program to Perform Sentiment Analysis using RNN.

ALOGORITHM:

1. Conduct Sentiment Analysis to understand text classification using Tensorflow!

2. Using Keras IMDB dataset. vocabulary size is a parameter that is used the get data
containing the given number of most occurring words in the entire corpus of textual data.

3. Check the range of the reviews we have in this dataset.

4. The longest review available is 2697 words and the shortest one is 70. While working with
Neural Networks, it is important to make all the inputs in a fixed size.

5. The most basic form of Recurrent Neural Networks that tries to memorize sequential
information.

PROGRAM:

from tensorflow.keras.layers import SimpleRNN, LSTM, GRU, Bidirectional, Dense,


Embedding
from tensorflow.keras.datasets import imdb
from tensorflow.keras.models import Sequential
import numpy as np
# Getting reviews with words that come under 5000
# most occurring words in the entire
# corpus of textual review data
vocab_size = 5000
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)

print(x_train[0])
# Getting all the words from word_index dictionary
word_idx = imdb.get_word_index()

# Originally the index number of a value and not a key,


# hence converting the index as key and the words as values
word_idx = {i: word for word, i in word_idx.items()}

# again printing the review


print([word_idx[i] for i in x_train[0]])

CCS355 NNADL B.PANDISELVI AP/CSE


# Get the minimum and the maximum length of reviews
print("Max length of a review:: ", len(max((x_train+x_test), key=len)))
print("Min length of a review:: ", len(min((x_train+x_test), key=len)))
from tensorflow.keras.preprocessing import sequence

# Keeping a fixed length of all reviews to max 400 words


max_words = 400

x_train = sequence.pad_sequences(x_train, maxlen=max_words)


x_test = sequence.pad_sequences(x_test, maxlen=max_words)

x_valid, y_valid = x_train[:64], y_train[:64]


x_train_, y_train_ = x_train[64:], y_train[64:]
# fixing every word's embedding size to be 32
embd_len = 32

# Creating a RNN model


RNN_model = Sequential(name="Simple_RNN")
RNN_model.add(Embedding(vocab_size,
embd_len,
input_length=max_words))

# In case of a stacked(more than one layer of RNN)


# use return_sequences=True
RNN_model.add(SimpleRNN(128,
activation='tanh',
return_sequences=False))
RNN_model.add(Dense(1, activation='sigmoid'))

# printing model summary


print(RNN_model.summary())

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

# Training the model


history = RNN_model.fit(x_train_, y_train_,
batch_size=64,
epochs=5,
verbose=1,
validation_data=(x_valid, y_valid))

CCS355 NNADL B.PANDISELVI AP/CSE


# Printing model score on test data
print()
print("Simple_RNN Score---> ", RNN_model.evaluate(x_test, y_test, verbose=0))
# Defining GRU model
gru_model = Sequential(name="GRU_Model")
gru_model.add(Embedding(vocab_size,
embd_len,
input_length=max_words))
gru_model.add(GRU(128,
activation='tanh',
return_sequences=False))
gru_model.add(Dense(1, activation='sigmoid'))

# Printing the Summary


print(gru_model.summary())

# Compiling the model


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

# Training the GRU model


history2 = gru_model.fit(x_train_, y_train_,
batch_size=64,
epochs=5,
verbose=1,
validation_data=(x_valid, y_valid))

# Printing model score on test data


print()
print("GRU model Score---> ", gru_model.evaluate(x_test, y_test, verbose=0))
# Defining LSTM model
lstm_model = Sequential(name="LSTM_Model")
lstm_model.add(Embedding(vocab_size,
embd_len,
input_length=max_words))
lstm_model.add(LSTM(128,
activation='relu',
return_sequences=False))
lstm_model.add(Dense(1, activation='sigmoid'))

# Printing Model Summary


print(lstm_model.summary())

CCS355 NNADL B.PANDISELVI AP/CSE


# Compiling the model
lstm_model.compile(
loss="binary_crossentropy",
optimizer='adam',
metrics=['accuracy']
)

# Training the model


history3 = lstm_model.fit(x_train_, y_train_,
batch_size=64,
epochs=5,
verbose=2,
validation_data=(x_valid, y_valid))

# Displaying the model accuracy on test data


print()
print("LSTM model Score---> ", lstm_model.evaluate(x_test, y_test, verbose=0))
# Defining Bidirectional LSTM model
bi_lstm_model = Sequential(name="Bidirectional_LSTM")
bi_lstm_model.add(Embedding(vocab_size,
embd_len,
input_length=max_words))
bi_lstm_model.add(Bidirectional(LSTM(128,
activation='tanh',
return_sequences=False)))
bi_lstm_model.add(Dense(1, activation='sigmoid'))

# Printing model summary


print(bi_lstm_model.summary())

# Compiling model summary


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

# Training the model


history4 = bi_lstm_model.fit(x_train_, y_train_,
batch_size=64,
epochs=5,
verbose=2,
validation_data=(x_test, y_test))

# Printing model score on test data


print()

CCS355 NNADL B.PANDISELVI AP/CSE


print("Bidirectional LSTM model Score---> ",
bi_lstm_model.evaluate(x_test, y_test, verbose=0))

OUTPUT:

['the', 'as', 'you', 'with', 'out', 'themselves', 'powerful', 'lets', 'loves', 'their', 'becomes', 'reaching',
'had', 'journalist', 'of', 'lot', 'from', 'anyone', 'to', 'have', 'after', 'out', 'atmosphere', 'never', 'more',
'room', 'and', 'it', 'so', 'heart', 'shows', 'to', 'years', 'of', 'every', 'never', 'going', 'and', 'help',
'moments', 'or', 'of', 'every', 'chest', 'visual', 'movie', 'except', 'her', 'was', 'several', 'of', 'enough',
'more', 'with', 'is', 'now', 'current', 'film', 'as', 'you', 'of', 'mine', 'potentially', 'unfortunately', 'of',
'you', 'than', 'him', 'that', 'with', 'out', 'themselves', 'her', 'get', 'for', 'was', 'camp', 'of', 'you',
'movie', 'sometimes', 'movie', 'that', 'with', 'scary', 'but', 'and', 'to', 'story', 'wonderful', 'that', 'in',
'seeing', 'in', 'character', 'to', 'of', '70s', 'and', 'with', 'heart', 'had', 'shadows', 'they', 'of', 'here',
'that', 'with', 'her', 'serious', 'to', 'have', 'does', 'when', 'from', 'why', 'what', 'have', 'critics', 'they',
'is', 'you', 'that', "isn't", 'one', 'will', 'very', 'to', 'as', 'itself', 'with', 'other', 'and', 'in', 'of', 'seen',
'over', 'and', 'for', 'anyone', 'of', 'and', 'br', "show's", 'to', 'whether', 'from', 'than', 'out',
'themselves', 'history', 'he', 'name', 'half', 'some', 'br', 'of', 'and', 'odd', 'was', 'two', 'most', 'of',
'mean', 'for', '1', 'any', 'an', 'boat', 'she', 'he', 'should', 'is', 'thought', 'and', 'but', 'of', 'script', 'you',
'not', 'while', 'history', 'he', 'heart', 'to', 'real', 'at', 'and', 'but', 'when', 'from', 'one', 'bit', 'then',
'have', 'two', 'of', 'script', 'their', 'with', 'her', 'nobody', 'most', 'that', 'with', "wasn't", 'to', 'with',
'armed', 'acting', 'watch', 'an', 'for', 'with', 'and', 'film', 'want', 'an']
Max length of a review:: 2697

Min length of a review:: 70

RESULT:

Thus the program to Perform Sentiment Analysis using RNN has been executed and
verified.

CCS355 NNADL B.PANDISELVI AP/CSE


Ex No: 4 TRANSFER LEARNING USING A
PRE-TRAINED MODEL (MOBILENETV2) FOR
Date: IMAGE CLASSIFICATION

AIM:

To Write a python program to transfer learning using a pre-trained model (MobileNetV2) for
image classification.

ALGORITHM:
1. Take layers from a previously trained model.
2. Freeze them, so as to avoid destroying any of the information they contain during future
training rounds.
3. Add some new, trainable layers on top of the frozen layers. They will learn to turn the old
features into predictions on a new dataset.
4. Train the new layers on your dataset.

PROGRAM:

layer = keras.layers.Dense(3)
layer.build((None, 4)) # Create the weights

print("weights:", len(layer.weights))
print("trainable_weights:", len(layer.trainable_weights))
print("non_trainable_weights:", len(layer.non_trainable_weights))
layer = keras.layers.BatchNormalization()
layer.build((None, 4)) # Create the weights

print("weights:", len(layer.weights))
print("trainable_weights:", len(layer.trainable_weights))
print("non_trainable_weights:", len(layer.non_trainable_weights))
layer = keras.layers.Dense(3)
layer.build((None, 4)) # Create the weights
layer.trainable = False # Freeze the layer

print("weights:", len(layer.weights))
print("trainable_weights:", len(layer.trainable_weights))
print("non_trainable_weights:", len(layer.non_trainable_weights))
# Make a model with 2 layers
layer1 = keras.layers.Dense(3, activation="relu")
layer2 = keras.layers.Dense(3, activation="sigmoid")
model = keras.Sequential([keras.Input(shape=(3,)), layer1, layer2])

# Freeze the first layer

CCS355 NNADL B.PANDISELVI AP/CSE


layer1.trainable = False

# Keep a copy of the weights of layer1 for later reference


initial_layer1_weights_values = layer1.get_weights()

# Train the model


model.compile(optimizer="adam", loss="mse")
model.fit(np.random.random((2, 3)), np.random.random((2, 3)))

# Check that the weights of layer1 have not changed during training
final_layer1_weights_values = layer1.get_weights()
np.testing.assert_allclose(
initial_layer1_weights_values[0], final_layer1_weights_values[0]
)
np.testing.assert_allclose(
initial_layer1_weights_values[1], final_layer1_weights_values[1]
)
inner_model = keras.Sequential(
[
keras.Input(shape=(3,)),
keras.layers.Dense(3, activation="relu"),
keras.layers.Dense(3, activation="relu"),
]
)

model = keras.Sequential(
[
keras.Input(shape=(3,)),
inner_model,
keras.layers.Dense(3, activation="sigmoid"),
]
)

model.trainable = False # Freeze the outer model

assert inner_model.trainable == False # All layers in `model` are now frozen


assert inner_model.layers[0].trainable == False # `trainable` is propagated recursively

base_model = keras.applications.Xception(
weights='imagenet', # Load weights pre-trained on ImageNet.
input_shape=(150, 150, 3),
include_top=False) # Do not include the ImageNet classifier at the top.
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 10))
for i, (image, label) in enumerate(train_ds.take(9)):

CCS355 NNADL B.PANDISELVI AP/CSE


ax = plt.subplot(3, 3, i + 1)
plt.imshow(image)
plt.title(int(label))
plt.axis("off")

OUTPUT:
weights: 2
trainable_weights: 2
non_trainable_weights: 0
weights: 4
trainable_weights: 2
non_trainable_weights: 2
weights: 2
trainable_weights: 0
non_trainable_weights: 2
1/1 [==============================] - 0s 321ms/step - loss: 0.0517
Downloading and preparing dataset 786.68 MiB (download: 786.68 MiB, generated: Unknown
size, total: 786.68 MiB) to
/usr/local/google/home/nkovela/tensorflow_datasets/cats_vs_dogs/4.0.0...

WARNING:absl:1738 images were corrupted and were skipped

Dataset cats_vs_dogs downloaded and prepared to


/usr/local/google/home/nkovela/tensorflow_datasets/cats_vs_dogs/4.0.0. Subsequent calls will
reuse this data.
Number of training samples: 9305
Number of validation samples: 2326
Number of test samples: 2326

RESULT:
Thus the transfer learning using a pre-trained model (MobileNetV2) for image
classification has been executed and verified.

CCS355 NNADL B.PANDISELVI AP/CSE


Ex No: 5 Implement an LSTM based Auto encoder in Tensor
Date: Flow/Keras.

AIM:
To Write a python program to Implement an LSTM based Auto encoder in Tensor Flow/Keras.

ALGORITHM:
1. Accept an input set of data (i.e., the input).
2. Internally compress the input data into a latent-space representation (i.e., a single vector
that compresses and quantifies the input).
3. Reconstruct the input data from this latent representation (i.e., the output).
Typically, we think of an auto encoder having two components/sub networks:
1. Encoder: Accepts the input data and compresses it into the latent-space. If we denote our input
data as and the encoder as , then the output latent-space representation, , would
be .
2. Decoder: The decoder is responsible for accepting the latent-space representation and then
reconstructing the original input. If we denote the decoder function as and the output of the
detector as , then we can represent the decoder as .

PROGRAM:
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.layers import Embedding, Dense, LSTM
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.sequence import pad_sequences

# Model configuration
additional_metrics = ['accuracy']
batch_size = 128
embedding_output_dims = 15
loss_function = BinaryCrossentropy()
max_sequence_length = 300
num_distinct_words = 5000
number_of_epochs = 5
optimizer = Adam()
validation_split = 0.20
verbosity_mode = 1

# Disable eager execution


tf.compat.v1.disable_eager_execution()

# Load dataset

CCS355 NNADL B.PANDISELVI AP/CSE


(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=num_distinct_words)
print(x_train.shape)
print(x_test.shape)

# Pad all sequences


padded_inputs = pad_sequences(x_train, maxlen=max_sequence_length, value = 0.0) # 0.0
because it corresponds with <PAD>
padded_inputs_test = pad_sequences(x_test, maxlen=max_sequence_length, value = 0.0) # 0.0
because it corresponds with <PAD>

# Define the Keras model


model = Sequential()
model.add(Embedding(num_distinct_words, embedding_output_dims,
input_length=max_sequence_length))
model.add(LSTM(10))
model.add(Dense(1, activation='sigmoid'))

# Compile the model


model.compile(optimizer=optimizer, loss=loss_function, metrics=additional_metrics)

# Give a summary
model.summary()

# Train the model


history = model.fit(padded_inputs, y_train, batch_size=batch_size, epochs=number_of_epochs,
verbose=verbosity_mode, validation_split=validation_split)

# Test the model after training


test_results = model.evaluate(padded_inputs_test, y_test, verbose=False)
print(f'Test results - Loss: {test_results[0]} - Accuracy: {100*test_results[1]}%')
Running the model
Time to run the model! Open up a terminal where at least TensorFlow and Python have been
installed, and run the model - python lstm.py.
You should see that the model starts training after e.g. a few seconds. If you have the IMDB
dataset not downloaded to your machine, it will be downloaded first.
Eventually, you'll approximately see an 87.1% accuracy on the evaluation set:

CCS355 NNADL B.PANDISELVI AP/CSE


OUTPUT:

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, 300, 15) 75000
_________________________________________________________________
lstm (LSTM) (None, 10) 1040
_________________________________________________________________
dense (Dense) (None, 1) 11
=================================================================
Total params: 76,051
Trainable params: 76,051
Non-trainable params: 0
_________________________________________________________________
2021-01-08 14:53:19.988309: I
tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR
optimization passes are enabled (registered 2)
Epoch 1/5
157/157 [==============================] - 19s 106ms/step - loss: 0.6730 - accuracy:
0.5799 - val_loss: 0.4866 - val_accuracy: 0.8174
Epoch 2/5
157/157 [==============================] - 13s 83ms/step - loss: 0.4312 - accuracy:
0.8445 - val_loss: 0.3694 - val_accuracy: 0.8540
Epoch 3/5
157/157 [==============================] - 14s 86ms/step - loss: 0.2997 - accuracy:
0.8955 - val_loss: 0.3333 - val_accuracy: 0.8680
Epoch 4/5
157/157 [==============================] - 15s 96ms/step - loss: 0.2499 - accuracy:
0.9133 - val_loss: 0.3078 - val_accuracy: 0.8782
Epoch 5/5
157/157 [==============================] - 14s 90ms/step - loss: 0.2032 - accuracy:
0.9316 - val_loss: 0.3152 - val_accuracy: 0.8806
Test results - Loss: 0.3316078186035156 - Accuracy: 87.09200024604797%

RESULT:
Thus the python program to Implement an LSTM based Auto encoder in Tensor
Flow/Keras has been executed and verified.

CCS355 NNADL B.PANDISELVI AP/CSE


Ex No: 6 Generating images using Generative Adversarial
Date: Networks

AIM:
To Write a python program to Generating images using Generative Adversarial Networks
ALGORITHM:
Generative Adversarial Networks (GANs) can be broken down into three parts:
 Generative: To learn a generative model, which describes how data is generated in terms
of a probabilistic model.
 Adversarial: The word adversarial refers to setting one thing up against another. This
means that, in the context of GANs, the generative result is compared with the actual
images in the data set. A mechanism known as a discriminator is used to apply a model
that attempts to distinguish between real and fake images.
 Networks: Use deep neural networks as artificial intelligence (AI) algorithms for training
purposes.

PROGRAM:
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import numpy as np

# Set device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Define a basic transform
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
train_dataset = datasets.CIFAR10(root='./data',\
train=True, download=True, transform=transform)
dataloader = torch.utils.data.DataLoader(train_dataset, \
batch_size=32, shuffle=True)
# Hyperparameters
latent_dim = 100
lr = 0.0002
beta1 = 0.5
beta2 = 0.999
num_epochs = 10
# Define the generator
class Generator(nn.Module):
def __init__(self, latent_dim):

CCS355 NNADL B.PANDISELVI AP/CSE


super(Generator, self).__init__()

self.model = nn.Sequential(
nn.Linear(latent_dim, 128 * 8 * 8),
nn.ReLU(),
nn.Unflatten(1, (128, 8, 8)),
nn.Upsample(scale_factor=2),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128, momentum=0.78),
nn.ReLU(),
nn.Upsample(scale_factor=2),
nn.Conv2d(128, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64, momentum=0.78),
nn.ReLU(),
nn.Conv2d(64, 3, kernel_size=3, padding=1),
nn.Tanh()
)

def forward(self, z):


img = self.model(z)
return img
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()

self.model = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=3, stride=2, padding=1),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1),
nn.ZeroPad2d((0, 1, 0, 1)),
nn.BatchNorm2d(64, momentum=0.82),
nn.LeakyReLU(0.25),
nn.Dropout(0.25),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(128, momentum=0.82),
nn.LeakyReLU(0.2),
nn.Dropout(0.25),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(256, momentum=0.8),
nn.LeakyReLU(0.25),
nn.Dropout(0.25),
nn.Flatten(),
nn.Linear(256 * 5 * 5, 1),
nn.Sigmoid()
)

CCS355 NNADL B.PANDISELVI AP/CSE


def forward(self, img):
validity = self.model(img)
return validity
# Define the generator and discriminator
# Initialize generator and discriminator
generator = Generator(latent_dim).to(device)
discriminator = Discriminator().to(device)

# Loss function
adversarial_loss = nn.BCELoss()

# Optimizers
optimizer_G = optim.Adam(generator.parameters()\
, lr=lr, betas=(beta1, beta2))
optimizer_D = optim.Adam(discriminator.parameters()\
, lr=lr, betas=(beta1, beta2))
# Training loop
for epoch in range(num_epochs):
for i, batch in enumerate(dataloader):
# Convert list to tensor
real_images = batch[0].to(device)

# Adversarial ground truths


valid = torch.ones(real_images.size(0), 1, device=device)
fake = torch.zeros(real_images.size(0), 1, device=device)

# Configure input
real_images = real_images.to(device)

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

optimizer_D.zero_grad()

# Sample noise as generator input


z = torch.randn(real_images.size(0), latent_dim, device=device)

# Generate a batch of images


fake_images = generator(z)

# Measure discriminator's ability


# to classify real and fake images
real_loss = adversarial_loss(discriminator\
(real_images), valid)

CCS355 NNADL B.PANDISELVI AP/CSE


fake_loss = adversarial_loss(discriminator\
(fake_images.detach()), fake)
d_loss = (real_loss + fake_loss) / 2

# Backward pass and optimize


d_loss.backward()
optimizer_D.step()

# -----------------
# Train Generator
# -----------------

optimizer_G.zero_grad()

# Generate a batch of images


gen_images = generator(z)

# Adversarial loss
g_loss = adversarial_loss(discriminator(gen_images), valid)

# Backward pass and optimize


g_loss.backward()
optimizer_G.step()

# ---------------------
# Progress Monitoring
# ---------------------

if (i + 1) % 100 == 0:
print(
f"Epoch [{epoch+1}/{num_epochs}]\
Batch {i+1}/{len(dataloader)} "
f"Discriminator Loss: {d_loss.item():.4f} "
f"Generator Loss: {g_loss.item():.4f}"
)

# Save generated images for every epoch


if (epoch + 1) % 10 == 0:
with torch.no_grad():
z = torch.randn(16, latent_dim, device=device)
generated = generator(z).detach().cpu()
grid = torchvision.utils.make_grid(generated,\
nrow=4, normalize=True)
plt.imshow(np.transpose(grid, (1, 2, 0)))
plt.axis("off")
plt.show()

CCS355 NNADL B.PANDISELVI AP/CSE


OUTPUT:

Epoch [10/10] Batch 1300/1563 Discriminator


Loss: 0.4473 Generator Loss: 0.9555
Epoch [10/10] Batch 1400/1563 Discriminator
Loss: 0.6643 Generator Loss: 1.0215
Epoch [10/10] Batch 1500/1563 Discriminator
Loss: 0.4720 Generator Loss: 2.5027

RESULT:
Thus the python program to Generating images using Generative Adversarial Networks
has been executed and verified.

CCS355 NNADL B.PANDISELVI AP/CSE


Ex No: 7 Write a python program to and implement a Feed-
Date: Forward Network in TensorFlow/Keras

AIM:
To implement a Feed-Forward Network in TensorFlow/Keras

ALGORITHM:
 To demonstrate how you can implement simple neural networks using the Keras library.
 Obtain a baseline using standard neural networks which we will later compare to
Convolutional Neural Networks (noting that CNNS will dramatically outperform our
previous methods).

PROGRAM:
# import the necessary packages
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import classification_report
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.datasets import mnist
from tensorflow.keras import backend as K
import matplotlib.pyplot as plt
import numpy as np
import argparse
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", required=True,
help="path to the output loss/accuracy plot")
args = vars(ap.parse_args())
# grab the MNIST dataset (if this is your first time using this
# dataset then the 11MB download may take a minute)
print("[INFO] accessing MNIST...")
((trainX, trainY), (testX, testY)) = mnist.load_data()
# each image in the MNIST dataset is represented as a 28x28x1
# image, but in order to apply a standard neural network we must
# first "flatten" the image to be simple list of 28x28=784 pixels
trainX = trainX.reshape((trainX.shape[0], 28 * 28 * 1))
testX = testX.reshape((testX.shape[0], 28 * 28 * 1))
# scale data to the range of [0, 1]
trainX = trainX.astype("float32") / 255.0
testX = testX.astype("float32") / 255.0
# convert the labels from integers to vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

CCS355 NNADL B.PANDISELVI AP/CSE


# define the 784-256-128-10 architecture using Keras
model = Sequential()
model.add(Dense(256, input_shape=(784,), activation="sigmoid"))
model.add(Dense(128, activation="sigmoid"))
model.add(Dense(10, activation="softmax"))
# train the model using SGD
print("[INFO] training network...")
sgd = SGD(0.01)
model.compile(loss="categorical_crossentropy", optimizer=sgd,
metrics=["accuracy"])
H = model.fit(trainX, trainY, validation_data=(testX, testY),
epochs=100, batch_size=128)
# evaluate the network
print("[INFO] evaluating network...")
predictions = model.predict(testX, batch_size=128)
print(classification_report(testY.argmax(axis=1),
predictions.argmax(axis=1),
target_names=[str(x) for x in lb.classes_]))
# plot the training loss and accuracy
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, 100), H.history["accuracy"], label="train_acc")
plt.plot(np.arange(0, 100), H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.savefig(args["output"])
Implementing feedforward neural networks with Keras and TensorFlow
$ python keras_mnist.py --output output/keras_mnist.png
[INFO] loading MNIST (full) dataset...
[INFO] training network...
Train on 52500 samples, validate on 17500 samples
Epoch 1/100
1s - loss: 2.2997 - acc: 0.1088 - val_loss: 2.2918 - val_acc: 0.1145
Epoch 2/100
1s - loss: 2.2866 - acc: 0.1133 - val_loss: 2.2796 - val_acc: 0.1233
Epoch 3/100
1s - loss: 2.2721 - acc: 0.1437 - val_loss: 2.2620 - val_acc: 0.1962
...
Epoch 98/100
1s - loss: 0.2811 - acc: 0.9199 - val_loss: 0.2857 - val_acc: 0.9153
Epoch 99/100
1s - loss: 0.2802 - acc: 0.9201 - val_loss: 0.2862 - val_acc: 0.9148

CCS355 NNADL B.PANDISELVI AP/CSE


Epoch 100/100
1s - loss: 0.2792 - acc: 0.9204 - val_loss: 0.2844 - val_acc: 0.9160
[INFO] evaluating network...
precision recall f1-score support
0.0 0.94 0.96 0.95 1726
1.0 0.95 0.97 0.96 2004
2.0 0.91 0.89 0.90 1747
3.0 0.91 0.88 0.89 1828
4.0 0.91 0.93 0.92 1686
5.0 0.89 0.86 0.88 1581
6.0 0.92 0.96 0.94 1700
7.0 0.92 0.94 0.93 1814
8.0 0.88 0.88 0.88 1679
9.0 0.90 0.88 0.89 1735
avg / total 0.92 0.92 0.92 17500

OUTPUT:

CCS355 NNADL B.PANDISELVI AP/CSE


RESULT:
Thus the implementation a Feed-Forward Network in TensorFlow/Keras has been
executed and verified.

CCS355 NNADL B.PANDISELVI AP/CSE

You might also like