Machine learning: Automatic Trainable Linear Regression using Tensorflow

Following the post on Understanding of Linear Regression using Spreadsheet, here is a Tensorflow program to implement Automated trainable Linear Regression.

Linear regression concept

y = a + bx
Here y = dependent output (e.g.: Mark Grade)
x = independent input (e.g.: Hours spent on Internet)
a = y intercept (e.g.: even with zero internet hours, this is minimum marks scored)
b = Slope – How far hours studied affects Grade
The above linear regression is given below in tensorflow perspective

In tensorflow example here:

linear_model = W*x + b
Here linear_model = predicted dependent variable (predicted y)
x = independent variable input
W = Weight <— Slope
b = bias <— y-intercept
y = actual output

 

Tensorflow python code

import tensorflow as tf

# Model parameters
W = tf.Variable([1], dtype=tf.float32)
b = tf.Variable([80], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W*x+b
y = tf.placeholder(tf.float32)

# loss
loss = tf.reduce_sum(tf.square(linear_model – y))
# optimizer
# Note: The optimizer gradient descent of 0.01 resulted in following output:
# W: [ nan] b: [ nan] loss: nan
# Changing it to 0.001 works!
optimizer = tf.train.GradientDescentOptimizer(0.001)
train = optimizer.minimize(loss)

# training data
x_train = [0,1,2,3,4,5,6,7,8,9,10] # Internet hours
y_train = [85,80,84,80,82,70,44,30,22,10,15] # Marks scored
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
# Note: Increasing the range value (10000) results in more accurate result
for i in range(10000):
  sess.run(train, {x: x_train, y: y_train})

# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print(“W: %s b: %s loss: %s”%(curr_W, curr_b, curr_loss))

# Result: W: [-8.67264175] b: [ 98.09030914] loss: 1130.4

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *