Building an ANN with Tensorflow

語言: CN / TW / HK

A step by step tutorial on how to build a neural network using TensorFlow

Photo by Halacious on Unsplash

If you are new to Artificial Neural networks, you can check out my blog on Introduction to ANN in the below link

In this tutorial, we will build an Artificial Neural Network on Fashion MNIST dataset which consists of 70,000 images out of which 60,000 images belong to the training set and 10,000 images belong to the test set. Each image is 28 pixels in height and 28 pixels in width, for a total of 784 pixels in total with ten labels associated with them. The pixel-value is an integer between 0 and 255. Each row is a separate image and we have 785 column labels including one class label. In this dataset, each image is associated with one of the labels mentioned below.

  • 0 → T-shirt/top
  • 1 → Trouser
  • 2 → Pullover
  • 3 → Dress
  • 4 → Coat
  • 5 → Sandal
  • 6 → Shirt
  • 7 → Sneaker
  • 8 → Bag
  • 9 → Ankle boot

Installing Tensorflow

You can either use Google Colab if your PC or laptop doesn’t have a GPU or else you can use Jupyter Notebook. If you use your system, upgrade pip and then install TensorFlow as follows

Import dependencies

In the above lines of code, I just imported all the libraries I will be needing in the process


Loading the dataset

Here we will load the Fashion mnist dataset using the load_data() method.


Here, we divide each image by the maximum number of pixels(255) so that the image range will be between 0 and 1. We normalize images so that our ANN models train faster.


We will get a 2d array as an output in which the first dimension corresponds to the index of the image and second dimension will be the vector containing all the pixels of the image

In the 2d array, we will change the two-dimensional array into the One-dimensional vector using reshaping. Here we pass two arguments

  • -1 → we need to reshape all the images
  • 28*28 → The size of the resultant vector(784 pixels → 1D)

Photo by NASA on Unsplash

Building ANN

The first thing we need to do before building a model is to create a model object itself, this object will be an instance of the class called Sequential.

Adding the first fully connected layer

If you are unaware of types of layers and their functionality, I would recommend you check my blog on Introduction to ANN which lets you know most of the concepts you should be aware of.


  • number of neurons: 128
  • activation function: ReLU
  • input_shape: (784, )

It means that the output of this operation should have 128 neurons in which we apply the ReLU activation function to break the linearity and the input_shape is (784, ). We add all these hyperparameters using the model.add() method.

Adding a Dropout layer

It is a Regularization technique where we randomly set neurons in a layer to zero. In this way, some percentage of neurons won’t be updated the whole training process is long and we have less chance for overfitting.

Adding Output layer

  • units =number of classes (10 in the case of Fashion MNIST)
  • activation = softmax(Returns probability of the class)

Compiling the model

It means that we have to connect the whole network to an optimizer and choose a loss. An optimizer is a tool that will update the weights during stochastic gradient descent i.e backpropagating your loss into the Neural Network.

Training the model

We use method to train the model, we pass three arguments inside the method which are

input → x_train is the input that is fed to the network

output → this contains the correct answers for the x_train i.e y_train

no.of.epochs → It means the number of times you are going to train the network with the dataset.

Evaluating the model

We are going to evaluate the performance of the model by applying it to the test set. here evaluate method returns two arguments one is the loss incurred in the test set by the prediction and the other one is the accuracy.