In this course, you’ll build a program that can recognize handwritten digits by training it with real-world data. By the end of this course, you’ll have your own working AI model and a better understanding of how AI works.
We’ll be using VScode and python to write the code in this project.
First, install the python debugger. Install tensorflow and keras using the terminal.
Terminal Installation
Verify python:
python3 --version or python --version
Install python if not present:
winget install Python.Python.3
Install pip:
python -m ensurepip --upgrade
Instal needed modules:
pip install tensorflow
pip install keras
First, the image needs to be turned into data that can be easily read by a machine.
It’s turned into pixels from a range of colours into only 2. This makes processing the data easier.
It also resizes the image to a uniform size.
AI uses a series of ‘layers’ to extract features from an image.
Convolutional Layers
Identifies patterns like edges, corners, textures, or shapes.
Pooling Layers
‘Summarizes’ regions. Removing unnecessary details while keeping important information.
Flattening
Converts the 2D image into 1D to analyze it better.
The AI model passes extracted features into dense layers to learn relationships between features and labels them.
These features may include objects shapes or textures.
In this case, shapes.
The rectified linear unit recognizes complex patterns.
Softmax sorts the output into possible classes. (1,2,3)
The class with the highest probability is then chosen as the predicted label (number).
The AI is trained on a set of images where each image has already been given a label
The loss functions measures how wrong the predictions are
The AI learns by repeating this process and adjusting its predictions.
Each repeat is called an Epoch.
The more Epochs the more accurate the test is.
After training, the AI evaluates new images by extracting them and comparing them with already labeled patterns.
The model assigns a score to each label
Measures how often the model is correct.
How fat off some of the model's predictions are (loss).
First import the needed libraries.
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
Loads the MNIST dataset, which contains images of handwritten digits (0–9).
Splits the data into two parts:
Training data: Used to teach the model.
Test data: Used to evaluate how well the model has learned.
(X_train, y_train), (X_test, y_test) = mnist.load_data()
Convert the pixel values from 0-255 to 0-1.
This converts colourful pixels to only black and white
Making it easier for the AI to process it.
X_train, X_test = X_train / 255.0, X_test / 255.0
Turn the image into a 28x28 image
Add dense layers to learn relationships between shapes and colours and sorts them into digits 0-9
Softmax turns the output into a probability
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
Uses the ‘Adam’ model to improve accuracy
Tracks the loss and accuracy of the prediction
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Repeat the model X times according to the epoch
model.fit(X_train, y_train, epochs=5)
Runs and outputs the accuracy
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", accuracy)
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
# Load and preprocess data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train, X_test = X_train / 255.0, X_test / 255.0
# Build the model
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, epochs=30)
# Evaluate on test data
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", accuracy)