Minimize function in Tensorflow
16 Jan 2018
This post is to show how easy is to use tensorflow simply to minimize a function. Found here
import tensorflow as tf
x = tf.Variable(10.0, trainable=True)
f_x = 2 * x* x - 5 *x + 4
loss = f_x
opt = tf.train.GradientDescentOptimizer(0.1).minimize(f_x)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(100):
print(sess.run([x,loss]))
sess.run(opt)
x minimizes to the minimum at 1.25 and the los is the value at that point that is 0.875