v9.1.1
Menu

Gradient Descent Step

mediumFREECalculus & OptimizationOptimizationCV: Mathematical Foundations

Implement a single step of gradient descent for a 2D function, a fundamental optimization technique in machine learning. This process involves iteratively updating parameters to minimize a function.

Gradient descent is based on the concept of minimizing a function f(x,y)f(x, y) by moving in the direction of the negative gradient, which is a vector of partial derivatives ∇f=[∂f/∂x,∂f/∂y]\nabla f = [\partial f/\partial x, \partial f/\partial y]. The update rule for gradient descent can be broken down into steps:

  1. Compute the gradient of the function at the current point.
  2. Scale the gradient by the learning rate α\alpha.
  3. Subtract the scaled gradient from the current point to obtain the new point.
xnew=xold−α∇f(xold)\mathbf{x}_{new} = \mathbf{x}_{old} - \alpha \nabla f(\mathbf{x}_{old})

This technique is widely used in machine learning for training models.

Example

Input
gradient_step([5.0, 3.0], [10.0, 6.0], 0.1)
Output
[4.0, 2.4]
Reasoning
  • Inputs: point is the current position (x,y)=(5,3)(x, y) = (5, 3); gradient is ∇f\nabla f already evaluated at that position, [10,6][10, 6] (for example, f(x,y)=x2+y2f(x, y) = x^2 + y^2 has ∇f=[2x,2y]\nabla f = [2x, 2y]); learning_rate is α=0.1\alpha = 0.1. The gradient is handed to you, so nothing needs differentiating.
  • Scale: α∇f=0.1×[10,6]=[1.0,0.6]\alpha \nabla f = 0.1 \times [10, 6] = [1.0, 0.6], the size of the step along each axis.
  • Subtract each component from its own coordinate:
    • xx: 5.0−1.0=4.05.0 - 1.0 = 4.0
    • yy: 3.0−0.6=2.43.0 - 0.6 = 2.4
  • Output: the new point [4.0,2.4][4.0, 2.4], rounded to 4 decimal places (here no digits are lost).
  • Notice the minus sign: the gradient points uphill, so moving against it lowers ff. For x2+y2x^2 + y^2 this step gives 0.8 x0.8\,\mathbf{x}, 20% closer to the minimum at the origin. With α=0.01\alpha = 0.01 the same gradient moves the point only to [4.9,2.94][4.9, 2.94].

Constraints

  • Starting point is [x, y]
  • Learning rate α > 0
  • Return new point [x', y'] rounded to 4 decimal places

Learn this in

More from CV: Mathematical Foundations

solution.py

Test results

Sign in to run your code against the test cases.