FastPrepImplement a Mini-Batch Training Loop

Implement a Mini-Batch Training Loop

NVIDIA logoNVIDIA● MediumFULLTIMEPHONE SCREEN
Learn

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, and 0 <= epochs <= 200.
  • x is an n by d matrix, y has n entries, and initial has d+1 entries.
  • Every feature, label and initial parameter is finite and lies in [-1,1]; 0 <= rate <= 0.01 and 1 <= batchSize <= 200.

More NVIDIA problems

See NVIDIA hiring insights
public double[] trainLinear(double[][] x, double[] y, double[] initial, double rate, int epochs, int batchSize) {
    // Write your code here
}
x[[1]]
y[1]
initial[0,0]
rate0.01
epochs1
batchSize1
expected[0.02,0.02]
Checking account…