0% found this document useful (0 votes)
14 views6 pages

DNN for Regression and MNIST Classification

Uploaded by

bihija7556
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)
14 views6 pages

DNN for Regression and MNIST Classification

Uploaded by

bihija7556
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

Deep Neural Network (DNN) Assignment

Part A: DNN for Regression


Aim:
To implement a Deep Neural Network for a regression task using back-propagation.

Summary:
A DNN with multiple hidden layers was trained on a synthetic regression dataset.
ReLU activation was used in hidden layers and Linear activation in the output layer.
Mean Squared Error was used as the loss function.

Part B: DNN for Classification (MNIST)


Aim:
To implement a Deep Neural Network for handwritten digit classification using the MNIST
dataset.

Dataset:
MNIST consists of 70,000 grayscale images of handwritten digits (0–9).

Model:
Input layer: 784 neurons
Hidden layers: Dense layers with ReLU
Output layer: 10 neurons with Softmax

Loss Function:
Categorical Cross-Entropy

Optimizer:
Adam

Python Code (MNIST Classification)


from [Link] import mnist
from [Link] import Sequential
from [Link] import Dense, Flatten
from [Link] import to_categorical

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

X_train = X_train / 255.0


X_test = X_test / 255.0
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

model = Sequential([
Flatten(input_shape=(28,28)),
Dense(128, activation='relu'),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])

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

[Link](X_train, y_train, epochs=10, batch_size=32)


1️⃣ Import Required Libraries

import numpy as np

import [Link] as plt

from [Link] import mnist

from [Link] import Sequential

from [Link] import Dense, Flatten

from [Link] import to_categorical

2️⃣ Load MNIST Dataset

# Load dataset

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

print("Training data shape:", X_train.shape)

print("Testing data shape:", X_test.shape)

3️⃣ Data Preprocessing

# Normalize pixel values (0–255 → 0–1)

X_train = X_train / 255.0

X_test = X_test / 255.0

# One-hot encode labels

y_train = to_categorical(y_train, 10)

y_test = to_categorical(y_test, 10)

4️⃣ Build Deep Neural Network Model


model = Sequential()

# Input + Flatten layer

[Link](Flatten(input_shape=(28, 28)))

# Hidden layers

[Link](Dense(128, activation='relu'))

[Link](Dense(64, activation='relu'))

# Output layer

[Link](Dense(10, activation='softmax'))

5️⃣ Compile the Model

[Link](

optimizer='adam',

loss='categorical_crossentropy',

metrics=['accuracy']

6️⃣ Model Summary

[Link]()

7️⃣ Train the Deep Neural Network

history = [Link](

X_train,

y_train,
epochs=10,

batch_size=32,

validation_split=0.2

8️⃣ Evaluate the Model

test_loss, test_accuracy = [Link](X_test, y_test)

print("Test Loss:", test_loss)

print("Test Accuracy:", test_accuracy)

9️⃣ Plot Training & Validation Accuracy

[Link]([Link]['accuracy'], label='Training Accuracy')

[Link]([Link]['val_accuracy'], label='Validation Accuracy')

[Link]('Epochs')

[Link]('Accuracy')

[Link]('DNN MNIST Accuracy')

[Link]()

[Link]()

🔟 Plot Training & Validation Loss

[Link]([Link]['loss'], label='Training Loss')

[Link]([Link]['val_loss'], label='Validation Loss')

[Link]('Epochs')

[Link]('Loss')

[Link]('DNN MNIST Loss')


[Link]()

[Link]()

1️⃣1️⃣ Make Predictions

predictions = [Link](X_test)

# Convert probabilities to class labels

predicted_labels = [Link](predictions, axis=1)

true_labels = [Link](y_test, axis=1)

1️⃣2️⃣ Display Sample Predictions

for i in range(5):

[Link](X_test[i], cmap='gray')

[Link](f"True: {true_labels[i]} | Predicted: {predicted_labels[i]}")

[Link]('off')

[Link]()

✅ Expected Results (For Assignment)

Metric Value

Training Accuracy ~98%

Test Accuracy ~97–98%

Loss Low & stable

Overfitting Minimal

You might also like