Aan de slagGa gratis aan de slag

Optimizing with gradients

You are given a loss function, \(y = x^{2}\), which you want to minimize. You can do this by computing the slope using the GradientTape() operation at different values of x. If the slope is positive, you can decrease the loss by lowering x. If it is negative, you can decrease it by increasing x. This is how gradient descent works.

The image shows a plot of y equals x squared. It also shows the gradient at x equals -1, x equals 0, and x equals 1.

In practice, you will use a high level tensorflow operation to perform gradient descent automatically. In this exercise, however, you will compute the slope at x values of -1, 1, and 0. The following operations are available: GradientTape(), multiply(), and Variable().

Deze oefening maakt deel uit van de cursus

Introduction to TensorFlow in Python

Cursus bekijken

Oefeninstructies

  • Define x as a variable with the initial value x0.
  • Set the loss function, y, equal to x multiplied by x. Do not make use of operator overloading.
  • Set the function to return the gradient of y with respect to x.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

def compute_gradient(x0):
  	# Define x as a variable with an initial value of x0
	x = ____(x0)
	with GradientTape() as tape:
		tape.watch(x)
        # Define y using the multiply operation
		y = ____
    # Return the gradient of y with respect to x
	return tape.gradient(____, ____).numpy()

# Compute and print gradients at x = -1, 1, and 0
print(compute_gradient(-1.0))
print(compute_gradient(1.0))
print(compute_gradient(0.0))
Code bewerken en uitvoeren