Tensorflow 7: First Neural Network

My previous post. .  All my code can be found on github (7_Basic_Neural_Network.ipynb)

The Brain!

Now that we know how to train a data graph lets create our first basic Neural Network (NN).  A Neural network is:

  • A digital network inspired by neurons in the brain.
  • Has a number of input nodes where data flows IN.
  • A number of output nodes where data flows OUT (Results).
  • A number of hidden nodes organised into layers that connect the input and output nodes.
  • Networks with only a few hidden layers are called shallow and networks with many hidden layers are called deep.
  • The connection between nodes have weights and biases values that change during training.
  • The arrangement of the nodes is called the network architecture and there are many different arrangements as can be seen here.

3 input nodes. 4 hidden nodes in 1 layer and 2 output nodes


At a basic level each Node in the network is meant to represent a Neuron in the brain.  It takes many inputs and generates an output.

As you can see each input to the node  is multiplied by the weight along that edge.   A bias value is then added and then this is passed through an activation function.  I will go into activation functions at a later time.

This calculates for each node:


When the nodes are arranges in a network they generally take the following form.  This is called a feed forward network.  This is because the data feeds from left to right through the network and is one of the simplest neural network architectures.

When we label all of the weights and biases for each of the nodes and edges of the network this looks like this:


Although this looks complicated we can thankfully represent this in the form of matrices and also the algebra in the form of matrix multiplications.

The tensorflow website explains this well. (Softmax as their activation function)

Which becomes:

that can be expressed as:

Code – Generate Data

We will train the network the in a similar way to the previous lesson by using labeled training data.  As the network trains the values for the weights and biases are changed in an attempt the make the inputs match the outputs.

I will aim to create a system that converts binary input (base 2 number) into decimal output (base 10 number).

Lets create some training data.

Our input will be a binary array.  Our output will have 16 nodes each representing a different digit.

CODE – Create Neural Network

Lets assign some parameters that describe the architecture of the network.

CODE – Placeholders

Now to pass this training data into the system we need placeholders same as the previous lesson

Note the shape section.  The None indicates how many items we will be training on at one time.  None indicates any number of training items.  It is often more efficient to train on multiple training samples at a time called batch training.

CODE – Variables

We then need to create Variables to hold our weights and biases.

  • Weights between the inputs and hidden layer
  • Weights between the hidden layer and output units.
  • Biases for the Hidden Layer
  • Biases for the output layer.

These will all be initialized with random data using the tensorflows tf.random_normal function.  We could have also used numpy as in the previous less.

Then we will add in the matrix multiplications that connect all of the layers together.

The outputs of the system need to undergo a softmax activation function. This is used for multiclass logistic regression.  It turns the total output of the system to equal 1.

CODE – Training

Now we need to undergo our normal training system.

  • Define the loss
  • Define the optimiser
  • Train on Data.

This is slightly different from using Mean Squared Error.

We will use the AdamOptimiser.  Tensorflow has a number of different optimizers. A good discussion with a nice animation on the differences can be found here.

To train on data we will need to pass in the training data using a feed_dictionary

This is very similar to previous lessons training.

Lets run the system and see how it performs.

The error rate looks really good except we probably could have stopped training before 100 steps let alone 2000.

Lets add in code to stop training when our error rate gets to an acceptable level ( <0.005 ).  Lets rerun and see.

It now exits after 36 steps.


Graph code

Now that the system is trained we can have a look at its performance.

For this we will input data into the data graph and output a bar graph to show what the system believes the correct output should be.

Firstly the following generates a matplotlib bar graph from an array of values.

We will then pass in all of the different options to see how the system performs.


softmax vs non softmax output.  This illustrates what the softmax function does for the 0 input.

So you can now test the trained network.

Next we will apply this system to the MNIST dataset for handwriting digits.