Fit a Line
Fit the line w * x + b to the points (x, y) by gradient descent. Start from w = 0 and b = 0. The loss is the mean of the squared differences from y. Take steps steps with torch.optim.SGD and learning rate lr, and return [w, b].
Example 1
x [1.0,2.0,3.0]
y [2.0,4.0,6.0]
lr 0.1
steps 1
returns [1.8666667,0.8]
Example 2
x [0.0,1.0,2.0]
y [1.0,3.0,5.0]
lr 0.1
steps 30
returns [1.9626495838165283,1.0518208742141724]
Constraints
- 3 to 8 points
1 <= steps <= 30- 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
- How Often RightMedium
- In BatchesMedium
- Many OutputsMedium
- Mean Squared ErrorMedium
- One Step DownhillMedium
- Two LayersMedium
- Two ParametersMedium
- Unit RowsMedium