Develop your first simple neural network using TensorFlow in Python

In this tutorial, you will learn about TensorFlow and how to use it to create a simple neural network. TensorFlow in Python was created by Google to enable us to create high functioning algorithms. It serves as a foundation library for deep learning programmers. TensorFlow eases learning and creating a process for programmers. Which is why it is an ideal platform to learn how to develop your first neural network.

The easiest way to understand what a neural network is, to create one from scratch. In this article, you will get a basic understanding of what a neural network is and the easiest way to create one using TensorFlow.

But, what is a neural network?

A neural network is roughly based on how the human brain works. Our brain is made up of infinite neurons that are all connected to one another. They pass information through these connections and ultimately help us in our day to day life.

For example, this is a picture of a bird at an extremely low resolution. But our brain has absolutely no difficulty in recognising it as a bird:

If you were to write a program, that takes in a picture and tells you what the picture is of, the task can be quite difficult. But, this is where machine learning and neural networks kick in and make the task easier for you.

So let’s learn how to make a neural network that can recognise different items of clothing.

What a neural network consists of:

  1. An input layer
  2. A hidden layer
  3. An output layer
  4. Weights between the layers
  5. A deliberate activation function for every hidden layer


There are multiple types of neural networks, but we will learn how to create the easiest one – that transfers data from front to back.

The architecture of the neural network we will create:

  • Our images essentially come in 28 by 28 pixels – so that means that there will be 28 rows and 28 columns. Principally transferring all this data into one neuron is difficult and it doesn’t work very well. So, what most people do is ‘flatten the data’.
  • Flattening the data means – taking an interior array and combining them all. For example: rather than having an array like this [[1],[2],[3]] we will have it like [1, 2, 3] this.
  • After flattening the data, we will get an array of the size of 784 pixels (28 x 28 = 784). This will serve as the input to the neural network.
  • Output layer – our output is going to be a number between 0-9. So, we will have 10 neurons – each representing one of the different digits (0, 1, 2, 3….9). So how this essentially works is suppose we input an image of an apple – then all the classes it is made up of will be lit up a certain amount. Then our neural network will choose the one with the highest probability and return the output respectively.
  • Moving onto our hidden layers – we could technically train our program to work with only two layers (the input layer and the output layer). But if we choose to work with only 2 layers – it restricts the activities we can do. To overcome that, we add a hidden layer between the input and output layer. We can usually arbitrarily pick the number of neurons in our hidden layer but it’s a good practice to decide that based on the percentage of the input layers. So, for example, we will choose 128 neurons for our hidden layer. So, what will happen now is that the input will be fully connected to the hidden layer and the hidden layer in turn will be connected to all the neurons in the output layer.
  • What we want our hidden layer to do is, recognise patterns in our input image. Then based on the highest probability choose one of the neurons in the output.

Lets start the coding:

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

data = keras.datasets.fashion_mnist

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

class_names = [‘T-shirt/top’, ‘Trouser’, ‘Pullover’, ‘Dress’, ‘Coat’, ‘Sandal’, ‘Shirt’, ‘Sneaker’, ‘Bag’, ‘Ankle boot’]

train_images = train_images/255.0
test_images = test_images/255.0

#keras.Sequential creates the layers and defines the order in which they need to be present in

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation=”relu”),
    keras.layers.Dense(10, activation=”softmax”)
    ])

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

model.fit(train_images, train_labels, epochs=7)

prediction = model.predict(test_images)

#the next set of code is to first show the input and then the prediction
for i in range(7): plt.grid(False) plt.imshow(test_images[i], cmap=plt.cm.binary) plt.xlabel(“Actual: ” + class_names[test_labels[i]]) plt.title(“Prediction” + class_names[np.argmax(prediction[i])]) plt.show()

Examples of input:

There it is, this is one of the easiest and quickest ways to develop a simple neural network using TensorFlow in Python. 

 

Leave a Reply

Your email address will not be published. Required fields are marked *