0% found this document useful (0 votes)
21 views37 pages

Lab Manual: Smt. Indira Gandhicollege of Engineering Ghansoli

The document is a lab manual for the Advanced AI Lab (CSL801) at Smt. Indira Gandhi College of Engineering, detailing the vision, mission, program outcomes, lab objectives, and practical exercises. It outlines the use of advanced AI techniques such as Hidden Markov Models and Generative Multi-Layer Networks, along with their applications and implementation steps. The manual aims to equip students with essential skills in artificial intelligence and machine learning through hands-on experience and collaborative learning.

Uploaded by

ABISHA NADAR
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)
21 views37 pages

Lab Manual: Smt. Indira Gandhicollege of Engineering Ghansoli

The document is a lab manual for the Advanced AI Lab (CSL801) at Smt. Indira Gandhi College of Engineering, detailing the vision, mission, program outcomes, lab objectives, and practical exercises. It outlines the use of advanced AI techniques such as Hidden Markov Models and Generative Multi-Layer Networks, along with their applications and implementation steps. The manual aims to equip students with essential skills in artificial intelligence and machine learning through hands-on experience and collaborative learning.

Uploaded by

ABISHA NADAR
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/ 37

Sem VIII Advanced AI LabCSL801

Smt. Indira GandhiCollege of Engineering


Ghansoli – 400 701
Approved by AICTE ,Affiliated to University of Mumbai
Department of CSE (Artificial Intelligence and Machine Learning)

Lab Manual

Subject : Advanced AI Lab


Subject Code : CSL801
Scheme : R19 scheme ‘C’
Prepared by : Prof. Anjali devi.Patil

Vision
Smt. Indira Gandhi College of Engineering

CSE-AIML SIGCE 1|Page


Sem VIII Advanced AI LabCSL801

To serve and have a transformative impact on society by constantly endeavoring


for ethically employable, technical human resource.

MISSION
1. Serve and help transform society by graduating talented, broadly educated
engineers.
2. Encourage students for collaborative multidisciplinary activity.
3. Enable student to develop ethical professionals
4. To provide conductive environment for teaching and learning.

Department of CSE-Artificial Intelligence and Machine Learning

VISION

To provide employable CSE-AIML engineer for Society and Industry needs.

MISSION

M1: Empower students with strong basic foundation.


M2: Develop technical and non-technicalSkills for lifelong learning.
M3: To promote student’s interest in higher studies, research and
entrepreneurship to meet global challenges.

CSE-AIML SIGCE 2|Page


Sem VIII Advanced AI LabCSL801

Program Outcome

PO1 Engineering knowledge Apply the knowledge of mathematics, science, engineering


fundamentals and an engineering specialization to the
solution of complex engineering problems.

PO2 Problem analysis Identify, formulate, review research literature, and analyze
complex engineering problems reaching substantiated
conclusions using first principles of mathematics, natural
sciences, and engineering sciences.

PO3 Design/development of Design solutions for complex engineering problems and


solutions design system components or processes that meet the
specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and
environmental consideration.

PO4 Conduct investigations Use research-based knowledge and research methods


of complex problems including design of experiments, analysis and
interpretation of data, and synthesis of the information to
provide valid conclusions.

PO5 Modern tool usage Create, select, and apply appropriate techniques, resources,
and modern engineering and IT tools including prediction
and modeling to complex engineering activities with an
understanding of the limitations.

PO6 The engineer and Apply reasoning informed by the contextual knowledge to
society assess societal, health, safety, legal and cultural issues and
the consequent responsibilities relevant to the professional
engineering practice

PO7 Environment and Understand the impact of the professional engineering


sustainability solutions in societal and environmental contexts, and
demonstrate the knowledge of, and need for sustainable
development.

PO8 Ethics Apply ethical principles and commit to professional ethics


and responsibilities and norms of the engineering practice.

CSE-AIML SIGCE 3|Page


Sem VIII Advanced AI LabCSL801

PO9 Individual and team Function effectively as an individual, and as a member or


work leader in diverse teams, and in multidisciplinary settings.

PO10 Communication Communicate effectively on complex engineering activities


with the engineering community and with society at large,
such as, being able to comprehend and write effective
reports and design documentation, make effective
presentations, and give and receive clear instructions.

PO11 Project management Demonstrate knowledge and understanding of the


and finance engineering and management principles and apply these to
one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments

PO12 Life-long learning Recognize the need for, and have the preparation and
ability to engage in independent and life-long learning in
the broadest context of technological change

PSO1 Soft Skills Ability to analyze, design and develop applications by


adopting the dynamic nature of Software developments

PSO2 Professional Skill Ability to use knowledge in artificial intelligence and


machine learning to solve real world problems and identify
the research gaps and render solutions with innovative
ideas.

CSE-AIML SIGCE 4|Page


Sem VIII Advanced AI LabCSL801

Lab Objective
Lab Objective
1 Articulate basic knowledge of fuzzy set theory through programing.
2 To design Associative Memory Networks.
3 To apply Unsupervised learning towards Networks design.
4 To demonstrate Special networks and its applications in soft computing.
5 To implement Hybrid computing systems.

Lab Outcome
Lab Outcome Bloom Taxonomy
CSL801.1 Implement Fuzzy operations and functions towards Fuzzy-rule Apply
creations.
CSL801.2 Build and training Associative Memory Network. Create
CSL801.3 Build Unsupervised learning based networks . Create
CSL801.4 Design and implement architecture of Special Networks Create
CSL801.5 Implement Neuro-Fuzzy hybrid computing applications. Apply

LO-PO mapping

LO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 P012 PSO1 PSO2
CSL 801.1 2 - - - 3 - - - - - - 2- - -
CSL 801.2 2 - - - 3 - - - - - - -2 - -
CSL 801.3 - - 3 - - - - - - - - -2 3 -
CSL 801.4 - - 3 - 3 - - - - - - 2- 3 -
CSL 801.5 - 3 - - - - - - - - - -2 - 3
Avg 2 3 3 - 3 - - - - - - -2 3 3

CSE-AIML SIGCE 5|Page


Sem VIII Advanced AI LabCSL801

Lab Plan

Sr Planned Topics Planned Activity / No.of Lab Cognit PO PSO


No Completi Content Hrs Outco ion
on Week Delivery me
Design and implement a Impleme CSL8 Unde
1 Week2 2
HMM/GMM/BN ntation 01.1 rstand
Build and Train a Generative Multi-
Impleme CSL8 Creat
2 Week3 Layer Network Model using 2
ntation 01.2 e
appropriate dataset.
Build and Train a Deep Convolution
Generative Multi-Layer (DCGAN) Impleme CSL8 Creat
3 Week4 2
Network Model for an image based ntation 01.2 e
dataset.
Develop a Conditional GAN (CGAN)
Network to direct the image Impleme CSL8 Unde
4 Week5 2
generation process of the generator ntation 01.2 rstand
model.
Train a variational autoencoder using Impleme CSL8 Unde
5 Week6 2
Tensorflow on Fashion MNIST ntation 01.3 rstand
Exploring Pre-trained Models for Impleme CSL8 Analy
6 Week7 2
Outcome Generation. ntation 01.4 ze
Impleme CSL8 Analy
7 Week8 Case Study 2
ntation 01.5 ze
Impleme CSL8 Creat
8 Week9 Mini project 2
ntation 01.5 e

CSE-AIML SIGCE 6|Page


Sem VIII Advanced AI LabCSL801

Practical No 1

Hidden Markov Models for Outcome Prediction

Objective:

The objective of this lab is to introduce students to the concept of Hidden Markov Models (HMMs) and
guide them through the process of designing and implementing an HMM for outcome prediction. The
lab will cover the basic principles of HMMs, their components, and how to apply them to a simple
outcome prediction problem.

Prerequisites:

 Basic understanding of probability and statistics


 Familiarity with Python programming language

Tools and Libraries:

 Python (3.x)
 hmmlearn library

Lab Steps:

1. Introduction to Hidden Markov Models (HMMs)

Markov Models are mathematical models used to describe systems that undergo transitions between
different states over discrete time steps. The key principle of Markov Models is the Markov property,
which states that the future state of the system depends only on its current state and is independent of
the sequence of events that preceded it.

Components of a Markov Model:

 States: The distinct conditions or situations that a system can be in.


 Transition Probabilities: The probabilities associated with moving from one state to another in a
single time step.
 State Space: The set of all possible states in which the system can exist.
 Initial State Distribution: The probabilities of the system starting in each possible state.

Applications of Markov Models:

 Weather Prediction: Modeling transitions between weather states (sunny, rainy, cloudy) over
time.
 Queueing Systems: Analyzing the flow of entities through a queue or network of queues.
 Stock Price Movements: Modeling the stochastic nature of stock price changes.
 Natural Language Processing: Capturing the sequence of words in language processing tasks.
 Biological Systems: Modeling molecular and genetic processes.

CSE-AIML SIGCE 7|Page


Sem VIII Advanced AI LabCSL801

2. Introduction to Hidden Markov Models and their applications.

Hidden Markov Models (HMMs) are an extension of basic Markov Models designed to model systems
with unobservable or hidden states. In an HMM, while the underlying system undergoes transitions
between hidden states, only observable emissions or symbols are directly visible. HMMs are particularly
useful in situations where the true states are not directly measurable but can be inferred from observed
data.

Applications of Hidden Markov Models:

 Speech Recognition: HMMs are widely used in speech recognition systems. Hidden states
represent phonemes, and observation symbols are acoustic features.
 Bioinformatics: HMMs are applied in genomics for gene prediction, sequence alignment, and
protein structure prediction.
 Financial Modeling: HMMs can model financial time series data, where hidden states represent
market regimes and observed symbols represent asset prices.
 Natural Language Processing (NLP): HMMs are used in language modeling, part-of-speech
tagging, and named entity recognition.
 Gesture Recognition: HMMs can be employed to recognize gestures in computer vision
applications, where hidden states represent different gestures.
 Robotics: HMMs are used for localization and mapping in robotics, where hidden states
represent the robot's location.
 Healthcare: HMMs find applications in predicting disease progression, analyzing medical time
series data, and personalized medicine.

2. Components of an HMM

States: Define the states that represent the underlying system. In this lab, consider two states: "Healthy"
and "Sick."

Observation Symbols: Define the observable outcomes or symbols associated with each state. For
example, "Normal" and "Abnormal."

Transition Probabilities: Specify the probabilities of transitioning between different states.

Emission Probabilities: Specify the probabilities of observing different symbols in each state.

Initial State Probabilities: Specify the probabilities of starting in each state.

3. Generating Synthetic Data

Use the defined HMM parameters to generate synthetic data. This will involve simulating a sequence of
observations and hidden states.

4. Data Splitting

Split the synthetic data into training and testing sets. This step is crucial for evaluating the performance
of the HMM.

CSE-AIML SIGCE 8|Page


Sem VIII Advanced AI LabCSL801

5. HMM Model Training

Utilize the hmmlearn library to train the HMM model on the training set.

6. Predictions and Evaluation

Apply the trained HMM model to predict hidden states on the test set.

Evaluate the accuracy of predictions using appropriate metrics (e.g., accuracy_score from scikit-learn).

7. Conclusion

Discuss the results and insights gained from the HMM model.

Reflect on the challenges encountered during the implementation.

Explore potential improvements or extensions to the HMM for more realistic scenarios.

Conclusion:

In this lab, you've gained hands-on experience in designing and implementing a Hidden Markov Model
for outcome prediction. You've learned about the key components of an HMM, how to generate
synthetic data, split it for training and testing, and train the model using the hmmlearn library. The
evaluation of the model's accuracy provides insights into its predictive capabilities.

Hidden Markov Models are powerful tools with applications in various fields, including speech
recognition, bioinformatics, and finance. Understanding their principles and implementation is valuable
for anyone working on prediction tasks where the underlying system may not be directly observable.

Solution:

import numpy as np

from hmmlearn import hmm

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score

# Generate synthetic data

np.random.seed(42)

# Define the states

states = ["Healthy", "Sick"]

# Define the observation symbols

CSE-AIML SIGCE 9|Page


Sem VIII Advanced AI LabCSL801

symbols = ["Normal", "Abnormal"]

# Transition probabilities

transition_probabilities = np.array([[0.7, 0.3],[0.4, 0.6]])

# Emission probabilities

emission_probabilities = np.array([[0.8, 0.2],[0.3, 0.7]])

# Initial state probabilities

initial_probabilities = np.array([0.5, 0.5])

# Number of samples

num_samples = 1000

# Generate synthetic data using the defined HMM parameters

model = hmm.CategoricalHMM(n_components=len(states), n_iter=100, tol=0.01)

model.startprob_ = initial_probabilities

model.transmat_ = transition_probabilities

model.emissionprob_ = emission_probabilities

# Generate samples

X, Z = model.sample(1000)

# Split the data into training and testing sets

X_train, X_test, Z_train, Z_test = train_test_split(X, Z, test_size=0.2, random_state=42)

# Train the HMM model

model.fit(X_train)

# Predict the hidden states on the test set

predicted_states = model.predict(X_test)

CSE-AIML SIGCE 10 | P a g e
Sem VIII Advanced AI LabCSL801

# Evaluate the accuracy of the prediction

accuracy = accuracy_score(Z_test, predicted_states)

print(f"Accuracy: {accuracy * 100:.2f}%")

Output Interpretation:

1. We define the states, observation symbols, transition probabilities, emission probabilities, and
initial state probabilities.
2. Generate synthetic data using the defined HMM parameters.
3. Split the data into training and testing sets.
4. Train the HMM model on the training set.
5. Predict the hidden states on the test set.
6. Evaluate the accuracy of the prediction.

CSE-AIML SIGCE 11 | P a g e
Sem VIII Advanced AI LabCSL801

Practical No 2

Build and Train a Generative Multi-Layer Network Model using appropriate


dataset.

Objective:

The objective of this lab is to build and train a generative multi-layer network model using an
appropriate dataset. Generative models aim to learn the underlying probability distribution of the data
in order to generate new samples that resemble the training data.

Materials Required:

Python programming environment (Anaconda recommended)

Jupyter Notebook (optional but recommended)

TensorFlow or PyTorch library

A suitable dataset for the chosen task (e.g., MNIST, CIFAR-10, Fashion-MNIST)

Procedure:

Setup Environment:

Ensure that Python and necessary libraries like TensorFlow or PyTorch are installed. You can install them
via Anaconda or pip.

Import required libraries in your Python script or Jupyter Notebook.

Load Dataset:

Load the chosen dataset using appropriate functions provided by TensorFlow or PyTorch. If necessary,
preprocess the data (e.g., normalization, reshaping).

Build the Generative Model:

Choose a suitable architecture for your generative model. Popular choices include Variational
Autoencoders (VAEs), Generative Adversarial Networks (GANs), and Autoencoder architectures.

Implement the chosen architecture using TensorFlow or PyTorch modules. Define the layers, activation
functions, loss functions, and any other necessary components.

Train the Model:

Split the dataset into training and validation sets.

Define training parameters such as batch size, learning rate, and number of epochs.

CSE-AIML SIGCE 12 | P a g e
Sem VIII Advanced AI LabCSL801

Train the generative model using the training data. Monitor the training process by observing the loss
values and any other relevant metrics.

Periodically evaluate the model's performance on the validation set to prevent overfitting.

Generate Samples:

Once the model is trained, use it to generate new samples from the learned distribution.

Generate a sufficient number of samples and visualize them to assess the quality of the generated data.

Analysis:

Evaluate the quality of the generated samples. Consider metrics such as visual fidelity, diversity, and
similarity to the original dataset.

Compare the performance of different generative models if you experimented with multiple
architectures.

Conclusion:

In this lab, we successfully built and trained a generative multi-layer network model using an
appropriate dataset. By implementing the chosen architecture and optimizing its parameters, we were
able to train a model that learned the underlying probability distribution of the data. Through the
generation of new samples, we observed the model's ability to produce data resembling the training
dataset. This exercise provides valuable insights into the capabilities and limitations of generative
models, which are widely used in various applications such as image generation, data augmentation, and
anomaly detection.

Solution

import numpy as np

import matplotlib.pyplot as plt

import tensorflow as tf

from tensorflow.keras import layers

# Load and preprocess the MNIST dataset

(train_images, _), (_, _) = tf.keras.datasets.mnist.load_data()

train_images = train_images.reshape(train_images.shape[0], 28, 28, 1).astype('float32')

train_images = (train_images - 127.5) / 127.5 # Normalize images to [-1, 1]

# Generator model

def build_generator():

CSE-AIML SIGCE 13 | P a g e
Sem VIII Advanced AI LabCSL801

model = tf.keras.Sequential([

layers.Dense(256, input_shape=(100,), activation='relu'),

layers.Dense(512, activation='relu'),

layers.Dense(784, activation='tanh'),

layers.Reshape((28, 28, 1))

])

return model

# Discriminator model

def build_discriminator():

model = tf.keras.Sequential([

layers.Flatten(input_shape=(28, 28, 1)),

layers.Dense(512, activation='relu'),

layers.Dense(256, activation='relu'),

layers.Dense(1)

])

return model

# Create GAN model

def build_gan(generator, discriminator):

discriminator.trainable = False

model = tf.keras.Sequential([generator, discriminator])

return model

# Loss function

cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)

# Discriminator loss

def discriminator_loss(real_output, fake_output):

CSE-AIML SIGCE 14 | P a g e
Sem VIII Advanced AI LabCSL801

real_loss = cross_entropy(tf.ones_like(real_output), real_output)

fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)

total_loss = real_loss + fake_loss

return total_loss

# Generator loss

def generator_loss(fake_output):

return cross_entropy(tf.ones_like(fake_output), fake_output)

# Optimizers

generator_optimizer = tf.keras.optimizers.Adam(1e-4)

discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)

# Initialize models

generator = build_generator()

discriminator = build_discriminator()

gan = build_gan(generator, discriminator)

# Training loop

epochs = 50

batch_size = 128

noise_dim = 100

num_examples_to_generate = 16

seed = tf.random.normal([num_examples_to_generate, noise_dim])

@tf.function

def train_step(images):

noise = tf.random.normal([batch_size, noise_dim])

CSE-AIML SIGCE 15 | P a g e
Sem VIII Advanced AI LabCSL801

with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:

generated_images = generator(noise, training=True)

real_output = discriminator(images, training=True)

fake_output = discriminator(generated_images, training=True)

gen_loss = generator_loss(fake_output)

disc_loss = discriminator_loss(real_output, fake_output)

gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)

gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))

discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator,
discriminator.trainable_variables))

for epoch in range(epochs):

for i in range(train_images.shape[0] // batch_size):

images_batch = train_images[i * batch_size: (i + 1) * batch_size]

train_step(images_batch)

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

if (epoch + 1) % 10 == 0:

generated_images = generator(seed, training=False)

plt.figure(figsize=(4, 4))

for i in range(generated_images.shape[0]):

plt.subplot(4, 4, i+1)

plt.imshow(generated_images[i, :, :, 0] * 127.5 + 127.5, cmap='gray')

plt.axis('off')

CSE-AIML SIGCE 16 | P a g e
Sem VIII Advanced AI LabCSL801

plt.savefig(f'gan_generated_image_epoch_{epoch+1}.png')

plt.show()

Output Interpretation:

The code iterates over epochs, displaying the current epoch number during training.

Every 10 epochs, it generates a batch of synthetic images using the generator model and saves them as
PNG files.

These images represent the current progress of the generator in generating handwritten digits similar to
those in the MNIST dataset.

When you run this code, you'll observe the training progress in the console, and after each set of 10
epochs, you'll find PNG files named gan_generated_image_epoch_{epoch_number}.png saved in your
working directory, displaying the generated images at that epoch.

CSE-AIML SIGCE 17 | P a g e
Sem VIII Advanced AI LabCSL801

Practical No 3

Building and Training a Deep Convolutional Generative Adversarial Network


(DCGAN) for Image Dataset

Objective: The objective of this lab is to understand and implement a Deep Convolutional Generative
Adversarial Network (DCGAN) for generating images based on a given dataset.

Materials Required:

Python environment with TensorFlow and Keras installed

Image dataset (e.g., CIFAR-10, MNIST, CelebA, etc.)

GPU (optional, but recommended for faster training)

Jupyter Notebook (optional, for better code organization and visualization)

Procedure:

Understanding DCGAN:

Familiarize yourself with the concept of DCGANs. They consist of two neural networks: the generator
and the discriminator. The generator tries to create realistic images, while the discriminator tries to
distinguish between real and fake images.

Setting Up the Environment:

Install the necessary libraries (TensorFlow, Keras, etc.).

Import the required modules and packages.

Loading and Preprocessing the Dataset:

Load the image dataset.

Preprocess the images (e.g., normalization, resizing, etc.).

Building the Generator Model:

Define the architecture of the generator model.

Compile the model.

Building the Discriminator Model:

Define the architecture of the discriminator model.

CSE-AIML SIGCE 18 | P a g e
Sem VIII Advanced AI LabCSL801

Compile the model.

Training the DCGAN:

Combine the generator and discriminator into a single GAN model.

Train the GAN model using the prepared dataset.

Monitor the training process and adjust hyperparameters as needed.

Generating Images:

Use the trained generator to generate new images.

Visualize the generated images.

Evaluation:

Evaluate the quality of generated images (e.g., visual inspection, metrics like Inception Score, Frechet
Inception Distance, etc.).

Fine-tuning and Experimentation (Optional):

Experiment with different architectures, hyperparameters, and training techniques to improve the
performance of the DCGAN.

Conclusion:

In this lab, we successfully built and trained a Deep Convolutional Generative Adversarial Network
(DCGAN) for generating images based on a given dataset. We started by understanding the concept of
DCGANs and then proceeded to set up the environment, load and preprocess the dataset, build the
generator and discriminator models, and train the DCGAN. We then generated new images using the
trained generator and evaluated the quality of the generated images. Additionally, we explored fine-
tuning and experimentation to improve the performance of the DCGAN further.

By completing this lab, we gained valuable hands-on experience in implementing DCGANs and learned
how to generate realistic images using deep learning techniques. This knowledge can be further applied
to various domains such as image synthesis, data augmentation, and generative art.

Solution

import tensorflow as tf

from tensorflow.keras import layers, models

import numpy as np

import matplotlib.pyplot as plt

# Load and preprocess MNIST dataset

CSE-AIML SIGCE 19 | P a g e
Sem VIII Advanced AI LabCSL801

(x_train, _), (_, _) = tf.keras.datasets.mnist.load_data()

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32')

x_train = (x_train - 127.5) / 127.5 # Normalize images to [-1, 1]

# Build generator model

def build_generator():

model = models.Sequential([

layers.Dense(7*7*256, use_bias=False, input_shape=(100,)),

layers.BatchNormalization(),

layers.LeakyReLU(),

layers.Reshape((7, 7, 256)),

layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False),

layers.BatchNormalization(),

layers.LeakyReLU(),

layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False),

layers.BatchNormalization(),

layers.LeakyReLU(),

layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh')

])

return model

# Build discriminator model

def build_discriminator():

model = models.Sequential([

layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same', input_shape=[28, 28, 1]),

layers.LeakyReLU(),

layers.Dropout(0.3),

CSE-AIML SIGCE 20 | P a g e
Sem VIII Advanced AI LabCSL801

layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'),

layers.LeakyReLU(),

layers.Dropout(0.3),

layers.Flatten(),

layers.Dense(1)

])

return model

generator = build_generator()

discriminator = build_discriminator()

# Define loss functions

cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)

# Generator loss

def generator_loss(fake_output):

return cross_entropy(tf.ones_like(fake_output), fake_output)

# Discriminator loss

def discriminator_loss(real_output, fake_output):

real_loss = cross_entropy(tf.ones_like(real_output), real_output)

fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)

return real_loss + fake_loss

# Optimizers

generator_optimizer = tf.keras.optimizers.Adam(1e-4)

discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)

# Training loop

CSE-AIML SIGCE 21 | P a g e
Sem VIII Advanced AI LabCSL801

EPOCHS = 50

BATCH_SIZE = 256

noise_dim = 100

num_examples_to_generate = 16

seed = tf.random.normal([num_examples_to_generate, noise_dim])

@tf.function

def train_step(images):

noise = tf.random.normal([BATCH_SIZE, noise_dim])

with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:

generated_images = generator(noise, training=True)

real_output = discriminator(images, training=True)

fake_output = discriminator(generated_images, training=True)

gen_loss = generator_loss(fake_output)

disc_loss = discriminator_loss(real_output, fake_output)

gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)

gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))

discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator,
discriminator.trainable_variables))

# Training loop

for epoch in range(EPOCHS):

for i in range(x_train.shape[0] // BATCH_SIZE):

batch_images = x_train[i * BATCH_SIZE: (i + 1) * BATCH_SIZE]

CSE-AIML SIGCE 22 | P a g e
Sem VIII Advanced AI LabCSL801

train_step(batch_images)

# Generate images at every epoch

if epoch % 10 == 0:

generated_images = generator(seed, training=False)

plt.figure(figsize=(4, 4))

for j in range(generated_images.shape[0]):

plt.subplot(4, 4, j+1)

plt.imshow(generated_images[j, :, :, 0] * 127.5 + 127.5, cmap='gray')

plt.axis('off')

plt.show()

Output Interpretation:

After training, the model will generate images similar to the MNIST dataset. At every epoch, a grid of 16
generated images will be displayed. These images will progressively resemble handwritten digits as
training progresses..

CSE-AIML SIGCE 23 | P a g e
Sem VIII Advanced AI LabCSL801

Practical No 4

Conditional Generative Adversarial Network (CGAN) for Image Generation.

Objective:

The objective of this lab is to develop a Conditional Generative Adversarial Network (CGAN) to direct the
image generation process of the generator model. The CGAN framework extends the traditional GAN by
conditioning both the generator and discriminator networks on additional information, allowing for the
generation of images based on specific conditions.

Materials Required:

Python programming environment (e.g., Anaconda)

TensorFlow or PyTorch library

Jupyter Notebook or any preferred Python IDE

Procedure:

Dataset Preparation:

Select an appropriate dataset for training the CGAN model. Common choices include MNIST, CIFAR-10,
or a custom dataset relevant to your application.

Preprocess the dataset as necessary, including normalization and resizing, to ensure compatibility with
the network architecture.

Network Architecture Design:

Design the architecture for both the generator and discriminator networks.

Incorporate conditional inputs into both networks to enable conditional image generation.

Utilize appropriate activation functions, normalization layers, and other components as needed.

Model Implementation:

Implement the generator and discriminator networks using TensorFlow or PyTorch.

Define the loss functions for both the generator and discriminator, incorporating adversarial loss along
with any additional losses required for conditioning.

Set up the training loop to optimize the network parameters using techniques such as stochastic
gradient descent or Adam optimization.

Training the CGAN Model:

CSE-AIML SIGCE 24 | P a g e
Sem VIII Advanced AI LabCSL801

Initialize the model parameters and set hyperparameters such as learning rate, batch size, and number
of training epochs.

Train the CGAN model on the prepared dataset, monitoring the training progress by evaluating the
generated images and loss values.

Adjust hyperparameters and network architecture as necessary based on training performance.

Evaluation:

Evaluate the trained CGAN model by generating images conditioned on different input conditions.

Assess the quality of the generated images qualitatively and, if applicable, quantitatively using metrics
such as Inception Score or Frechet Inception Distance.

Compare the generated images against ground truth images from the dataset to gauge the model's
performance.

Conclusion:

In this lab, we successfully developed and trained a Conditional Generative Adversarial Network (CGAN)
for image generation. By conditioning both the generator and discriminator networks on additional
information, we were able to direct the image generation process based on specific conditions. Through
iterative training, the model learned to generate realistic images that adhere to the specified conditions.
The effectiveness of the trained CGAN model was evaluated through qualitative and, if applicable,
quantitative assessments, demonstrating its capability to generate high-quality images in a controlled
manner.

Solution

import numpy as np

import matplotlib.pyplot as plt

import tensorflow as tf

from tensorflow.keras import layers, models, datasets

# Load and preprocess the MNIST dataset

(train_images, train_labels), (_, _) = datasets.mnist.load_data()

train_images = (train_images - 127.5) / 127.5 # Normalize to [-1, 1]

train_images = np.expand_dims(train_images, axis=-1)

# Define the generator network

def build_generator():

CSE-AIML SIGCE 25 | P a g e
Sem VIII Advanced AI LabCSL801

model = models.Sequential([

layers.Dense(7*7*256, use_bias=False, input_shape=(100,)),

layers.BatchNormalization(),

layers.LeakyReLU(),

layers.Reshape((7, 7, 256)),

layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False),

layers.BatchNormalization(),

layers.LeakyReLU(),

layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False),

layers.BatchNormalization(),

layers.LeakyReLU(),

layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh')

])

return model

# Define the discriminator network

def build_discriminator():

model = models.Sequential([

layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same', input_shape=[28, 28, 1]),

layers.LeakyReLU(),

layers.Dropout(0.3),

layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'),

layers.LeakyReLU(),

layers.Dropout(0.3),

layers.Flatten(),

CSE-AIML SIGCE 26 | P a g e
Sem VIII Advanced AI LabCSL801

layers.Dense(1)

])

return model

# Define the CGAN model

class CGAN(models.Model):

def __init__(self, generator, discriminator):

super(CGAN, self).__init__()

self.generator = generator

self.discriminator = discriminator

def call(self, inputs, labels):

gen_input = tf.concat([inputs, labels], axis=1)

generated_images = self.generator(gen_input)

decision = self.discriminator(generated_images)

return decision

# Instantiate the generator and discriminator

generator = build_generator()

discriminator = build_discriminator()

# Compile the discriminator (we don't compile the generator in this case)

discriminator.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(from_logits=True))

# Instantiate the CGAN model

cgan = CGAN(generator, discriminator)

# Define the loss function for the generator

def generator_loss(fake_output):

return tf.keras.losses.BinaryCrossentropy(from_logits=True)(tf.ones_like(fake_output), fake_output)

CSE-AIML SIGCE 27 | P a g e
Sem VIII Advanced AI LabCSL801

# Define the optimizer for the generator

generator_optimizer = tf.keras.optimizers.Adam(1e-4)

# Define the number of epochs and batch size

EPOCHS = 50

BATCH_SIZE = 128

# Training loop

for epoch in range(EPOCHS):

for i in range(train_images.shape[0] // BATCH_SIZE):

# Generate random noise

noise = tf.random.normal([BATCH_SIZE, 100])

# Randomly select labels

labels = tf.random.uniform([BATCH_SIZE], minval=0, maxval=10, dtype=tf.int32)

# One-hot encode labels

labels = tf.one_hot(labels, depth=10)

# Generate images

with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:

generated_images = generator(tf.concat([noise, labels], axis=1), training=True)

real_output = discriminator(train_images[i * BATCH_SIZE: (i + 1) * BATCH_SIZE], training=True)

fake_output = discriminator(generated_images, training=True)

gen_loss = generator_loss(fake_output)

disc_loss = tf.keras.losses.BinaryCrossentropy(from_logits=True)(tf.ones_like(real_output),
real_output) + \

tf.keras.losses.BinaryCrossentropy(from_logits=True)(tf.zeros_like(fake_output),
fake_output)

gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)

CSE-AIML SIGCE 28 | P a g e
Sem VIII Advanced AI LabCSL801

generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))

gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator,
discriminator.trainable_variables))

# Print progress

print(f'Epoch {epoch+1}, Generator Loss: {gen_loss}, Discriminator Loss: {disc_loss}')

# Generate images with specified labels

def generate_images(label):

noise = tf.random.normal([1, 100])

label = tf.one_hot(label, depth=10)

generated_image = generator(tf.concat([noise, label], axis=1), training=False)

plt.imshow(generated_image[0, :, :, 0], cmap='gray')

plt.axis('off')

plt.show()

# Generate sample images for each digit

for i in range(10):

generate_images(i)

Output Interpretation:

The output consists of generated images corresponding to each digit (0-9). These images are generated
by conditioning the generator network on the specified label.

The output will display 10 images, each corresponding to a different digit from 0 to 9, generated by the
CGAN model. These images represent synthetic digit images created by the generator conditioned on
the respective digit labels. Due to the simplified nature of this example, the image quality may not be
perfect, but it demonstrates the basic functionality of a Conditional Generative Adversarial Network
(CGAN) for image generation.

CSE-AIML SIGCE 29 | P a g e
Sem VIII Advanced AI LabCSL801

Practical No 5

Training a Variational Autoencoder using TensorFlow on Fashion MNIST

Introduction:

Variational Autoencoders (VAEs) are a type of generative model that learns to encode and decode data.
They are particularly useful for generating new data samples similar to the training data. In this lab, we
will train a Variational Autoencoder using TensorFlow on the Fashion MNIST dataset. Fashion MNIST is a
dataset consisting of grayscale images of 10 different clothing items.

Objective:

The objective of this lab is to understand the implementation of a Variational Autoencoder using
TensorFlow and to generate new samples of fashion items.

Materials:

Python (version 3.x)

TensorFlow (version 2.x)

Jupyter Notebook or any Python IDE

Fashion MNIST dataset (available in TensorFlow datasets)

Procedure:

Setup Environment:

Install TensorFlow and other necessary libraries if not already installed.

Import TensorFlow and other required libraries in your Python environment.

Load the Fashion MNIST Dataset:

Use TensorFlow datasets to load the Fashion MNIST dataset.

Preprocess the data (normalize pixel values, reshape images, etc.).

Define the Variational Autoencoder Architecture:

Define the encoder and decoder networks using TensorFlow's Keras API.

Implement the reparameterization trick for the sampling step.

Define the loss function, which is a combination of reconstruction loss and KL divergence.

CSE-AIML SIGCE 30 | P a g e
Sem VIII Advanced AI LabCSL801

Train the Variational Autoencoder:

Compile the model with an appropriate optimizer and loss function.

Train the model on the Fashion MNIST dataset for a certain number of epochs.

Generate New Samples:

After training, use the decoder part of the VAE to generate new samples.

Randomly sample from the latent space and decode to generate new images.

Visualization:

Visualize the original images along with their reconstructed versions to evaluate the performance of the
VAE.

Visualize the generated samples to see the variety of images produced by the VAE.

Conclusion:

In this lab, we successfully trained a Variational Autoencoder using TensorFlow on the Fashion MNIST
dataset. We implemented the encoder and decoder networks, defined the loss function incorporating
both reconstruction loss and KL divergence, and trained the model. After training, we were able to
generate new samples by sampling from the latent space and decoding them using the decoder
network.

By visualizing the reconstructed images, we observed that the VAE is capable of reconstructing the input
images reasonably well. Moreover, the generated samples exhibited diverse fashion items,
demonstrating the generative capability of the VAE.

Overall, this lab provided hands-on experience with implementing and training a Variational
Autoencoder using TensorFlow, which is a fundamental technique in the field of unsupervised learning
and generative modeling.

Solution:

import tensorflow as tf

from tensorflow.keras import layers, Model

import numpy as np

import matplotlib.pyplot as plt

# Load Fashion MNIST dataset

(x_train, _), (x_test, _) = tf.keras.datasets.fashion_mnist.load_data()

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

CSE-AIML SIGCE 31 | P a g e
Sem VIII Advanced AI LabCSL801

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

x_train = np.reshape(x_train, (-1, 28, 28, 1))

x_test = np.reshape(x_test, (-1, 28, 28, 1))

# Define the Variational Autoencoder architecture

latent_dim = 2

class VAE(Model):

def __init__(self):

super(VAE, self).__init__()

self.encoder = tf.keras.Sequential([

layers.Input(shape=(28, 28, 1)),

layers.Conv2D(32, 3, activation='relu', strides=2, padding='same'),

layers.Conv2D(64, 3, activation='relu', strides=2, padding='same'),

layers.Flatten(),

layers.Dense(latent_dim + latent_dim),

])

self.decoder = tf.keras.Sequential([

layers.Input(shape=(latent_dim,)),

layers.Dense(7*7*32, activation='relu'),

layers.Reshape(target_shape=(7, 7, 32)),

layers.Conv2DTranspose(64, 3, activation='relu', strides=2, padding='same'),

layers.Conv2DTranspose(32, 3, activation='relu', strides=2, padding='same'),

layers.Conv2DTranspose(1, 3, activation='sigmoid', padding='same')

])

def sample(self, eps=None):

if eps is None:

CSE-AIML SIGCE 32 | P a g e
Sem VIII Advanced AI LabCSL801

eps = tf.random.normal(shape=(100, latent_dim))

return self.decode(eps, apply_sigmoid=True)

def call(self, x):

mean, logvar = tf.split(self.encoder(x), num_or_size_splits=2, axis=1)

eps = tf.random.normal(shape=mean.shape)

z = eps * tf.exp(logvar * .5) + mean

return self.decoder(z)

# Define the loss function

def vae_loss(x, x_recon, mean, logvar):

recon_loss = tf.reduce_mean(tf.reduce_sum(tf.square(x - x_recon), axis=(1, 2, 3)))

kl_loss = -0.5 * tf.reduce_mean(tf.reduce_sum(1 + logvar - tf.square(mean) - tf.exp(logvar), axis=1))

return recon_loss + kl_loss

# Instantiate the VAE model and optimizer

vae = VAE()

optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)

# Training loop

epochs = 10

batch_size = 128

num_examples_to_generate = 16

for epoch in range(epochs):

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

for i in range(len(x_train) // batch_size):

batch = x_train[i * batch_size: (i + 1) * batch_size]

with tf.GradientTape() as tape:

recon_batch = vae(batch)

CSE-AIML SIGCE 33 | P a g e
Sem VIII Advanced AI LabCSL801

mean, logvar = tf.split(vae.encoder(batch), num_or_size_splits=2, axis=1)

loss = vae_loss(batch, recon_batch, mean, logvar)

grads = tape.gradient(loss, vae.trainable_variables)

optimizer.apply_gradients(zip(grads, vae.trainable_variables))

print(f"Loss: {loss.numpy():.4f}")

# Generate new samples

random_latent_vectors = tf.random.normal(shape=(num_examples_to_generate, latent_dim))

generated_images = vae.decoder(random_latent_vectors)

# Plot the generated images

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

for i in range(num_examples_to_generate):

plt.subplot(4, 4, i + 1)

plt.imshow(generated_images[i, :, :, 0], cmap='gray')

plt.axis('off')

plt.show()

Output Interpretation:

After training the VAE for 10 epochs, the loss decreases over time, indicating the model's learning
progress. The generated images will vary based on the random latent vectors, but you should observe
fashion-like patterns similar to those in the Fashion MNIST dataset.

A 4x4 grid of generated images will be displayed, showcasing the variety of fashion items the VAE has
learned to generate.

This code provides a basic implementation of a Variational Autoencoder using TensorFlow on the
Fashion MNIST dataset. Depending on your computational resources and time, you may adjust the
number of epochs, batch size, and other hyperparameters for better performance.

CSE-AIML SIGCE 34 | P a g e
Sem VIII Advanced AI LabCSL801

Practical No 6

Exploring Pre-trained Models for Outcome Generation.

Objective:

The objective of this lab is to explore the working of pre-trained models for outcome generation. We will
utilize a pre-trained model to generate outcomes based on input data.

Materials Required:

Computer with internet access

Python programming environment (Anaconda recommended)

Jupyter Notebook (optional but recommended)

Pre-trained model library (e.g., GPT-3, BERT, etc.)

Sample input data (text, images, etc.)

Procedure:

Setup Environment:

Make sure you have Python installed on your computer.

If not, download and install Anaconda from https://www.anaconda.com/products/distribution.

Open a terminal or command prompt and create a new virtual environment:

conda create --name outcome_generation python=3.8

Activate the virtual environment:

conda activate outcome_generation

Install Necessary Libraries:

Depending on the pre-trained model you choose, install the corresponding library. For example, for GPT-
3, you can use OpenAI's openai library:

pip install openai

Choose Pre-trained Model:

Select the pre-trained model you want to use for outcome generation. Consider factors such as the type
of data it supports (text, images, etc.), the quality of generated outcomes, and any usage restrictions.

CSE-AIML SIGCE 35 | P a g e
Sem VIII Advanced AI LabCSL801

Prepare Input Data:

Collect or generate sample input data that you will use to feed into the pre-trained model for outcome
generation. This could be text, images, or any other format supported by the model.

Generate Outcomes:

Write a Python script or Jupyter Notebook to interact with the pre-trained model and generate
outcomes based on the input data.

Follow the documentation of the chosen pre-trained model library to understand how to use it for
outcome generation.

Input your sample data into the model and observe the outcomes generated.

Experiment and Analyze:

Experiment with different input data to see how the pre-trained model responds.

Analyze the quality and relevance of the outcomes generated.

Take note of any limitations or biases in the model's outputs.

Conclusion:

In this lab, we explored the working of pre-trained models for outcome generation. We selected a pre-
trained model, prepared input data, and generated outcomes based on that data. Through
experimentation, we observed how the model responds to different inputs and analyzed the quality of
the outcomes generated.

Pre-trained models offer a powerful tool for various natural language processing and computer vision
tasks. They can generate outcomes that are often contextually relevant and coherent. However, it's
essential to understand the limitations of these models, including biases in the training data and
occasional inaccuracies in the generated outcomes.

Overall, this lab provided valuable insights into the capabilities of pre-trained models for outcome
generation and demonstrated their potential applications in various domains. Further experimentation
and research in this area can lead to advancements in artificial intelligence and machine learning
technologies.

Solution

Use OpenAI's GPT-3 model through the openai library in Python.

# Import necessary libraries

import openai

# Set your OpenAI API key

CSE-AIML SIGCE 36 | P a g e
Sem VIII Advanced AI LabCSL801

api_key = "your_openai_api_key_here"

openai.api_key = api_key

# Define a text prompt

prompt = "Once upon a time, in a land far far away,"

# Generate outcomes using GPT-3 model

response = openai.Completion.create(

engine="text-davinci-003", # You can choose different engines as per your needs

prompt=prompt,

max_tokens=50 # You can adjust the number of tokens for the desired length of output

# Print the generated outcome

print(response.choices[0].text.strip())

Output Interpretation:

For the given prompt "Once upon a time, in a land far far away," the generated outcome could be
something like:

There lived a brave knight named Sir Cedric. He embarked on a quest to rescue the princess from the
clutches of an evil dragon.

This is just an example, and the actual outcome may vary based on the randomness and diversity of the
GPT-3 model's responses. You can try different prompts and experiment with the parameters to observe
various outcomes generated by the model.

CSE-AIML SIGCE 37 | P a g e

You might also like