Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
@3TTx

This comment has been minimized.

Copy link

commented Jun 25, 2019

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

@DerekChia

This comment has been minimized.

Copy link
Owner Author

commented Jun 28, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.