A Basic Neural Network!!

This post consist of a Basic Neural Network Implementation using TensorFlow, Keras, Numpy and Python.

TensorFlow

TensorFlow is a free and open-source software library for dataflow and differentiable programming across a range of tasks. It is a symbolic math library, and is also used for machine learning applications such as neural networks.

Deep neural networks are all about networks of neurons, with each neuron learning to do its own operation as part of a larger picture. Data such as images enters this network as input, and flows through the network as it adapts itself at training time or predicts outputs in a deployed system.

Tensors are the standard way of representing data in deep learning. Simply put, tensors are just multidimensional arrays, an extension of two-dimensional tables (matrices) to data with higher dimensionality. Just as a black-and-white (grayscale) images are represented as “tables” of pixel values, RGB images are represented as tensors (three-dimensional arrays), with each pixel having three values corresponding to red, green, and blue components.

In TensorFlow, computation is approached as a dataflow graph (Figure below). Broadly speaking, in this graph, nodes represent operations (such as addition or multiplication), and edges represent data (tensors) flowing around the system.

_config.yml A dataflow computation graph. Data in the form of tensors flows through a graph of computational operations that make up our deep neural networks.

TensorFlow Architecture

TensorFlow architecture works in three parts:

  • Preprocessing the data
  • Build the model
  • Train and estimate the model

It is called TensorFlow because it takes input as a multi-dimensional array, also known as tensors. You can construct a sort of flowchart of operations (called a Graph) that you want to perform on that input. The input goes in at one end, and then it flows through this system of multiple operations and comes out the other end as output.

This is why it is called TensorFlow because the tensor goes in it flows through a list of operations, and then it comes out the other side.

Keras

Keras is a minimalist Python library for deep learning that can run on top of Theano or TensorFlow.

It was developed to make implementing deep learning models as fast and easy as possible for research and development.

It runs on Python 2.7 or 3.5 and can seamlessly execute on GPUs and CPUs given the underlying frameworks. It is released under the permissive MIT license.

Installation

Create a Virtual Environment

pip3 install virtualenv

virtualenv -p ~/home tensorflow

Activate Virtual Environment

source ~/home/tensorflow/bin/activate

Install TensorFlow

pip3 install tensorflow

Install Keras

pip3 install Keras

Install Numpy

pip3 install numpy

Artificial Neural Network

In Layman's terms a Artificial Neural Network is the artificial implementataion of how the human brain works, It consists of Neurons which we call Nodes or units.

A neural network is composed of a number of nodes, or units, connected by links. Each link has a numeric weight associated with it. Weights are the primary means of long-term storage in neural networks, and learning usually takes place by updating the weights. Some of the units are connected to the external environment, and can be designated as input or output units. The weights are modified so as to try to bring the network's input/output behavior more into line with that of the environment providing the inputs.

Artificial Neural Networks are of different types. Such as Convolutional Neural Network (CNN), Recurrent Neural Network (RNN), etc. A very basic single layer neural network is known as Perceptron. We will not go into much detail here.


_config.yml

Implementation

We will start with a very basic mathematical relation between X and Y.

_config.yml

Here, Different values of X and Y are given. There's a formula that maps X to Y. Computing that manually is kind of tough as we need to do lots of Hit and trials to arrive at a conclusion.

The conclusion is:

Y = 2X - 1

We can use a Neural Network to compute this for us in fraction of seconds. What a Neural Network will do is to guess the value again and again just like we were doing by Hit and Trial.

Below is the code to implement this,



This code will give the following output:

_config.yml

Here, We can see that we got the value of Y as 18.987537 for a value of X = 10.0.

We should have got Y = 19.0 but as we have trained our model for 500 epochs only and that too with very little training data i.e. just 6 values of X and Y. So, this much discrepancies in results are common and expected.

Just like this Artificial Neural Networks could be used in a number of ways such as Image Classifications, Natural language Processing, Voice recognition and a lot.

TensorFlow is the latest and very reliable library or framework to build Neural Networks. It also consists of visualisation toolkits such as TensorBoard and a flexible, high-performance serving system to serve/ export models known as TensorFlow Serving.

Written on May 29, 2019