TensorFlow: A Simple Introduction Case Study

Case Study

Suppose I have a series of x-y data points with a linear relationship between x and y. How would we fit a straight line to this data?

In the program below, train_x is a series of numbers between -1 and 1, and train_y is twice x plus 10, with a random number between 0 and 1 added. Next, we build the model. X and Y are tensor placeholders waiting to be initialized. During optimization, the XY values in the model will continuously change to data from train_x and train_y, and then the optimizer will optimize by changing the slope w and intercept b in a direction that reduces error. Through iteration, w and b will eventually make the model fit the data.

After the model is built, we start running it. First, we open a session, and we must remember to initialize all variables. Next, we iterate through all the data 10 times. In each iteration, we input a coordinate, calculate the error, and use gradient descent to correct w and b. Finally, we output the calculated values of w and b.

import tensorflow as tf
import numpy as np

train_x = np.linspace(-1, 1, 101)
train_y = 2 * train_x + np.random.rand(train_x.shape[0])  + 10

X = tf.placeholder("float")
Y = tf.placeholder("float")
w = tf.Variable(0.0, name = "w")
b = tf.Variable(0.0, name = "b")
loss = tf.square(Y - tf.multiply(X,w) - b)
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)

with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    for i in range(10):
        for x,y in zip(train_x, train_y):
            session.run(train_op, feed_dict={X:x, Y:y})
    print("w: ", session.run(w))
    print("b: ", session.run(b))

Running results: Results

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy