0% found this document useful (0 votes)
4 views47 pages

Practical Deep Learning

The document outlines a series of practical exercises involving the implementation of Recurrent Neural Networks (RNNs) and Long Short-Term Memory (LSTM) networks for various tasks such as predicting the next letter in a word, next word in a sentence, and sentiment analysis on movie reviews. Each practical includes code snippets using TensorFlow and Keras, demonstrating how to preprocess data, build models, train them, and make predictions. The exercises also cover advanced topics like using text from PDF files for training and prediction.

Uploaded by

nepaliboiii00
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)
4 views47 pages

Practical Deep Learning

The document outlines a series of practical exercises involving the implementation of Recurrent Neural Networks (RNNs) and Long Short-Term Memory (LSTM) networks for various tasks such as predicting the next letter in a word, next word in a sentence, and sentiment analysis on movie reviews. Each practical includes code snippets using TensorFlow and Keras, demonstrating how to preprocess data, build models, train them, and make predictions. The exercises also cover advanced topics like using text from PDF files for training and prediction.

Uploaded by

nepaliboiii00
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

PRACTICAL 17:

17. Write a program to demonstrate a simple Recurrent Neural Network (RNN) that predicts the next
letter in a given word. Example “CONGRATULATIONS.”

import numpy as np
from [Link] import pad_sequences
from [Link] import Sequential
from [Link] import Embedding, SimpleRNN, Dense, Input
from [Link] import to_categorical

word = "CONGRATULATIONS"
chars = sorted(list(set(word)))
char_to_idx = {char: i for i, char in enumerate(chars)}
idx_to_char = {i: char for i, char in enumerate(chars)}
vocab_size = len(chars)

print(f"Target Word: {word}")


print(f"Character Vocabulary: {chars}")
print(f"Char to Index: {char_to_idx}")
print(f"Index to Char: {idx_to_char}")
print(f"Vocabulary Size: {vocab_size}")

sequences = []
for i in range(1, len(word)):
current_sequence = word[:i+1]
[Link]([char_to_idx[char] for char in current_sequence])

max_len = len(word)
sequences = pad_sequences(sequences, maxlen=max_len, padding='pre')
X = sequences[:, :-1]
y = sequences[:, -1]
print(f"\nShape of X (input sequences): {[Link]}")
print(f"Shape of y (target characters): {[Link]}")
print("\nExample X and y for the first few sequences:")
for i in range(min(5, len(X))):
print(f"X: {X[i]} (chars: {[idx_to_char[idx] for idx in X[i] if idx != 0]}), y: {y[i]} (char:
{idx_to_char[y[i]]})")

model = Sequential()
[Link](Input(shape=(max_len - 1,)))
[Link](Embedding(input_dim=vocab_size, output_dim=10))
[Link](SimpleRNN(units=32))
[Link](Dense(vocab_size, activation='softmax'))
[Link](optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
[Link]()

print("\nTraining the model...")


history = [Link](X, y, epochs=500, verbose=0)
print("Training complete!\n")

def predict_next_char(seed_text, n_chars=1):


generated_text = seed_text
for _ in range(n_chars):
token_list = [char_to_idx[char] for char in generated_text if char in char_to_idx]
token_list = pad_sequences([token_list], maxlen=max_len - 1, padding='pre')
probabilities = [Link](token_list, verbose=0)[0]
predicted_idx = [Link](probabilities)
if len(generated_text) + 1 > len(word) or idx_to_char[predicted_idx] not in chars:
break
generated_text += idx_to_char[predicted_idx]
return generated_text
print(f"Input: 'C' -> Predicted word segment: {predict_next_char('C', n_chars=5)}")
print(f"Input: 'CONG' -> Predicted word segment: {predict_next_char('CONG', n_chars=5)}")
print(f"Input: 'CONGRATUL' -> Predicted word segment: {predict_next_char('CONGRATUL',
n_chars=5)}")
print(f"Input: 'CONGRATULATION' -> Predicted word segment:
{predict_next_char('CONGRATULATION', n_chars=5)}")

print(f"\nFull word prediction starting from 'C': {predict_next_char('C', n_chars=len(word)-1)}")


OUTPUT:
PRACTICAL 18:
18. Write a program that implements a Recurrent Neural Network (RNN) to predict the next word in a
given sentence

import numpy as np
from [Link] import Tokenizer
from [Link] import pad_sequences
from [Link] import Sequential
from [Link] import Embedding,SimpleRNN,Dense
from [Link] import Input
text=[
"I love machine learning",
"I love deep learning",
"I enjoy learning",
"machine learning is fun"

]
tokenizer=Tokenizer()
tokenizer.fit_on_texts(text)
word_index=tokenizer.word_index
vocab_size=len(word_index)+1
sequences=[]
for line in text:
token_list=tokenizer.texts_to_sequences([line])[0]
for i in range(1,len(token_list)):
n_gram=token_list[:i+1]
[Link](n_gram)
max_len=max(len(seq) for seq in sequences)
sequences=pad_sequences(sequences,maxlen=max_len,padding='pre')
X=sequences[:,:-1]
y=sequences[:,-1]
model=Sequential()

[Link](Input(shape=(max_len-1,)))
[Link](Embedding(input_dim=vocab_size,output_dim=10))
[Link](SimpleRNN(units=32))
[Link](Dense(vocab_size,activation='softmax'))
[Link](optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
[Link]()
[Link](X,y,epochs=200,verbose=0)
def predict_next_word(seed_text):
token_list=tokenizer.texts_to_sequences([seed_text])[0]
token_list=pad_sequences([token_list],maxlen=max_len-1,padding='pre')
pred=[Link](token_list,verbose=0)
predicted_word=tokenizer.index_word[[Link](pred)]
return predicted_word
print("Input:I love->",predict_next_word("I love"))
print("Input:machine learning->",predict_next_word("machine learning"))
OUTPUT:
PRACTICAL 19:
19. Develop a program to implement a Recurrent Neural Network (RNN) that predicts words based
on a given paragraph.

import numpy as np
from [Link] import Tokenizer
from [Link] import pad_sequences
from [Link] import Sequential
from [Link] import Embedding, SimpleRNN, Dense, Input

paragraph = """
Recurrent neural networks (RNNs) are a class of neural networks that are good at processing
sequential data. They are particularly effective for natural language processing tasks, such as language
modeling, machine translation, and speech recognition. Unlike traditional feedforward neural
networks, RNNs have a 'memory' that allows them to use information from previous steps in the
sequence. This memory makes them suitable for tasks where the order of data matters. Training RNNs
can be challenging due to issues like vanishing and exploding gradients, but advanced architectures
like LSTMs and GRUs have largely addressed these problems. RNNs continue to be a fundamental
component in many state-of-the-art deep learning applications.
"""

paragraph = [Link]().replace('.', '').replace(',', '').replace('(', '').replace(')', '').replace('"', '')

tokenizer = Tokenizer()
tokenizer.fit_on_texts([paragraph])

word_index = tokenizer.word_index
vocab_size = len(word_index) + 1

print(f"Total unique words (vocabulary size): {vocab_size-1}")


print(f"Word to index mapping (first 10): {dict(list(word_index.items())[:10])}")

input_sequences = []
for line in [Link]('\n'):

token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i+1]
input_sequences.append(n_gram_sequence)

max_sequence_len = max(len(seq) for seq in input_sequences) if input_sequences else 1

padded_sequences = pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre')

X = padded_sequences[:, :-1]
y = padded_sequences[:, -1]

print(f"\nMax sequence length: {max_sequence_len}")


print(f"Shape of X (input sequences): {[Link]}")
print(f"Shape of y (target words): {[Link]}")
print("\nExample X and y for the first few sequences:")
for i in range(min(5, len(X))):
input_words = [tokenizer.index_word[idx] for idx in X[i] if idx != 0]
target_word = tokenizer.index_word[y[i]]
print(f"X: {input_words} -> y: {target_word}")

model = Sequential()
[Link](Input(shape=(max_sequence_len - 1,)))
[Link](Embedding(input_dim=vocab_size, output_dim=100))
[Link](SimpleRNN(units=128, return_sequences=False))
[Link](Dense(vocab_size, activation='softmax'))

[Link](optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])


[Link]()
print("\nTraining the model...")

history = [Link](X, y, epochs=100, verbose=1)


print("Training complete!")
def predict_next_words(seed_text, n_words, model, tokenizer, max_sequence_len):
generated_text = seed_text
for _ in range(n_words):
token_list = tokenizer.texts_to_sequences([generated_text])[0]

token_list = pad_sequences([token_list], maxlen=max_sequence_len - 1, padding='pre')


predicted_probabilities = [Link](token_list, verbose=0)[0]

predicted_word_idx = [Link](predicted_probabilities)
output_word = ""
if predicted_word_idx in tokenizer.index_word:
output_word = tokenizer.index_word[predicted_word_idx]
else:

break

generated_text += " " + output_word


return generated_text

seed_text_1 = "recurrent neural networks are"


predicted_output_1 = predict_next_words(seed_text_1, 5, model, tokenizer, max_sequence_len)
print(f"Seed: '{seed_text_1}' -> Predicted sequence: '{predicted_output_1}'")

seed_text_2 = "training rnns can"


predicted_output_2 = predict_next_words(seed_text_2, 4, model, tokenizer, max_sequence_len)
print(f"Seed: '{seed_text_2}' -> Predicted sequence: '{predicted_output_2}'")

seed_text_3 = "advanced architectures like"


predicted_output_3 = predict_next_words(seed_text_3, 3, model, tokenizer, max_sequence_len)
print(f"Seed: '{seed_text_3}' -> Predicted sequence: '{predicted_output_3}'")
OUTPUT:
PRACTICAL 20
20. Design and implement a Recurrent Neural Network (RNN) model for sentiment analysis on
movie reviews using the IMDB dataset.

from [Link] import imdb


from [Link] import Sequential
from [Link] import Dense,SimpleRNN,Embedding,Input
from [Link] import pad_sequences

num_words = 10000
maxlen = 50

(X_train,y_train),(X_test,y_test)=imdb.load_data(num_words=num_words)

X_train=pad_sequences(X_train,padding='post',maxlen=maxlen)
X_test=pad_sequences(X_test,padding='post',maxlen=maxlen)

model=Sequential()
[Link](Input(shape=(maxlen,)))
[Link](Embedding(input_dim=num_words, output_dim=32, input_length=maxlen))
[Link](SimpleRNN(32))
[Link](Dense(1,activation='sigmoid'))
[Link](optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
[Link]()
print("\nTraining the model...")
history = [Link](X_train, y_train, epochs=5, batch_size=64, validation_split=0.2, verbose=1)
print("Training complete!")
loss, accuracy = [Link](X_test, y_test, verbose=0)
print(f"\nTest Loss: {loss:.4f}")
print(f"Test Accuracy: {accuracy:.4f}")
word_to_id = imdb.get_word_index()
id_to_word = {value: key for key, value in word_to_id.items()}
def decode_review(text):
return ' '.join([id_to_word.get(i - 3, '?') for i in text])

def predict_sentiment(review_sequence, model, maxlen):


padded_review = pad_sequences([review_sequence], padding='post', maxlen=maxlen)
prediction = [Link](padded_review)[0][0]
sentiment = "Positive" if prediction >= 0.5 else "Negative"
return sentiment, prediction

sample_review_idx = 0
sample_review_sequence = X_test[sample_review_idx]
original_review_text = decode_review(sample_review_sequence)
true_sentiment = "Positive" if y_test[sample_review_idx] == 1 else "Negative"

predicted_sentiment, prediction_score = predict_sentiment(sample_review_sequence, model,


maxlen=50)

print(f"\n--- Sample Review Prediction ---")


print(f"Original Review (decoded): {original_review_text[:150]}...")
print(f"True Sentiment: {true_sentiment}")
print(f"Predicted Sentiment: {predicted_sentiment} (Score: {prediction_score:.4f})")
OUTPUT:
PRACTICAL 21
21. Develop a program to implement a Long Short-Term Memory (LSTM) network that predicts the
next word based on a given input sentence.

import numpy as np
from [Link] import Tokenizer
from [Link] import pad_sequences
from [Link] import Input,Embedding,LSTM,Dense
from [Link] import Sequential

text="deep learning models are powerful deep learning is useful"


tokenizer=Tokenizer()
tokenizer.fit_on_texts([text])

total_words=len(tokenizer.word_index)+1
input_sequences=[]
tokens=tokenizer.texts_to_sequences([text])[0]

for i in range(1,len(tokens)):
input_sequences.append(tokens[:i+1])
max_len=max(len(seq) for seq in input_sequences)
input_sequences=pad_sequences(input_sequences,maxlen=max_len,padding='pre')
X=input_sequences[:,:-1]
y=input_sequences[:,-1]

model=Sequential([
Input(shape=(max_len-1,)),
Embedding(total_words,10),
LSTM(20),
Dense(total_words,activation='softmax')
])
[Link](optimizer='adam',loss='sparse_categorical_crossentropy')
[Link]()
[Link](X,y,epochs=200,verbose=1)
def predict_next_word(seed_text):
seq=tokenizer.texts_to_sequences([seed_text])[0]
seq=pad_sequences([seq],maxlen=max_len-1,padding='pre')
pred=[Link](seq,verbose=0)
return tokenizer.index_word[[Link](pred)]

print("Input:deep learning")
print("Next word:",predict_next_word("deep learning"))
OUTPUT:
PRACTICAL 22
22. Develop an LSTM-based model that uses text extracted from an uploaded PDF file to predict the
next word in a given input sequence.

import numpy as np
from [Link] import Tokenizer
from [Link] import pad_sequences
from [Link] import Input,Embedding,LSTM,Dense
from [Link] import Sequential
from [Link] import to_categorical

text="""
Blockchain technology enables secure and decentralized transactions.
It ensures transparency and immutability of data.
Smart contracts automate processes without intermediaries.
"""
tokenizer=Tokenizer()
tokenizer.fit_on_texts([text])

total_words=len(tokenizer.word_index)+1
input_sequences=[]

for line in [Link]('\n'):


token_list=tokenizer.texts_to_sequences([line])[0]

for i in range(1,len(token_list)):
n_gram_seq=token_list[:i+1]
input_sequences.append(n_gram_seq)

max_seq_len=max(len(seq) for seq in input_sequences)


input_sequences=[Link](pad_sequences(input_sequences,maxlen=max_seq_len,padding='pre'))
X=input_sequences[:,:-1]
y=input_sequences[:,-1]
y=to_categorical(y,num_classes=total_words)
model=Sequential([
Input(shape=(max_seq_len-1,)),
Embedding(total_words,64),
LSTM(100),
Dense(total_words,activation='softmax')
])
[Link](optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
[Link]()

[Link](X,y,epochs=50,verbose=1)
def predict_next_word(seed_text):
token_list=tokenizer.texts_to_sequences([seed_text])[0]
token_list=pad_sequences([token_list],maxlen=max_seq_len-1,padding='pre')

predicted_pobs=[Link](token_list,verbose=0)
predicted_index=[Link](predicted_pobs)

for word,index in tokenizer.word_index.items():


if index==predicted_index:
return word

print("Input:Blockchain technology enables")


print("Next word:",predict_next_word("Blockchain technology enables"))
OUTPUT:
PRACTICAL 23
23. Modify the above program to generate and predict sentences or paragraphs by extracting and
learning from text content in an uploaded PDF file.

!pip install PyPDF2

from [Link] import files


import PyPDF2
import io

uploaded = [Link]()

pdf_text = ""
for filename in [Link]():
print(f'User uploaded file "{filename}" with length {len(uploaded[filename])} bytes')
pdf_file_obj = [Link](uploaded[filename])
pdf_reader = [Link](pdf_file_obj)
for page_num in range(len(pdf_reader.pages)):
page_obj = pdf_reader.pages[page_num]
pdf_text += page_obj.extract_text() + " "

pdf_text = pdf_text.lower()
pdf_text = ' '.join(pdf_text.split())

print(f"Extracted {len(pdf_text)} characters from the PDF.")


from [Link] import Tokenizer
from [Link] import pad_sequences
from [Link] import Sequential
from [Link] import Embedding, LSTM, Dense, Input
from [Link] import to_categorical
import numpy as np
tokenizer_pdf = Tokenizer()
tokenizer_pdf.fit_on_texts([pdf_text])

total_words_pdf = len(tokenizer_pdf.word_index) + 1

input_sequences_pdf = []

words = tokenizer_pdf.texts_to_sequences([pdf_text])[0]

for i in range(1, len(words)):


n_gram_sequence = words[:i+1]
input_sequences_pdf.append(n_gram_sequence)

max_seq_len_pdf = max(len(seq) for seq in input_sequences_pdf) if input_sequences_pdf else 1


input_sequences_pdf = [Link](pad_sequences(input_sequences_pdf, maxlen=max_seq_len_pdf,
padding='pre'))

X_pdf = input_sequences_pdf[:, :-1]


y_pdf = input_sequences_pdf[:, -1]
y_pdf = to_categorical(y_pdf, num_classes=total_words_pdf)

print(f"Total unique words (vocabulary size) from PDF: {total_words_pdf-1}")


print(f"Max sequence length from PDF: {max_seq_len_pdf}")
print(f"Shape of X_pdf (input sequences): {X_pdf.shape}")
print(f"Shape of y_pdf (target words - one-hot encoded): {y_pdf.shape}")

model_pdf = Sequential([
Input(shape=(max_seq_len_pdf - 1,)),
Embedding(total_words_pdf, 100),
LSTM(150),
Dense(total_words_pdf, activation='softmax')
])
model_pdf.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model_pdf.summary()

print("\nTraining the model with PDF text...")


model_pdf.fit(X_pdf, y_pdf, epochs=5, verbose=1)
print("Training complete!")

def generate_text_from_pdf(seed_text, next_words, model, tokenizer, max_sequence_len):


generated_text = seed_text
for _ in range(next_words):
token_list = tokenizer.texts_to_sequences([generated_text])[0]
token_list = pad_sequences([token_list], maxlen=max_sequence_len - 1, padding='pre')

predicted_probabilities = [Link](token_list, verbose=0)[0]


predicted_word_index = [Link](len(predicted_probabilities),
p=predicted_probabilities)

output_word = ""
for word, index in tokenizer.word_index.items():
if index == predicted_word_index:
output_word = word
break
if output_word == "":
break
generated_text += " " + output_word
return generated_text

seed_phrase_pdf = "blockchain technology"


num_words_to_generate = 20
print(f"\nGenerating text based on seed: '{seed_phrase_pdf}'")
predicted_paragraph = generate_text_from_pdf(seed_phrase_pdf, num_words_to_generate,
model_pdf, tokenizer_pdf, max_seq_len_pdf)
print(f"Generated text: {predicted_paragraph}")

seed_phrase_pdf_2 = "smart contracts"


num_words_to_generate_2 = 15

print(f"\nGenerating text based on seed: '{seed_phrase_pdf_2}'")


predicted_paragraph_2 = generate_text_from_pdf(seed_phrase_pdf_2, num_words_to_generate_2,
model_pdf, tokenizer_pdf, max_seq_len_pdf)
print(f"Generated text: {predicted_paragraph_2}")
OUTPUT:
PRACTICAL 23
23. Develop a program that demonstrates and compares the performance of Recurrent Neural
Network (RNN) and Long Short-Term Memory (LSTM) models using the same dataset, evaluating
them based on metrics such as accuracy, loss, and training efficiency

import numpy as np
from [Link] import Tokenizer
from [Link] import pad_sequences
from [Link] import Sequential
from [Link] import Embedding, SimpleRNN, LSTM, Dense
from [Link] import to_categorical
import [Link] as plt

text = """
Recurrent neural networks are powerful for sequence data.
LSTMs are a type of RNN that solve vanishing gradient problems.
Both can be used for text generation and prediction tasks.
LSTMs often outperform SimpleRNNs on long sequences.
"""

text = [Link]().replace('.', '').replace(',', '')


tokenizer = Tokenizer()
tokenizer.fit_on_texts([text])

total_words = len(tokenizer.word_index) + 1
input_sequences = []
for line in [Link]('\n'):
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i+1]
input_sequences.append(n_gram_sequence)
max_sequence_len = max(len(seq) for seq in input_sequences) if input_sequences else 1

padded_sequences = [Link](pad_sequences(input_sequences, maxlen=max_sequence_len,


padding='pre'))

X = padded_sequences[:, :-1]
y = padded_sequences[:, -1]
y_categorical = to_categorical(y, num_classes=total_words)

print(f"Total unique words: {total_words-1}")


print(f"Max sequence length: {max_sequence_len}")
print(f"Shape of X (input sequences): {[Link]}")
print(f"Shape of y (target words - categorical): {y_categorical.shape}")
rnn_model = Sequential([
Embedding(total_words, 10, input_length=max_sequence_len - 1),
SimpleRNN(50),
Dense(total_words, activation='softmax')
])

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


rnn_model.summary()

print("\nTraining SimpleRNN Model...")


rnn_history = rnn_model.fit(X, y_categorical, epochs=200, verbose=0)
print("SimpleRNN Training Complete!")
lstm_model = Sequential([
Embedding(total_words, 10, input_length=max_sequence_len - 1),
LSTM(50),
Dense(total_words, activation='softmax')
])
lstm_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
lstm_model.summary()

print("\nTraining LSTM Model...")


lstm_history = lstm_model.fit(X, y_categorical, epochs=200, verbose=0)
print("LSTM Training Complete!")
rnn_loss, rnn_accuracy = rnn_model.evaluate(X, y_categorical, verbose=0)
lstm_loss, lstm_accuracy = lstm_model.evaluate(X, y_categorical, verbose=0)

print("\n--- Model Performance Comparison ---")


print(f"SimpleRNN - Final Accuracy: {rnn_accuracy:.4f}, Final Loss: {rnn_loss:.4f}")
print(f"LSTM - Final Accuracy: {lstm_accuracy:.4f}, Final Loss: {lstm_loss:.4f}")

[Link](figsize=(12, 6))

[Link](1, 2, 1)
[Link](rnn_history.history['accuracy'], label='SimpleRNN Accuracy')
[Link](lstm_history.history['accuracy'], label='LSTM Accuracy')
[Link]('Training Accuracy Comparison')
[Link]('Epoch')
[Link]('Accuracy')
[Link]()

[Link](1, 2, 2)
[Link](rnn_history.history['loss'], label='SimpleRNN Loss')
[Link](lstm_history.history['loss'], label='LSTM Loss')
[Link]('Training Loss Comparison')
[Link]('Epoch')
[Link]('Loss')
[Link]()
plt.tight_layout()
[Link]()
def predict_next_word_comp(model, tokenizer, max_sequence_len, seed_text):
token_list = tokenizer.texts_to_sequences([seed_text])[0]
token_list = pad_sequences([token_list], maxlen=max_sequence_len - 1, padding='pre')
predicted_probabilities = [Link](token_list, verbose=0)[0]
predicted_word_idx = [Link](predicted_probabilities)
output_word = ""
if predicted_word_idx in tokenizer.index_word:
output_word = tokenizer.index_word[predicted_word_idx]
return output_word

print("\n--- Next Word Prediction Examples ---")


seed = "recurrent neural networks"
rnn_prediction = predict_next_word_comp(rnn_model, tokenizer, max_sequence_len, seed)
lstm_prediction = predict_next_word_comp(lstm_model, tokenizer, max_sequence_len, seed)
print(f"Seed: '{seed}'")
print(f"SimpleRNN predicts: '{rnn_prediction}'")
print(f"LSTM predicts: '{lstm_prediction}'")

seed = "lstms often"


rnn_prediction = predict_next_word_comp(rnn_model, tokenizer, max_sequence_len, seed)
lstm_prediction = predict_next_word_comp(lstm_model, tokenizer, max_sequence_len, seed)
print(f"\nSeed: '{seed}'")
print(f"SimpleRNN predicts: '{rnn_prediction}'")
print(f"LSTM predicts: '{lstm_prediction}'")
OUTPUT:
Practical 24
24. train an autoencoder that learns to encode input images into a lower-dimensional representation
and reconstruct them with minimal reconstruction error

import numpy as np
import tensorflow as tf
from [Link] import layers,Model
import [Link] as plt
(x_train,_),(x_test,_)=[Link].load_data()
x_train=x_train.astype("float32")/255.
x_test=x_test.astype("float32")/255.

x_train=np.expand_dims(x_train,axis=-1)
x_test=np.expand_dims(x_test,axis=-1)
print(x_test.shape)
class Autoencoder(Model):
def __init__(self, latent_dim):
super(Autoencoder, self).__init__()
[Link]=[Link]([
[Link](shape=(28,28,1)),
[Link](),
[Link](128,activation='relu'), # Added comma here
[Link](latent_dim,activation='relu')
])
[Link]=[Link]([
[Link](128,activation='relu'),
[Link](28*28,activation='sigmoid'),
[Link]((28,28,1))

])
def call(self,x):
encoded=[Link](x)
decoded=[Link](encoded)
return decoded
latent_dim=64
autoencoder=Autoencoder(latent_dim)
[Link](
optimizer='adam',
loss='mse'
)
history=[Link](
x_train, x_train,
epochs=5,
batch_size=256,
shuffle=True,
validation_data=(x_test,x_test)
)
encoded_imgs=[Link](x_test).numpy()
decoded_imgs=[Link](encoded_imgs).numpy()
n=10
[Link](figsize=(20,4))
for i in range(n):
ax=[Link](2,n,i+1)
[Link](x_test[i].reshape(28,28),cmap='gray')
[Link]('original')
[Link]('off')

ax=[Link](2,n,i+1+n)
[Link](decoded_imgs[i].reshape(28,28),cmap='gray')
[Link]('reconstructed')
[Link]('off')

[Link]()
OUTPUT:
PRACTICAL 25
25. Train a denoising autoencoder that learns to remove noise from images and reconstruct clean
images.

import numpy as np
import tensorflow as tf
from [Link] import fashion_mnist
#load dataset
(x_train,_),(x_test,_)=fashion_mnist.load_data()
print(x_train.shape)
x_train=x_train.astype("float32")/255.
x_test=x_test.astype("float32")/255.

x_train=np.expand_dims(x_train,-1)
x_test=np.expand_dims(x_test,-1)
print(x_train.shape)
noise_factor=0.2
x_train_noisy=x_train + noise_factor * [Link](shape=x_train.shape)
x_test_noisy=x_test + noise_factor * [Link](shape=x_test.shape)

x_train_noisy=tf.clip_by_value(x_train_noisy, clip_value_min=0., clip_value_max=1.)


x_test_noisy=tf.clip_by_value(x_test_noisy, clip_value_min=0., clip_value_max=1.)
import [Link] as plt
n=10
[Link](figsize=(20,2))
for i in range(n):
ax=[Link](1,n,i+1)
[Link]("original+ noise")
[Link]([Link](x_test_noisy[i]))
[Link]()
[Link]()
from [Link] import Model, layers
class Denoise(Model):
def __init__(self):
super(Denoise,self).__init__()
[Link]=[Link]([
[Link](shape=(28,28,1)),
layers.Conv2D(16,(3,3),activation='relu',padding='same',strides=2),
layers.Conv2D(8,(3,3),activation='relu',padding='same',strides=2)
])
[Link]=[Link]([
layers.Conv2DTranspose(8,kernel_size=3, strides=2, activation='relu' , padding='same'),
layers.Conv2DTranspose(16,kernel_size=3, strides=2, activation='relu' , padding='same'),
layers.Conv2D(1, kernel_size=(3,3),activation='sigmoid', padding='same')
])
def call(self,x):
encoded=[Link](x)
decoded=[Link](encoded)
return decoded
autoencoder=Denoise()
from [Link] import losses
[Link](optimizer='adam',loss=[Link]())
[Link](x_train_noisy,x_train,
epochs=3,
shuffle=True,
validation_data=(x_test_noisy,x_test))
[Link]()
[Link]()
decoded_imgs = [Link](x_test_noisy)
n=10
[Link](figsize=(20,4))
for i in range(n):
ax=[Link](2,n,i+1)
[Link]("original+noise")
[Link]([Link](x_test_noisy[i]))
[Link]()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

bx=[Link](2,n,i+n+1)
[Link]("reconstructed")
[Link]([Link](decoded_imgs[i]))
[Link]()
bx.get_xaxis().set_visible(False)
bx.get_yaxis().set_visible(False)
[Link]()
OUTPUT:
PRACTICAL 26

26. Train a GAN that can generate new realistic handwritten digit images similar to MNIST.

import numpy as np
import tensorflow as tf
from [Link] import layers
import [Link] as plt

# Load data
(x_train, _), (_, _) = [Link].load_data()

# Normalize [-1,1]
x_train = (x_train.astype('float32') - 127.5) / 127.5

# FIXED shape
x_train = np.expand_dims(x_train, axis=-1)

# 🔥 Reduce dataset for speed


x_train = x_train[:10000]

BUFFER_SIZE = 10000
BATCH_SIZE = 64

dataset = [Link].from_tensor_slices(x_train)\
.shuffle(BUFFER_SIZE).batch(BATCH_SIZE)

# Generator
def build_generator():
return [Link]([
[Link](256, input_dim=100),
[Link](0.2),
[Link](),
[Link](512),
[Link](0.2),
[Link](),

[Link](1024),
[Link](0.2),
[Link](),

[Link](28*28*1, activation='tanh'),
[Link]((28,28,1))
])

# Discriminator
def build_discriminator():
return [Link]([
[Link](input_shape=(28,28,1)),

[Link](512),
[Link](0.2),

[Link](256),
[Link](0.2),

[Link](1, activation='sigmoid')
])

generator = build_generator()
discriminator = build_discriminator()
# Loss
cross_entropy = [Link]()
def generator_loss(fake_output):
return cross_entropy(tf.ones_like(fake_output), fake_output)

def discriminator_loss(real_output, fake_output):


real_loss = cross_entropy(tf.ones_like(real_output), real_output)
fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
return real_loss + fake_loss

# Optimizers
generator_optimizer = [Link](1e-4)
discriminator_optimizer = [Link](1e-4)

# Train step (FIXED dynamic batch)


@[Link]
def train_step(images):
batch_size = [Link](images)[0]
noise = [Link]([batch_size, 100])

with [Link]() as gen_tape, [Link]() as disc_tape:


generated_images = generator(noise, training=True)

real_output = discriminator(images, training=True)


fake_output = discriminator(generated_images, training=True)

gen_loss = generator_loss(fake_output)
disc_loss = discriminator_loss(real_output, fake_output)

gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)


gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)

generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator,
discriminator.trainable_variables))

return gen_loss, disc_loss

# Training
EPOCHS = 10
noise_seed = [Link]([16, 100])

def generate_images(model, epoch):


predictions = model(noise_seed, training=False)
[Link](figsize=(4,4))

for i in range([Link][0]):
[Link](4,4,i+1)
[Link](predictions[i,:,:,0]*127.5 + 127.5, cmap='gray')
[Link]('off')

[Link]()

# Train loop
for epoch in range(EPOCHS):
for image_batch in dataset:
g_loss, d_loss = train_step(image_batch)

print(f"Epoch {epoch+1}, G Loss: {g_loss.numpy():.4f}, D Loss: {d_loss.numpy():.4f}")

# Show images every 5 epochs


if (epoch+1) % 5 == 0:
generate_images(generator, epoch)
OUTPUT:

You might also like