Session Ready
Exercise

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().

Instructions
100 XP
  • 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.