Fixed gradient decent
This commit is contained in:
parent
cf94310598
commit
98f2219366
2 changed files with 44 additions and 22 deletions
|
|
@ -40,17 +40,8 @@ public class LinearRegression {
|
||||||
Matrix.Elemental.pow(normalized,2));
|
Matrix.Elemental.pow(normalized,2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static double calculateDiff(double[] vector1, double[] vector2){
|
||||||
* Calculates the gradiant of the current provided theta.
|
return Math.abs(Matrix.sum(vector1) - Matrix.sum(vector2));
|
||||||
*/
|
|
||||||
protected static double calculateGradiant(double[][] x, double[] y, double[] theta){
|
|
||||||
int m = y.length; // number of training examples
|
|
||||||
double[] hypothesis = calculateHypothesis(x, theta);
|
|
||||||
double[] normalized = Matrix.subtract(hypothesis, y);
|
|
||||||
|
|
||||||
return 1/m * Matrix.sum(
|
|
||||||
Matrix.Elemental.multiply(Matrix.transpose(x), normalized));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -58,12 +49,16 @@ public class LinearRegression {
|
||||||
*/
|
*/
|
||||||
public static double[] gradientDescent(double[][] x, double[] y, double[] theta, double alpha){
|
public static double[] gradientDescent(double[][] x, double[] y, double[] theta, double alpha){
|
||||||
double[] newTheta = theta.clone();
|
double[] newTheta = theta.clone();
|
||||||
double gradient;
|
double[] prevTheta = new double[newTheta.length];
|
||||||
|
double thetaDiff = 0;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
for (int i=0; (gradient = calculateGradiant(x, y, newTheta)) != 0; i++) {
|
do {
|
||||||
logger.fine("Gradient Descent iteration " + i + ", gradiant: " + gradient);
|
logger.fine("Gradient Descent iteration " + i + ", diff to previous iteration: " + thetaDiff);
|
||||||
|
System.arraycopy(newTheta, 0, prevTheta, 0, newTheta.length);
|
||||||
newTheta = gradientDescentIteration(x, y, newTheta, alpha);
|
newTheta = gradientDescentIteration(x, y, newTheta, alpha);
|
||||||
}
|
++i;
|
||||||
|
} while ((thetaDiff=calculateDiff(prevTheta, newTheta)) > 0.0001);
|
||||||
|
|
||||||
return newTheta;
|
return newTheta;
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +79,7 @@ public class LinearRegression {
|
||||||
double[] normalized = Matrix.subtract(hypothesis, y);
|
double[] normalized = Matrix.subtract(hypothesis, y);
|
||||||
|
|
||||||
for (int j= 0; j < theta.length; j++) {
|
for (int j= 0; j < theta.length; j++) {
|
||||||
newTheta[j] = theta[j] - alpha * (1.0/m) * Matrix.sum(
|
newTheta[j] = theta[j] - (alpha/m) * Matrix.sum(
|
||||||
Matrix.Elemental.multiply(normalized, Matrix.getColumn(x, j)));
|
Matrix.Elemental.multiply(normalized, Matrix.getColumn(x, j)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package zutil.ml;
|
package zutil.ml;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import zutil.io.MultiPrintStream;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
@ -36,8 +37,8 @@ public class LinearRegressionTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Does not work
|
// Does not work
|
||||||
//@Test
|
@Test
|
||||||
public void gradientAscent() {
|
public void gradientDescent() {
|
||||||
double[][] x = {
|
double[][] x = {
|
||||||
{1.0, 0.1, 0.6, 1.1},
|
{1.0, 0.1, 0.6, 1.1},
|
||||||
{1.0, 0.2, 0.7, 1.2},
|
{1.0, 0.2, 0.7, 1.2},
|
||||||
|
|
@ -59,14 +60,40 @@ public class LinearRegressionTest {
|
||||||
2
|
2
|
||||||
};
|
};
|
||||||
|
|
||||||
double[] resultTheta = LinearRegression.gradientDescent(x, y, theta, 0);
|
// Alpha zero
|
||||||
|
|
||||||
assertEquals(0.73482, LinearRegression.calculateCost(x, y, resultTheta), 0.000001);
|
double[] resultTheta = LinearRegression.gradientDescent(x, y, theta, 0);
|
||||||
|
System.out.println("Result Theta (alpha = 0):");
|
||||||
|
System.out.println(MultiPrintStream.dumpToString(resultTheta));
|
||||||
|
|
||||||
|
assertArrayEquals(theta, resultTheta, 0.000001);
|
||||||
|
|
||||||
|
// Alpha +
|
||||||
|
|
||||||
|
resultTheta = LinearRegression.gradientDescent(x, y, theta, 0.1);
|
||||||
|
System.out.println("Result Theta (alpha = 0.1):");
|
||||||
|
System.out.println(MultiPrintStream.dumpToString(resultTheta));
|
||||||
|
|
||||||
|
assertArrayEquals(
|
||||||
|
new double[]{-1.31221, -1.98259, 0.36131, 1.70520},
|
||||||
|
resultTheta, 0.001);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void gradientAscentIteration() {
|
public void gradientDescentIteration() {
|
||||||
double[] theta = LinearRegression.gradientDescentIteration( // one iteration
|
// Zero iterations
|
||||||
|
|
||||||
|
double[] theta = LinearRegression.gradientDescentIteration(
|
||||||
|
/* x */ new double[][]{{1, 5},{1, 2},{1, 4},{1, 5}},
|
||||||
|
/* y */ new double[]{1, 6, 4, 2},
|
||||||
|
/* theta */ new double[]{0, 0},
|
||||||
|
/* alpha */0.0);
|
||||||
|
|
||||||
|
assertArrayEquals(new double[]{0.0, 0.0}, theta, 0.000001);
|
||||||
|
|
||||||
|
// One iteration
|
||||||
|
|
||||||
|
theta = LinearRegression.gradientDescentIteration(
|
||||||
/* x */ new double[][]{{1, 5},{1, 2},{1, 4},{1, 5}},
|
/* x */ new double[][]{{1, 5},{1, 2},{1, 4},{1, 5}},
|
||||||
/* y */ new double[]{1, 6, 4, 2},
|
/* y */ new double[]{1, 6, 4, 2},
|
||||||
/* theta */ new double[]{0, 0},
|
/* theta */ new double[]{0, 0},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue