Skip to content

Instantly share code, notes, and snippets.

@tomonari-masada
Created May 16, 2016 11:20
Show Gist options
  • Save tomonari-masada/ed2fbc94a9f6252036eea507b7119045 to your computer and use it in GitHub Desktop.
Save tomonari-masada/ed2fbc94a9f6252036eea507b7119045 to your computer and use it in GitHub Desktop.
The simplest least squares with TensorFlow
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)
@tomonari-masada
Copy link
Author

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