/// <summary> /// /// </summary> /// <param name="theta"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="lambda"></param> /// <returns>Item1 - cost, Item2 - grad</returns> public Tuple <double, double[]> CostFunction() { int m = y.Length; int n = Theta.Length; double[] one_minus_y = MLMath.ItemCalculate(1, y, (a, b) => a - b); double[] h = H(); double[] log_h = MLMath.ItemCalculate(h, i => Math.Log(i)); double[] one_minus_h = MLMath.ItemCalculate(1, h, (a, b) => a - b); double[] log_1_minus_h = MLMath.ItemCalculate(one_minus_h, i => Math.Log(i)); double cost = (1.0 / m) * (-MLMath.ItemMultiplyAndSum(y, log_h) - MLMath.ItemMultiplyAndSum(one_minus_y, log_1_minus_h)); //Regularization double[] theta_reg = new double[n]; theta_reg[0] = 0; for (int i = 1; i < n; i++) { theta_reg[i] = Theta[i]; } double reg = Lambda / (2 * m) * (MLMath.ItemMultiplyAndSum(theta_reg, theta_reg)); cost += reg; //Gradient double[] grad = new double[n]; for (int i = 0; i < n; i++) { double[] x_i_vector = MLMath.GetVector(x, 1, i); grad[i] = -Alpha * ((1.0 / m) * MLMath.ItemMultiplyAndSum(x_i_vector, MLMath.Minus(h, y)) + Lambda / m * theta_reg[i]); } return(new Tuple <double, double[]>(cost, grad)); }
public double[] H() { int m = x.GetLength(0); double[] z = new double[m]; for (int i = 0; i < m; i++) { double[] x_vector = MLMath.GetVector(x, 0, i); z[i] = MLMath.ItemMultiplyAndSum(Theta, x_vector); } return(MLMath.ItemCalculate(z, c => MLMath.Sigmoid(c))); }