lekaha's blog Change the world     Wiki     Blog     Feed

01_linear_regression

Loading needed modules

In [1]:

import tensorflow as tf
import numpy as np

Initial variables

In [2]:

trX = np.linspace(-1, 1, 101)
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33 # create a y value which is approximately linear but with some random noise

X = tf.placeholder("float") # create symbolic variables
Y = tf.placeholder("float")

Modeling

In [3]:

def model(X, w):
    return tf.mul(X, w) # lr is just X*w so this model line is pretty simple

Training

In [5]:

# create a shared variable (like theano.shared) for the weight matrix
w = tf.Variable(0.0, name="weights") 
y_model = model(X, w)

cost = tf.square(Y - y_model) # use square error for cost function

# construct an optimizer to minimize cost and fit line to my data
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(cost) 

# Launch the graph in a session
with tf.Session() as sess:
    # you need to initialize variables (in this case just variable W)
    tf.initialize_all_variables().run()

    for i in range(100):
        for (x, y) in zip(trX, trY):
            sess.run(train_op, feed_dict={X: x, Y: y})

    print(sess.run(w))  # It should be something around 2
1.96822

In [ ]: