Implement a simple Neural Network
TensorFlow is an open-source deep learning and machine learning
framework developed by Google. It provides tools to build, train,
evaluate, and deploy neural network models.
Keras is its high-level API for building models easily.
TensorFlow → Framework
Keras → High-level API (library within TensorFlow)
NumPy → Library
Pandas → Library
Matplotlib → Library
Scikit-learn → Machine learning library
Sequential means the layers are arranged one
after another. Input → Dense Layer → Output
A Dense layer means every input is connected to
every neuron.
Units =1 means the Dense layer contains
exactly one neuron.
What is a neuron?
A neuron is the basic processing unit of a neural network. It:
1. Takes one or more inputs.
2. Multiplies them by weights.
3. Adds a bias. Output=W×X+B
4. Produces an output.
1 input feature (input_shape=[1])
[Link](Dense(units=1, input_shape=[1])) 1 neuron (units=1)
For example, if:
Input(x)
X=4
Weight = 3
Bias = 0
Neuron
The neuron calculates:
Y=(3×4)+0=12
Output(y)
This prepares the model for training. It tells TensorFlow:
● how to update weights
● how to calculate error
One epoch means the model sees all training examples once.
After one epoch, it updates the weights.
To implement a simple Neural Network for classifying
handwritten digits from the MNIST dataset.
MNIST : Modified National Institute of Standards and Technology.
The MNIST dataset is one of the most famous datasets in Machine Learning
and Deep Learning.
It is used to train and test models that recognize handwritten digits.
It contains images of handwritten digits from 0 to 9.
Dataset size : The MNIST dataset has:
Dataset Number of Images
Training set 60,000 ● Training set: Used to teach the neural network.
● Test set: Used to evaluate how well the model performs on
Test set 10,000
unseen images.
Total 70,000
Image size
Each image is:
28 × 28 pixels, So each image contains 28×28=784 pixels.
Pixel values
Each pixel has a value between: 0 to 255 0 = Black
255 = White
Values in between = Shades of gray
Labels Every image has a corresponding label, which is the correct digit.
So the neural network learns: Image → Digit
FLATTEN : Converts a 2D image into a 1D vector.
MNIST DATASET:
Shape before Flatten:
(28, 28)
Flatten converts it into
784 numbers
Dense layer CANNOT take a 2D image directly
the neuron expects its inputs as a single list…..not IN MATRIX FORM
Batch Size
A batch is a small group of training examples that the neural network processes before
updating its weights and bias.
The batch size is the number of training examples in one batch.
Suppose you have only 8 images.
Image1
Image2
Image3
Image4
Image5
Image6
Image7
Image8
If batch_size = 2
TensorFlow divides the data into batches like this: Batch 1 → Image1, Image2 Batch 2 → Image3, Image4
Batch 3 → Image5, Image6 Batch 4 → Image7, Image8 Each batch contains 2 images.
Suppose Batch 1 contains MNIST contains 60,000 images
Image1 → Digit 5 If batch_size = 32 then each batch contains 32 images.
Image2 → Digit 8 So the batches are:
The neural network Batch 1
Images 1–32
1. Makes predictions.
2. Calculates the error (loss).
↓
3. Updates the weights and bias.
Then it moves to Batch 2. Update weights
Image3 Batch 2
Images 33–64
Image4 ↓
Again, Update weights
● Predict Batch 3
● Calculate error Images 65–96 AND SO ON….
● Update weights
This repeats until all batches are processed.
Number of batches:
So during one epoch, TensorFlow processes 1875 batches.
NORMALIZE : Neural networks learn faster when input values are small and on a similar scale.
each pixel has values : 0 to 255
Example
0
120
255
80
Values are in single, double , triple digit
After dividing by 255 :
0.00
0.47 Now every pixel lies between 0 and 1
1.00
0.31
# Import required libraries
import tensorflow as tf
from [Link] import Sequential
from [Link] import Dense, Flatten Dense : Fully connected layer.
Flatten : Converts a 2D image into a 1D vector.
# Load MNIST dataset
(X_train, y_train), (X_test, y_test) = [Link].load_data()
# Normalize the data X_train contains 60000 images
X_train = X_train / 255.0 While y_train contains 60000 labels
X_test = X_test / 255.0
# Build the Model Creates a hidden layer. 128 neurons
model = Sequential ([ Each neuron learns different features of
the digit.
Flatten(input_shape=(28,28)), LAYER 1 Input
Dense(128, activation='relu'), LAYER 2 Hidden For example
Dense(10, activation='softmax') ]) LAYER 3 Output ● one neuron may detect curves
# Compile the model ● another detects vertical lines
[Link](optimizer='adam', loss='sparse_categorical_crossentropy', ● another detects loops
metrics=['accuracy'])
10 neurons : MNIST has 0 to 9 digits
# Train the model
Softmax converts outputs into
[Link](X_train, y_train, epochs=10, batch_size=32)
probabilities.
# Evaluate the model
Adam updates the weights and biases to reduce the
loss, accuracy = [Link](X_test, y_test) error.
print("Test Accuracy:", accuracy)
print("Test Loss:", loss)