0% found this document useful (0 votes)
0 views

Dl lab answers batch 2

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)
0 views

Dl lab answers batch 2

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/ 27

DL LAB QNS AND ANS

1. Implement a Deep Neural Network (DNN) architecture to solve the XOR problem using a simple
dataset.
2. Write Python code for a DNN solving the XOR problem, utilizing a dataset with input-output pairs (0,
0), (0, 1), (1, 0), and (1, 1).

PROGRAM:
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
x=np.array([[0,0],[0,1],[1,0],[1,1]],dtype=np.float32)
y=np.array([[0],[1],[1],[0]],dtype=np.float32)
model=tf.keras.Sequential([
tf.keras.layers.Dense(16,input_shape=(2,),activation='relu'),
tf.keras.layers.Dense(1,activation='sigmoid')])
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(x,y,epochs=1000,verbose=0)
loss,accuracy=model.evaluate(x,y)
print(f"Loss:{loss:.4f},Accuracy:{accuracy:.4f}")
predictions=model.predict(x)
print("Predictions:")
for i in range(len(x)):
print(f"Input:{x[i]}Output:{predictions[i][0]:.4f}")

OUTPUT:
3. Build a Convolutional Neural Network (CNN) for character recognition using the MNIST dataset.
Utilize a straightforward architecture and evaluate its performance.
4. Implement a CNN for character recognition using the EMNIST dataset. Apply basic techniques to
enhance accuracy.
7. Apply a transfer learning approach for character recognition using a pre-trained CNN model (e.g.,
VGG16). Demonstrate the model's enhanced performance in character recognition tasks.

PROGRAM:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
(x_train,y_train),(x_test,y_test)=mnist.load_data()
x_train=np.expand_dims(x_train,-1).astype('float32')/255.0
x_test=np.expand_dims(x_test,-1).astype('float32')/255.0
y_train=tf.keras.utils.to_categorical(y_train,num_classes=10)
y_test=tf.keras.utils.to_categorical(y_test,num_classes=10)
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=128, epochs=10, validation_data=(x_test, y_test))
predictions=model.predict(x_test)
recognized_labels = np.argmax(predictions, axis=1)
plt.figure(figsize=(10, 10))
for i in range(10):
plt.subplot(2, 5, i + 1)
plt.imshow(x_test[i].reshape(28, 28), cmap='gray')
plt.title(f"Predicted: {recognized_labels[i]}\nActual: {np.argmax(y_test[i])}")
plt.axis('off')
plt.tight_layout()
plt.show()

OUTPUT:
5. Develop a CNN model for face recognition using the LFW dataset.
6. Implement face recognition using a CNN on a custom dataset of face images.
PROGRAM:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder
import numpy as np
from tensorflow.keras.datasets import mnist # Replace with LFW data later
from sklearn.datasets import fetch_lfw_people

# Load LFW dataset


lfw_data = fetch_lfw_people(min_faces_per_person=20, resize=0.4)
X = lfw_data.images # Images are grayscale with dimensions (50x37)
y = lfw_data.target
labels = lfw_data.target_names

print(f"Dataset Shape: {X.shape}")


print(f"Number of Classes: {len(labels)}")

# Preprocess data
X = X[..., np.newaxis] # Add channel dimension (50, 37, 1)
X = X / 255.0 # Normalize pixel values
# Encode labels as one-hot vectors
encoder = LabelEncoder()
y_encoded = encoder.fit_transform(y)
y_onehot = to_categorical(y_encoded)
X_train, X_test, y_train, y_test = train_test_split(X, y_onehot, test_size=0.2, random_state=42)
model = Sequential([
# Convolutional layers
Conv2D(32, (3, 3), activation='relu', input_shape=(50, 37, 1)),
BatchNormalization(),
MaxPooling2D((2, 2)),
Dropout(0.25),

Conv2D(64, (3, 3), activation='relu'),


BatchNormalization(),
MaxPooling2D((2, 2)),
Dropout(0.25),

Conv2D(128, (3, 3), activation='relu'),


BatchNormalization(),
Flatten(),Dense(256, activation='relu'),
BatchNormalization(),
Dropout(0.5),
Dense(len(labels), activation='softmax') # Output layer])
model.compile(optimizer=Adam(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])
model.summary()
batch_size = 32
epochs = 20
history = model.fit(
X_train, y_train,
validation_data=(X_test, y_test),
batch_size=batch_size,
epochs=epochs,
verbose=1
)
loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test Loss: {loss:.4f}, Test Accuracy: {accuracy:.4f}")
# Test on a single image
import matplotlib.pyplot as plt
index = 0
test_image = X_test[index]
predicted_label = np.argmax(model.predict(test_image[np.newaxis, ...]))
true_label = np.argmax(y_test[index])
plt.imshow(test_image.squeeze(), cmap='gray')
plt.title(f"True: {labels[true_label]}, Predicted: {labels[predicted_label]}")
plt.show()
model.save("lfw_face_recognition_cnn.h5")
OUTPUT:
8. Create a language model using Recurrent Neural Network (RNN) on a small corpus of text documents.

9. Implement a character-level language model using RNN. Demonstrate its handling of character sequences
for language modelling.

PROGRAM:

import torch
from transformers import GPT2LMHeadModel,GPT2Tokenizer
model_name='gpt2'
model=GPT2LMHeadModel.from_pretrained(model_name)
tokenizer=GPT2Tokenizer.from_pretrained(model_name)
model.eval()
def generate_text(prompt,max_length=100,temperature=0.7):
input_ids=tokenizer.encode(prompt,return_tensors='pt')
output = model.generate(input_ids, max_length=max_length, temperature=temperature,
num_return_sequences=1,do_sample=True)
generated_text=tokenizer.decode(output[0],skip_special_tokens=True)
return generated_text
prompt="Once Up on a Time,There lived a ghost"
generate=generate_text(prompt,max_length=100)
print(generate)

OUTPUT:
Once Up on a Time,There lived a ghostly man, whose name was Peter. While he lived, there was no sound of
the world. At night he dreamed that there were no dead persons anywhere. He dreamed that he was alone.
And when he awoke, he saw the world as he had once seen it.From the end of the first book, it is obvious that
the author was interested in the "death" of the author by the ghost of the author. The narrator, who has
already been seen, is dead, and the author has been seen. He has to get up and go to the next town to get
another chance. After he gets there, he finds that the town is deserted. The ghost of the author is there. The
town is deserted, and the ghost of the author is there. The narrator is dead, the ghost of the author is dead,
the ghost of the author is dead, and the ghost of the author is dead.
11. Perform sentiment analysis on movie reviews using Long Short-Term Memory (LSTM) networks
with the IMDB dataset.
12. Build an LSTM-based sentiment analysis model for Twitter data. Illustrate how LSTMs capture
sequential information and address the challenges of analyzing tweets.

PROGRAM:
import numpy as np
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Embedding, LSTM, Dense
from keras.preprocessing.sequence import pad_sequences
vocab_size = 10000
maxlen = 100
embedding_dim = 32
batch_size = 64
epochs = 15
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)
x_train = pad_sequences(x_train, maxlen=maxlen)
x_test = pad_sequences(x_test, maxlen=maxlen)
model = Sequential()
model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim,
input_length=maxlen))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test))
score, accuracy = model.evaluate(x_test, y_test)
print(f'Test Accuracy: {accuracy:.4f}')
def predict_sentiment(review):
review_sequence = imdb.get_word_index()
review_sequence = {k: (v + 3) for k, v in review_sequence.items()}
review_sequence = [review_sequence.get(word, 0) for word in review.split()]
review_sequence = pad_sequences([review_sequence], maxlen=maxlen)
prediction = model.predict(review_sequence)
return "Positive" if prediction >= 0.5 else "Negative"
sample_review = "I loved this movie. It was fantastic!"
print(f"Review: '{sample_review}' - Sentiment: {predict_sentiment(sample_review)}")

OUTPUT:

13. Implement parts of speech tagging using Sequence to Sequence architecture.

14. Develop a Sequence-to-Sequence model for translating English sentences into parts of speech tags.
Demonstrate how the model translates sentences into parts of speech tags.

PROGRAM:

import numpy as np

import tensorflow as tf

from tensorflow.keras import layers, models

from tensorflow.keras.preprocessing.sequence import pad_sequences

from sklearn.model_selection import train_test_split

data = [

(["I", "love", "coding"], ["PRON", "VERB", "NOUN"]),

(["Python", "is", "great"], ["PROPN", "VERB", "ADJ"]),


(["This", "is", "a", "test"], ["DET", "VERB", "DET", "NOUN"]),

(["Keras", "is", "fun"], ["PROPN", "VERB", "ADJ"]),

# Prepare the vocabulary

words = set()

tags = set()

for sentence, tag_sequence in data:

words.update(sentence)

tags.update(tag_sequence)

word_to_index = {word: idx + 1 for idx, word in enumerate(sorted(words))}

tag_to_index = {tag: idx + 1 for idx, tag in enumerate(sorted(tags))}

# Prepare input and output sequences

X = [[word_to_index[word] for word in sentence] for sentence, _ in data]

y = [[tag_to_index[tag] for tag in tag_sequence] for _, tag_sequence in data]

# Pad sequences

max_len = max(max(len(seq) for seq in X), max(len(seq) for seq in y))

X = pad_sequences(X, maxlen=max_len, padding='post')

y = pad_sequences(y, maxlen=max_len, padding='post')

# Split the data

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,

random_state=42)

# Define the Seq2Seq model

input_dim = len(word_to_index) + 1

output_dim = len(tag_to_index) + 1

embedding_dim = 64
model = models.Sequential([

layers.Embedding(input_dim=input_dim, output_dim=embedding_dim,

input_length=max_len),

layers.LSTM(64, return_sequences=True),

layers.TimeDistributed(layers.Dense(output_dim, activation='softmax')),

])

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam',

metrics=['accuracy'])

# Train the model

y_train_reshaped = np.expand_dims(y_train, -1)

model.fit(X_train, y_train_reshaped, batch_size=1, epochs=100)

# Evaluate the model

loss, accuracy = model.evaluate(X_test, np.expand_dims(y_test, -1))

print(f'Test Accuracy: {accuracy:.4f}')

# Example prediction function

def predict_pos(sentence):

sequence = [word_to_index.get(word, 0) for word in sentence]

padded_sequence = pad_sequences([sequence], maxlen=max_len,

padding='post')

predictions = model.predict(padded_sequence)

predicted_indices = np.argmax(predictions, axis=-1)[0]

return [list(tag_to_index.keys())[list(tag_to_index.values()).index(idx)] for

idx in predicted_indices if idx != 0]

sample_sentence = ["Keras", "is", "fun"]

print(f"Sentence: '{' '.join(sample_sentence)}' - POS Tags: {predict_pos(sample_sentence)}")


OUTPUT:

15. Implement English to other languages (e.g., Tamil) machine translation using an Encoder-Decoder
model.

16. Implement a basic Encoder-Decoder model for translating English sentences into French. Evaluate its
performance on a small set of sample sentences.

PROGRAM:

import tensorflow as tf

from tensorflow.keras.layers import Embedding, LSTM, Dense, TimeDistributed

from tensorflow.keras.models import Sequential

from tensorflow.keras.preprocessing.text import Tokenizer

from tensorflow.keras.preprocessing.sequence import pad_sequences

import numpy as np

# Sample data (replace with your dataset)

source_sentences = ["I am hungry.", "He is a programmer.", "She loves to code."]

target_sentences = ["J'ai faim.", "Il est programmeur.", "Elle aime coder."]

# Tokenization

source_tokenizer = Tokenizer()
source_tokenizer.fit_on_texts(source_sentences)

source_sequences = source_tokenizer.texts_to_sequences(source_sentences)

target_tokenizer = Tokenizer()

target_tokenizer.fit_on_texts(target_sentences)

target_sequences = target_tokenizer.texts_to_sequences(target_sentences)

# Padding

source_sequences = pad_sequences(source_sequences, padding='post')

max_target_sequence_length = source_sequences.shape[1]

target_sequences = pad_sequences(target_sequences, padding='post', maxlen=max_target_sequence_length)

# Build the model

model = Sequential()

model.add(Embedding(input_dim=len(source_tokenizer.word_index) + 1, output_dim=256,
input_length=source_sequences.shape[1]))

model.add(LSTM(256, return_sequences=True))

model.add(TimeDistributed(Dense(len(target_tokenizer.word_index) + 1, activation='softmax')))

# Compile the model with the correct loss function

model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')

# Convert target sequences to numpy array and reshape to add an additional dimension

target_sequences = np.expand_dims(target_sequences, -1)

# Train the model (replace with your training data)

model.fit(source_sequences, target_sequences, epochs=150)

# Translate a sentence

input_sentence = "I am hungry."

input_sequence = source_tokenizer.texts_to_sequences([input_sentence])
input_sequence = pad_sequences(input_sequence, padding='post',maxlen=source_sequences.shape[1])

predicted_sequence = model.predict(input_sequence)

# Decode the predicted sequence

predicted_sentence =
[list(target_tokenizer.word_index.keys())[list(target_tokenizer.word_index.values()).index(word_index)] for
word_index in predicted_sequence[0].argmax(axis=1) if word_index in
list(target_tokenizer.word_index.values())]

print("Input sentence:", input_sentence)

print("Translated sentence:", ' '.join(predicted_sentence))

OUTPUT:

17. Implement image augmentation using Generative Adversarial Networks (GANs) on a small dataset of
animals.
18. Develop a GAN-based image augmentation system for enhancing medical images.
PROGRAM:
import torch
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import numpy as np
transform = transforms.Compose([transforms.ToTensor()])
dataset = datasets.MNIST(root='./data', train=True, transform=transform, download=True)
num_images_to_display = 5
original_images = [dataset[i][0] for i in range(num_images_to_display)]
def invert_image(image):
inverted_image = 1.0 - image # Invert the image by subtracting pixel values from 1.0
return inverted_image
for original_image in original_images:
inverted_image = invert_image(original_image)
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.imshow(original_image.squeeze().cpu().numpy(), cmap='gray')
plt.title('Original')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(inverted_image.squeeze().cpu().numpy(), cmap='gray')
plt.title('Inverted')
plt.axis('off')
plt.show()
OUTPUT:
20. Build a deep learning model to detect hate speech and offensive language in social media posts.

PROGRAM:
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Embedding, Dropout
import matplotlib.pyplot as plt

# Load your dataset (replace with actual dataset path)


# Assume the dataset has columns 'text' for the post content and 'label' for the hate speech classification
data = pd.read_csv("/content/hate_speech_dataset.csv") # Update with actual path

# Checking the dataset


print(data.head())

# Preprocessing the labels (encoding categorical labels: "hate", "offensive", "neutral")


label_encoder = LabelEncoder()
data['label'] = label_encoder.fit_transform(data['label'])

# Prepare the text data and labels


texts = data['text'].values
labels = data['label'].values

# Tokenize the text data


tokenizer = Tokenizer(num_words=10000) # Consider top 10,000 words
tokenizer.fit_on_texts(texts)
X = tokenizer.texts_to_sequences(texts)

# Padding sequences to have a fixed length


max_len = 100 # Maximum length of a post (you can adjust based on your data)
X = pad_sequences(X, maxlen=max_len)

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.2, random_state=42)

# Build the LSTM model


model = Sequential()
model.add(Embedding(input_dim=10000, output_dim=128, input_length=max_len))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(3, activation='softmax')) # Output layer for 3 categories: hate speech, offensive, neutral

# Compile the model


model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

# Train the model


history = model.fit(X_train, y_train, epochs=5, batch_size=64, validation_data=(X_test, y_test))

# Evaluate the model


loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {accuracy:.4f}")

# Plotting training and validation accuracy/loss


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

# Accuracy plot
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Test Accuracy')
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

# Loss plot
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Test Loss')
plt.title('Model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.show()

# Model Summary
model.summary()

# Example prediction function for a new social media post


def predict_hate_speech(post):
sequence = tokenizer.texts_to_sequences([post])
padded_sequence = pad_sequences(sequence, maxlen=max_len, padding='post')
predictions = model.predict(padded_sequence)
predicted_class = np.argmax(predictions, axis=-1)[0]
return label_encoder.inverse_transform([predicted_class])[0]

# Test the prediction function with an example post


sample_post = "the dog, barks loudly to alert its owner." # Example hate speech
print(f"Post: '{sample_post}' - Prediction: {predict_hate_speech(sample_post)}")
OUTPUT:

19. Create a deep learning model to identify fake news articles from real news.
PROGRAM:
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Embedding, Dropout
import matplotlib.pyplot as plt

# Create a dataset with text and labels


data = {
"text": [
"Breaking news: Government launches a new scheme for farmers.",
"Fake news alert: Alien spaceship lands in New York City.",
"Study finds that coffee increases productivity by 25%.",
"Hoax: Drinking water at night causes health problems.",
"Local mayor announces new measures to tackle traffic issues.",
"Scam alert: Lottery winner gives away money on social media.",
"New discovery: Scientists find cure for common cold.",
"False claim: Eating chocolate daily guarantees weight loss.",
"Breaking: Stock market hits record high this morning.",
"Fake news: Popular actor retires unexpectedly after winning award.",
"World's richest man gives away 90% of his fortune to charity.",
"Hoax: Facebook announces plan to charge users for posts.",
"Scientific breakthrough: New drug promises to cure cancer.",
"Breaking: Massive earthquake hits California, damages homes.",
"Fake news: World will end in 2025 due to asteroid collision.",
"Real: Governments around the world agree to new climate policies.",
"Exclusive: Celebrities caught in a major money laundering scandal.",
"Local bakery wins national award for best pastries.",
"New evidence suggests link between sleep and improved memory.",
"Hoax: Government plans to remove all private cars by 2030.",
"Breaking: Major tech company launches next-gen smartphone.",
"False claim: Drinking lemon juice can reverse aging process.",
"Study reveals: Regular exercise reduces the risk of heart disease.",
"Fake news: Zombies sighted in the streets of London.",
"Real: Research shows the benefits of meditation for mental health."
],
"label": [
"real", "fake", "real", "fake", "real", "fake", "real", "fake", "real", "fake",
"real", "fake", "real", "real", "fake", "real", "fake", "real", "real", "fake",
"real", "fake", "real", "fake","real"]}

# Convert the dataset into a pandas DataFrame


data = pd.DataFrame(data)
data.head()
# Checking the dataset
print("Dataset Overview:")
print(data.head())

# Preprocessing the labels (binary classification: 0 for real, 1 for fake)


label_encoder = LabelEncoder()
data['label'] = label_encoder.fit_transform(data['label'])
# Prepare the text data
texts = data['text'].values
labels = data['label'].values

# Tokenize the text data


tokenizer = Tokenizer(num_words=10000) # Consider top 10,000 words
tokenizer.fit_on_texts(texts)
X = tokenizer.texts_to_sequences(texts)

# Padding sequences to have a fixed length


max_len = 200 # Maximum length of a news article (you can adjust based on your data)
X = pad_sequences(X, maxlen=max_len)

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.2, random_state=42)

# Build the LSTM model


model = Sequential()
model.add(Embedding(input_dim=10000, output_dim=128, input_length=max_len))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid')) # Binary output (fake or real)

# Compile the model


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

# Train the model


history = model.fit(X_train, y_train, epochs=25, batch_size=64, validation_data=(X_test, y_test))

# Evaluate the model


loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {accuracy:.4f}")
# Plotting training and validation accuracy/loss
plt.figure(figsize=(12, 6))

# Accuracy plot
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Test Accuracy')
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

# Loss plot
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Test Loss')
plt.title('Model Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.show()

# Model Summary
model.summary()

# Example prediction function


def predict_news_article(article):
# Preprocess and tokenize the article
sequence = tokenizer.texts_to_sequences([article])
padded_sequence = pad_sequences(sequence, maxlen=max_len, padding='post')
# Make prediction (output will be a value between 0 and 1)
prediction = model.predict(padded_sequence)

if prediction >= 0.5:


return "Fake News"
else:
return "Real News"

# Test with an example article


sample_article = "A new study claims that drinking green tea daily can reduce the risk of cancer."
print(f"Article: {sample_article}\nPrediction: {predict_news_article(sample_article)}")

OUTPUT:

-----
10. Compare and contrast the performance of basic RNNs and LSTMs for language modelling. Use a
common dataset and highlight the strengths and weaknesses of each type of RNN architecture.
PROGRAM:
from keras.datasets import imdb
from keras.preprocessing.sequence import pad_sequences

# Load the dataset


num_words = 5000 # Use the top 5000 words
max_len = 500 # Maximum sequence length

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

# Pad sequences to ensure uniform length


x_train = pad_sequences(x_train, maxlen=max_len)
x_test = pad_sequences(x_test, maxlen=max_len)
from keras.models import Sequential
from keras.layers import Embedding, SimpleRNN, Dense, Dropout

# Build the RNN model


rnn_model = Sequential()
rnn_model.add(Embedding(input_dim=num_words, output_dim=32, input_length=max_len))
rnn_model.add(SimpleRNN(units=100, dropout=0.2))
rnn_model.add(Dense(1, activation='sigmoid'))

# Compile the model


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

# Train the model


rnn_history = rnn_model.fit(x_train, y_train, epochs=5, batch_size=64, validation_data=(x_test, y_test))

# Evaluate the model


rnn_loss, rnn_acc = rnn_model.evaluate(x_test, y_test)
print(f'Basic RNN Test Accuracy: {rnn_acc}')
from keras.layers import LSTM

# Build the LSTM model


lstm_model = Sequential()
lstm_model.add(Embedding(input_dim=num_words, output_dim=32, input_length=max_len))
lstm_model.add(LSTM(units=100, dropout=0.2, recurrent_dropout=0.2))
lstm_model.add(Dense(1, activation='sigmoid'))

# Compile the model


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

# Train the model


lstm_history = lstm_model.fit(x_train, y_train, epochs=5, batch_size=64, validation_data=(x_test, y_test))

# Evaluate the model


lstm_loss, lstm_acc = lstm_model.evaluate(x_test, y_test)
print(f'LSTM Test Accuracy: {lstm_acc}')
import matplotlib.pyplot as plt

# Plot training & validation accuracy values


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

plt.subplot(1, 2, 1)
plt.plot(rnn_history.history['accuracy'])
plt.plot(rnn_history.history['val_accuracy'])
plt.title('Basic RNN Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(['Train', 'Test'], loc='upper left')

plt.subplot(1, 2, 2)
plt.plot(lstm_history.history['accuracy'])
plt.plot(lstm_history.history['val_accuracy'])
plt.title('LSTM Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(['Train', 'Test'], loc='upper left')

plt.tight_layout()
plt.show()

OUTPUT:

Epoch 1/5
391/391 [==============================] - 30s 70ms/step - loss: 0.5537 - accuracy: 0.7134 -
val_loss: 0.4691 - val_accuracy: 0.7714
...
Epoch 1/15
391/391 [==============================] - 60s 153ms/step - loss: 0.5154 - accuracy: 0.7427 -
val_loss: 0.4292 - val_accuracy: 0.8080
...
Basic RNN Test Accuracy: 0.77
LSTM Test Accuracy: 0.82

You might also like