One Step of 1D Gradient Descent

Basics
BasicsGradient DescentOptimizationGoogleMetaAmazon

Problem

For the 1D quadratic loss L(w)=(w−target)2L(w) = (w - \text{target})^2, implement gd_one_step(w, target, lr) that computes the gradient dLdw=2(w−target)\frac{dL}{dw} = 2(w - \text{target}) and returns (w_new, grad) after one Gradient Descent step wnew=w−lr⋅gradw_{\text{new}} = w - \text{lr} \cdot \text{grad}.

Why It Matters: The single update w←w−η⋅∇L(w)w \leftarrow w - \eta \cdot \nabla L(w) is the optimization engine behind logistic regression, linear models, and every deep neural network.

Sample Inputs & Expected Outputs

Example 1 — Step down from above target

>>> gd_one_step(w=10.0, target=4.0, lr=0.1)
(8.8, 12.0)

grad = 2(10 - 4) = 12.0 -> w_new = 10.0 - 0.112.0 = 8.8.

Example 2 — Step up from below target

>>> gd_one_step(w=-2.0, target=3.0, lr=0.25)
(0.5, -10.0)

grad = 2(-2 - 3) = -10.0 -> w_new = -2.0 - 0.25*(-10.0) = 0.5.*

Example 3 — Already at optimum

>>> gd_one_step(w=4.0, target=4.0, lr=0.1)
(4.0, 0.0)

Zero gradient means w does not move.

Constraints & Edge Cases

  • Return (w_new, grad) as Python floats.

Constraints & Evaluation Criteria

  • Return `(w_new, grad)` as Python floats.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
w=10, target=4, lr=0.1
Expected Output / Criteria
(8.8, 12.0)