Batch Gradient vs. One Example's Gradient
Kestrel Freight's deliveries no longer fit in memory, so the trainer has to choose how much data one move is allowed to look at. Full-batch training reads every delivery and moves on the average of what they all say. Stochastic training reads one delivery and moves on that alone: far cheaper per move, far noisier. To show the team the difference, the tool prints both figures for the slope dial on the same line and the same data. Each delivery's own figure is the steepness of that one delivery's squared residual — one delivery's worth, with nothing halved and nothing averaged inside it — where a residual is the real cost minus the line's quote.
Task: Print the full-batch figure for the slope dial and the single-delivery figure for the chosen delivery.
Input
The first line holds two numbers separated by a single space: the line's intercept, then its slope. The second line holds one integer n, the number of deliveries. Then come n lines, each holding a distance and the real cost, separated by a single space. The last line holds one integer j, which picks the delivery for the single-delivery figure, counting from 0 so that 0 is the first delivery listed.
Output
Two lines, in this order. First the word batch, a single space, and the average of every delivery's figure, rounded to 4 decimal places. Then the word stochastic, a single space, and delivery j's own figure, rounded to 4 decimal places. Print what the arithmetic produces without tidying it: if a value comes out as a negative zero, the minus sign stays.
Example:
Input:
0 0
3
1 2
2 4
3 6
1
Output:
batch -18.6667
stochastic -16.0000
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in