0% found this document useful (0 votes)
15 views17 pages

Exp 6,7,8

Uploaded by

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

Exp 6,7,8

Uploaded by

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

6.

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

RNN (Recurrent Neural Network):

 RNNs are designed for sequential data, like text or time series.

LSTM (Long Short-Term Memory):

 LSTM is a type of RNN designed to address the vanishing gradient problem.

GRU (Gated Recurrent Unit):

 GRU is a simplified version of LSTM with fewer gates.

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

# Load IMDB dataset


num_words = 15000 # Limit vocabulary to top 15,000 words
maxlen = 130 # Max review length for padding
(X_train, Y_train), (X_test, Y_test) = imdb.load_data(num_words=num_words)

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

# Compile and Train the Model


model.compile(loss='binary_crossentropy', optimizer='rmsprop',
metrics=['accuracy'])
history = model.fit(X_train, Y_train, validation_data=(X_test, Y_test),
epochs=3, batch_size=128, verbose=1)

# 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 for the first 10 samples


print("\nPredictions for the first 10 reviews:")
reverse_word_index = {value: key for (key, value) in word_index.items()}
def decode_review(encoded_review):
return " ".join([reverse_word_index.get(i - 3, "[UNK]") for i in
encoded_review if i >= 3])

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

# Load and preprocess MNIST data


(X_train, _), (X_test, _) = mnist.load_data()
X_train, X_test = [x.astype('float32').reshape(len(x), -1) / 255. for x in
(X_train, X_test)]

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

# Visualize original and reconstructed images


n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
ax = plt.subplot(2, n, i + 1)
plt.imshow(X_test[i].reshape(28, 28), cmap='gray')
plt.title("Original")
plt.axis('off')
ax = plt.subplot(2, n, i + n + 1)
plt.imshow(decoded_data[i].reshape(28, 28), cmap='gray')
plt.title("Reconstructed")
plt.axis('off')
plt.show()

print("Encoded representations (first 5 samples):\n", encoded_data[:5])

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

def forward(self, img):


return self.model(img)

# Initialize models and optimizers


generator, discriminator = Generator().to(device),
Discriminator().to(device)
optimizer_G = optim.Adam(generator.parameters(), lr=0.0002, betas=(0.5,
0.999))
optimizer_D = optim.Adam(discriminator.parameters(), lr=0.0002,
betas=(0.5, 0.999))
criterion = nn.BCELoss()

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

# Generate and Display Images


noise = torch.randn(25, latent_dim, device=device)
gen_imgs = generator(noise).detach().cpu()
fig, axs = plt.subplots(5, 5, figsize=(5, 5))
for i, ax in enumerate(axs.flat):
ax.imshow(gen_imgs[i].squeeze(0), cmap='gray')
ax.axis('off')
plt.tight_layout()
plt.show()

RESULT: Applying the Generative Adversial Networks for image generation and
unsupervised tasks is done successfully and verified output.

You might also like