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:
This technique is widely used in machine learning for training models.
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:
Iteration 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.