paint-brush
Build your first Machine Learning & Tensorflow model in 5 minutes!by@nattykohn
10,062 reads
10,062 reads

Build your first Machine Learning & Tensorflow model in 5 minutes!

by Nathaniel KohnSeptember 19th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

So you hear about <a href="https://www.tensorflow.org/" target="_blank">Tensorflow</a>, Machine Learning &amp; Deep learning so much and you want to join the party&nbsp;:)
featured image - Build your first Machine Learning & Tensorflow model in 5 minutes!
Nathaniel Kohn HackerNoon profile picture

So you hear about Tensorflow, Machine Learning & Deep learning so much and you want to join the party :)

In this post we are going to build a model that will calculate:

Y = W*x + b

Hence when providing x to the model, it should predict the best possible Y.

We are going to train the model to give us the best W and b possible according to the what we’d train it on.

For example if we want our model to learn that W should be -1 and b 9, meaning Y = -1*x + 9; we should train it on many training pairs like this:

(x = input, Y = desired_output) = (x, Y) = (5, 4);

Y = -1*5 + 9 => Y = 4 (when the input is 5 predict Y to be 4)

(x, Y) = (3, 6); Y = -1*3 +9 => Y = 6 (when the input x is 3 predict Y to be 6)

The idea here is that we want the model to learn that W should be -1 and 9 and it would learn it with experience — the more training pairs (x, Y) we’d feed it the better it would learn to predict correctly.

So Let’s get down to business!

First we should start by setting up the environment.

Let’s first create a directory, run:

mkdir tf_linear_regression

Now (recommended) let’s run all in a virtual environment, follow these steps (it works for Mac, if you have a Linux or Windows try using Anaconda)

Install Python3

brew install python3

(If you don’t have Brew — just install it from here)

Now install the virtual environment

Link to full explanation

pip3 install virtualenv

Create the environment

cd tf_linear_regression

virtualenv -p python3 <path-to-our-dir>

Now activate it:

source <path-to-our-dir>/bin/activate

get out of the virtual env run:

To get out of the virtual environment run:

deactivate

And now for the fun stuff

Install Tensorflow

pip3 install tensorflow

Now (just for fun) install Jyputer notebook (you don’t have to, you can copy the code below and paste it in a python file and run it as a Python script

pip3 install jupyter

(link here)

Now let’s try to do something simple & have a look on the equation:

y = W*x + B

Assuming we have pairs of inputs and their labels => (X, Y) = [(x1,y1), (x2,y2), (x3,y3),.., (xn,yn)];

i.e for every input x we have the output y. This is a supervised machine learning (ML) where for every input we know the desired output.

Now we are going to train W and B in the equation above. Let assume we have (X, Y) = [ (5, 4), (3, 6), (-1, 10)…]

If we are going to guess what W and B would be…. (remember):

y = W*x + B

4 = []*5 +[]; 6 = []*3 + []; 10 = []*(-1) + []. you can see that W = -1 and

B = 9 would be a good fit us…

Now let’s build a model in Tensorflow (TF) which we’ll train to find those values for W and B. the training process in each step would be:

step1: for the input 5 the result is 4 , for the input 3 the result is 6 etc.

You can see that this is the same way as we are teaching a machine to recognize a cat in a picture: 1. “this input picture is a cat”; 2. “this input picture is a cat”; 3. “this input picture is not a cat” etc.

Let’s START!

Importing TF and Numpy:

Declaring x, y, w, b; Variables are used as trainables and Placeholders are for given train set inputs:

Declaring the training sets for X and for Y model input, and declaring the linear equation:

Now we’re setting the error function (model_delta), the loss function & the optimizer + initializing the variables we declared above:

Now this is a little counter intuitive, but in TF in contrary to program in Python, up until now nothing is running, now we should declare a session and then start the training process:

You can play with the number of iterations, you can see that for 100 we are not even close, but for 1000 it’s much better and 10000 we are nearly there:

  • 0.9999 ~ -1
  • 8.9998 ~ 9


For the code in Jupyter notebook go to here:https://github.com/kohn1001/tesorflow-playground/blob/master/linear_regression/linear_regression_tensorflow_basic.ipynb