Skip to content

Instantly share code, notes, and snippets.

@ahmadyan
Created November 12, 2018 04:19
Show Gist options
  • Save ahmadyan/27c1195c7769fb5792f0d650680a3f79 to your computer and use it in GitHub Desktop.
Save ahmadyan/27c1195c7769fb5792f0d650680a3f79 to your computer and use it in GitHub Desktop.
Autodifferentiation of Rosenbrock function using Tensorflow
x = tf.Variable([1., 3.], tf.float32)
# Rosenbrock function
y = tf.add(tf.pow(tf.subtract(1.0, x[0]), 2.0),
tf.multiply(100.0, tf.pow(tf.subtract(x[1],tf.pow(x[0], 2.0)), 2.0)), 'y')
dx = tf.gradients(y, x)[0]
val = np.array([1, 3], dtype=np.float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print (sess.run(y, {x: val}), sess.run(dx, {x: val}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment