Building a Basic Neural Network with TensorFlow
Python’s extensibility and readability make it a popular choice for scientific computing. When it comes to machine learning, Python provides numerous libraries and frameworks, allowing data scientists to precisely and efficiently build machine learning models. One such library is TensorFlow. In this tutorial, we’ll guide you through building a basic neural network using TensorFlow.

Table of Contents
- Introduction
- Prerequisite
- Understanding TensorFlow
- Understanding Neural Networks
- Installing TensorFlow
- Building a Neural Network with TensorFlow
- Conclusion
Introduction
TensorFlow is a powerful open-source library for numerical computation, particularly well-suited for large-scale Machine Learning. Its basic principle is simple: you first define in Python a graph of computations to perform, and then TensorFlow takes that graph and runs it efficiently using optimized C++ code.
Prerequisite
For this tutorial, it’s assumed you have basic knowledge of Python programming and familiarity with machine learning concepts. Basic understanding of Neural Networks would be beneficial but not mandatory.
Understanding TensorFlow
TensorFlow allows developers to create large-scale neural networks with many layers. TensorFlow is versatile, offering an easy-to-use platform for deploying machine learning models and conducting complex mathematical operations. Computing nodes of Tensorflow can be executed in parallel across multiple CPUs or GPUs, making it suitable for distributed computing.
Understanding Neural Networks
Neural networks are a series of algorithms that mimic human brains, designed to recognize patterns. They interpret sensory data through machine perception, labeling or clustering raw input. A neural network takes input data, trains itself to recognize patterns present in the data, and finally, it uses this training to make predictions.
The granularity of these layers represents the model’s capacity for learning: the more granular the layers, the better its ability to learn from data.
Installing TensorFlow
You can easily install TensorFlow using pip:
pip install tensorflow
If you’re using a Jupyter notebook, ensure you include “!”:
!pip install tensorflow
Building a Neural Network with TensorFlow
Step 1: Importing Required Libraries
We need to import the necessary libraries. We need TensorFlow and NumPy for calculations and Matplotlib for visualization.
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
Step 2: Load the Data
Let’s use MNIST dataset for this exercise. It’s a set of 70,000 small images of digits handwritten by high school students and employees of the US Census Bureau.
mnist = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
Step 3: Data Preprocessing
Normalization is an essential stage of preprocessing. It helps the model to train faster.
training_images = training_images / 255.0
test_images = test_images / 255.0
Step 4: Building the Model
The basic building block of a neural network is the layer. For a sequential model, you can add layers using add()
. For our basic neural network, we will be using tf.keras.layers.Flatten
for our first layer and tf.keras.layers.Dense
for the other two layers.
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
Step 5: Compiling the Model
Once the model is created, call its compile()
method to specify the loss function and the optimizer.
model.compile(optimizer = tf.keras.optimizers.Adam(),
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
Step 6: Training the Model
Now, we train the model by invoking the fit
method.
model.fit(training_images, training_labels, epochs=5)
Step 7: Evaluating the Model
You can use the evaluate()
method to test the model.
model.evaluate(test_images, test_labels)
Conclusion
Congratulations! You’ve built a basic neural network using TensorFlow, trained it and tested its classification capabilities. This tutorial serves just as a starting point. TensorFlow provides a wealth of functionality for building and training complex neural networks.
Please keep exploring the TensorFlow library and experiment with different types of layers and classifiers for implementing more sophisticated and accurate models. Happy programming!
References:
- TensorFlow official documentation, https://www.tensorflow.org/overview
- GĂ©ron, A. (2019). Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems. O’Reilly Media.
Remember, the key to mastering machine learning concepts and frameworks like TensorFlow is consistent practice and implementation. Do not limit yourself to the examples and algorithms in this tutorial—venture out and develop more complex models with TensorFlow. This will not only improve your understanding of TensorFlow but also enable you to solve real-world machine learning problems.