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

Implement Nesterov Momentum (NAG)

Optimization
Easy

Perform one Nesterov momentum update using a gradient already evaluated at the look-ahead position.

vt=μvt−1+ηgtv_t = \mu v_{t-1} + \eta g_tvt​=μvt−1​+ηgt​ wt=wt−1−vtw_t = w_{t-1} - v_twt​=wt−1​−vt​

Here, wt−1w_{t-1}wt−1​ is the current parameter array, vt−1v_{t-1}vt−1​ is the previous velocity, gtg_tgt​ is the supplied look-ahead gradient, η\etaη is lr, and μ\muμ is momentum. Return a dictionary containing new_w and new_v as NumPy arrays.

Loading visualization...

Examples

Input: w = [1.0, -1.0], v = [0.0, 0.0], grad = [0.5, -0.25], lr = 0.1, momentum = 0.9

Output: {"new_w": [0.95, -0.975], "new_v": [0.05, -0.025]}

Explanation: With no previous velocity, the first update is the learning-rate-scaled gradient.

Input: w = [1.0, 2.0], v = [0.5, -0.3], grad = [0.1, 0.2], lr = 0.1, momentum = 0.9

Output: {"new_w": [0.54, 2.25], "new_v": [0.46, -0.25]}

Input: w = [2.0], v = [0.0], grad = [0.0], lr = 0.1, momentum = 0.9

Output: {"new_w": [2.0], "new_v": [0.0]}

Hint 1

Compute new_v = momentum * v + lr * grad first.

Hint 2

Subtract new_v from the current parameters to obtain new_w.

Requirements

  • Apply the stated velocity update before updating the parameters
  • Return exactly new_w and new_v in a dictionary
  • Both returned values must be NumPy arrays with the input shape

Constraints

  • w, v, and grad have the same nonempty numeric shape
  • lr is positive
  • momentum is at least 0 and less than 1
  • Use NumPy only
Try Similar Problems
Adam OptimizerAdamw OptimizerNadam OptimizerRmsprop OptimizerGradient Descent Quadratic

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
PrevNext

Implement Nesterov Momentum (NAG)

Optimization
Easy

Perform one Nesterov momentum update using a gradient already evaluated at the look-ahead position.

vt=μvt−1+ηgtv_t = \mu v_{t-1} + \eta g_tvt​=μvt−1​+ηgt​ wt=wt−1−vtw_t = w_{t-1} - v_twt​=wt−1​−vt​

Here, wt−1w_{t-1}wt−1​ is the current parameter array, vt−1v_{t-1}vt−1​ is the previous velocity, gtg_tgt​ is the supplied look-ahead gradient, η\etaη is lr, and μ\muμ is momentum. Return a dictionary containing new_w and new_v as NumPy arrays.

Loading visualization...

Examples

Input: w = [1.0, -1.0], v = [0.0, 0.0], grad = [0.5, -0.25], lr = 0.1, momentum = 0.9

Output: {"new_w": [0.95, -0.975], "new_v": [0.05, -0.025]}

Explanation: With no previous velocity, the first update is the learning-rate-scaled gradient.

Input: w = [1.0, 2.0], v = [0.5, -0.3], grad = [0.1, 0.2], lr = 0.1, momentum = 0.9

Output: {"new_w": [0.54, 2.25], "new_v": [0.46, -0.25]}

Input: w = [2.0], v = [0.0], grad = [0.0], lr = 0.1, momentum = 0.9

Output: {"new_w": [2.0], "new_v": [0.0]}

Hint 1

Compute new_v = momentum * v + lr * grad first.

Hint 2

Subtract new_v from the current parameters to obtain new_w.

Requirements

  • Apply the stated velocity update before updating the parameters
  • Return exactly new_w and new_v in a dictionary
  • Both returned values must be NumPy arrays with the input shape

Constraints

  • w, v, and grad have the same nonempty numeric shape
  • lr is positive
  • momentum is at least 0 and less than 1
  • Use NumPy only
Try Similar Problems
Adam OptimizerAdamw OptimizerNadam OptimizerRmsprop OptimizerGradient Descent Quadratic

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.