Skip to content

Instantly share code, notes, and snippets.

@anujonthemove
Forked from DominicBreuker/gd_simple.py
Created July 1, 2018 14:27
Show Gist options
  • Save anujonthemove/616200dca9046625630e6c8b40f1e24c to your computer and use it in GitHub Desktop.
Save anujonthemove/616200dca9046625630e6c8b40f1e24c to your computer and use it in GitHub Desktop.
Simple example of gradient descent in tensorflow
import tensorflow as tf
x = tf.Variable(2, name='x', dtype=tf.float32)
log_x = tf.log(x)
log_x_squared = tf.square(log_x)
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(log_x_squared)
init = tf.initialize_all_variables()
def optimize():
with tf.Session() as session:
session.run(init)
print("starting at", "x:", session.run(x), "log(x)^2:", session.run(log_x_squared))
for step in range(10):
session.run(train)
print("step", step, "x:", session.run(x), "log(x)^2:", session.run(log_x_squared))
optimize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment