Gradient descent optimization is considered an important concept in data science.
Consider the following steps to understand the implementation of gradient descent optimization −
Step 1
includes the necessary modules and declares the x and y variables through them, through which we will define the gradient descent optimization.
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)
Step 2
initializes the necessary variables and calls the optimizer to define and call the optimizer with the corresponding function.
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()
The output generated by the above line of code is shown in the screenshot below −
We can see that the calculated necessary epochs and number of iterations are shown in the output.