top of page
Gradient With Circle
Image by Nick Morrison

Insights Across Technology, Software, and AI

Discover articles across technology, software, and AI. From core concepts to modern tech and practical implementations.

TensorFlow in Python: Build Your First Handwritten Digit Classifier

  • Aug 12, 2024
  • 5 min read

Updated: Feb 16

TensorFlow is one of the most popular open-source frameworks for machine learning and deep learning in Python. It provides a flexible and powerful environment for building, training, and deploying neural networks, from simple models to complex deep learning architectures.


In this tutorial, we’ll walk through a hands-on example using the MNIST dataset, a collection of handwritten digits, to build and train a neural network in Python. You’ll learn how to load and preprocess data, define and compile a model, train it, and evaluate its performance. By the end, you’ll have a working TensorFlow model capable of accurately classifying handwritten digits and a clear understanding of the workflow for building deep learning models in Python.


tensorflow in python - colabcodes

What Is TensorFlow in Python?

TensorFlow is an open-source framework for machine learning and deep learning, widely used for building, training, and deploying a variety of models. Developed by Google, it provides a comprehensive and flexible ecosystem for researchers and developers to implement complex neural networks and other machine learning algorithms. In Python, TensorFlow is a powerful tool for handling large-scale numerical computations, especially those involving matrix operations and neural network training. It supports applications ranging from simple linear regression to advanced models like Convolutional Neural Networks (CNNs) and Recurrent Neural Networks (RNNs). Its architecture allows execution on CPUs, GPUs, and TPUs (Tensor Processing Units), making it highly scalable for both small projects and large-scale production environments.


A key strength of TensorFlow is its graph-based computation model, where nodes represent mathematical operations and edges represent tensors (data) flowing between them. This approach enables efficient computation, distributed execution, and easy visualization, which is particularly valuable when building deep learning models in Python. Key features of tensorflow


  1. Scalability: TensorFlow handles large-scale machine learning tasks and supports distributed computing across multiple devices and clusters.

  2. Flexibility: Its framework allows defining computational graphs for a wide range of models, from simple linear regressions to multi-layer neural networks.

  3. Extensive Ecosystem: TensorFlow includes tools like TensorFlow Lite for mobile and embedded devices, TensorFlow.js for JavaScript development, and TensorFlow Extended (TFX) for end-to-end ML pipelines.

  4. Community and Support: TensorFlow benefits from a large, active community, extensive documentation, tutorials, and continuous updates from Google and the open-source community.


TensorFlow is widely adopted by developers and researchers for its versatility, scalability, and robust support for machine learning tasks. Whether you’re building a simple model for a small dataset or a complex deep learning architecture for large-scale data, TensorFlow provides the tools and framework needed to develop, train, and deploy models efficiently.


Building a Simple Neural Network with TensorFlow in Python

Building a simple neural network with TensorFlow in Python is an excellent way to dive into deep learning. Using TensorFlow’s high-level Keras API, you can quickly create a neural network model with just a few lines of code. Start by defining the model using a Sequential structure, which allows you to stack layers easily. A common approach is to use a Flatten layer to convert input data into a one-dimensional array, followed by one or more Dense layers for processing, with ReLU as the activation function. The final layer typically uses softmax activation for multi-class classification. After defining the model, compile it with an optimizer (like Adam) and a loss function, then train it on your dataset. This simple approach lets you build a neural network to solve tasks like image classification, making TensorFlow a powerful yet accessible tool for deep learning projects. Let’s walk through creating a simple neural network model to classify images from the MNIST dataset, a classic dataset in the field of machine learning.


Step 1: Import Libraries

First, we set up the environment by importing TensorFlow and the Keras modules for building neural networks. We also import the MNIST dataset, which contains images of handwritten digits that we’ll use to train and test our model. These imports give us all the tools we need to define, train, and evaluate a neural network in Python.

import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import mnist

Step 2: Load and Preprocess the Data

Next, we load the MNIST dataset into training and testing sets. Convolutional neural networks expect a channel dimension, so we reshape the images to (samples, 28, 28, 1). We also normalize the pixel values to the range [0, 1] by dividing by 255, which helps the model learn more efficiently and speeds up training.

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255

Step 3: Define the Model

We define a simple feedforward neural network using Keras’ Sequential API. The model flattens the input images into a 1D vector, then passes them through a dense layer with 128 neurons and ReLU activation, followed by an output layer with 10 neurons and softmax activation to predict the digit classes.

model = models.Sequential([
    layers.Flatten(input_shape=(28, 28, 1)),
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])

Step 4: Compile the Model

Before training, we compile the model by specifying the optimizer, loss function, and evaluation metrics. Here we use the Adam optimizer and sparse categorical cross-entropy loss, which is standard for multi-class classification tasks.

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Step 5: Train the Model

We train the model on the training dataset for 5 epochs using a batch size of 32. During training, the model gradually improves its accuracy and reduces its loss.

model.fit(train_images, train_labels, epochs=5, batch_size=32)

Output:
Epoch 1/5 - accuracy: 0.8761 - loss: 0.4347
.
.
.
Epoch 5/5 - accuracy: 0.9870 - loss: 0.0435

Step 6: Evaluate the Model

Finally, we evaluate the model on the test dataset to see how well it generalizes to unseen data.

test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')

Output:
Test accuracy: 0.9778

This shows the model achieved 97.78% accuracy on the test set, demonstrating that even a simple neural network can effectively classify handwritten digits with TensorFlow in Python.


Conclusion

TensorFlow in Python provides a robust and versatile platform for developing and deploying deep learning models. Its combination of flexibility, scalability, and ease of use makes it an ideal choice for a wide range of applications, from research to production. By leveraging TensorFlow’s high-level Keras API, developers can quickly build, train, and optimize neural networks with minimal complexity. Whether you're just starting out in machine learning or looking to tackle advanced AI challenges, TensorFlow equips you with the tools needed to succeed in the ever-evolving field of deep learning. Embrace TensorFlow to unlock the full potential of your data and drive innovation in your projects. By offering robust support for large-scale computations and a wide range of applications, TensorFlow continues to be a leading tool in the AI community. Whether you're just starting with deep learning or looking to implement complex models in production, TensorFlow provides the tools and resources needed to achieve your goals.

Get in touch for customized mentorship, research and freelance solutions tailored to your needs.

bottom of page