PIXELBANKv9.1.0
Menu

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.0βˆ’0.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.0βˆ’0.1Γ—8.0=3.2x_2 = 4.0 - 0.1 \times 8.0 = 3.2
  4. Pattern: Each step reduces xx by 20% xβ†’xβˆ’0.1Γ—2x=xΓ—(1βˆ’0.2)=0.8xx \rightarrow x - 0.1 \times 2x = x \times (1 - 0.2) = 0.8x

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

  6. Result: xβ‰ˆ0.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
solution.py

Test Results

0/0
Run code to see test results.