linear regression with tensorflow
TOC
- Cost Function with TensorFlow
- Gradient Descent with TensorFlow
- Modify Gradient Descent with TensorFlow
- Simple Linear Regression with TensorFlow
- Linear Regression with Placeholder in TensorFlow
Cost Function with TensorFlow
import tensorflow as tf
import matplotlib.pyplot as plt
# Y = X
X = [1,2,3]
Y = [1,2,3]
# Weight
W = tf.placeholder(tf.float32)
# Simplified hypothesis for linear model X * W
hypothesis = X * W
# Cost function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Launch the graph in a session
sess = tf.Session()
# Initializes global variables in the graph
sess.run(tf.global_variables_initializer())
# Variables for plotting cost function
W_val = []
cost_val = []
for i in range(-30, 50):
feed_W = i * 0.1
curr_cost, curr_W = sess.run([cost, W], feed_dict={W:feed_W})
W_val.append(curr_W)
cost_val.append(curr_cost)
# Show the cost function
plt.plot(W_val, cost_val)
plt.title("Cost function")
plt.xlabel("W")
plt.ylabel("Cost(W)")
plt.show()
Image 1. Cost function with TensorFlow
Gradient Descent with TensorFlow
import tensorflow as tf
import matplotlib.pyplot as plt
# y = x
x_data = [1,2,3]
y_data = [1,2,3]
# Weight
W = tf.Variable(tf.random_normal([1]), name="weight")
# [1] means its rank is 1, and the number of data is 1.
# Placeholder for X & Y
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# Simplified hypothesis
hypothesis = X * W
# Cost function: Mean Square Error
cost = tf.reduce_sum(tf.square(hypothesis - Y))
# Minimize error
# Gradient descent using derivative
# W -= learning_rate * dereivative
learning_rate = 0.01
gradient = tf.reduce_mean((W * X - Y) * X)
descent = W - learning_rate * gradient
update = W.assign(descent)
# It can be relpaced by
# optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
# train = optimizer.minimize(cost)
# Launch the graph in a session
sess = tf.Session()
# Initializes global variables in the graph
sess.run(tf.global_variables_initializer())
# For graph
steps = []
outputs = {"cost" : [], "weight" : []}
# Train
for step in range(51):
# W is a variable, update is an operation to update W
# After update, W will be updated.
sess.run(update, feed_dict={X: x_data,Y: y_data})
_cost = sess.run(cost, feed_dict={X: x_data, Y: y_data})
_W = sess.run(W)
steps.append(step)
outputs["cost"].append(_cost)
outputs["weight"].append(_W)
if step in (1, 10, 20, 30, 40, 50):
print("Step: {0:4d}, Cost: {1:13.10f}, Weight: {2:13.10f}".\
format(step, _cost, _weight[0]))
# Draw graph
for k, v in outputs.items():
plt.plot(steps, v)
plt.title(k)
plt.xlabel("step")
plt.ylabel(k)
plt.show()
Step: 1, Cost: 3.6350340843, Weight: 1.0379939079
Step: 10, Cost: 1.5378515720, Weight: 1.0379939079
Step: 20, Cost: 0.5913023949, Weight: 1.0379939079
Step: 30, Cost: 0.2273551524, Weight: 1.0379939079
Step: 40, Cost: 0.0874179304, Weight: 1.0379939079
Step: 50, Cost: 0.0336120725, Weight: 1.0379939079
Image 2. Weight
Image 3. Cost
Modify Gradient Descent with TensorFlow
import tensorflow as tf
import matplotlib.pyplot as plt
# y = x
X = [1,2,3]
Y = [1,2,3]
# Weight for manual gradient and default gradient, default weight is 5
m_W = tf.Variable(5.)
d_W = tf.Variable(5.)
# Simplified hypothesis for manual gradient and default gradient
m_hypo = X * m_W
d_hypo = X * d_W
# Manual gradient
# This multiplies 2 to what we used before.
manual_gradient = tf.reduce_mean((m_W * X - Y) * X) * 2
descent = m_W - learning_rate * manual_gradient
update = m_W.assign(descent)
# Cost function for default gradient
d_cost = tf.reduce_mean(tf.square(d_hypo - Y))
# Define default gradient descent node
d_optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
# Get default gradient descent node
gd = d_optimizer.compute_gradients(d_cost)
# gd can be modified, but we use default gd
# d_W will be update with do_gradients
do_gradients = d_optimizer.apply_gradients(gd)
# Launch the graph in a session
sess = tf.Session()
# Initializes global variables in the graph
sess.run(tf.global_variables_initializer())
# For graph
steps = []
outputs = {"manual" : [], "default": []}
for step in range(101):
M = sess.run(m_W)
D = sess.run(d_W)
steps.append(step)
outputs["manual"].append(M)
outputs["default"].append(D)
if step in (1, 10, 100):
print(\
"Step: {0:4d}, Manual Weight: {1:13.10f}, Default Weight: {2:13.10f}".\
format(step, M, D))
sess.run(update)
sess.run(do_gradients)
# Draw graph
for k, v in outputs.items():
plt.plot(steps, v, label=k)
plt.title("Weights")
plt.xlabel("step")
plt.ylabel("Weight")
plt.legend()
plt.show()
Step: 1, Manual Weight: 4.6266665459, Default Weight: 4.6266665459
Step: 10, Manual Weight: 2.5015385151, Default Weight: 2.5015385151
Step: 100, Manual Weight: 1.0002222061, Default Weight: 1.0002222061
Image 4. Gradient descent
Simple Linear Regression with TensorFlow
import tensorflow as tf
import matplotlib.pyplot as plt
# y = x
x_train = [1,2,3]
y_train = [1,2,3]
# Variable is trainable variable for tensorflow.
# It will be upated automatically by tensorflow.
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
# [1] means its rank is 1, and the number of data is 1.
# Hypothesis: W * x + b
hypothesis = x_train * W + b
# reduce_mean calculates average.
cost = tf.reduce_mean(tf.square(hypothesis - y_train))
# Training system
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
#Until this, we build graph.
# Launch the graph in a session
sess = tf.Session()
# Initialize global variables in the grpah.
sess.run(tf.global_variables_initializer())
# For graph
steps = []
outputs = {"cost" : [], "weight" : [], "bias" : []}
# Train
for step in range(1001):
sess.run(train)
_cost = sess.run(cost)
_weight = sess.run(W)
_bias = sess.run(b)
steps.append(step)
outputs["cost"].append(_cost)
outputs["weight"].append(_weight[0])
outputs["bias"].append(_bias[0])
if step in (1, 10, 100, 1000):
print("Step: {0:4d}, Cost: {1:13.10f}, Weight: {2:13.10f}, Bias: {3:13.10f}".\
format(step, _cost, _weight[0], _bias[0]))
# Draw graph
for k, v in outputs.items():
plt.plot(steps, v)
plt.title(k)
plt.xlabel("step")
plt.ylabel(k)
plt.show()
Step: 1, Cost: 13.5835304260, Weight: -0.0048628896, Bias: -1.5833736658
Step: 10, Cost: 1.7461298704, Weight: 0.9168848991, Bias: -1.1534380913
Step: 100, Cost: 0.0814563707, Weight: 1.3314682245, Bias: -0.7535393834
Step: 1000, Cost: 0.0010701300, Weight: 1.0379939079, Bias: -0.0863692015
Image 5. Bias
Image 6. Weight
Image 7. Cost
Linear Regression with Placeholder in TensorFlow
import tensorflow as tf
import matplotlib.pyplot as plt
# Placeholder can be used as input data
X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])
# [None] means it is 1 deimensional tensor
# and its number of values is not determined.
# Variable is trainable variable for tensorflow.
# It will be upated automatically by tensorflow.
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
# [1] means its rank is 1, and its total count is 1.
# Hypothesis: W * x + b
hypothesis = X * W + b
# reduce_mean calculates average.
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Training system
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
#Until this, we build graph.
# Launch the graph in a session
sess = tf.Session()
# Initialize global variables in the grpah.
sess.run(tf.global_variables_initializer())
# For graph
steps = []
outputs = {"cost" : [], "weight" : [], "bias" : []}
# Train
for step in range(1001):
# Return order is the same as the input order of run
_cost, _weight, _bias, _ = sess.run([cost, W, b, train],\
feed_dict={X:[1,2,3], Y:[1,2,3]})
steps.append(step)
outputs["cost"].append(_cost)
outputs["weight"].append(_weight[0])
outputs["bias"].append(_bias[0])
if step in (1, 10, 100, 1000):
print(\
"Step: {0:4d}, Cost: {1:13.10f}, Weight: {2:13.10f}, Bias: {3:13.10f}".\
format(step, _cost, _weight[0], _bias[0]))
# After training, we can test hypothesis with new input
print(sess.run(hypothesis, feed_dict={X:[5]}))
print(sess.run(hypothesis, feed_dict={X:[1.8, 3.2]}))
# Draw graph
for k, v in outputs.items():
plt.plot(steps, v)
plt.title(k)
plt.xlabel("step")
plt.ylabel(k)
plt.show()
Step: 1, Cost: 0.1927358508, Weight: 0.8746519685, Bias: 0.6404360533
Step: 10, Cost: 0.0642042160, Weight: 0.7945960760, Bias: 0.5903141499
Step: 100, Cost: 0.0304005835, Weight: 0.7979824543, Bias: 0.4592365921
Step: 1000, Cost: 0.0003993845, Weight: 0.9768448472, Bias: 0.0526370667
[ 4.93686152]
[ 1.81095779 3.17854071]
Image 8. Bias
Image 9. Weight
Image 10. Cost
COMMENTS