Last active
January 6, 2020 06:09
-
-
Save DerekChia/f12a4d60b7afc9bcb509595d3b727d6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import tensorflow as tf | |
import matplotlib.pyplot as plt | |
def generate_dataset(): | |
x_batch = np.linspace(0, 2, 100) | |
y_batch = 1.5 * x_batch + np.random.randn(*x_batch.shape) * 0.2 + 0.5 | |
return x_batch, y_batch | |
def linear_regression(): | |
x = tf.placeholder(tf.float32, shape=(None, ), name='x') | |
y = tf.placeholder(tf.float32, shape=(None, ), name='y') | |
with tf.variable_scope('lreg') as scope: | |
w = tf.Variable(np.random.normal(), name='W') | |
b = tf.Variable(np.random.normal(), name='b') | |
y_pred = tf.add(tf.multiply(w, x), b) | |
loss = tf.reduce_mean(tf.square(y_pred - y)) | |
return x, y, y_pred, loss | |
def run(): | |
x_batch, y_batch = generate_dataset() | |
x, y, y_pred, loss = linear_regression() | |
optimizer = tf.train.GradientDescentOptimizer(0.1) | |
train_op = optimizer.minimize(loss) | |
with tf.Session() as session: | |
session.run(tf.global_variables_initializer()) | |
feed_dict = {x: x_batch, y: y_batch} | |
for i in range(30): | |
_ = session.run(train_op, feed_dict) | |
print(i, "loss:", loss.eval(feed_dict)) | |
print('Predicting') | |
y_pred_batch = session.run(y_pred, {x : x_batch}) | |
plt.scatter(x_batch, y_batch) | |
plt.plot(x_batch, y_pred_batch, color='red') | |
plt.xlim(0, 2) | |
plt.ylim(0, 2) | |
plt.savefig('plot.png') | |
if __name__ == "__main__": | |
run() |
Hello Derek,
I saw your article at TowardsDataScience.com; thank you.
Newbie here; if I have a spyder working enviro and I cut and paste this code I get a return of "NameError: name 'y_batch' is not defined". But it is defined in Line 7. My spyder is in Python3.7
Would you mind a brief comment to help send me on a life of enlightenment. Thanks.T
Hi T,
I tried rerunning my code again and it works. Can you check if you have any indentation error? Otherwise, can you break the code into different parts and make sure each of the part runs?
Cheers,
Derek
Hello Derek,
Thank you for the detailed explanation. Can you please let me know how to extract the Slope and Bias from the Best Fit Line (Red Line in your plot). Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Derek,
I saw your article at TowardsDataScience.com; thank you.
Newbie here; if I have a spyder working enviro and I cut and paste this code I get a return of "NameError: name 'y_batch' is not defined". But it is defined in Line 7. My spyder is in Python3.7
Would you mind a brief comment to help send me on a life of enlightenment. Thanks.
T