Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Deep Learning Lab-Rahul

The document is a lab manual for a Deep Learning course at Dronacharya Group of Institutions for the academic year 2024-2025. It includes various programming tasks such as designing perceptrons, artificial neural networks, convolutional neural networks, and implementing data augmentation techniques. Each task is accompanied by sample code and instructions for implementation using datasets like MNIST and CIFAR-10.

Uploaded by

monkey.panda07
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)
7 views23 pages

Deep Learning Lab-Rahul

The document is a lab manual for a Deep Learning course at Dronacharya Group of Institutions for the academic year 2024-2025. It includes various programming tasks such as designing perceptrons, artificial neural networks, convolutional neural networks, and implementing data augmentation techniques. Each task is accompanied by sample code and instructions for implementation using datasets like MNIST and CIFAR-10.

Uploaded by

monkey.panda07
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

DRONACHARYA GROUP OF INSTITUTIONS,

GREATER NOIDA

DEEP LEARNING Lab (KIT751A)


LAB MANUAL
Year :2024- 2025
Course Code :KCS078
Semester :VIIth
Branch :CSIT

Submitted By:
Rahul Raj Pandey
University Roll No – 2102300110042
Collage Roll no :16318

Submitted To:
Ms.Kajol Kathuria
(Assistant Professor)
S. No. Name of the Program

Design a single unit perceptron for classification of a linearly separable binary dataset
without using pre-defined models. Use the Perceptron() from sklearn.
1

Build an Artificial Neural Network by implementing the Backpropagation algorithm


2 and test the same using appropriate data sets. Vary the activation
functions used and compare the results.

Build a Deep Feed Forward ANN by implementing the Backpropagation algorithm and test
3 the same using appropriate data sets.

Design and implement an Image classification model to classify a dataset of images


4 using Deep Feed Forward NN Use the MNIST, CIFAR-10 datasets.

Design and implement a CNN model (with 2 layers of convolutions) to classify multi
5 category image datasets.

Design and implement a CNN model (with 2+ layers of convolutions) to classify


multi category image datasets. Use the concept of padding and Batch Normalization
6 while designing the CNN model.

Use the concept of Data Augmentation to increase the data size from a single image.
7

Implement RNN for sentiment analysis on movie reviews


8
1. Design a single unit perceptron for classification of a linearly separable binary
dataset without using pre-defined models. Use the Perceptron() from sklearn.

Program:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.linear_model import Perceptron
from mlxtend.plotting import plot_decision_regions
import tqdm

# Load the dataset


df = pd.read_csv('/content/placement.csv', encoding= 'unicode_escape')

# Prepare features and target variable


X = df.iloc[:, 0:2]
y = df.iloc[:, -1]

# Initialize and train the Perceptron model


p = Perceptron()
p.fit(X, y)

# Print model coefficients and intercept


print("Model coefficients:", p.coef_)
print("Model intercept:", p.intercept_)

# Calculate and display accuracy score


accuracy_score = p.score(X, y)
print("Accuracy score:", accuracy_score)

# Plot decision regions


plt.figure(figsize=(8, 6))
plot_decision_regions(X.values, y.values, clf=p, legend=2)
plt.title("Decision Boundary for Perceptron")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
OUTPUT:
2. Build an Artificial Neural Network by implementing the Backpropagation
algorithm and test the same using appropriate data sets. Vary the activation
functions used and compare the results.

Program:

import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense, Activation

# Load the Iris dataset


iris = datasets.load_iris()
X, y = datasets.load_iris(return_X_y=True)

# Split the data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40,
random_state=42)

# Define the neural network model


model = Sequential()
model.add(Dense(2, input_shape=(4,)))
model.add(Activation('sigmoid'))
model.add(Dense(1))
model.add(Activation('sigmoid'))

# Compile the model


model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])

# Print a summary of the Keras model


model.summary()

# Train the model


model.fit(X_train, y_train, epochs=5, batch_size=32)

# Evaluate the model on the test data


score = model.evaluate(X_test, y_test)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
OUTPUT :

3. Design and implement an Image classification model to classify a dataset of


images using Deep Feed Forward NN. Record the accuracy corresponding to the
number of epochs. Use the MNIST datasets

Program:

import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras import Input
from keras.layers import Dense
import pandas as pd
import numpy as np
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt

# Load the MNIST dataset


(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()

# Print dataset shapes


print("Shape of X_train:", X_train.shape)
print("Shape of y_train:", y_train.shape)
print("Shape of X_test:", X_test.shape)
print("Shape of y_test:", y_test.shape)

# Display the first 10 digits in the training set with their labels
fig, axs = plt.subplots(2, 5, figsize=(12, 6), facecolor='white')
n=0
for i in range(2):
for j in range(5):
axs[i, j].matshow(X_train[n], cmap='gray')
axs[i, j].set(title=f"Label: {y_train[n]}")
axs[i, j].axis('off')
n += 1
plt.show()

# Reshape and normalize input data


X_train = X_train.reshape(60000, 784).astype("float32") / 255
X_test = X_test.reshape(10000, 784).astype("float32") / 255

# Print reshaped dataset shapes


print("New shape of X_train:", X_train.shape)
print("New shape of X_test:", X_test.shape)

# Define the Deep Feedforward Neural Network


model = Sequential(name="DFF-Model")
model.add(Input(shape=(784,), name='Input-Layer'))
model.add(Dense(128, activation='relu', name='Hidden-Layer-1',
kernel_initializer='HeNormal'))
model.add(Dense(64, activation='relu', name='Hidden-Layer-2',
kernel_initializer='HeNormal'))
model.add(Dense(32, activation='relu', name='Hidden-Layer-3',
kernel_initializer='HeNormal'))
model.add(Dense(10, activation='softmax', name='Output-Layer'))

# Compile the model


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

# Print model summary


model.summary()

# Train the model


model.fit(X_train, y_train,
batch_size=10,
epochs=5,
verbose=1,
validation_split=0.2,
shuffle=True)

# Make predictions
pred_labels_tr = np.array(tf.math.argmax(model.predict(X_train), axis=1))
pred_labels_te = np.array(tf.math.argmax(model.predict(X_test), axis=1))

# Evaluate model performance


print("\n---------- Evaluation on Training Data ----------")
print(classification_report(y_train, pred_labels_tr))

print("\n---------- Evaluation on Test Data ----------")


print(classification_report(y_test, pred_labels_te))

OUTPUT:
4. Design and implement a CNN model (with 2 layers of convolutions) to classify
multi category image datasets. Use the MNIST, CIFAR-10 datasets.

Program:

# Program Number 4
import tensorflow as tf
from tensorflow.keras import layers, models, datasets
import matplotlib.pyplot as plt

# Load the dataset (Choose between MNIST and CIFAR-10)


dataset_name = "MNIST" # Change to "CIFAR-10" for CIFAR dataset

if dataset_name == "MNIST":
(X_train, y_train), (X_test, y_test) = datasets.mnist.load_data()
X_train = X_train.reshape(-1, 28, 28, 1) / 255.0
X_test = X_test.reshape(-1, 28, 28, 1) / 255.0
num_classes = 10
input_shape = (28, 28, 1)
elif dataset_name == "CIFAR-10":
(X_train, y_train), (X_test, y_test) = datasets.cifar10.load_data()
X_train = X_train / 255.0
X_test = X_test / 255.0
num_classes = 10
input_shape = (32, 32, 3)

# One-hot encode labels


y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)

# Define the CNN model


def create_cnn_model(input_shape, num_classes):
model = models.Sequential([
# 1st Convolutional Layer
layers.Conv2D(32, (3, 3), activation='relu', input_shape=input_shape),
layers.MaxPooling2D((2, 2)),

# 2nd Convolutional Layer


layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),

# Flatten and Dense layers


layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(num_classes, activation='softmax')
])
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model

# Create the CNN model


model = create_cnn_model(input_shape, num_classes)

# Train the model


epochs = 10
history = model.fit(X_train, y_train, validation_data=(X_test, y_test),
epochs=epochs, batch_size=32, verbose=1)

# Plot training and validation accuracy


plt.figure(figsize=(8, 6))
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Model Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()

# Evaluate the model


test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.2f}")
OUTPUT:
5. Design and implement a CNN model (with 4+ layers of convolutions) to classify
multi category image datasets. Record the accuracy corresponding to the
number of epochs. Use the Fashion MNIST datasets

Program:

import keras
from keras.datasets import fashion_mnist
from keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.utils import to_categorical
import matplotlib.pyplot as plt

# Load the Fashion MNIST dataset


(train_X, train_Y), (test_X, test_Y) = fashion_mnist.load_data()

# Reshape the data to include the channel dimension


train_X = train_X.reshape(-1, 28, 28, 1)
test_X = test_X.reshape(-1, 28, 28, 1)

# Normalize the data


train_X = train_X.astype('float32') / 255.0
test_X = test_X.astype('float32') / 255.0

# One-hot encode the labels


train_Y_one_hot = to_categorical(train_Y)
test_Y_one_hot = to_categorical(test_Y)

# Define the CNN model


model = Sequential()

# 1st Convolutional Layer


model.add(Conv2D(256, (3, 3), input_shape=(28, 28, 1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# 2nd Convolutional Layer


model.add(Conv2D(128, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# 3rd Convolutional Layer


model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))

# 4th Convolutional Layer


model.add(Conv2D(28, (3, 3)))
model.add(Activation('relu'))

# Flatten the output from the convolutional layers


model.add(Flatten())

# Fully Connected Layer


model.add(Dense(64, activation='relu'))

# Output Layer
model.add(Dense(10, activation='softmax'))

# Compile the model


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

# Model summary
model.summary()

OUTPUT:
6. Use the concept of Data Augmentation to increase the data size from a single
image.

Program:

# Program Number 7
from numpy import expand_dims
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot

# Load the image


img = image.load_img('/content/pexels-pixabay-45201.jpg')

# Convert to numpy array


data = image.img_to_array(img)

# Expand dimension to one sample


samples = expand_dims(data, 0)

# Create image data augmentation generator


datagen = ImageDataGenerator(width_shift_range=[-100, 100])

# Prepare iterator
it = datagen.flow(samples, batch_size=1)

# Generate samples and plot


for i in range(9):
# Define subplot
pyplot.subplot(330 + 1 + i)

# Generate batch of images


batch = next(it)

# Convert to unsigned integers for viewing


image = batch[0].astype('uint8')

# Plot raw pixel data


pyplot.imshow(image)

# Show the figure


pyplot.show()
OUTPUT:
7. Design and implement a CNN model to classify CIFAR10 image dataset. Use the
concept of Data Augmentation while designing the CNN model.

Program:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D

import matplotlib.pyplot as plt

# Load the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

print('x_train shape:', x_train.shape)


print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# Number of classes
num_classes = 10

# Convert class vectors to binary class matrices


y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)

# Normalize the data


x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

# Build the CNN model


model_1 = Sequential()
# 5x5 convolution with 2x2 stride and 32 filters
model_1.add(Conv2D(32, (5, 5), strides=(2, 2), padding='same',
input_shape=x_train.shape[1:]))
model_1.add(Activation('relu'))

# Another 5x5 convolution with 2x2 stride and 32 filters


model_1.add(Conv2D(32, (5, 5), strides=(2, 2)))
model_1.add(Activation('relu'))

# 2x2 max pooling


model_1.add(MaxPooling2D(pool_size=(2, 2)))
model_1.add(Dropout(0.25))
# Flatten layer
model_1.add(Flatten())
model_1.add(Dense(512))
model_1.add(Activation('relu'))
model_1.add(Dropout(0.5))

# Output layer
model_1.add(Dense(num_classes))
model_1.add(Activation('softmax'))

# Print model summary


model_1.summary()

# Batch size and optimizer


batch_size = 32
opt = tf.keras.optimizers.RMSprop(learning_rate=0.0005, decay=1e-6)

# Compile the model


model_1.compile(loss='categorical_crossentropy', optimizer=opt,
metrics=['accuracy'])

# Image data augmentation


datagen = ImageDataGenerator(
featurewise_center=False, # Don't set input mean to 0 over the dataset
samplewise_center=False, # Don't set each sample mean to 0
featurewise_std_normalization=False, # Don't divide inputs by std of the dataset
samplewise_std_normalization=False, # Don't divide each input by its std
zca_whitening=False, # Don't apply ZCA whitening
rotation_range=0, # Randomly rotate images in the range (0 to 180 degrees)
width_shift_range=0.1, # Randomly shift images horizontally
height_shift_range=0.1, # Randomly shift images vertically
horizontal_flip=True, # Randomly flip images horizontally
vertical_flip=False # Don't randomly flip images vertically
)

# Compute any required statistics from the training set


datagen.fit(x_train)

# Fit the model on the augmented data


model_1.fit(
datagen.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=5,
validation_data=(x_test, y_test)
)

# Evaluate the model


test_loss, test_acc = model_1.evaluate(x_test, y_test)
print('Test loss:', test_loss)
print('Test accuracy:', test_acc)
OUTPUT:
8. Implement RNN for sentiment analysis on movie reviews.

Program:

# program 8
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D

import matplotlib.pyplot as plt

# Load the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

print('x_train shape:', x_train.shape)


print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# Number of classes
num_classes = 10

# Convert class vectors to binary class matrices


y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)

# Normalize the data


x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

# Build the CNN model


model_1 = Sequential()

# 5x5 convolution with 2x2 stride and 32 filters


model_1.add(Conv2D(32, (5, 5), strides=(2, 2), padding='same',
input_shape=x_train.shape[1:]))
model_1.add(Activation('relu'))

# Another 5x5 convolution with 2x2 stride and 32 filters


model_1.add(Conv2D(32, (5, 5), strides=(2, 2)))
model_1.add(Activation('relu'))

# 2x2 max pooling


model_1.add(MaxPooling2D(pool_size=(2, 2)))
model_1.add(Dropout(0.25))
# Flatten layer
model_1.add(Flatten())
model_1.add(Dense(512))
model_1.add(Activation('relu'))
model_1.add(Dropout(0.5))

# Output layer
model_1.add(Dense(num_classes))
model_1.add(Activation('softmax'))
# Print model summary
model_1.summary()

# Batch size and optimizer


batch_size = 32
opt = tf.keras.optimizers.RMSprop(learning_rate=0.0005, decay=1e-6)

# Compile the model


model_1.compile(loss='categorical_crossentropy', optimizer=opt,
metrics=['accuracy'])

# Image data augmentation


datagen = ImageDataGenerator(
featurewise_center=False, # Don't set input mean to 0 over the dataset
samplewise_center=False, # Don't set each sample mean to 0
featurewise_std_normalization=False, # Don't divide inputs by std of the dataset
samplewise_std_normalization=False, # Don't divide each input by its std
zca_whitening=False, # Don't apply ZCA whitening
rotation_range=0, # Randomly rotate images in the range (0 to 180 degrees)
width_shift_range=0.1, # Randomly shift images horizontally
height_shift_range=0.1, # Randomly shift images vertically
horizontal_flip=True, # Randomly flip images horizontally
vertical_flip=False # Don't randomly flip images vertically
)

# Compute any required statistics from the training set


datagen.fit(x_train)

# Fit the model on the augmented data


model_1.fit(
datagen.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=5,
validation_data=(x_test, y_test)
)

# Evaluate the model


test_loss, test_acc = model_1.evaluate(x_test, y_test)
print('Test loss:', test_loss)
print('Test accuracy:', test_acc)
OUTPUT:

You might also like