Created
May 16, 2016 11:20
-
-
Save tomonari-masada/ed2fbc94a9f6252036eea507b7119045 to your computer and use it in GitHub Desktop.
The simplest least squares with TensorFlow
This file contains hidden or 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 tensorflow as tf | |
x = tf.placeholder(tf.float32, [None, 1]) | |
y_ = tf.placeholder(tf.float32, [None, 1]) | |
b = tf.Variable(tf.zeros([1])) | |
w = tf.Variable(tf.zeros([1, 1])) | |
y = w * x + b | |
loss = tf.reduce_sum((y - y_) * (y - y_)) | |
train_step = tf.train.GradientDescentOptimizer(0.005).minimize(loss) | |
sess = tf.Session() | |
sess.run(tf.initialize_all_variables()) | |
for step in range(10): | |
sess.run(train_step, feed_dict={x:[[2.3],[1.7],[-3.8],[0.5],[-4.1],[-1.5],[-2.5],[6.2]], | |
y_:[[-4.4],[-3.6],[7.7],[-0.9],[8.3],[2.9],[4.9],[-12.2]]}) | |
print step, sess.run(w), sess.run(b) |
Rewrite as 'print(step, sess.run(w), sess.run(b))'.
But this code was written many years ago. Other lines do not work for the current version of TensorFlow. Please google similar codes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear Tomonari Masada,
I tried to run it in JupyterLab Notebook, but error to print:
print step, sess.run(w), sess.run(b).
The error code is:
Cell In [1], line 35
print step, sess.run(w), sess.run(b)
^
SyntaxError: invalid syntax.
Please advise.
Thanks.
Hoe Min