Demo - Regression with TensorFlow


In [1]:
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn import model_selection
from sklearn import metrics


# generate some data
np.random.seed(5)
X = np.arange(0, 100)
y = 20 + 3 * X + np.random.normal(0, 80, 100)


X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.33, random_state=42)

plt.scatter(X_train, y_train, color='green', label="training data")
plt.scatter(X_test, y_test, color='blue', label="test data")



rng = np.random

# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50


n_samples = len(X_train)

# Create tensorflow regression model
X = tf.placeholder("float")
Y = tf.placeholder("float")

# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
#  Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()

# Start training
with tf.Session() as sess:
    
    # Run the initializer
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(X_train, y_train):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: X_train, Y:y_train})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "W=", sess.run(W), "b=", sess.run(b))


    y_pred = sess.run(W) * X_test + sess.run(b)

    
order = np.argsort(X_test)

plt.plot(np.array(X_test)[order], np.array(y_pred)[order], color='red', linewidth=3, linestyle='solid', label="model")
plt.legend()

plt.draw()

print('Mean squared error: %.2f' % metrics.mean_squared_error(y_test, y_pred))
print('R^2 score: %.2f' % metrics.r2_score(y_test, y_pred))


Epoch: 0050 cost= 2916.098388672 W= 3.1398025 b= 4.0865774
Epoch: 0100 cost= 2881.823974609 W= 3.09385 b= 7.512357
Epoch: 0150 cost= 2854.056884766 W= 3.0533442 b= 10.532073
Epoch: 0200 cost= 2831.481445312 W= 3.01764 b= 13.193837
Epoch: 0250 cost= 2813.058593750 W= 2.9861686 b= 15.540047
Epoch: 0300 cost= 2797.966552734 W= 2.9584277 b= 17.608149
Epoch: 0350 cost= 2785.554931641 W= 2.9339755 b= 19.431063
Epoch: 0400 cost= 2775.307128906 W= 2.9124215 b= 21.037922
Epoch: 0450 cost= 2766.811767578 W= 2.893423 b= 22.454271
Epoch: 0500 cost= 2759.741455078 W= 2.8766763 b= 23.702742
Epoch: 0550 cost= 2753.834472656 W= 2.861915 b= 24.803204
Epoch: 0600 cost= 2748.879638672 W= 2.8489032 b= 25.773224
Epoch: 0650 cost= 2744.708251953 W= 2.837434 b= 26.628256
Epoch: 0700 cost= 2741.183349609 W= 2.8273246 b= 27.381935
Epoch: 0750 cost= 2738.195312500 W= 2.8184135 b= 28.046263
Epoch: 0800 cost= 2735.652832031 W= 2.8105586 b= 28.631866
Epoch: 0850 cost= 2733.483642578 W= 2.8036342 b= 29.148054
Epoch: 0900 cost= 2731.626953125 W= 2.7975311 b= 29.60304
Epoch: 0950 cost= 2730.032958984 W= 2.792151 b= 30.004135
Epoch: 1000 cost= 2728.662109375 W= 2.7874093 b= 30.357632
Mean squared error: 6036.08
R^2 score: 0.65

In [2]:
np.random.seed(5)
X = np.arange(0, 100)
y = np.power(X, 2) + np.random.normal(0, 500, 100)

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.33, random_state=42)

plt.scatter(X_train, y_train, color='green', label="training data")
plt.scatter(X_test, y_test, color='blue', label="test data")

rng = np.random

# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50


n_samples = len(X_train)

# Create tensorflow regression model
X = tf.placeholder("float")
Y = tf.placeholder("float")

# Set model weights
W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
#  Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()

# Start training
with tf.Session() as sess:
    
    # Run the initializer
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(X_train, y_train):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: X_train, Y:y_train})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "W=", sess.run(W), "b=", sess.run(b))


    y_pred = sess.run(W) * X_test + sess.run(b)

    
order = np.argsort(X_test)

plt.plot(np.array(X_test)[order], np.array(y_pred)[order], color='red', linewidth=3, linestyle='solid', label="model")
plt.legend()

plt.draw()

print('Mean squared error: %.2f' % metrics.mean_squared_error(y_test, y_pred))
print('R^2 score: %.2f' % metrics.r2_score(y_test, y_pred))


Epoch: 0050 cost= 646450.562500000 W= 82.065575 b= -200.9506
Epoch: 0100 cost= 588818.312500000 W= 84.45491 b= -379.07706
Epoch: 0150 cost= 544629.312500000 W= 86.56102 b= -536.0878
Epoch: 0200 cost= 510815.031250000 W= 88.417465 b= -674.4865
Epoch: 0250 cost= 484999.906250000 W= 90.05386 b= -796.48096
Epoch: 0300 cost= 465345.968750000 W= 91.49628 b= -904.01337
Epoch: 0350 cost= 450431.125000000 W= 92.76771 b= -998.79956
Epoch: 0400 cost= 439156.843750000 W= 93.88838 b= -1082.3457
Epoch: 0450 cost= 430673.250000000 W= 94.87621 b= -1155.9894
Epoch: 0500 cost= 424325.156250000 W= 95.74699 b= -1220.9056
Epoch: 0550 cost= 419607.968750000 W= 96.5145 b= -1278.124
Epoch: 0600 cost= 416131.937500000 W= 97.19104 b= -1328.56
Epoch: 0650 cost= 413598.093750000 W= 97.787384 b= -1373.0177
Epoch: 0700 cost= 411776.281250000 W= 98.313065 b= -1412.2075
Epoch: 0750 cost= 410490.437500000 W= 98.77643 b= -1446.7516
Epoch: 0800 cost= 409605.687500000 W= 99.18484 b= -1477.1989
Epoch: 0850 cost= 409019.093750000 W= 99.54488 b= -1504.0397
Epoch: 0900 cost= 408651.906250000 W= 99.862206 b= -1527.6965
Epoch: 0950 cost= 408444.906250000 W= 100.14191 b= -1548.5491
Epoch: 1000 cost= 408353.250000000 W= 100.388504 b= -1566.9321
Mean squared error: 590835.60
R^2 score: 0.93