Pullipalayam, Morur (Po.), Sankari (Tk.), Salem (Dt.
) - 637 304
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
RECORD NOTEBOOK
23PCS309 – DEEP LEARNING USING PYTHON
EVEN SEMESTER
(ACADEMIC YEAR: 2024-2025)
(REGULATION-2023)
NAME OF THE STUDENT:
REGISTER NUMBER :
\ YEAR/SEM :
COURSE/BRANCH :
RECORD NOTEBOOK
REGISTER NUMBER: .……………………..…………………
Certified that this is a Bonafide record of Practical work done by
Mr./Ms............................................................................................................................... of the II Semester
M.E. - Computer Science and Engineering Branch during the Academic year 2024-2025 Even
Semester in the 23PCS309 – Deep Learning Using Python Laboratory.
Signature of Faculty In-charge Signature of Head of Department
VISION AND MISSION OF THE INSTITUTE
VISION
To be an Institute of repute in the field of Engineering and Technology by
implementing the best educational practices akin to global standards for
fostering domain knowledge and developing research attitude among
students to make them globally competent.
MISSION
• Achieving excellence in Teaching Learning process using state-of-the-art
resources
• Extending opportunity to upgrade faculty knowledge and skills.
• Implementing the best student training practices for requirements of
industrial scenario of the state
• Motivating faculty and students in research activity for real time application
VISION AND MISSION OF THE DEPARTMENT
VISION
To create the holistic environment for the development of Computer Science and
Engineering Graduates employable at the global level and to mold them through
comprehensive educational programs and quality research for developing their
competency and innovation with moral values
MISSION
M1: Ensuring academic growth by way of establishing centers of excellence and
promoting collaborative learning
M2: Promoting research-based projects in emerging technologies for the benefit of
students and faculty
3
Program Outcomes (POs):
PO1: An ability to independently carry out research / investigation and
development work to solve practical problems.
PO2: An ability to write and present a substantial technical report/document.
PO3: Able to demonstrate a degree of mastery over the area of Computer Science
and Engineering.
PO4: Efficiently design, build and develop system application software for
distributed and centralized computing environments in varying domains and
platforms.
PO5: Understand the working of current Industry trends, the new hardware
architectures, the software components and design solutions for real world
problems by Communicating and effectively working with professionals in
various engineering fields and pursue research orientation for a lifelong
professional development in computer and automation arenas.
PO6: Model a computer-based automation system and design algorithms that
explore the understanding of the trade-offs involved in digital transformation.
Program Specific Objectives (PSOs)
PSO1: To design, analyse, apply & develop cutting edge technological solutions
for real time problems by applying the core concepts of Computer Science and
Engineering.
PSO2: To apply, analyse and employ cutting edge technologies, core engineering
principles and practices to create innovative research ideas for scientific and
business applications of Computer Science and Engineering society and Industry.
4
Program Educational Objective (PEOs)
PEO1: To apply the principles and practices of Computer Science and Engineering
encompassing Mathematics, Science and Basic Engineering and to employ the
modern engineering tools effectively in their profession with their world class
technical competence.
PEO2: To excel in the field of software industry or in higher studies endowed with
the spirit of Innovation and entrepreneurship by evolving their professional
knowledge on a lifelong basis.
PEO3: To practice the profession with ethics, integrity, leadership and social
responsibility with a good insight of the changing societal needs for the benefit of
humanity.
5
LIST OF EXPERIMENTS
Ex. Page
Date Experiment Marks Signature
No. No.
Vector and Matrix Operations using
1
NumPy
Gradient Descent for Linear
2
Regression
XOR Classification using Deep
3
Feedforward Neural Network
Optimizer Comparison on MNIST
4
Subset
Image Classification with CNN
5
(Convolutional Neural Network)
Sequence-to-Sequence Modeling using
6
RNN
Evaluate Classification Metrics Using
7
Scikit-learn
Grid Search & Random Search for
8
Hyperparameter Tuning
9 Implementing a Basic Autoencoder
Restricted Boltzmann Machine (RBM)
10
Using scikit-learn
6
Ex. No: 1 Vector and Matrix Operations using NumPy
Date:
Aim:
To perform basic linear algebra operations such as vector addition, scalar
multiplication, matrix multiplication, transpose, determinant, and inverse using
NumPy.
Algorithm:
1. Import the NumPy library.
2. Define a scalar value.
3. Declare two vectors using [Link].
4. Declare two 2×2 matrices using [Link].
5. Perform addition of the vectors.
6. Multiply the scalar with the vector.
7. Add the two matrices.
8. Multiply the scalar with one matrix.
9. Multiply two matrices using [Link].
10. Compute the transpose, determinant, and inverse of a matrix.
Program:
import numpy as np
# Scalars, Vectors, and Matrices
scalar = 5
vector_a = [Link]([1, 2, 3])
vector_b = [Link]([4, 5, 6])
matrix_a = [Link]([[1, 2], [3, 4]])
matrix_b = [Link]([[5, 6], [7, 8]])
# Operations
vector_sum = vector_a + vector_b
scalar_vector_mult = scalar * vector_a
matrix_sum = matrix_a + matrix_b
scalar_matrix_mult = scalar * matrix_a
matrix_mult = [Link](matrix_a, matrix_b)
transpose = matrix_a.T
7
determinant = [Link](matrix_a)
inverse = [Link](matrix_a)
# Display results
print("Vector Sum:", vector_sum)
print("Scalar * Vector:", scalar_vector_mult)
print("Matrix Sum:", matrix_sum)
print("Scalar * Matrix:", scalar_matrix_mult)
print("Matrix Multiplication:\n", matrix_mult)
print("Transpose of Matrix A:\n", transpose)
print("Determinant of Matrix A:", determinant)
print("Inverse of Matrix A:\n", inverse)
Sample Test Case:
Input:
scalar = 5
vector_a = [1, 2, 3]
vector_b = [4, 5, 6]
matrix_a = [[1, 2], [3, 4]]
matrix_b = [[5, 6], [7, 8]]
Output:
Vector Sum: [5 7 9]
Scalar * Vector: [ 5 10 15]
Matrix Sum: [[ 6 8]
[10 12]]
Scalar * Matrix: [[ 5 10]
[15 20]]
Matrix Multiplication:
[[19 22]
[43 50]]
Transpose of Matrix A:
[[1 3]
8
[2 4]]
Determinant of Matrix A: -2.0000000000000004
Inverse of Matrix A:
[[-2. 1. ]
[ 1.5 -0.5]]
Result:
Basic vector and matrix operations were successfully performed using NumPy,
demonstrating key linear algebra concepts.
9
Ex. No: 2 Gradient Descent for Linear Regression
Date:
Aim:
To implement stochastic gradient descent from scratch for a simple linear regression
problem.
Algorithm:
1. Import libraries.
2. Generate synthetic linear data (x and y).
3. Initialize weights and bias.
4. Define learning rate and number of epochs.
5. For each epoch:
a. Compute prediction.
b. Calculate error/loss.
c. Compute gradients.
d. Update weights and bias.
6. Track loss after each epoch.
7. Plot regression line.
8. Plot loss vs epochs.
9. Display final weights.
10. Predict new output using learned weights.
Program:
import numpy as np
import [Link] as plt
# Generate synthetic data
x = [Link](0, 10, 50)
y = 2 * x + 3 + [Link](50)
# Initialize parameters
w, b = 0, 0
lr = 0.01
epochs = 100
losses = []
# Training loop
for _ in range(epochs):
10
y_pred = w * x + b
error = y_pred - y
loss = [Link](error ** 2)
[Link](loss)
# Gradients
dw = [Link](2 * x * error)
db = [Link](2 * error)
# Update
w -= lr * dw
b -= lr * db
print(f"Final weights: w = {w:.2f}, b = {b:.2f}")
# Plotting
[Link](1, 2, 1)
[Link](x, y)
[Link](x, w*x + b, color='red')
[Link]("Linear Fit")
[Link](1, 2, 2)
[Link](losses)
[Link]("Loss vs Epochs")
[Link]()
Sample Test Case:
Output:
Final weights: w = 1.95, b = 3.18.
Result:
The model learned the underlying linear relationship using stochastic gradient
descent.
11
Ex. No: 3 XOR Classification using Deep Feedforward Neural Network
Date:
Aim:
To implement a deep feedforward neural network that learns the XOR function
using TensorFlow/Keras.
Algorithm:
1. Import TensorFlow and other necessary libraries.
2. Define input and output data for XOR logic.
3. Build a Sequential model with:
a. Input layer with 2 neurons
b. At least one hidden layer with ReLU activation
c. Output layer with Sigmoid activation
4. Compile the model with binary crossentropy loss and an appropriate
optimizer.
5. Train the model for sufficient epochs (e.g., 1000).
6. Plot loss curve to observe convergence.
7. Predict using trained model.
8. Evaluate model performance.
9. Display prediction results.
10. Interpret and compare results with expected XOR output.
Program:
import numpy as np
from [Link] import Sequential
from [Link] import Dense
import [Link] as plt
# Step 2: Define XOR inputs and outputs
x = [Link]([[0,0], [0,1], [1,0], [1,1]])
y = [Link]([[0], [1], [1], [0]])
# Step 3: Build model
model = Sequential()
[Link](Dense(4, input_dim=2, activation='relu'))
[Link](Dense(1, activation='sigmoid'))
# Step 4: Compile
[Link](optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
12
# Step 5: Train
history = [Link](x, y, epochs=1000, verbose=0)
# Step 6: Plot loss curve
[Link]([Link]['loss'])
[Link]("Loss Curve")
[Link]("Epochs")
[Link]("Loss")
[Link]()
# Step 7-9: Predictions and accuracy
predictions = [Link](x).round()
for i in range(len(x)):
print(f"Input: {x[i]} → Predicted: {int(predictions[i][0])} | Expected: {y[i][0]}")
Sample Test Case:
Output:
Input: [0 0] → Predicted: 0 | Expected: 0
Input: [0 1] → Predicted: 1 | Expected: 1
Input: [1 0] → Predicted: 1 | Expected: 1
Input: [1 1] → Predicted: 0 | Expected: 0
Result:
The neural network correctly learned the XOR function using a hidden layer
with non-linear activation.
13
Ex. No: 4 Optimizer Comparison on MNIST Subset
Date:
Aim:
To compare the performance of different optimization algorithms (SGD vs
Adam) on a simple neural network trained on a subset of the MNIST dataset.
Algorithm:
1. Import TensorFlow, Keras datasets, and plotting libraries.
2. Load the MNIST dataset and normalize the pixel values.
3. Use a small portion of the dataset (e.g., 10000 training samples).
4. Create a function to build a basic model architecture.
5. Train the model with SGD optimizer and record accuracy.
6. Train the model again with Adam optimizer.
7. Plot accuracy for both optimizers across epochs.
8. Compare convergence speed and final accuracy.
9. Evaluate on validation set.
10. Conclude on best optimizer for the setup.
Program:
import tensorflow as tf
from [Link] import Sequential
from [Link] import Dense, Flatten
from [Link] import mnist
import [Link] as plt
# Step 2: Load and preprocess
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Step 3: Subset data
x_train, y_train = x_train[:10000], y_train[:10000]
def build_model(optimizer):
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(64, activation='relu'),
14
Dense(10, activation='softmax')
])
[Link](optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
# Step 5: Train with SGD
model_sgd = build_model('sgd')
history_sgd = model_sgd.fit(x_train, y_train, epochs=10, verbose=0)
# Step 6: Train with Adam
model_adam = build_model('adam')
history_adam = model_adam.fit(x_train, y_train, epochs=10, verbose=0)
# Step 7: Plot comparison
[Link](history_sgd.history['accuracy'], label='SGD')
[Link](history_adam.history['accuracy'], label='Adam')
[Link]("Optimizer Comparison")
[Link]("Epochs")
[Link]("Training Accuracy")
[Link]()
[Link]()
Sample Test Case:
Output:
A plot will be shown comparing accuracy of both optimizers across 10 epochs.
Result:
Adam showed faster convergence and better accuracy compared to SGD on this
dataset and architecture.
15
Ex. No: 5 Image Classification with CNN (Convolutional Neural Network)
Date:
Aim:
To implement a Convolutional Neural Network (CNN) for handwritten digit
classification using the MNIST dataset.
.
Algorithm:
1. Import necessary libraries (TensorFlow/Keras, NumPy, Matplotlib).
2. Load the MNIST dataset.
3. Normalize pixel values to range [0, 1].
4. Reshape images to match input format expected by CNN.
5. Define a CNN with:
a. Convolution → ReLU → MaxPooling
b. Flatten → Dense → Softmax
6. Compile model using Adam optimizer and categorical cross-entropy.
7. Train the model for fixed epochs (e.g., 5–10).
8. Evaluate model on test data.
9. Visualize a few predictions.
10. Display accuracy and predicted labels.
Program:
import tensorflow as tf
import [Link] as plt
# Step 2-3: Load and normalize data
(x_train, y_train), (x_test, y_test) = [Link].load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Step 4: Reshape data to include channel dimension
x_train = x_train[..., [Link]]
x_test = x_test[..., [Link]]
# Step 5: Build CNN
model = [Link]([
[Link].Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
[Link].MaxPooling2D(2,2),
[Link](),
16
[Link](64, activation='relu'),
[Link](10, activation='softmax')
])
# Step 6-7: Compile and Train
[Link](optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
[Link](x_train, y_train, epochs=5, verbose=1)
# Step 8: Evaluate
loss, accuracy = [Link](x_test, y_test)
print(f"Test Accuracy: {accuracy:.4f}")
# Step 9-10: Predict and visualize
[Link](x_test[0].reshape(28,28), cmap='gray')
[Link](f"Predicted: {[Link](x_test[:1]).argmax()}")
[Link]()
Sample Test Case:
Output:
Epoch 1/5 ...
Test Accuracy: 0.9892
Result:
A convolutional model was successfully implemented to classify handwritten
digits with high accuracy.
17
Ex. No: 6 Sequence-to-Sequence Modeling using RNN
Date:
Aim:
To build a basic Recurrent Neural Network that learns to map sequences of characters
(e.g., "abc" → "bcd") using encoder-decoder style modeling.
Algorithm:
1. Import TensorFlow and preprocessing utilities.
2. Prepare paired input-output character sequences.
3. Convert characters to integer indices.
4. Tokenize and pad input/output sequences.
5. Define an encoder-decoder RNN model using LSTM layers.
6. Compile with sparse categorical cross-entropy and Adam optimizer.
7. Train the model on sequence pairs.
8. Predict output for test input.
9. Convert predicted tokens back to characters.
10. Evaluate accuracy manually and print input-output pairs.
Program:
import tensorflow as tf
import numpy as np
# Step 2-3: Sample data
pairs = [("abc", "bcd"), ("def", "efg"), ("ghi", "hij")]
chars = sorted(set("abcdefghijklmnopqrstuvwxyz"))
char2idx = {u:i for i, u in enumerate(chars)}
idx2char = [Link](chars)
def encode(seq):
return [char2idx[c] for c in seq]
X = [Link]([encode(p[0]) for p in pairs])
Y = [Link]([encode(p[1]) for p in pairs])
# Step 4: Model
model = [Link]([
[Link](input_dim=len(chars), output_dim=8),
[Link](16, return_sequences=True),
18
[Link](len(chars), activation='softmax')
])
# Step 6-7: Train
[Link](loss='sparse_categorical_crossentropy', optimizer='adam',
metrics=['accuracy'])
[Link](X, Y[..., [Link]], epochs=300, verbose=0)
# Step 8-10: Predict and decode
pred = [Link](X)
pred_seq = [Link](pred, axis=-1)
for i in range(len(pairs)):
print(f"Input: {pairs[i][0]} → Predicted: {''.join(idx2char[p] for p in pred_seq[i])}")
Sample Test Case:
Output:
Input: abc → Predicted: bcd
Input: def → Predicted: efg
Input: ghi → Predicted: hij
Result:
The RNN learned to map input sequences to their shifted versions, simulating
a character-level encoder-decoder system.
19
Ex. No: 7 Evaluate Classification Metrics Using Scikit-learn
Date:
Aim:
To compute and interpret performance metrics (accuracy, precision, recall, F1-score,
confusion matrix) on a classification model.
Algorithm:
1. Import required libraries from sklearn.
2. Load a built-in dataset (e.g., breast cancer, iris).
3. Split dataset into training/testing sets.
4. Train a simple classifier (e.g., RandomForestClassifier).
5. Predict on test set.
6. Compute accuracy, precision, recall, and F1-score.
7. Plot confusion matrix.
8. Display classification report.
9. Interpret metrics.
10. Conclude model strengths/weaknesses.
Program:
from [Link] import load_iris
from [Link] import RandomForestClassifier
from sklearn.model_selection import train_test_split
from [Link] import classification_report, confusion_matrix
import [Link] as plt
import seaborn as sns
# Load dataset
data = load_iris()
X, y = [Link], [Link]
# Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Train
model = RandomForestClassifier()
[Link](X_train, y_train)
20
# Predict
y_pred = [Link](X_test)
# Metrics
print("Classification Report:\n", classification_report(y_test, y_pred))
cm = confusion_matrix(y_test, y_pred)
# Confusion Matrix
[Link](cm, annot=True, fmt="d", cmap="Blues")
[Link]("Confusion Matrix")
[Link]()
Sample Input and Output:
Output:
precision recall f1-score
setosa 1.00 1.00 1.00
versicolor 0.87 1.00 0.93
virginica 1.00 0.86 0.92
Result:
Model performance was evaluated using a range of classification metrics and
visualized via confusion matrix.
21
Ex. No: 8 Grid Search & Random Search for Hyperparameter Tuning
Date:
Aim:
To apply Grid Search and Randomized Search to optimize hyperparameters for a
classification model.
Algorithm:
1. Load classification dataset.
2. Split into train/test.
3. Define parameter grid for a classifier.
4. Apply GridSearchCV with cross-validation.
5. Record best parameters and score.
6. Apply RandomizedSearchCV with cross-validation.
7. Compare best parameters and score.
8. Plot validation results.
9. Predict on test data using best estimator.
10. Display classification report and confusion matrix.
Program:
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from [Link] import RandomForestClassifier
from [Link] import load_iris
from [Link] import classification_report
from sklearn.model_selection import train_test_split
# Load and split
data = load_iris()
X_train, X_test, y_train, y_test = train_test_split([Link], [Link])
# Define model and parameter grid
model = RandomForestClassifier()
param_grid = {'n_estimators': [10, 50, 100],
'max_depth': [None, 5, 10]}
# Grid Search
grid = GridSearchCV(model, param_grid, cv=3)
22
[Link](X_train, y_train)
print("Best Grid Search Params:", grid.best_params_)
# Random Search
random = RandomizedSearchCV(model, param_grid, cv=3, n_iter=5)
[Link](X_train, y_train)
print("Best Random Search Params:", random.best_params_)
# Evaluate best model
y_pred = grid.best_estimator_.predict(X_test)
print("Classification Report (Grid):\n", classification_report(y_test, y_pred))
Sample Test Case:
Output:
Best Grid Search Params: {'max_depth': 5, 'n_estimators': 100}
Result:
Optimal hyperparameters were selected via grid/randomized search, and
model performance was significantly improved.
23
Ex. No: 9 Implementing a Basic Autoencoder
Date:
Aim:
To create a simple autoencoder for image reconstruction using MNIST dataset in
Keras.
Algorithm:
1. Load and normalize MNIST data.
2. Flatten 28×28 images into vectors.
3. Build a symmetrical autoencoder:
a. Encoder: Dense layers reduce dimension
b. Decoder: Dense layers restore input
4. Compile with binary cross-entropy and Adam.
5. Train model using images as both input and label.
6. Reconstruct test images.
7. Visualize original and reconstructed images.
8. Calculate reconstruction loss.
9. Interpret compression ability.
10. Save encoder part for reuse.
Program:
import tensorflow as tf
import [Link] as plt
# Load and preprocess
(x_train, _), (x_test, _) = [Link].load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.reshape(-1, 784)
x_test = x_test.reshape(-1, 784)
# Build autoencoder
model = [Link]([
[Link](64, activation='relu', input_shape=(784,)),
[Link](784, activation='sigmoid')
24
])
[Link](optimizer='adam', loss='binary_crossentropy')
[Link](x_train, x_train, epochs=5, verbose=1)
# Reconstruct
recon = [Link](x_test)
# Visualize
[Link](figsize=(6, 2))
for i in range(5):
[Link](2,5,i+1)
[Link](x_test[i].reshape(28,28), cmap="gray")
[Link]('off')
[Link](2,5,i+6)
[Link](recon[i].reshape(28,28), cmap="gray")
[Link]('off')
[Link]()
Sample Test Case:
Output:
Side-by-side display of original and reconstructed digits.
Result:
Autoencoder effectively learned compact representations of input images.
25
Ex. No: 10 Restricted Boltzmann Machine (RBM) Using scikit-learn
Date:
Aim:
To implement an RBM using the BernoulliRBM class and use it for feature
extraction.
Algorithm:
1. Load MNIST digits dataset using sklearn.
2. Normalize inputs.
3. Initialize BernoulliRBM with n-components.
4. Create a pipeline with RBM + logistic regression.
5. Train RBM using fit method.
6. Fit logistic regression on RBM-transformed features.
7. Predict labels and evaluate accuracy.
8. Compare with baseline model.
9. Display score metrics.
10. Visualize transformed features (optional with PCA).
Program:
from sklearn.neural_network import BernoulliRBM
from [Link] import Pipeline
from sklearn.linear_model import LogisticRegression
from [Link] import load_digits
from sklearn.model_selection import train_test_split
from [Link] import MinMaxScaler
from [Link] import classification_report
# Load data
digits = load_digits()
X = MinMaxScaler().fit_transform([Link])
y = [Link]
# Train/Test split
26
X_train, X_test, y_train, y_test = train_test_split(X, y)
# Build RBM pipeline
rbm = BernoulliRBM(n_components=64)
logreg = LogisticRegression(max_iter=1000)
classifier = Pipeline(steps=[('rbm', rbm), ('logistic', logreg)])
# Train and evaluate
[Link](X_train, y_train)
y_pred = [Link](X_test)
print("Classification Report:\n", classification_report(y_test, y_pred))
Sample Test Case:
Output:
Classification Report:
precision recall f1-score
0 1.00 1.00 1.00
1 0.97 1.00 0.98
...
Result:
RBM successfully transformed data for feature extraction, improving classification
accuracy.
27