Class05 Answer:

Write a simple script which compares TensorFlow to scikit-learn

This is an easy lab.

To a previous demo, I just needed to add a few lines of syntax of the scikit-learn API.


"""
demo11.py

This script should compare TensorFlow to scikit-learn
Ref:
http://ml4.us/cclasses/class05tf13

Demo:
python demo11.py
"""

import tensorflow as tf
import numpy as np

# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b

# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# Before starting, initialize the variables.  We will 'run' this first.
#init = tf.initialize_all_variables()
init  = tf.global_variables_initializer() # better than above line.

# Launch the graph.
sess = tf.Session()
sess.run(init)

# Fit the line.
for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(W), sess.run(b))

# Learns best fit is W: [0.1], b: [0.3]
print('TensorFlow calculates W to be:')
print(sess.run(W))
print('TensorFlow calculates b to be:')
print(sess.run(b))

# I should use scikit-learn to fit a line to x_data and y_data.

# Start by reshaping x_data to have rows and one column:
x_data1col_a = x_data.reshape((len(x_data) ,1))

# I should use the scikit-learn API now:
from sklearn import linear_model
linr_model = linear_model.LinearRegression()
linr_model.fit(x_data1col_a, y_data)
# That was easy. I needed only 3 lines of syntax.

print('scikit-learn calculates W to be:')
print(linr_model.coef_[0])
print('scikit-learn calculates b to be:')
print(linr_model.intercept_)

'bye'

I ran the above syntax and I saw this:


ml4@ub100:~/ml4/public/class05tf $ python demo11.py
0 [-0.3054198] [ 0.67117405]
20 [-0.01335273] [ 0.35730076]
40 [ 0.07279795] [ 0.31375086]
60 [ 0.09347214] [ 0.3032999]
80 [ 0.09843346] [ 0.30079192]
100 [ 0.09962407] [ 0.30019006]
120 [ 0.09990977] [ 0.30004561]
140 [ 0.09997834] [ 0.30001098]
160 [ 0.0999948] [ 0.30000263]
180 [ 0.09999876] [ 0.30000064]
200 [ 0.09999971] [ 0.30000016]
TensorFlow calculates W to be:
[ 0.09999971]
TensorFlow calculates b to be:
[ 0.30000016]
scikit-learn calculates W to be:
0.100000008941
scikit-learn calculates b to be:
0.299999992811
ml4@ub100:~/ml4/public/class05tf $
ml4@ub100:~/ml4/public/class05tf $
ml4@ub100:~/ml4/public/class05tf $

Class05 Lab


ml4.us About Blog Contact Class01 Class02 Class03 Class04 Class05 Class06 Class07 Class08 Class09 Class10 dan101 Forum Google Hangout Vboxen