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...
w2-10w+25 ---------------------- (i)
i.e.
(w - 5)2
Thus, the coefficients from (i) would be 1, -10 and 25
w = tf.Variable([0], dtype=tf.float32)
cost = (x[0][0] * w ** 2) + (x[1][0] * w) + x[2][0] # (w-5)^2
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
print(session.run(w))
The output would be 4.9999886, i.e. closest to 5
Try changing the line 4 to 1, -20, 100. Then the outcome would be 9.999977 and equation would be
(w - 10)2
These weights are multiplied with input (x) to compute cost, thus minimizing it.
The equation is below...
w2-10w+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 4w = 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))
Try changing the line 4 to 1, -20, 100. Then the outcome would be 9.999977 and equation would be
(w - 10)2
Comments