Dl lab answers batch 2
Dl lab answers batch 2
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
# 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),
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:
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
data = [
words = set()
tags = set()
words.update(sentence)
tags.update(tag_sequence)
# Pad sequences
random_state=42)
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'])
def predict_pos(sentence):
padding='post')
predictions = model.predict(padded_sequence)
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
import numpy as np
# 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
max_target_sequence_length = source_sequences.shape[1]
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')))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
# Convert target sequences to numpy array and reshape to add an additional dimension
# Translate a sentence
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)
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())]
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
# 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()
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
# 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()
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
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