Implement a Mini-Batch Training Loop
Problem statement
Implement a training loop for a supplied dataset and model setup.
For this exercise, assume the model is linear regression: prediction = sum(w[j]*x[i][j]) + bias. The initial array contains the d feature weights followed by the bias. Return those final parameters in the same order.
For this exercise, use mini-batch gradient descent on the mean squared error, without regularization or shuffling. In each epoch, process contiguous batches from the first sample to the last. Freeze the current weights while computing all residuals in one batch. The feature gradient is 2/b * sum(error[i]*x[i][j]) and the bias gradient is 2/b * sum(error[i]), where b is the actual batch size. Update every parameter simultaneously by subtracting rate * gradient. A final partial batch uses its own size.
For this exercise, zero epochs or zero rate leave the initial parameters unchanged. Double-precision outputs are accepted with relative-or-absolute error 10^-6.
Function
trainLinear(x: double[][], y: double[], initial: double[], rate: double, epochs: int, batchSize: int) → double[]Examples
Example 1
x = [[1]]y = [1]initial = [0,0]rate = 0.01epochs = 1batchSize = 1return = [0.02,0.02]The initial error is -1; weight and bias each increase by 0.02.
Example 2
x = [[0],[1]]y = [0,1]initial = [0,0]rate = 0.01epochs = 1batchSize = 2return = [0.01,0.01]Mean batch gradients are -1 for the weight and bias, so both become 0.01.
Example 3
x = [[1],[0],[1]]y = [1,0,1]initial = [0,0]rate = 0.01epochs = 1batchSize = 2return = [0.0296,0.0296]The first batch sets both parameters to 0.01; the final one-item batch uses its own size and frozen starting weights.
Constraints
- For this exercise, assume
1 <= n <= 200,1 <= d <= 8, and0 <= epochs <= 200. xis ann by dmatrix,yhasnentries, andinitialhasd+1entries.- Every feature, label and initial parameter is finite and lies in
[-1,1];0 <= rate <= 0.01and1 <= batchSize <= 200.