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

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

training_epoch = 100
learning_rate = 0.005

In [3]:
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.234245  theta_1= 0.809059  error= -0.828116
theta_0= 0.249934  theta_1= 0.843952  error= -0.609545
theta_0= 0.255603  theta_1= 0.844187  error= -0.601682
theta_0= 0.260614  theta_1= 0.842923  error= -0.603187
theta_0= 0.265394  theta_1= 0.841645  error= -0.605035
theta_0= 0.269973  theta_1= 0.840418  error= -0.606822
theta_0= 0.274359  theta_1= 0.839243  error= -0.608534
theta_0= 0.278561  theta_1= 0.838117  error= -0.610176
theta_0= 0.282585  theta_1= 0.837039  error= -0.611748
theta_0= 0.28644  theta_1= 0.836006  error= -0.613253
Optimized

In [4]:
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 [ ]: