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

Build Image Classifier with TensorFlow

The document outlines a deep learning lab session focused on building an image classifier using TensorFlow and Keras with the MNIST dataset. It covers essential concepts of deep learning, the structure of neural networks, and provides step-by-step instructions for importing data, preprocessing, building, compiling, training, and evaluating a neural network model. Additionally, it introduces solving the XOR problem using Convolutional Neural Networks (CNN).

Uploaded by

ambikutty1985
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Build Image Classifier with TensorFlow

The document outlines a deep learning lab session focused on building an image classifier using TensorFlow and Keras with the MNIST dataset. It covers essential concepts of deep learning, the structure of neural networks, and provides step-by-step instructions for importing data, preprocessing, building, compiling, training, and evaluating a neural network model. Additionally, it introduces solving the XOR problem using Convolutional Neural Networks (CNN).

Uploaded by

ambikutty1985
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Deep Learning Lab Session 🚀

Your First Neural Network: Building


an Image Classifier

Using TensorFlow & Keras with the MNIST


Dataset
What is Deep Learning? 🤔

Deep learning is a powerful subfield of Machine Learning


that uses neural networks with many layers to learn
complex patterns from data.
Neural Network: A series of connected layers.

Layer: A collection of neurons.

Input Layer: Takes the data.

Hidden Layers: The "deep" part of the network, where


the learning happens.

Output Layer: Produces the final prediction.


The MNIST Dataset 🔢

The MNIST dataset is often called the "Hello


World" of deep learning. It's perfect for our
first lab due to its simplicity and cleanliness.
Dataset Characteristics:
• Content: 70,000 grayscale images of
handwritten digits (0-9).
• Split:
• 60,000 images for training the model.
• 10,000 images for testing the model's
performance
• Image Size: Each image is 28 times 28 pixels.
• Representation: Pixel values range from 0
(black) to 255 (white).
Step 1: Importing Libraries and Loading the
Data
import tensorflow as tf
from tensorflow import keras
import numpy as np
import [Link] as plt

# Load the MNIST dataset


(train_images, train_labels), (test_images, test_labels) =
[Link].load_data()

# Print the shape of the data to show what we're working with
print("Training images shape:", train_images.shape)
print("Training labels shape:", train_labels.shape)

Downloading data from


[Link]
11490434/11490434 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step Training images shape:
(60000, 28, 28) Training labels shape: (60000,)
Step 2: Preprocessing the Data
# Normalize the pixel values from 0-255 to 0-1
# This makes the training process more stable
train_images = train_images / 255.0
test_images = test_images / 255.0

# Optional: Display one of the images to visualize


the data
[Link]()
[Link](train_images[0], cmap=[Link])
[Link]()
[Link](False)
[Link](f"Label: {train_labels[0]}")
[Link]()
Step 3: Building the Neural Network Model
# Define the neural network architecture
model = [Link]([
# Input layer: Flattens the 28x28 image into a 784-element array
[Link](input_shape=(28, 28)),
# Hidden layer: A densely connected layer with 128 neurons and a ReLU
activation function
[Link](128, activation='relu'),
# Output layer: A densely connected layer with 10 neurons (for 10 classes)
# The softmax activation function ensures the output is a probability
distribution
[Link](10, activation='softmax')])
Step 4: Compiling the Model
# Compile the model
[Link](
optimizer='adam',
loss=[Link](from_logits=True),
metrics=['accuracy']
• )

# Optional: Print a summary of the model's architecture
• [Link]()
Step 5: Training the Model
# Compile the model
[Link](
optimizer='adam',
loss=[Link](from_logits=True),
metrics=['accuracy']
• )

# Optional: Print a summary of the model's architecture
• [Link]()
Step 6: Evaluating the Model

# Evaluate the model on the test dataset


test_loss, test_accuracy = [Link](test_images, test_labels,
verbose=2)

print(f"\nTest accuracy: {test_accuracy}")


Exp 2: Solving XOR Problem Using CNN

What is CNN? What is XOR Logic?

• CNN refers to Convolutional Neural Network, in


which the mathematical operation “Convolution” is
used in the hidden layers to calculate the weights in
the process of recognizing the patterns and features.

• In this method, small grids of parameters


called kernels or filters slide across the input data to
detect specific features, like edges or textures.
(Refer the Pooling Process we have discussed in the It shall also written as,
class). XOR = (X₁ OR X₂) AND NOT (X₁
AND X₂)
Step 1 : Import libraries
import numpy as np
from [Link] import Sequential
from [Link] import Dense
import [Link] as plt

• numpy (np): helps in creating arrays. Here, we are using it to form two arrays, input and output
arrays.
• [Link]: By this library, we shall built multi layer CNN
• [Link]: It represents the kind of connection between each neurons in the neural
network. In dense network, all neurons of one level is connected to the all neurons of next level.
• [Link] (plt): used for plotting graphs (here, accuracy over epochs).
Step 2 : Create Arrays for XOR Input and Output

X = [Link]([[0,0], [0,1], [1,0], [1,1]])

y = [Link]([[0], [1], [1], [0]])

Arrays created for inputs X1,X2 and


Output Y.
XOR Output
Input X1 Input X2
(y)
0 0 0
0 1 1
1 0 1
1 1 0
Step 3 : Build the Model

Arrays created for inputs X1,X2 and


Output Y.
XOR Output
Input X1 Input X2
(y)
0 0 0
0 1 1
1 0 1
1 1 0

You might also like