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

p = tf.constant([2.5,3.0,5.0,3.0,4.0],name ='x')
q = tf.constant([1.5,6.0,3.5,2.0,6.0],name='y')

training_epoch = 100
learning_rate = 0.005

In [12]:
t1=0.0
t0=0.0
with tf.Session() as sess:
    for epoch in range (training_epoch):
        for i in range (0,5):
            x1=tf.multiply(t1,x[i])
            pred=tf.add(t0,x1)
            error=tf.subtract(pred,y[i])
            e2=tf.square(error)
            t0=tf.subtract(t0,tf.multiply(learning_rate,error))
            t1=tf.subtract(t1,tf.multiply(learning_rate,tf.multiply(error,x[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.272411  theta_1= 0.890729  error= -2.36576
theta_0= 0.303268  theta_1= 0.922577  error= -2.19281
theta_0= 0.323381  theta_1= 0.918756  error= -2.18753
theta_0= 0.342403  theta_1= 0.913622  error= -2.18919
theta_0= 0.360762  theta_1= 0.908602  error= -2.19107
theta_0= 0.378495  theta_1= 0.90375  error= -2.1929
theta_0= 0.395625  theta_1= 0.899063  error= -2.19467
theta_0= 0.412171  theta_1= 0.894536  error= -2.19638
theta_0= 0.428155  theta_1= 0.890162  error= -2.19803
theta_0= 0.443595  theta_1= 0.885938  error= -2.19962
Optimized

In [13]:
sess=tf.Session()
x1=sess.run(p)
y1=sess.run(q)
plt.plot(x1, y1, 'ro', label='Original data')
plt.plot(x1, sess.run(t1) * x1 + sess.run(t0), label='Fitted line')
plt.legend()
plt.show()



In [ ]: