In this notebook, we introduce TensorFlow by fitting a line of the form y=m*x+b point by point. This is a derivation of Jared Ostmeyer's Naked Tensor code.
In [1]:
import numpy as np
np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf
tf.set_random_seed(42)
In [2]:
xs = [0., 1., 2., 3., 4., 5., 6., 7.] # feature (independent variable)
ys = [-.82, -.94, -.12, .26, .39, .64, 1.02, 1.] # labels (dependent variable)
In [3]:
fig, ax = plt.subplots()
_ = ax.scatter(xs, ys)
In [4]:
m = tf.Variable(-0.5)
b = tf.Variable(1.0)
In [5]:
ys_model = m*xs + b
total_error = tf.reduce_sum((ys-ys_model)**2)
In [6]:
optimizer_operation = tf.train.GradientDescentOptimizer(learning_rate=0.005).minimize(total_error)
In [7]:
initializer_op = tf.global_variables_initializer()
In [8]:
with tf.Session() as sess:
sess.run(initializer_op)
n_epochs = 100
for iteration in range(n_epochs):
sess.run(optimizer_operation)
slope, intercept = sess.run([m, b])
In [9]:
slope
Out[9]:
In [10]:
intercept
Out[10]:
In [11]:
y_hat = slope*np.array(xs) + intercept
In [12]:
pd.DataFrame(list(zip(ys, y_hat)), columns=['y', 'y_hat'])
Out[12]:
In [13]:
fig, ax = plt.subplots()
ax.scatter(xs, ys)
x_min, x_max = ax.get_xlim()
y_min, y_max = intercept, intercept + slope*(x_max-x_min)
ax.plot([x_min, x_max], [y_min, y_max])
_ = ax.set_xlim([x_min, x_max])
In [ ]: