TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Gradient Descent for a 1D Quadratic

Optimization
Easy

Implement vanilla gradient descent to minimize the one-dimensional quadratic

f(x)=ax2+bx+cf(x) = ax^2 + bx + cf(x)=ax2+bx+c

Starting from x0x_0x0​, determine the gradient at the current value of xxx and apply the following update exactly steps times:

x←x−mathrmlr,mathrmgradientx ← x - mathrm{lr} , mathrm{gradient}x←x−mathrmlr,mathrmgradient

Return the final value of xxx as a Python float. Deriving the gradient of the quadratic is part of the task.

Loading visualization...

Examples

Input: a = 1.0, b = -4.0, c = 3.0, x0 = 0.0, lr = 0.1, steps = 50

Output: 1.999971

Explanation: Repeated updates move x toward the minimum without jumping directly to it.

Input: a = 2.0, b = 8.0, c = 0.0, x0 = 10.0, lr = 0.05, steps = 200

Output: -2.0

Hint 1

Use a loop that performs exactly steps parameter updates.

Hint 2

Compute the gradient at the current x before applying the learning-rate update.

Requirements

  • Compute the derivative at the current value of x on every iteration
  • Apply the gradient descent update exactly steps times
  • Do not return the closed-form minimizer instead of performing the updates
  • Return the final value as a Python float

Constraints

  • Time limit: 200 ms; Memory: 64 MB
  • Pure Python / NumPy (no ML libs)
Try Similar Problems
Adam OptimizerRmsprop OptimizerNesterov MomentumAdamw OptimizerGradient Clipping

Sign in to take notes on this problem

Case 1
Case 2

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.
PrevNext

Implement Gradient Descent for a 1D Quadratic

Optimization
Easy

Implement vanilla gradient descent to minimize the one-dimensional quadratic

f(x)=ax2+bx+cf(x) = ax^2 + bx + cf(x)=ax2+bx+c

Starting from x0x_0x0​, determine the gradient at the current value of xxx and apply the following update exactly steps times:

x←x−mathrmlr,mathrmgradientx ← x - mathrm{lr} , mathrm{gradient}x←x−mathrmlr,mathrmgradient

Return the final value of xxx as a Python float. Deriving the gradient of the quadratic is part of the task.

Loading visualization...

Examples

Input: a = 1.0, b = -4.0, c = 3.0, x0 = 0.0, lr = 0.1, steps = 50

Output: 1.999971

Explanation: Repeated updates move x toward the minimum without jumping directly to it.

Input: a = 2.0, b = 8.0, c = 0.0, x0 = 10.0, lr = 0.05, steps = 200

Output: -2.0

Hint 1

Use a loop that performs exactly steps parameter updates.

Hint 2

Compute the gradient at the current x before applying the learning-rate update.

Requirements

  • Compute the derivative at the current value of x on every iteration
  • Apply the gradient descent update exactly steps times
  • Do not return the closed-form minimizer instead of performing the updates
  • Return the final value as a Python float

Constraints

  • Time limit: 200 ms; Memory: 64 MB
  • Pure Python / NumPy (no ML libs)
Try Similar Problems
Adam OptimizerRmsprop OptimizerNesterov MomentumAdamw OptimizerGradient Clipping

Sign in to take notes on this problem

Case 1
Case 2

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

Accepts: number

You must run your code first.