Linear regression model is one of the simplest regression models. It assumes linear relationship between X and Y. The output equation is defined as follows: $$\hat{y} = WX + b$$
In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
In [2]:
# Training Parameters
learning_rate = 1e-1
training_epochs = 2000
display_step = 200
In [3]:
# Load data. Advertising dataset from "An Introduction to Statistical Learning",
# textbook by Gareth James, Robert Tibshirani, and Trevor Hastie
import numpy as np
data = pd.read_csv('dataset/Advertising.csv',index_col=0)
train_X = data[['TV']].values
train_Y = data.Sales.values
train_Y = train_Y[:,np.newaxis]
n_samples = train_X.shape[0]
print n_samples
print train_X.shape, train_Y.shape
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.show()
In [4]:
import tensorflow as tf
# Define tf Graph Inputs
X = tf.placeholder("float",[None,1])
y = tf.placeholder("float",[None,1])
# Create Model variables
# Set model weights
W = tf.Variable(np.random.randn(), name="weight")
b = tf.Variable(np.random.randn(), name="bias")
# Construct a linear model
y_pred = tf.add(tf.mul(X, W), b)
In [5]:
# Minimize the squared errors
cost = tf.reduce_sum(tf.pow(y_pred-y,2))/(2*n_samples) #L2 loss
# Define the optimizer
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost) #Gradient descent
In [6]:
# Add summary ops to collect data
w_hist = tf.histogram_summary("weights", W)
b_hist = tf.histogram_summary("biases", b)
y_hist = tf.histogram_summary("y", y_pred)
cost_summary = tf.scalar_summary("cost", cost)
# Merge all the summaries and write them out to /tmp/linear_regression
merged = tf.merge_all_summaries()
In [7]:
# Initializing the variables
init = tf.initialize_all_variables()
# Launch the graph
with tf.Session() as sess:
writer = tf.train.SummaryWriter("tmp/linear_regression", sess.graph)
sess.run(init)
# Fit all training data
for epoch in range(training_epochs):
result = sess.run([optimizer, cost, merged], feed_dict={X: train_X, y: train_Y})
summary_str = result[2]
writer.add_summary(summary_str, epoch)
#Display logs per epoch step
if epoch % display_step == 0:
print "Epoch:", '%04d' % (epoch+1), "cost=", \
"{:.9f}".format(sess.run(cost, feed_dict={X: train_X, y:train_Y})), \
"W=", sess.run(W), "b=", sess.run(b)
print "Optimization Finished!"
print "cost=", sess.run(cost, feed_dict={X: train_X, y: train_Y}), \
"W=", sess.run(W), "b=", sess.run(b)
#Graphic display
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
plt.legend()
plt.show()
In [ ]: