In [1]:
import tensorflow as tf
import matplotlib.pyplot as plt

c = tf.constant([1.0,2.0,3,4,8],name ='c')
p = tf.constant([1.0,2.0,2.0,3.0,5.0],name='p')

training_epoch = 100
learning_rate = 0.005

In [2]:
t1=0.0
t0=0.0
with tf.Session() as sess:
    for epoch in range (training_epoch):
        for i in range (0,5):
            c1=tf.multiply(t1,c[i])
            pred=tf.add(t0,c1)
            error=tf.subtract(pred,p[i])
            t0=tf.subtract(t0,tf.multiply(learning_rate,error))
            t1=tf.subtract(t1,tf.multiply(learning_rate,tf.multiply(error,c[i])))
        if (epoch+1) % 10 == 0:
            print("theta_0=",sess.run(t0)," theta_1=",sess.run(t1)," error=",sess.run(error))
    print("Optimized")


theta_0= 0.163767  theta_1= 0.627594  error= 0.273356
theta_0= 0.197597  theta_1= 0.624119  error= 0.282297
theta_0= 0.228453  theta_1= 0.618762  error= 0.26452
theta_0= 0.256987  theta_1= 0.6138  error= 0.247983
theta_0= 0.283377  theta_1= 0.609211  error= 0.232689
theta_0= 0.307784  theta_1= 0.604967  error= 0.218545
theta_0= 0.330356  theta_1= 0.601042  error= 0.205464
theta_0= 0.351231  theta_1= 0.597411  error= 0.193367
theta_0= 0.370537  theta_1= 0.594054  error= 0.182178
theta_0= 0.388392  theta_1= 0.590949  error= 0.171829
Optimized

In [3]:
sess=tf.Session()
c1=sess.run(c)
p1=sess.run(p)
plt.plot(c1, p1, 'ro', label='Original data')
plt.plot(c1, sess.run(t1) * c1 + sess.run(t0), label='Fitted line')
plt.legend()
plt.show()



In [ ]: