0% found this document useful (0 votes)
16 views23 pages

RLDL

deep learning

Uploaded by

Somya Sharma
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)
16 views23 pages

RLDL

deep learning

Uploaded by

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

Experiment-1

Aim:- Setting up the Spyder IDE Environment and Executing a Python Program

Source Code:
Steps:
Steps to Install and Use Spyder on Fedora:
1. Installation:
 Open the terminal.
 Update the Fedora package list:

Install Spyder using the following command:

2. Launching Spyder:
 Launch Spyder by typing the following in the terminal:

3. Creating a Python Script:


 In the Spyder editor, create a new Python script file.
 Type the following simple Python code:

4. Executing the Program:


 Run the script by pressing the 'Run' button in the Spyder toolbar or by pressing F5
on your keyboard.

11
Output:

The Spyder IDE should open without issues, and the Python script should execute, displaying
"Hello, World!" in the console output.

12
Experiment-2

Aim:- Installing Keras, Tensorflow and Pytorch libraries and making use of them.

Source Code:
Steps:
1. Installation:
 Open a terminal.
 Ensure Python and pip are installed:
o Check Python version:

Check pip version:

Install TensorFlow:

Keras Installation:
 Keras is already included in TensorFlow, so a separate installation isn't
needed. Install PyTorch:
 Visit the official PyTorch website and select the appropriate command based on your
system configuration (OS, package manager, CUDA version, etc.). For example:

2. Verification:
After installation, you can verify both TensorFlow (with Keras) and PyTorch by running the
following Python script:
Verification Script:

13
3. Running the Script:
 Save the above Python script in a file (e.g., verify_libraries.py).
 Run the script in your terminal:

Output:

14
Successful installation messages for each library.
Printed version numbers for TensorFlow and PyTorch.
Confirmation messages that models and tensors are successfully created and displayed.

15
Experiment-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 agent to play game
 Watch trained agent play game

Source Code:
Steps:
1. Environment Setup and Introduction to OpenAI Gym
1. Install OpenAI Gym:
o Install the gym library using pip.

2. Import OpenAI Gym and Initialize an Environment:

2. Writing the Q-learning Algorithm


1. Initialize the Q-table with Zeros:
o The Q-table stores the Q-values for each state-action pair. For continuous state
spaces like CartPole, we discretize the state space.

2. Discretizing Continuous States:

20
3. Q-learning Algorithm:
 For each episode, initialize the state and repeatedly choose actions based on the
current state, update the Q-table, and repeat until the episode ends.
 The Q-values are updated using the Bellman equation:
Q(state, action) = Q(state, action) + α * [reward + γ * max(Q(next_state, all_actions))
- Q(state, action)]

3. Training the Agent to Play the Game


1. Set the Number of Episodes for Training:
o In the previous code, episodes = 10000 specifies the number of training
episodes.
2. Monitor the Training Process:
o During training, every 100 episodes, the progress (e.g., total reward) is printed
to the console to track improvements.

4. Watching the Trained Agent Play the Game


1. Simulate the Game with the Trained Agent:

20
Output:
Training Output:

Agent Performance:

20
Experiment-4

Aim:- Implement deep Q‐network with PyTorch

Source Code:
Steps:
1. Environment Setup and PyTorch Installation
1. Install PyTorch and OpenAI Gym

2. Initializing the DQN Model


1. Define a Neural Network in PyTorch:
o Create a class for the DQN model that takes the state as input and outputs
action values.

3. Define the Training Process


1. Initialize the Environment and DQN Model:

29
2. Epsilon-Greedy Strategy for Action Selection:

3. Collect Experience and Store in Replay Buffer:

4. Sample from the Buffer and Train the Network:

4. Training the Network


1. Training Loop:

5. Evaluating the Agent

21
0
1. Test the Trained Model:

Output:
Training Output:
 You will see the rewards per episode, with rewards generally increasing over time as
the agent learns.

Evaluation Output:
 After training, the agent should perform well in the environment, maintaining high
rewards.

21
1
Experiment-5

Aim:- Python implementation of the iterative policy evaluation and update

Source Code:
Steps:
1. Environment Setup:

2. Define the Policy:

3. Implement Iterative Policy Evaluation


1. Initialize the Value Function:

2. Iterative Policy Evaluation:

21
2
4. Policy Update (Improvement)
1. Greedy Policy Improvement:

5. Convergence Check and Execution


1. Iterative Process:

2. Testing the Implementation:

21
3
Output:

Value Function: Displays the expected value for each state under the policy.
Improved Policy: Shows the best action to take in each state to maximize the expected return.

30
Experiment-6

Aim:- Chatbot using bi‐directional LSTMs

Source Code:
Steps:
1. Environment and Library Setup:

2. Data Preparation: For simplicity, we'll use a small dataset and preprocess it. In practice,
you'd use a larger conversational dataset.

3. Model Design: Design a Bi-LSTM model using TensorFlow:

31
5
4. Training the Model: Compile and train the model:

5. Evaluation and Testing


Evaluate the model using sample dialogues:

6. Integration
For a real-time chat interface, you would integrate this model into a web application or chat
platform. Here’s a simplified approach for integration:

31
6
Output:
Training Output:
During training, you'll see the progress of the loss and accuracy metrics.
Response:
For the input "What is your name?", you might get an output like:

Chat Interface:
In the chat interface, you would interact with the chatbot in real-time.

31
7
Experiment-7

Aim:- Image classification on MNIST dataset (CNN model with fully connected layer)

Source Code:
Steps:
1. Environment and Library Setup
First, install TensorFlow:

2. Data Preparation
Load and preprocess the MNIST dataset:

3. Model Design
Define the CNN model architecture:

31
8
4. Training the Model
Compile and train the model:

5. Evaluation and Testing


Evaluate the model on the test set and print the results:

Output:
Model Summary: The model summary will show the architecture of the CNN, including the
number of parameters in each layer.
Training Output: During training, you'll see the progress of loss and accuracy metrics for
both training and validation sets.
Evaluation Output:

Prediction Output:

31
9
Experiment – 8

Aim: Train a sentiment analysis model on IMDB dataset, use RNN layers with LSTM/GRU

Source Code:
Steps:
Environment and Library Setup:
 Install necessary Python libraries such as TensorFlow or PyTorch, and Keras for high-
level neural network API: pip install tensorflow keras
Data Preparation:
 Load the IMDB dataset, available directly via Keras datasets.
 Preprocess the data: tokenize the text and convert it to sequences. Use padding to ensure all
sequences are of the same length.
Model Design:
 Design an RNN model using either LSTM or GRU layers:
 Start with an embedding layer to convert review integers into dense vectors.
 Add LSTM or GRU layers to learn the dependencies in the review text.
 Include a fully connected layer after the LSTM/GRU layers to interpret the features.
 End with a sigmoid activation function to output the sentiment as a
probability. Training the Model:
 Compile the model using binary cross-entropy loss and an appropriate optimizer like
Adam.
 Fit the model on the training data and validate it using the test set.
Evaluation and Testing:
 Evaluate the model’s performance on the test set using accuracy and loss metrics.
 Use the model to predict sentiment on new movie reviews to test its generalization
capabilities.

52
0
import tensorflow as tf
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense

# Load IMDB dataset


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

# Preprocess data
x_train = sequence.pad_sequences(x_train, maxlen=500)
x_test = sequence.pad_sequences(x_test, maxlen=500)

# Build LSTM model


model = Sequential()
model.add(Embedding(input_dim=10000, output_dim=64, input_length=500))
model.add(LSTM(64))
model.add(Dense(1, activation='sigmoid'))

# Compile and train the model


model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5, batch_size=32)

# Evaluate
loss, accuracy = model.evaluate(x_test,
y_test) print(f"Test Accuracy: {accuracy}")

Output:
A sentiment analysis model trained to classify the sentiments of movie reviews as positive
or negative with a reported accuracy on the test data.

52
1
Experiment – 9

Aim: Applying the Deep Learning Models in the field of Natural Language Processing

Source Code:

import tensorflow as tf
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 Embedding, LSTM, Dense

# Sample text data (replace with real NLP data)


texts = ["Deep learning is amazing", "Natural Language Processing is fun"]
tokenizer = Tokenizer(num_words=1000)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)

# Pad sequences
data = pad_sequences(sequences, maxlen=10)

# Build LSTM model for NLP


model = Sequential()
model.add(Embedding(input_dim=1000, output_dim=64, input_length=10))
model.add(LSTM(64))
model.add(Dense(1, activation='sigmoid'))

# Compile and train the model


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

Output:
 A well-trained deep learning model capable of performing the selected NLP
task with high accuracy and efficiency.
 Demonstrable results on real-world data showing the model's application in
practical scenarios.

60
Experiment – 10

Aim: Applying the Convolution Neural Network on computer vision problems

Source Code:

import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# Load CIFAR-10 dataset (for computer vision tasks)


(x_train, y_train), (x_test, y_test) =
cifar10.load_data()

# Preprocess data
x_train = x_train.astype('float32') /
255.0 x_test = x_test.astype('float32') /
255.0

# Build CNN model


model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

# Compile and train the model


model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=32)

# Evaluate
loss, accuracy = model.evaluate(x_test,

Output:

62
3

You might also like