1 of 20

2 of 20

A TensorFlow very (very…) simple Linear Regression model

How to train a TensorFlow linear regression model to fit data

3 of 20

About myself

  • Felipe Caballero
  • Full Stack Developer
  • Worked for a while doing websites
  • Interested in Data Science
  • Recommended course: deeplearning.ai (uses Python)

https://github.com/caballerofelipe

https://medium.com/@felipecaballero

https://www.linkedin.com/in/felipe-caballero-74578412/

A TensorFlow very simple Linear Regression model

4 of 20

Objectives

  • Train a Linear Regression model
  • Use TensorFlow in a very simple way
  • Show how TensorFlow’s low level API works

A TensorFlow very simple Linear Regression model

5 of 20

WTF is Linear Regression?

  • We have points in a space
  • We need to have a good estimator for new points
  • We create a function to help us estimate

A TensorFlow very simple Linear Regression model

6 of 20

How to estimate a function using Linear Regression?

  • Define an equation form to estimate all points
  • The idea is to have an equation as close as possible to all existing points
  • Define a cost function which will tell how close the estimator is to all the points
  • Minimize the cost
  • The estimator is dependent on a and b
  • The cost function also is dependent on a and b

The idea of cost minimization is used a lot in Machine Learning.

A TensorFlow very simple Linear Regression model

7 of 20

Minimize cost using Gradient Descent

  • Gradient descent uses the derivative of a function at a certain point to travel to a lower point
  • It does this several times until the minimization reaches convergence
  • The learning rate is important as this can make Gradient Descent work in unexpected ways

Image from https://flylib.com/books/2/71/1/html/2/files/17fig06.gif

A TensorFlow very simple Linear Regression model

8 of 20

Ok, so… how do we do it?

  • Prepare the environment (conda)
  • Prepare Python to handle the model (imports)
  • Prepare the data (and if want also check it)
  • Create the TensorFlow Graph
  • Run the model
  • Check the results

A TensorFlow very simple Linear Regression model

9 of 20

Install stuff

Activate

Test

Create dir

Create file

Prepare the environment

$ conda create -n envName python=3.6 tensorflow matplotlib jupyter

$ source activate envName

(envName) $ python --version

(envName) $ mkdir linreg

(envName) $ cd linreg

(envName) $ touch main.py

A TensorFlow very simple Linear Regression model

10 of 20

Importing Libraries

# Prepare stuff

import numpy as np

import matplotlib.pyplot as plt

import tensorflow as tf

# To be used in Jupyter Notebooks

from tensorflow.python.framework import ops

A TensorFlow very simple Linear Regression model

11 of 20

Disable warning

import os

# See https://stackoverflow.com/a/47227886/1071459

# Just disables the warning, doesn't enable AVX/FMA

# To avoid tensorflow warnings, uncomment the following line

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

A TensorFlow very simple Linear Regression model

12 of 20

Prepare the data

# Prepare the data

x = np.array([

-7, 5, 7, 12, 2,

4, 5, 6, 19, 14,

16, 15, 11, 8, 18,

-2, 14, 17, 7, 17

])

y = np.array([

-7.56, 21.01, 29.11, 47.89, 14.11,

16.69, 29.81, 28.67, 63.94, 54.8,

49.65, 52.26, 44.86, 40.45, 70.92,

-0.37, 44.34, 64.27, 32.85, 50.14

])

A TensorFlow very simple Linear Regression model

13 of 20

Check the data

# Check the data

print('x.shape():')

print(x.shape)

print('y.shape():')

print(y.shape)

input("Press enter to continue...")

plt.scatter(x, y)

plt.show()

A TensorFlow very simple Linear Regression model

14 of 20

The TensorFlow part — Config

# TensorFlow Model

# Config

num_epochs = 1000

learning_rate = 0.001

# /Config

A TensorFlow very simple Linear Regression model

15 of 20

The TensorFlow part If using Jupyter

# This is necessary if the code is run inside Jupyter

ops.reset_default_graph()

A TensorFlow very simple Linear Regression model

16 of 20

The TensorFlow part — The graph

# Creating the graph

X = tf.placeholder(tf.float32, name='X')

Y = tf.placeholder(tf.float32, name='Y')

a = tf.get_variable('a', initializer=0.)

b = tf.get_variable('b', initializer=0.)

h = a * X + b

cost = tf.reduce_mean( (h - Y)**2 )

optimizer = tf.train.GradientDescentOptimizer(

learning_rate=learning_rate

).minimize(cost)

init = tf.global_variables_initializer()

A TensorFlow very simple Linear Regression model

17 of 20

Run the model — Store result in vars

# Running the Model

found_a = 0

found_b = 0

A TensorFlow very simple Linear Regression model

18 of 20

Run the model — The actual run

with tf.Session() as sess:

sess.run(init)

for epoch in range(num_epochs):

_, costValue = sess.run(

[optimizer, cost],

feed_dict={

X: x,

Y: y,

}

)

found_a = a.eval()

found_b = b.eval()

if epoch % (num_epochs/10) == 0: # Every 10 percent

print("... epoch: " + str(epoch))

print(f"cost[{str(costValue)}] / a[{str(a.eval())}] / b[{str(b.eval())}]")

A TensorFlow very simple Linear Regression model

19 of 20

Visualize the results

# Seeing the obtained values in a plot

xrange = np.linspace(-10, 30, 2)

# Plot points

plt.plot(x, y, 'ro')

# Plot resulting function

plt.plot(xrange, xrange * found_a + found_b, 'b')

plt.show()

A TensorFlow very simple Linear Regression model

20 of 20

Possible growth — …to enhance the code

We could:

  • Enhance the model, add a square element in the equation for instance
  • Save the data, save found values to disk
  • Save iterations, save different values in time

A TensorFlow very simple Linear Regression model