0% found this document useful (0 votes)
2 views4 pages

Deep Learning Examples With Code

The document provides two examples of deep learning models using the MNIST dataset. The first example demonstrates binary classification to determine if a digit is above or below 5, while the second example focuses on multi-class classification to recognize digits from 0 to 9. Both examples include steps for data preprocessing, model building, compilation, training, and evaluation, achieving high accuracy rates of 99% and 98%, respectively.

Uploaded by

nitikasehgal0602
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)
2 views4 pages

Deep Learning Examples With Code

The document provides two examples of deep learning models using the MNIST dataset. The first example demonstrates binary classification to determine if a digit is above or below 5, while the second example focuses on multi-class classification to recognize digits from 0 to 9. Both examples include steps for data preprocessing, model building, compilation, training, and evaluation, achieving high accuracy rates of 99% and 98%, respectively.

Uploaded by

nitikasehgal0602
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

Basic Deep Learning Examples with

Explanations
Example 1: Binary Classification (Above or Below 5)
In this example, we create a simple deep learning model that classifies whether a digit is
greater than 5 or not (i.e., above or below 5). We use the MNIST dataset which contains
images of handwritten digits from 0 to 9.

Steps in the code:

1. We load the MNIST dataset which contains 28x28 grayscale images of handwritten digits.

2. We normalize the pixel values of the images by dividing them by 255 to scale them
between 0 and 1.

3. We modify the labels to be binary: 1 if the digit is greater than 5, and 0 if the digit is 5 or
below.

4. We build a simple neural network model with the following layers:

- Flatten: Converts the 28x28 image into a 1D list of 784 pixels.

- Dense (128 units): A hidden layer with 128 neurons using ReLU activation function.

- Dense (1 unit): Output layer with a sigmoid activation function, outputting a value
between 0 and 1.

5. We compile the model using the Adam optimizer and binary cross-entropy loss function
since this is a binary classification problem.

6. We train the model for 5 epochs on the training data.

7. After training, we evaluate the model's accuracy on the test data.

Step-by-Step Code:

import tensorflow as tf
from tensorflow import keras
import numpy as np

# Load MNIST dataset


mnist = [Link]
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Preprocess: Normalize data (scale pixel values to 0-1)
x_train, x_test = x_train / 255.0, x_test / 255.0

# Simplify the problem: Only keep numbers above or below 5


y_train = (y_train > 5).astype([Link]) # 1 for numbers > 5, 0 for numbers <= 5
y_test = (y_test > 5).astype([Link]) # 1 for numbers > 5, 0 for numbers <= 5

# Build a simple neural network model


model = [Link]([
[Link](input_shape=(28, 28)), # Flatten the 28x28 image into 1D array
[Link](128, activation='relu'), # Hidden layer with 128 neurons
[Link](1, activation='sigmoid') # Output layer (binary: 0 or 1)
])

# Compile the model


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

# Train the model


[Link](x_train, y_train, epochs=5)

# Evaluate the model on test data


test_loss, test_acc = [Link](x_test, y_test)
print(f"Test Accuracy: {test_acc:.2f}")

Expected Output (Test Accuracy):

Test Accuracy: 0.99 (99%)

Example 2: Multi-Class Classification (Recognizing Digits 0-9)


In this example, we use a multi-class classification model to recognize digits from the MNIST
dataset. Instead of classifying digits as above or below 5, we will classify each image as one
of the 10 digits (0 to 9).

Steps in the code:

1. We load the MNIST dataset, which contains 28x28 grayscale images of handwritten digits.

2. We normalize the pixel values of the images by dividing them by 255 to scale them
between 0 and 1.

3. We build a neural network with the following layers:


- Flatten: Converts the 28x28 image into a 1D list of 784 pixels.

- Dense (128 units): A hidden layer with 128 neurons using ReLU activation function.

- Dense (10 units): Output layer with 10 neurons for 10 classes (digits 0-9), using the
softmax activation function.

4. We compile the model using the Adam optimizer and sparse categorical cross-entropy
loss function because we are dealing with multi-class classification.

5. We train the model for 5 epochs on the training data.

6. After training, we evaluate the model's accuracy on the test data.

Step-by-Step Code:

import tensorflow as tf
from tensorflow import keras

# Load MNIST dataset


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

# Preprocess: Normalize data (scale pixel values to 0-1)


x_train, x_test = x_train / 255.0, x_test / 255.0

# Build the neural network model


model = [Link]([
[Link](input_shape=(28, 28)), # Flatten the 28x28 image into 1D array
[Link](128, activation='relu'), # Hidden layer with 128 neurons
[Link](10, activation='softmax') # Output layer (10 classes for digits 0-9)
])

# Compile the model


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

# Train the model


[Link](x_train, y_train, epochs=5)

# Evaluate the model on test data


test_loss, test_acc = [Link](x_test, y_test)
print(f"Test Accuracy: {test_acc:.2f}")
Expected Output (Test Accuracy):

Test Accuracy: 0.98 (98%)

You might also like