Import As Import As Return
Import As Import As Return
Import As Import As Return
There’s an important parameter η which scales the gradient and thus controls the step size. In
machine learning, it is called learning rate and have a strong influence on performance.
Algorithm:
Gradient Descent method’s steps are:
1. choose a starting point (initialisation)
2. calculate gradient at this point
3. make a scaled step in the opposite direction to the gradient (objective: minimise)
4. repeat points 2 and 3 until one of the criteria is met:
maximum number of iterations reached
Step size is smaller than the tolerance (due to scaling or a small
gradient). Python Code:
import tensorflow as tf
import matplotlib.pyplot as plt
def fu(x,z):
return x**2.0-x*3+z**2
def fu_min():
return 2**x-x*3+z**z
def reset():
x=tf.Variable(10.0)
z=tf.Variable(10.0)
return x,z
x,z=reset()
opt=tf.keras.optimizers.SGD(learning_rate=0.1)
for i in range(50):
with tf.GradientTape() as tape:
y = fu(x, z)
grads = tape.gradient(y, [x,z])
processed_grads = [g for g in grads]
grads_and_vars = zip(processed_grads, [x, z])
print ('y = {:.1f}, x = {:.1f}, z = {:.1f}, grads0 = {:.1f}, gra
ds1 = {:.1f} '.format(y.numpy(), x.numpy(), z.numpy(), grads[0].n
umpy(), grads[1].numpy()))
opt.apply_gradients(grads_and_vars)
Output: