Logistic Regression
Train a logistic regression by gradient descent. There is one weight per column of X and one bias, all starting at 0. The score of an example is its row of X times the weights, plus the bias, and the loss is the binary cross-entropy of those scores against y (zeros and ones), averaged over the examples. Take steps steps with torch.optim.SGD and learning rate lr. Return the weights followed by the bias, as one 1-D tensor.
Example 1
X [[0.0],[1.0],[2.0],[3.0]]
y [0.0,0.0,1.0,1.0]
lr 0.5
steps 10
returns [0.8601177930831909,-0.6169396638870239]
Constraints
- 4 to 8 examples, 1 or 2 columns
1 <= steps <= 25- 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
- Skip the PaddingHard
- Slide a KernelHard
- The Second DerivativeHard
- Windows and TargetsHard
- A Table of ZerosBeginner
- Add Them UpBeginner
- Biggest and WhereBeginner
- Column AveragesBeginner