FastPrepGradient Descent for Linear Regression

Gradient Descent for Linear Regression

Microsoft logoMicrosoft● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Implement gradient descent for a linear-regression model without a machine-learning or automatic-differentiation package.

features contains n samples, each with d real-valued features. targets[i] is the target for sample i. The model prediction is prediction[i] = bias + sum_j(weights[j] * features[i][j]).

Loss and updates

Use mean squared error L = sum_i((prediction[i] - targets[i])^2) / n, with no factor of one half and no regularization.

  1. Initialize every feature weight and the bias to 0.0.
  2. Perform exactly iterations full-batch updates. For each update, calculate every loss derivative using all samples and the same current parameter snapshot.
  3. Update every parameter simultaneously using parameter = parameter - learningRate * derivative. Do not change parameters between samples or coordinates, shuffle samples, use momentum or stop early.

Output

Return a double[] containing the d final feature weights in feature-column order, followed by the final bias. Zero iterations or zero learning rate return all zeros. Do not mutate the input arrays. Each returned value is accepted with absolute or relative error at most 10^-6.

Function

trainLinearRegression(features: double[][], targets: double[], learningRate: double, iterations: int) → double[]

Examples

Example 1

features = [[1.0]]targets = [1.0]learningRate = 0.01iterations = 1return = [0.02,0.02]

The initial error is -1. Both MSE derivatives are -2, so one update produces weight 0.02 and bias 0.02.

Example 2

features = [[0.0],[1.0]]targets = [0.0,1.0]learningRate = 0.01iterations = 2return = [0.0198,0.0197]

The first update gives weight 0.01 and bias 0.01. The next errors are 0.01 and -0.98, giving weight gradient -0.98 and bias gradient -0.97. The simultaneous second update returns [0.0198,0.0197].

Example 3

features = [[1.0,-1.0],[-1.0,1.0]]targets = [1.0,-1.0]learningRate = 0.005iterations = 0return = [0.0,0.0,0.0]

Zero iterations perform no updates. Return two zero feature weights followed by the zero bias: [0.0,0.0,0.0].

Constraints

  • 1 <= n <= 200 and 1 <= d <= 8.
  • features.length == targets.length == n, and every feature row has exactly d entries.
  • All feature and target values are finite and lie in [-1.0, 1.0].
  • 0 <= learningRate <= 0.01 and 0 <= iterations <= 200.
  • Use double-precision arithmetic and implement the derivatives directly.

More Microsoft problems

See Microsoft hiring insights
public double[] trainLinearRegression(double[][] features, double[] targets, double learningRate, int iterations) {
    // Return feature weights followed by the bias.
}
features[[1.0]]
targets[1.0]
learningRate0.01
iterations1
expected[0.02,0.02]
Checking account…