Calculate a mini-batch mean gradient

Check signs and decimal points before moving to the next value. · 141 words

A running app predicts calories burned from distance run. To keep the arithmetic small, let x be kilometers and y be calories measured in units of ten. Use the one-parameter model f(w)=w*x and squared error L_i=(w*x_i-y_i)^2. A mini-batch is a small subset of training examples used for one parameter update. Its mean gradient is the average of the per-example gradients, rather than their sum. Averaging keeps the update scale similar when the batch size changes.

Take w=2 and three examples: (x,y)=(1,3), (2,5), and (3,7). Their predictions are 2, 4, and 6, so each residual, prediction minus actual, is -1. Their gradients are 2(-1)(1)=-2, 2(-1)(2)=-4, and 2(-1)(3)=-6. The mini-batch mean gradient is (-2-4-6)/3=-4. With learning rate eta=0.1, gradient descent changes the parameter to w_new=2-0.1*(-4)=2.4. Using the sum, -12, would make the same step three times as large. That is why averaging matters.

Check your recall

What is the mean gradient for the mini-batch (1,3), (2,5), and (3,7) when w=2?

Check your answer

The three gradients are -2, -4, and -6. Their mean is (-2-4-6)/3=-4, so a learning rate of 0.1 updates w from 2 to 2.4.

Sources and credits

Original KeyTouch lesson. Background reading · Google · Linear regression.