Gradient Descent

Machine learning course on coursera

by de Montalembert Jonathan

JavaScript

// univariate
function gradientDescent(x, y, alpha, theta_0, theta_1, iterations) {
  var m = x.length;
  var derivative_0 = 0;
  var derivative_1 = 0;

  x.forEach(function(xi, i) {
    derivative_0 += ((theta_0 * xi[0] + theta_1 * xi[1]) - y[i]) * xi[0] / m;
    derivative_1 += ((theta_0 * xi[0] + theta_1 * xi[1]) - y[i]) * xi[1] / m;
  });

  theta_0 = theta_0 - alpha * derivative_0;
  theta_1 = theta_1 - alpha * derivative_1;
  iterations = iterations - 1;

  if (iterations > 0) {
    return gradientDescent(x, y, alpha, theta_0, theta_1, iterations);
  } else {
    return {
      theta_0: theta_0,
      theta_1: theta_1
    };
  }
}

// Values to predict in that case
// theta_0 is 3
// theta_1 is 2
console.log(gradientDescent([
  [1, 1],
  [1, 2],
  [1, 3],
  [1, 4]
], [5, 7, 9, 11], 0.1, 0, 1, 500));