Tensorflow - Simplest Gradient Descent using GradientDescentOptimizer
Below is the code to find the minimum possible value of weight (w) such that the outcome of equation is closest to 0, using Gradient Descent method. These weights are multiplied with input (x) to compute cost, thus minimizing it. The equation is below... w 2 - 10 w + 25 ---------------------- (i) i.e. ( w - 5 ) 2 Thus, the coefficients from (i) would be 1, -10 and 25 import numpy as np import tensorflow as tf coefficients = np.array([[ 1 ], [ - 10 ], [ 25 ]]) # try 1, -20, 100 -- line 4 w = tf.Variable([ 0 ], dtype = tf.float32) x = tf.placeholder(tf.float32, [ 3 , 1 ]) cost = (x[ 0 ][ 0 ] * w ** 2 ) + (x[ 1 ][ 0 ] * w) + x[ 2 ][ 0 ] # (w-5)^2 train = tf.train.GradientDescentOptimizer( 0.01 ).minimize(cost) init = tf.global_variables_initializer() with tf.Session() as session: session.run(init) print (session.run(w)) for i in range ( 1000 ): session.run(train, feed_dict = {x:coefficients}) print (session.run(w)) T