Tuesday, March 6, 2018

A small explanation of Machine learning & trend analysis.. using TensorFlow

We will understand machine learning by looking at the most common api for ML - tensorFlow.

Machine Learning is trying to make the machine understand how to interpret trends and provide close to accurate results as would a human mind do. 

TensorFlow is a tool which helps doing just that and works with python, java, c++, javascript and some other languanges.

Tensor means : 

A mathematical object analogous to but more general than a vector, represented by an array of components that are functions of the coordinates of a space.


Before we go into the specifics, let's understand how to install and run tensorFlow on a MacOS box.

You need to have Phython installed before we start installing TensorFlow.

Step 1: Install pip and virtualenv

sudo easy_install pip
pip install --upgrade virtualenv


Step 2: Create a target directory for e.g.: 'tens-flow' and establish a virtualenv

virtualenv --system-site-packages tens-flow # for Python 2.7

Step 3: Activate the virtualenv
$ cd tens-flow
$ source ./bin/activate  
Prompt will change to the following -->
(tens-flow)$

Step 4: Start easy_install using pip

(tens-flow)$easy_install -U pip

Step 5: Install tensor flow

(tens-flow)$ pip install --upgrade tensorflow # for Python 2.7


Validate Installation - by the below steps 
$ cd tens-flow
$ source ./bin/activate  

Prompt should change to -
(tens-flow)$

After working and completion you can deactivate the tensorFlow 
environment by using below command.
(tens-flow)$ deactivate 

Okay so now your tensorFlow environment is up and running.
Let's check via simple tensorFlow heartbeat test. 
Type in the following commands and phython program

$ python

# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))


Output should be : 
Hello, TensorFlow!

If above is true - we are all set!

Now let's continue with machine learning --> Normal data acquisition 
and analysis process:

Raw data collected from real world --> Data is processed --> Clean Data

Clean Data can be fed to --> Exploratory Data for Analysis or Machine Learning Algorithms, Statistical Models or Sent to communicate visualisations 

Machine Learning Alogrithms can then be used to --> Build a data product


Okay so TensorFlow works on the below principles - 

First the graph is constructed

Training is done using the input variables

Estimator is nothing but a form of basic linear regressor model, let's break it down. 

These are steps for any model.
Estimator : train() --> evaluate() --> predict() --> export_savemodel() 
: => Checkpoint.

You need to first train, then evaluate, followed by predict and then 
save a checkpoint state. 

So this workflow evaluates to a checkpoint which is helpful to 
synchronise in a distributed system when they restart.

So let's take a simple example - 

Train phase: 

For e.g. an algorithm which identifies apple types by looking at the 
image or characteristics. 

Let's limit this to 3 types for now - 
  • Fuji, Golden Delicious & Granny Smith
First we develop a simple script which takes three inputs, say images of apples, we need to train this - so how to do that --> 

Check the color of apple - if apple = red , 95% probability it's 'Fuji'

if 'Green' --> it might be 'Granny Smith' more likely.

So with each characteristic we have a set of values which arrives at a result of the image being - how close to accurate - for e.g. -->

Once we start evaluation - we compare probabilities for output with each set of data. 

Predict phase: 

Once evaluation is complete - we can predict the result. Obviously once prediction is close to 100% - the checkpoint is established and saved,

Apple A = can be either

Granny Smith - 99%
Fuji  - 0.01%
Golden Delicious - 0.09%

So tensorFlow helps to do just that! Further it can be used for A/B testing or predicting trends.

You can refer to sample examples on google site to understand the basic tensorFlow and ML training algorithms.

https://opensource.google.com/projects/tensorflow

PS: I have referred to Google tutorials and some videos by experts to give the above picture.

We will discuss about APIs in my next blog and a more interesting tensorFlow example. We will also talk about other models for prediction, stay tuned.

No comments:

Post a Comment