Gradient Descent Step
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) by moving in the direction of the negative gradient, which is a vector of partial derivatives ∇f=[∂f/∂x,∂f/∂y]. The update rule for gradient descent can be broken down into steps:
- Compute the gradient of the function at the current point.
- Scale the gradient by the learning rate α.
- Subtract the scaled gradient from the current point to obtain the new point.
This technique is widely used in machine learning for training models.
Example:
f(x) = x² f'(x) = 2x x₀ = 5.0 learning_rate = 0.1 iterations = 50
x = 0.0 (minimum found)
Gradient descent update rule:
xnew=xold−η⋅∇f(xold)
where η is the learning rate.
-
Initial state: x0=5.0
-
Iteration 1:
- Gradient: f′(5.0)=2×5.0=10.0
- Update: x1=5.0−0.1×10.0=4.0
-
Iteration 2:
- Gradient: f′(4.0)=2×4.0=8.0
- Update: x2=4.0−0.1×8.0=3.2
-
Pattern: Each step reduces x by 20% x→x−0.1×2x=x×(1−0.2)=0.8x
-
After many iterations: x→0 (the minimum of x2)
-
Result: x≈0.0
The function f(x)=x2 has its minimum at x=0 where f′(x)=0.
Constraints:
- Starting point is [x, y]
- Learning rate α > 0
- Return new point [x', y'] rounded to 4 decimal places