Keep Rolling
Minimize the sum of (w - target)² over the elements, starting from w = start, with torch.optim.SGD given the learning rate lr and the momentum mu. Take steps steps and return w.
Example 1
start [0.0]
target [1.0]
lr 0.1
mu 0.9
steps 2
returns [0.54]
The first step moves by 0.1 times the gradient, to 0.2. The second adds 0.9 of the old velocity to the new gradient and moves further, to 0.54.
Example 2
start [0.0]
target [1.0]
lr 0.1
mu 0.9
steps 1
returns [0.2]
Constraints
- 1 to 4 elements
1 <= steps <= 15- Answers within 0.001 are accepted
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More PyTorch problems
- Learning Rate StepsHard
- Logistic RegressionHard
- Skip the PaddingHard
- Slide a KernelHard
- The Second DerivativeHard
- Windows and TargetsHard
- A Table of ZerosBeginner
- Add Them UpBeginner