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

%matplotlib inline

In [2]:
x = np.arange(10)
y = 2 * x

In [3]:
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

W = tf.Variable(0.0)

In [4]:
H = tf.multiply(W, X)
cost = tf.reduce_mean(tf.square(H - Y))

In [5]:
descent = W - tf.multiply(
    0.01,
    tf.reduce_mean(
        tf.multiply(H - Y, X)
    )
)
update = W.assign(descent)

In [6]:
init = tf.global_variables_initializer()

In [7]:
with tf.Session() as sess:
    sess.run(init)
    
    for _ in range(20):
        sess.run(update, feed_dict={X: x, Y: y})
        print((_, sess.run(cost, feed_dict={X: x, Y: y}), sess.run(W)))


(0, 58.279652, 0.56999999)
(1, 29.794012, 0.97755003)
(2, 15.231442, 1.2689483)
(3, 7.7866945, 1.477298)
(4, 3.9807541, 1.626268)
(5, 2.0350609, 1.7327816)
(6, 1.0403746, 1.8089389)
(7, 0.53186542, 1.8633913)
(8, 0.27190247, 1.9023248)
(9, 0.13900363, 1.9301622)
(10, 0.071062043, 1.950066)
(11, 0.036328737, 1.9642972)
(12, 0.018572103, 1.9744725)
(13, 0.009494463, 1.9817479)
(14, 0.0048538623, 1.9869497)
(15, 0.0024814226, 1.990669)
(16, 0.001268565, 1.9933283)
(17, 0.00064853346, 1.9952297)
(18, 0.00033154088, 1.9965893)
(19, 0.00016949352, 1.9975613)