0% found this document useful (0 votes)
46 views32 pages

RLDL PRACTICAL FILe

The document is a lab file for a course on Reinforcement Learning and Deep Learning at Bhagwan Parshuram Institute of Technology. It outlines the vision, mission, program educational objectives, specific outcomes, and common program outcomes for engineering graduates, along with a detailed index of practical experiments related to setting up environments, implementing algorithms, and utilizing various libraries. Each practical includes aims, source code, and outputs demonstrating the application of concepts in Python programming and machine learning frameworks.

Uploaded by

gkgk93005
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)
46 views32 pages

RLDL PRACTICAL FILe

The document is a lab file for a course on Reinforcement Learning and Deep Learning at Bhagwan Parshuram Institute of Technology. It outlines the vision, mission, program educational objectives, specific outcomes, and common program outcomes for engineering graduates, along with a detailed index of practical experiments related to setting up environments, implementing algorithms, and utilizing various libraries. Each practical includes aims, source code, and outputs demonstrating the application of concepts in Python programming and machine learning frameworks.

Uploaded by

gkgk93005
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

Bhagwan Parshuram

Institute of Technology

REINFORCEMENT LEARNING
& DEEP LEARNING
LAB FILE

Subject Code: ML-409P

Submitted to: Submitted By:


Ms. Vishaka Sehdev Verma Mohit Gusain
Assistant Professor (CSE) (08420802722)
CSE-B (7th Sem)
BHAGWAN PARSHURAM INSTITUTE OF TECHNOLOGY
Department of Computer Science &Engineering

Vision

To emerge as a centre of excellence, in the field of Computer Science and Engineering and research,
by grooming our pupils with strong conceptual knowledge to enable them as a professional and
researcher.

Mission

1. To inculcate self-motivation among the students, who can find and understand the need of the day.
2. To produce best quality professionals with strong conceptual knowledge and hands on experience.
3. To enable the students to be technically competent among their peers and serve as ethical software
professionals.
4. To facilitate industry interaction exposure for the benefit of stakeholder
5. To motivate faculties & students for their continuous improvement of their academic standards with
qualitative research.

Program Educational Objectives (PEOs)

PEO 1: To promulgate strong foundation in applied sciences, mathematics and engineering


fundamentals.
PEO2: To be able to comprehend, analyze and map the computational logics with real time problems
PEO3: To provide in extensive knowledge to design and build products with innovative solutions for
problems using their skills in Computer Science and Engineering field and other related domains.
PEO4: To inculcate self-confidence, ethics, teamwork skills, impart leadership skills, build
communication skills and life-long learning
PEO5: To succeed with excellence as computer professional or successful entrepreneurs or pursue
higher studies through quality education

Program Specific Outcomes (PSOs)

By the completion of Computer Science & Engineering program the student will have the following
Program Specific outcomes.
PSO1: Foundation of Computer System: Ability to comprehend mathematical science principles,
coupled with engineering specialization to analyze & design solutions to real world problems.
PSO2: Proficiency in Software Development Skills: Applying the concepts for building new innovations
with a wide range of programming languages and recent open source platforms, by upgrading with new
skills and techniques.
PSO3: Successful Career and Entrepreneurship: Ability to excel in his/her innovative career ethically
and engaging himself/herself professionally as an entrepreneur, software professional, pursue higher
studies with good communication and leadership skills, for the benefit of the society
PROGRAM OUTCOMES(POs) COMMON TO ALL BRANCHES

Engineering Graduates will be able to:

1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,


and an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information
to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports
and design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
BHAGWAN PARSHURAM INSTITUTE OF TECHNOLOGY
INDEX

Subject: Reinforcement Learning & Deep Learning Lab Branch: CSE-B Semester: 7th

Exp. Date Faculty


No. Objective of the Exp. Performed Evaluation Signature
1. Setting up the Spyder IDE Environment and 07/08/25
executing a Python Program.
2. Installing Keras, Tensorflow and Pytorch 14/08/25
libraries and making use of them.
3. Implement Q-learning with pure Python to play 21/08/25
a game
4. Implement deep Q-network with PyTorch. 26/08/25
5. Python implementation of iterative policy 04/09/25
evaluation and update.
6. Write a program to implement Chatbot using bi- 11/09/25
directional LSTMs.
7. Create a program for Image classification on 18/09/25
MNIST dataset (CNN model with fully
connected layer)
8. Write a program to Train a sentiment analysis 16/10/25
model on IMDB dataset, use RNN layers with
LSTM/GRU
9. Apply the Deep learning models in the field of 30/10/25
Natural Language Processing.
10. Applying the Convolution Neural Network on 06/11/25
computer vision problems.

Name: MOHIT GUSAIN Roll No.: 08420802722


Practical - 1
AIM: Setting up the Spyder IDE Environment and executing a Python
Program.

STEPS TO SET UP SPYDER:


1) Download and Install Spyder:
a) Open your browser and go to the official Spyder website:
[Link]
b) Download the Spyder 6.1.0 (64-bit) setup file.
c) Run the installer and follow the on-screen instructions:
o Click Next on the welcome screen.
o Accept the MIT License Agreement by clicking I Agree.
o Choose the destination folder (default is fine).
o Click Next, then Install.
d) Wait for installation to complete, then click Finish.
2) Start Spyder: Open Start menu and launch 'Spyder'.
3) Create/Open a file and run: Place your .py files in a folder, open them in Spyder,
press F5 (or Run button).
SOURCE CODE:
import sys, platform, subprocess, importlib, os
import warnings, contextlib
import numpy as np
import matplotlib
[Link]('Agg')
import [Link] as plt

SEED = 42
[Link](SEED)

packages = [
('numpy', 'np'), ('matplotlib', 'plt'), ('tensorflow', 'tf'), ('keras', 'keras'),
('torch', 'torch'), ('gymnasium', 'gym'), ('gym', 'gym_fallback')
]

def version(modname):
try:
# Suppress noisy INFO/warnings from TF/Gym during import
[Link]('TF_CPP_MIN_LOG_LEVEL', '2') # suppress TF INFO logs
with warnings.catch_warnings():
[Link]("ignore")
# Gym sometimes prints deprecation notes to stderr/stdout on import
devnull = open([Link], 'w')
if modname in ("gym", "gymnasium", "tensorflow", "keras"):
with contextlib.redirect_stderr(devnull), contextlib.redirect_stdout(devnull):
m = importlib.import_module(modname)
else:
with contextlib.redirect_stderr(devnull):
m = importlib.import_module(modname)
v = getattr(m, '__version__', 'unknown')
return v
except Exception as e:
return f"not installed ({e})"

def sample_plot():
x = [Link](0, 2*[Link], 400); y = [Link](x) * [Link](-0.1*x)
[Link](figsize=(8,4))
[Link](x, y, label='damped sine')
[Link]('Environment Check: Matplotlib Plot')
[Link]('x'); [Link]('y'); [Link](True, alpha=0.3); [Link]()
out = 'rldl_env_plot.png'
plt.tight_layout(); [Link](out, dpi=200)
print(f"Saved plot: {out}")

if __name__ == '__main__':
print(f"Python: {platform.python_version()} ({[Link]})")
print(f"Platform: {[Link]()}")
print('\nPackages:')
for name, _alias in packages:
print(f" - {name:12s}: {version(name)}")
a = [Link](3,3); u, s, vt = [Link](a)
print('\nNumPy demo: SVD singular values:', [Link](s, 3))
sample_plot()
print('\nDone.')

OUTPUT:
Practical - 2
AIM: Installing Keras, Tensorflow and Pytorch libraries and making use of
them.

SOURCE CODE:
import os, sys, time
[Link]['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import matplotlib
[Link]('Agg')
import [Link] as plt
import warnings
[Link]("ignore")

[Link](42)
# TensorFlow / Keras test
try:
import tensorflow as tf
from tensorflow import keras
print(f'TensorFlow: {tf.__version__}')
print(f'Keras: {keras.__version__}')

x = [Link](256, 10).astype('float32')
y = ([Link](256) > 0.5).astype('int32')

model = [Link]([
[Link](shape=(10,)),
[Link](16, activation='relu'),
[Link](1, activation='sigmoid')
])
[Link](optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
h = [Link](x, y, epochs=2, batch_size=32, verbose=0)
print('Keras smoke test accuracy:', float([Link]['accuracy'][-1]))
except Exception as e:
print('TensorFlow/Keras smoke test skipped:', e)

# PyTorch test
try:
import torch, [Link] as nn
print(f'PyTorch: {torch.__version__}')
class TinyNet([Link]):
def __init__(self):
super().__init__()
[Link] = [Link]([Link](10,16), [Link](), [Link](16,1))
def forward(self, x): return [Link](x)
X = [Link](256,10); y = ([Link](256,1)>0.5).float()
net = TinyNet(); opt = [Link]([Link](), lr=1e-2); loss = [Link]()
for _ in range(50):
opt.zero_grad(); out = net(X); l = loss(out, y); [Link](); [Link]()
with torch.no_grad():
acc = (([Link](net(X))>0.5) == (y>0.5)).float().mean().item()
print('PyTorch smoke test accuracy:', round(acc, 3))
except Exception as e:
print('PyTorch smoke test skipped:', e)

print('\nSaved simple figure for verification.')


xs = [Link](0,1,100); [Link](xs, xs**2, label='y=x^2'); [Link](); plt.tight_layout();
[Link]('frameworks_smoke_plot.png', dpi=160)
print('Done.')

OUTPUT:
Practical - 3
AIM: Implement Q-learning with pure Python to play a game
• Environment set up and intro to openAI Gym
• Write Q-learning algorithm and train agents to play game
• Watch trained agent play game

SOURCE CODE:
import numpy as np
import random
import os
[Link]['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
[Link]("ignore")

[Link](42); [Link](42)

# Try to import gymnasium first, fallback to gym


try:
import gymnasium as gym
new_api = True
except Exception:
import gym
new_api = False

env = [Link]('FrozenLake-v1', is_slippery=False)


state_space = env.observation_space.n
action_space = env.action_space.n

Q = [Link]((state_space, action_space))

alpha = 0.8 # learning rate


gamma = 0.95 # discount factor
epsilon = 1.0 # exploration rate
epsilon_min = 0.05
epsilon_decay = 0.995
episodes = 500
max_steps = 100

rewards = []

for ep in range(1, episodes+1):


if new_api:
s, _ = [Link]()
else:
s = [Link]()
total = 0
for t in range(max_steps):
a = env.action_space.sample() if [Link]() < epsilon else [Link](Q[s])
if new_api:
s2, r, term, trunc, _ = [Link](a)
done = term or trunc
else:
s2, r, done, _ = [Link](a)
# reward shaping for faster learning
if done and r == 0: r = -0.1
Q[s, a] = (1-alpha)*Q[s, a] + alpha*(r + gamma*[Link](Q[s2]))
s = s2; total += r
if done: break
epsilon = max(epsilon_min, epsilon*epsilon_decay)
[Link](total)
if ep % 50 == 0:
print(f'Episode {ep:4d} | Reward: {total:5.2f} | Epsilon: {epsilon:5.3f}')
print('\nTraining complete.')
[Link]('q_table_frozenlake.npy', Q)
print('Saved Q-table: q_table_frozenlake.npy')

def run_episode(render=False):
if new_api:
s, _ = [Link]()
else:
s = [Link]()
total = 0
for _ in range(max_steps):
a = int([Link](Q[s]))
if new_api:
s, r, term, trunc, _ = [Link](a)
done = term or trunc
else:
s, r, done, _ = [Link](a)
total += r
if render:
# text render when available
try:
[Link]()
except Exception:
pass
if done:
break
return total
wins = sum(run_episode() for _ in range(50))
print(f'Win count over 50 evaluation episodes: {int(wins)}')
print('Done.')
OUTPUT:

q_table_frozenlake.npy
Practical - 4
AIM: Implement deep Q-network with PyTorch.

SOURCE CODE:
import random, os
import numpy as np
import collections
try:
import gymnasium as gym
new_api = True
except Exception:
import gym
new_api = False

import torch
import [Link] as nn
import [Link] as F
device = [Link]('cuda' if [Link].is_available() else 'cpu')
class DQN([Link]):
def __init__(self, obs_dim, act_dim):
super().__init__()
[Link] = [Link](
[Link](obs_dim, 128), [Link](),
[Link](128, 128), [Link](),
[Link](128, act_dim)
)
def forward(self, x): return [Link](x)

class Replay:
def __init__(self, cap=5000):
[Link] = [Link](maxlen=cap)
def push(self, *ex): [Link](ex)
def sample(self, bs):
batch = [Link]([Link], bs)
s,a,r,s2,d = map([Link], zip(*batch))
return s,a,r,s2,d
def __len__(self): return len([Link])
seed = 42
[Link](seed); [Link](seed); torch.manual_seed(seed)
env = [Link]('CartPole-v1')
obs_dim = env.observation_space.shape[0]; act_dim = env.action_space.n
q = DQN(obs_dim, act_dim).to(device)
q_t = DQN(obs_dim, act_dim).to(device)
q_t.load_state_dict(q.state_dict())
opt = [Link]([Link](), lr=1e-3)
replay = Replay()
bs = 64; gamma = 0.99; eps, eps_min, eps_decay = 1.0, 0.05, 0.995
updates = 0; target_sync = 200

returns = []
def step_env(a):
if new_api:
s2, r, term, trunc, _ = [Link](a); return s2, r, term or trunc
else:
s2, r, done, _ = [Link](a); return s2, r, done

for ep in range(1, 61): # short training for demo


if new_api:
s, _ = [Link]()
else:
s = [Link]()
total = 0
while True:
a = env.action_space.sample() if [Link]() < eps else int([Link](q([Link](s,
dtype=torch.float32, device=device)).unsqueeze(0)).item())
s2, r, done = step_env(a)
[Link](s, a, r, s2, done)
s = s2; total += r
if done: break
if len(replay) >= bs:
S,A,R,S2,D = [Link](bs)
S = [Link](S, dtype=torch.float32, device=device)
A = [Link](A, dtype=[Link], device=device).unsqueeze(1)
R = [Link](R, dtype=torch.float32, device=device).unsqueeze(1)
S2 = [Link](S2, dtype=torch.float32, device=device)
D = [Link](D, dtype=torch.float32, device=device).unsqueeze(1)
qv = q(S).gather(1, A)
with torch.no_grad():
maxq = q_t(S2).max(1, keepdim=True)[0]
tgt = R + gamma * (1-D) * maxq
loss = F.mse_loss(qv, tgt)
opt.zero_grad(); [Link](); [Link]()
updates += 1
if updates % target_sync == 0: q_t.load_state_dict(q.state_dict())
eps = max(eps_min, eps*eps_decay)
[Link](total)
if ep % 10 == 0:
avg = [Link](returns[-10:])
print(f'Episode {ep:3d} | Return: {total:6.1f} | 10-ep avg: {avg:6.1f} | eps={eps:5.3f}')
print('\nTraining finished. Saving model to dqn_cartpole.pt')
[Link](q.state_dict(), 'dqn_cartpole.pt')

print('Done.')
OUTPUT:

dqn_cartpole.pt
Practical - 5
AIM: Python implementation of iterative policy evaluation and update.

SOURCE CODE:
import numpy as np

# Simple 4x4 Gridworld (Sutton & Barto style)


# States 0..15; terminal at 0 and 15; Actions: 0=U,1=R,2=D,3=L
# Reward -1 per step, 0 at terminal

n=4
S = n*n; A = 4
terminals = {0, S-1}

def step(s, a):


if s in terminals: return s, 0.0
r, c = divmod(s, n)
if a == 0: r = max(0, r-1)
if a == 2: r = min(n-1, r+1)
if a == 3: c = max(0, c-1)
if a == 1: c = min(n-1, c+1)
s2 = r*n + c
reward = 0.0 if s2 in terminals else -1.0
return s2, reward

P = [Link]((S, A, S))
R = [Link]((S, A), -1.0)
for s in range(S):
for a in range(A):
s2, r = step(s, a)
P[s,a,s2] = 1.0
R[s,a] = r

# Iterative Policy Evaluation for a given policy

def policy_evaluation(policy, gamma=1.0, theta=1e-5, max_iter=1000):


V = [Link](S)
for _ in range(max_iter):
delta = 0
for s in range(S):
v = V[s]
a = policy[s]
V[s] = sum(P[s,a,s2]*(R[s,a] + gamma*V[s2]) for s2 in range(S))
delta = max(delta, abs(v - V[s]))
if delta < theta: break
return V
# Policy Improvement

def policy_improvement(V, gamma=1.0):


policy = [Link](S, dtype=int)
for s in range(S):
q = [Link](A)
for a in range(A):
q[a] = sum(P[s,a,s2]*(R[s,a] + gamma*V[s2]) for s2 in range(S))
policy[s] = int([Link](q))
return policy

# Policy Iteration

def policy_iteration(gamma=0.99, max_iter=100):


policy = [Link](0, A, size=S)
stable = False; it = 0
while not stable and it < max_iter:
it += 1
V = policy_evaluation(policy, gamma)
new_policy = policy_improvement(V, gamma)
stable = np.array_equal(new_policy, policy)
policy = new_policy
print(f'Iteration {it}: policy stable = {stable}')
return policy, V

print('Running policy iteration ...')


policy, V = policy_iteration()

print('\nOptimal state values (reshaped 4x4):')


print([Link]([Link](n, n), 2))
print('\nOptimal policy (0=U,1=R,2=D,3=L):')
print([Link](n, n))
print('\nDone.')
OUTPUT:
Practical - 6
AIM: Write a program to implement Chatbot using bi-directional LSTMs.

SOURCE CODE:
import numpy as np
import tensorflow as tf
from [Link] import Model
from [Link] import Input, LSTM, Bidirectional, Dense, Embedding
import os
[Link]['TF_CPP_MIN_LOG_LEVEL'] = '3'
[Link]['TF_ENABLE_ONEDNN_OPTS'] = '0'
import warnings
[Link]("ignore")

print('='*70)
print('RLDL Practical 6: Chatbot using Bi-directional LSTMs (Keras)')
print('='*70)

questions = [
"hi",
"how are you",
"what is your name",
"where do you live",
"bye"
]

answers = [
"hello",
"i am fine thank you",
"i am a chatbot created with lstm",
"i live on your computer",
"goodbye"
]

from [Link] import Tokenizer


from [Link] import pad_sequences

tokenizer = Tokenizer(filters='', lower=True)


# Include <start> and <end> in tokenizer vocabulary
tokenizer.fit_on_texts(questions + answers + ["<start>", "<end>"])
vocab_size = len(tokenizer.word_index) + 1
print("Vocabulary size:", vocab_size)

encoder_input_data = tokenizer.texts_to_sequences(questions)
decoder_input_data = tokenizer.texts_to_sequences(["<start> " + t for t in answers])
decoder_target_data = tokenizer.texts_to_sequences([t + " <end>" for t in answers])
max_len = 10
encoder_input_data = pad_sequences(encoder_input_data, maxlen=max_len, padding='post')
decoder_input_data = pad_sequences(decoder_input_data, maxlen=max_len, padding='post')
decoder_target_data = pad_sequences(decoder_target_data, maxlen=max_len, padding='post')

embedding_dim = 64
latent_dim = 128

encoder_inputs = Input(shape=(None,))
x = Embedding(vocab_size, embedding_dim)(encoder_inputs)
encoder_outputs, forward_h, forward_c, backward_h, backward_c = Bidirectional(
LSTM(latent_dim, return_state=True)
)(x)

state_h = [Link]()([forward_h, backward_h])


state_c = [Link]()([forward_c, backward_c])

decoder_inputs = Input(shape=(None,))
decoder_embedding_layer = Embedding(vocab_size, embedding_dim)
decoder_embedding = decoder_embedding_layer(decoder_inputs)
decoder_lstm = LSTM(latent_dim * 2, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_embedding, initial_state=[state_h, state_c])
decoder_dense = Dense(vocab_size, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)

model = Model([encoder_inputs, decoder_inputs], decoder_outputs)


[Link](optimizer='adam', loss='sparse_categorical_crossentropy')
[Link]([encoder_input_data, decoder_input_data],
np.expand_dims(decoder_target_data, -1),
batch_size=2, epochs=300, verbose=0)
print(" Training complete!")

encoder_model = Model(encoder_inputs, [state_h, state_c])

decoder_state_input_h = Input(shape=(latent_dim * 2,))


decoder_state_input_c = Input(shape=(latent_dim * 2,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_inputs_infer = Input(shape=(None,))
decoder_emb_infer = decoder_embedding_layer(decoder_inputs_infer)
decoder_outputs2, state_h2, state_c2 = decoder_lstm(decoder_emb_infer,
initial_state=decoder_states_inputs)
decoder_outputs2 = decoder_dense(decoder_outputs2)
decoder_model = Model([decoder_inputs_infer] + decoder_states_inputs,
[decoder_outputs2, state_h2, state_c2])

reverse_word_index = {v: k for k, v in tokenizer.word_index.items()}


def decode_sequence(input_text):
seq = tokenizer.texts_to_sequences([input_text])
seq = pad_sequences(seq, maxlen=max_len, padding='post')
states_value = encoder_model.predict(seq)
target_seq = [Link]([[tokenizer.word_index['<start>']]])
decoded_sentence = ""
for _ in range(max_len):
output_tokens, h, c = decoder_model.predict([target_seq] + states_value)
sampled_token_index = [Link](output_tokens[0, -1, :])
sampled_word = reverse_word_index.get(sampled_token_index, '')
if sampled_word == '<end>' or sampled_word == '':
break
decoded_sentence += ' ' + sampled_word
target_seq = [Link]([[sampled_token_index]])
states_value = [h, c]
return decoded_sentence.strip()

while True:
user_input = input("You: ").lower()
if user_input in ['quit', 'exit', 'bye']:
print("Bot: Goodbye!")
break
print("Bot:", decode_sequence(user_input))
OUTPUT:
Practical - 7
AIM: Create a program for Image classification on MNIST dataset (CNN
model with fully connected layer)

SOURCE CODE:
import numpy as np
[Link](42)
import matplotlib
[Link]('Agg')
import [Link] as plt
import os
[Link]['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
[Link]("ignore")

print('='*70)
print('RLDL Practical 7: Image Classification on MNIST (Keras CNN)')
print('='*70)

from tensorflow import keras


from [Link] import layers

# Load MNIST
data = [Link]
(x_train, y_train), (x_test, y_test) = data.load_data()

x_train = x_train.astype('float32')/255.0
x_test = x_test.astype('float32')/255.0
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)

num_classes = 10

model = [Link]([
[Link](shape=(28,28,1)),
layers.Conv2D(32, 3, activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, activation='relu'),
layers.MaxPooling2D(),
[Link](),
[Link](128, activation='relu'),
[Link](num_classes, activation='softmax')
])

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


h = [Link](x_train, y_train, epochs=1, batch_size=128, validation_split=0.1, verbose=1)

loss, acc = [Link](x_test, y_test, verbose=0)


print(f'Test accuracy: {acc:.4f}')
[Link]('mnist_cnn.h5')
print('Saved model: mnist_cnn.h5')

# Plot a few predictions


pred = [Link](x_test[:25], verbose=0).argmax(axis=1)
[Link](figsize=(8,8))
for i in range(25):
[Link](5,5,i+1)
[Link](x_test[i].squeeze(), cmap='gray')
[Link](f'true={y_test[i]}, pred={pred[i]}')
[Link]('off')
plt.tight_layout(); [Link]('mnist_predictions_grid.png', dpi=200)
print('Saved: mnist_predictions_grid.png')
print('Done.')

OUTPUT:
Visualization of CNN Predictions on MNIST Test Images
Practical - 8
AIM: Write a program to Train a sentiment analysis model on IMDB
dataset, use RNN layers with LSTM/GRU

SOURCE CODE:
import numpy as np
[Link](42)
import matplotlib
[Link]('Agg')
import os
[Link]['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
[Link]("ignore")

print('='*70)
print('RLDL Practical 8: Sentiment Analysis on IMDB (LSTM/GRU)')
print('='*70)

from tensorflow import keras


from [Link] import layers

max_words = 10000
maxlen = 200

(x_train, y_train), (x_test, y_test) = [Link].load_data(num_words=max_words)

x_train = [Link].pad_sequences(x_train, maxlen=maxlen)


x_test = [Link].pad_sequences(x_test, maxlen=maxlen)

model = [Link]([
[Link](max_words, 64, input_length=maxlen),
[Link]([Link](64)),
[Link](64, activation='relu'),
[Link](1, activation='sigmoid')
])
[Link](optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
h = [Link](x_train[:10000], y_train[:10000], epochs=1, batch_size=128,
validation_split=0.1, verbose=1)
eval_loss, eval_acc = [Link](x_test[:5000], y_test[:5000], verbose=0)
print(f'Test accuracy (subset): {eval_acc:.4f}')
[Link]('imdb_lstm.h5')
print('Saved model: imdb_lstm.h5')

print('Done.')
OUTPUT:

Visualization of LSTM Model Predictions on IMDB Movie Reviews


Practical - 9
AIM: Apply the Deep learning models in the field of Natural Language
Processing.

SOURCE CODE:
import numpy as np
[Link](42)
import matplotlib
[Link]('Agg')
import os
[Link]['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
[Link]("ignore")

print('='*70)
print('RLDL Practical 9: NLP Text Classification with 1D CNN (Reuters)')
print('='*70)
from tensorflow import keras
from [Link] import layers
max_words = 10000
maxlen = 400

(x_train, y_train), (x_test, y_test) = [Link].load_data(num_words=max_words)


num_classes = [Link](y_train) + 1

x_train = [Link].pad_sequences(x_train, maxlen=maxlen)


x_test = [Link].pad_sequences(x_test, maxlen=maxlen)
model = [Link]([
[Link](max_words, 64, input_length=maxlen),
layers.Conv1D(128, 5, activation='relu'),
layers.MaxPooling1D(2),
layers.Conv1D(128, 5, activation='relu'),
layers.GlobalMaxPooling1D(),
[Link](128, activation='relu'),
[Link](num_classes, activation='softmax')
])
[Link](optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
h = [Link](x_train[:8000], y_train[:8000], epochs=1, batch_size=128,
validation_split=0.1, verbose=1)
eval_loss, eval_acc = [Link](x_test, y_test, verbose=0)
print(f'Test accuracy: {eval_acc:.4f}')
[Link]('reuters_text_cnn.h5')
print('Saved model: reuters_text_cnn.h5')

print('Done.')
OUTPUT:

Visualization of CNN-based text classification on the Reuters dataset.


Practical - 10
AIM: Applying the Convolution Neural Network on computer vision
problems.

SOURCE CODE:
import numpy as np
[Link](42)
import matplotlib
[Link]('Agg')
import os
[Link]['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
[Link]("ignore")
import [Link] as plt

print('='*70)
print('RLDL Practical 10: CNN on CIFAR-10 (Keras)')
print('='*70)

from tensorflow import keras


from [Link] import layers

(x_train, y_train), (x_test, y_test) = [Link].cifar10.load_data()

x_train = x_train.astype('float32')/255.0
x_test = x_test.astype('float32')/255.0
y_train = y_train.flatten(); y_test = y_test.flatten()

num_classes = 10

model = [Link]([
[Link](shape=(32,32,3)),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
[Link](),
[Link](128, activation='relu'),
[Link](num_classes, activation='softmax')
])
[Link](optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
h = [Link](x_train[:20000], y_train[:20000], epochs=1, batch_size=128, validation_split=0.1,
verbose=1)
loss, acc = [Link](x_test[:5000], y_test[:5000], verbose=0)
print(f'Test accuracy (subset): {acc:.4f}')
pred = [Link](x_test[:16], verbose=0).argmax(axis=1)
[Link](figsize=(8,6))
for i in range(16):
[Link](4,4,i+1)
[Link](x_test[i])
[Link](f'true={y_test[i]}, pred={pred[i]}', fontsize=8)
[Link]('off')
plt.tight_layout(); [Link]('cifar10_predictions_grid.png', dpi=180)
print('Saved: cifar10_predictions_grid.png')
print('Done.')

OUTPUT:

Visualization of CNN predictions on CIFAR-10 Dataset

You might also like