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

Assignment-10.1 NLP 2103a51375

The document outlines a lab assignment focused on implementing and training generative models, specifically Variational Autoencoders (VAE) and Generative Adversarial Networks (GAN), using datasets like MNIST. It provides detailed code for both models, including the architecture, training processes, and visualization of the latent space and generated images. The document also suggests next steps for further experimentation with advanced datasets and model enhancements.

Uploaded by

vamshi panaka
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 views8 pages

Assignment-10.1 NLP 2103a51375

The document outlines a lab assignment focused on implementing and training generative models, specifically Variational Autoencoders (VAE) and Generative Adversarial Networks (GAN), using datasets like MNIST. It provides detailed code for both models, including the architecture, training processes, and visualization of the latent space and generated images. The document also suggests next steps for further experimentation with advanced datasets and model enhancements.

Uploaded by

vamshi panaka
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/ 8

1.

Generative models are a class of machine learning models that generate new data
instances that resemble your training data. These models can be used for various tasks,
such as creating realistic images, text, or even music. In this lab assignment, we'll focus on
implementing and training generative models, particularly a Variational Autoencoder
(VAE) and a Generative Adversarial Network (GAN), using a simple dataset like MNIST or a
text dataset for generating synthetic samples. [CO5]

(i) Impliment Variational Autoencoder (VAE)

(ii) Implement the GAN

(iii) Visualize the latent space and generated images to understand how well the model
captures the data distribution.

Ans:

(i): Implement Variational Autoencoder (VAE)

A VAE consists of an encoder, a decoder, and a latent space. The encoder maps inputs to a
latent space distribution, and the decoder reconstructs the inputs from sampled points in the
latent space.

Code for VAE

import numpy as np

import tensorflow as tf

from tensorflow.keras.layers import Input, Dense, Lambda, Flatten, Reshape

from tensorflow.keras.models import Model

from tensorflow.keras.losses import binary_crossentropy

from tensorflow.keras.datasets import mnist

import matplotlib.pyplot as plt

# Load MNIST dataset

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

x_train = x_train.astype("float32") / 255.0

x_test = x_test.astype("float32") / 255.0

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


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

# Hyperparameters

latent_dim = 2

input_dim = 28 * 28

# Encoder

inputs = Input(shape=(input_dim,))

h = Dense(512, activation='relu')(inputs)

z_mean = Dense(latent_dim)(h)

z_log_var = Dense(latent_dim)(h)

# Sampling function

def sampling(args):

z_mean, z_log_var = args

epsilon = tf.random.normal(shape=(tf.shape(z_mean)[0], latent_dim))

return z_mean + tf.exp(0.5 * z_log_var) * epsilon

z = Lambda(sampling)([z_mean, z_log_var])

# Decoder

decoder_h = Dense(512, activation='relu')

decoder_mean = Dense(input_dim, activation='sigmoid')

h_decoded = decoder_h(z)

x_decoded_mean = decoder_mean(h_decoded)

# VAE model

vae = Model(inputs, x_decoded_mean)


# Loss

reconstruction_loss = binary_crossentropy(inputs, x_decoded_mean) * input_dim

kl_loss = -0.5 * tf.reduce_sum(1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var), axis=-1)

vae_loss = tf.reduce_mean(reconstruction_loss + kl_loss)

vae.add_loss(vae_loss)

vae.compile(optimizer='adam')

vae.summary()

# Train VAE

vae.fit(x_train, x_train, epochs=50, batch_size=128, validation_data=(x_test, x_test))

# Encoder and Decoder Models for Visualization

encoder = Model(inputs, z_mean)

decoder_input = Input(shape=(latent_dim,))

_h_decoded = decoder_h(decoder_input)

_x_decoded_mean = decoder_mean(_h_decoded)

decoder = Model(decoder_input, _x_decoded_mean)

Visualizing Latent Space

# Visualize Latent Space

x_test_encoded = encoder.predict(x_test)

plt.figure(figsize=(8, 6))

plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=_[:10000], cmap='viridis')

plt.colorbar()

plt.title("Latent Space Visualization")

plt.show()

Generate New Images


# Generate New Images from Random Points in Latent Space

random_latent_vectors = np.random.normal(size=(10, latent_dim))

generated_images = decoder.predict(random_latent_vectors)

# Plot generated images

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

for i, image in enumerate(generated_images):

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

plt.imshow(image.reshape(28, 28), cmap="gray")

plt.axis("off")

plt.show()

Step (ii): Implement Generative Adversarial Network (GAN)

GANs consist of a generator that learns to produce realistic data and a discriminator that
learns to distinguish real data from generated data.

Code for GAN

from tensorflow.keras.layers import LeakyReLU, BatchNormalization

from tensorflow.keras.optimizers import Adam

# Hyperparameters

latent_dim = 100

# Generator

def build_generator():

model = tf.keras.Sequential([

Dense(256, input_dim=latent_dim),

LeakyReLU(alpha=0.2),
BatchNormalization(momentum=0.8),

Dense(512),

LeakyReLU(alpha=0.2),

BatchNormalization(momentum=0.8),

Dense(1024),

LeakyReLU(alpha=0.2),

BatchNormalization(momentum=0.8),

Dense(28 * 28, activation="tanh"),

Reshape((28, 28))

])

return model

# Discriminator

def build_discriminator():

model = tf.keras.Sequential([

Flatten(input_shape=(28, 28)),

Dense(512),

LeakyReLU(alpha=0.2),

Dense(256),

LeakyReLU(alpha=0.2),

Dense(1, activation="sigmoid")

])

return model

# Compile GAN

generator = build_generator()

discriminator = build_discriminator()

discriminator.compile(optimizer=Adam(0.0002, 0.5), loss="binary_crossentropy",


metrics=["accuracy"])
# Combined model

discriminator.trainable = False

z = Input(shape=(latent_dim,))

generated_img = generator(z)

valid = discriminator(generated_img)

combined = Model(z, valid)

combined.compile(optimizer=Adam(0.0002, 0.5), loss="binary_crossentropy")

Training the GAN

# Training Function

def train_gan(epochs, batch_size):

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

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

for epoch in range(epochs):

# Train discriminator

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

real_imgs = x_train[idx]

z = np.random.normal(size=(batch_size, latent_dim))

fake_imgs = generator.predict(z)

d_loss_real = discriminator.train_on_batch(real_imgs, real)

d_loss_fake = discriminator.train_on_batch(fake_imgs, fake)

d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

# Train generator

z = np.random.normal(size=(batch_size, latent_dim))

g_loss = combined.train_on_batch(z, real)


if epoch % 1000 == 0:

print(f"{epoch} [D loss: {d_loss[0]}, acc.: {100 * d_loss[1]}] [G loss: {g_loss}]")

generate_and_save_images(epoch)

# Generate and Save Images

def generate_and_save_images(epoch, examples=10):

z = np.random.normal(size=(examples, latent_dim))

generated_images = generator.predict(z)

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

for i in range(examples):

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

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

plt.axis("off")

plt.show()

# Train GAN

train_gan(epochs=10000, batch_size=64)

Step (iii): Visualizing the Latent Space and Generated Images

For VAE:

- The latent space scatter plot shows how inputs are mapped.

- Generated images can be visualized by sampling random points in the latent space.

For GAN:

- The discriminator's accuracy and loss help evaluate training.

- Generated images demonstrate how well the generator learns to mimic the dataset.
Next Steps

1. Use advanced datasets like CIFAR-10 or CelebA.

2. Implement conditional GANs for controlled data generation.

3. Experiment with larger latent dimensions and deeper networks for better generation
quality.

You might also like