PIXELBANKv8.2.1
Menu

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)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:
f(x) = x²
f'(x) = 2x
x₀ = 5.0
learning_rate = 0.1
iterations = 50
Output:
x = 0.0 (minimum found)
Reasoning:

Gradient descent update rule:

xnew=xoldηf(xold)x_{\text{new}} = x_{\text{old}} - \eta \cdot \nabla f(x_{\text{old}})

where η\eta is the learning rate.

  1. Initial state: x0=5.0x_0 = 5.0

  2. Iteration 1:

    • Gradient: f(5.0)=2×5.0=10.0f'(5.0) = 2 \times 5.0 = 10.0
    • Update: x1=5.00.1×10.0=4.0x_1 = 5.0 - 0.1 \times 10.0 = 4.0
  3. Iteration 2:

    • Gradient: f(4.0)=2×4.0=8.0f'(4.0) = 2 \times 4.0 = 8.0
    • Update: x2=4.00.1×8.0=3.2x_2 = 4.0 - 0.1 \times 8.0 = 3.2
  4. Pattern: Each step reduces xx by 20% xx0.1×2x=x×(10.2)=0.8xx \rightarrow x - 0.1 \times 2x = x \times (1 - 0.2) = 0.8x

  5. After many iterations: x0x \rightarrow 0 (the minimum of x2x^2)

  6. Result: x0.0x \approx 0.0

The function f(x)=x2f(x) = x^2 has its minimum at x=0x = 0 where f(x)=0f'(x) = 0.

Constraints:

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

Test Results

0/0
Run code to see test results.