Exp 6,7,8
Exp 6,7,8
Train a sentiment analysis model on IMDB dataset, use RNN layers with LSTM/GRU
notes
AIM: To Train a sentiment analysis model on IMDB dataset, use RNN layers with
LSTM/GRU notes
THEORY:
IMDB dataset: IT is a dataset of 50,000 highly polarized reviews from the Internet Movie
Database. They’re split into 25,000 reviews for training and 25,000 reviews for testing, each set
consisting of 50% negative and 50% positive reviews. The variables train_data and test_data are
lists of reviews; each review is a list of word indices (encoding a sequence of words).
train_labels and test_labels are lists of 0s and 1s, where 0 stands for negative and 1 stands for
positive
RNNs are designed for sequential data, like text or time series.
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import imdb
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Embedding, LSTM, Dense
# Vocabulary Information
word_index = imdb.get_word_index() # Complete word index
vocabulary_size = len(word_index)
print(f"Vocabulary Size (Full): {vocabulary_size}")
print(f"Vocabulary Size (Top {num_words}): {num_words}")
# Padding sequences
X_train = pad_sequences(X_train, maxlen=maxlen)
X_test = pad_sequences(X_test, maxlen=maxlen)
# Model Definition
model = Sequential()
model.add(Embedding(num_words, 32, input_length=maxlen))
model.add(LSTM(16, activation='tanh'))
model.add(Dense(1, activation='sigmoid'))
# Combined Plot
plt.figure(figsize=(10, 6))
epochs = range(1, len(history.history["accuracy"]) + 1)
# Accuracy
plt.plot(epochs, history.history["accuracy"], label="Training Accuracy",
color="blue")
plt.plot(epochs, history.history["val_accuracy"], label="Validation
Accuracy", color="green")
# Loss
plt.plot(epochs, history.history["loss"], label="Training Loss",
color="red", linestyle="--")
plt.plot(epochs, history.history["val_loss"], label="Validation Loss",
color="orange", linestyle="--")
# Plot Details
plt.title("Training and Validation Metrics")
plt.xlabel("Epochs")
plt.ylabel("Metrics")
plt.legend()
plt.grid(True)
plt.show()
predictions = model.predict(X_test[:10])
for i in range(10):
print(f"Review {i + 1}:")
print(f"Decoded: {decode_review(X_test[i])}")
print(f"Actual Sentiment: {'Positive' if Y_test[i] == 1 else
'Negative'}")
print(f"Predicted Sentiment: {'Positive' if predictions[i] > 0.5 else
'Negative'}\n")
RESULT: The Training of a sentiment analysis model on IMDB dataset, using RNN layers
with LSTM notes is observed and verified successfully.
7. Applying the Autoencoder algorithms for encoding the real-world data
AIM: To Apply the Autoencoder algorithms for encoding the real-world data.
THEORY: Autoencoders can also be adapted for other real-world datasets by preprocessing them
appropriately. An Autoencoder is a type of artificial neural network used to learn efficient
representations of data, typically for the purpose of dimensionality reduction or feature extraction. The
network is trained to map the input to itself through a compressed, lower-dimensional representation.
Preprocessing:
MNIST dataset is normalized to the range [0, 1].
Data is reshaped from 28x28 to a 1D vector for compatibility with the dense autoencoder.
Autoencoder Architecture:
Encoder: Reduces the input to a lower-dimensional latent space (64 dimensions).
Decoder: Reconstructs the input from the latent space.
Training: Autoencoder is trained with binary cross-entropy loss to minimize reconstruction error.
Visualization: Original and reconstructed images are displayed for comparison.Encoded representations
(latent space) are printed for inspection.
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model
from keras.layers import Input, Dense
from keras.datasets import mnist
# Autoencoder architecture
input_dim, encoding_dim = X_train.shape[1], 64
input_layer = Input(shape=(input_dim,))
encoded = Dense(encoding_dim, activation='relu')(input_layer)
decoded = Dense(input_dim, activation='sigmoid')(encoded)
autoencoder = Model(input_layer, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
# Train autoencoder
history = autoencoder.fit(X_train, X_train, epochs=10, batch_size=256,
validation_data=(X_test, X_test), verbose=0)
# Encoder model
encoder = Model(input_layer, encoded)
encoded_data = encoder.predict(X_test)
decoded_data = autoencoder.predict(X_test)
# Plot loss curves
plt.plot(history.history['loss'], label='Train')
plt.plot(history.history['val_loss'], label='Validation')
plt.title('Loss During Training')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
RESULT: Applying the Autoencoder algorithms for encoding the real-world data is observed
and verified output successfully.
8. Applying Generative Adversial Networks for image generation and unsupervised tasks.
AIM: To Apply Generative Adversial Networks for image generation and unsupervised tasks.
THEORY:
Generative adversarial networks (GANs), introduced in 2014 by Goodfellow et al.,8 are an alternative to
variational autoencoders(VAEs) for learning latent spaces of images. They enable the generation of fairly
realistic synthetic images by forcing the generated images to be statistically almost indistinguishable from
real ones.
GAN is made of two parts:
Generator network—Takes as input a random vector (a random point in the latent space), and decodes it
into a synthetic image.
Discriminator network (or adversary)—Takes as input an image (real or synthetic), and predicts
whether the image came from the training set or was created by the generator network.
PROGRAM:
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
# Hyperparameters
latent_dim = 100
batch_size = 128
epochs = 50
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Data preparation
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])])
dataloader = DataLoader(datasets.MNIST('./data', train=True,
transform=transform, download=True), batch_size=batch_size, shuffle=True)
# Models
class Generator(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Linear(latent_dim, 256), nn.LeakyReLU(0.2),
nn.Linear(256, 512), nn.LeakyReLU(0.2),
nn.Linear(512, 28*28), nn.Tanh()
)
def forward(self, z):
return self.model(z).view(-1, 1, 28, 28)
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Flatten(),
nn.Linear(28*28, 512), nn.LeakyReLU(0.2),
nn.Linear(512, 256), nn.LeakyReLU(0.2),
nn.Linear(256, 1), nn.Sigmoid()
)
# Training Loop
for epoch in range(epochs):
for imgs, _ in dataloader:
real_imgs = imgs.to(device)
batch_size = real_imgs.size(0)
# Discriminator Loss
noise = torch.randn(batch_size, latent_dim, device=device)
fake_imgs = generator(noise)
real_loss = criterion(discriminator(real_imgs),
torch.ones(batch_size, 1, device=device))
fake_loss = criterion(discriminator(fake_imgs.detach()),
torch.zeros(batch_size, 1, device=device))
d_loss = (real_loss + fake_loss) / 2
optimizer_D.zero_grad()
d_loss.backward()
optimizer_D.step()
# Generator Loss
g_loss = criterion(discriminator(fake_imgs),
torch.ones(batch_size, 1, device=device))
optimizer_G.zero_grad()
g_loss.backward()
optimizer_G.step()
RESULT: Applying the Generative Adversial Networks for image generation and
unsupervised tasks is done successfully and verified output.