From a938b70d57c156f7bea80111bae904756e48c646 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 22 May 2018 17:31:58 +0200 Subject: [PATCH] fixed gradient ascent --- src/zutil/math/Matrix.java | 17 +++++++++++++++++ src/zutil/ml/LinearRegression.java | 5 +++-- test/zutil/math/MatrixTest.java | 10 ++++++++++ test/zutil/ml/LinearRegressionTest.java | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/zutil/math/Matrix.java b/src/zutil/math/Matrix.java index 6ca6d76..d382e0f 100755 --- a/src/zutil/math/Matrix.java +++ b/src/zutil/math/Matrix.java @@ -112,6 +112,22 @@ public class Matrix { return result; } + /** + * Element multiplication, each element in vector1 will be + * multiplied with the corresponding element in vector2. + * + * @return a new vector with the result + */ + public static double[] multiply(double[] vector1, double[] vector2) { + vectorPreCheck(vector1, vector2); + double[] result = new double[vector1.length]; + + for (int i = 0; i < vector1.length; ++i) { + result[i] = vector1[i] * vector2[i]; + } + return result; + } + /** * Element multiplication, each element in matrix1 will be * multiplied with the corresponding element in matrix2. @@ -160,6 +176,7 @@ public class Matrix { return result; } + private static void elementalPreCheck(double[][] matrix1, double[][] matrix2) { if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length) throw new IllegalArgumentException("Matrices need to be of same dimension: " + diff --git a/src/zutil/ml/LinearRegression.java b/src/zutil/ml/LinearRegression.java index c5c6ff3..0d8d4a5 100755 --- a/src/zutil/ml/LinearRegression.java +++ b/src/zutil/ml/LinearRegression.java @@ -46,14 +46,15 @@ public class LinearRegression { * * @return the theta that was found to minimize the cost function */ - public static double[] gradientDescent(double[][] x, double[] y, double[] theta, double alpha){ + public static double[] gradientDescentIteration(double[][] x, double[] y, double[] theta, double alpha){ double[] newTheta = new double[theta.length]; double m = y.length; double[] hypothesis = calculateHypothesis(x, theta); double[] normalized = Matrix.subtract(hypothesis, y); for (int j= 0; j < theta.length; j++) { - newTheta[j] = theta[j] - alpha * (1.0/m) * Matrix.sum(Matrix.add(normalized, Matrix.getColumn(x, j))); + newTheta[j] = theta[j] - alpha * (1.0/m) * Matrix.sum( + Matrix.Elemental.multiply(normalized, Matrix.getColumn(x, j))); } return newTheta; diff --git a/test/zutil/math/MatrixTest.java b/test/zutil/math/MatrixTest.java index a19444a..dd9af15 100755 --- a/test/zutil/math/MatrixTest.java +++ b/test/zutil/math/MatrixTest.java @@ -105,6 +105,16 @@ public class MatrixTest { @Test public void vectorMultiply(){ + assertArrayEquals( + new double[]{0.1, 0.4, 0.9, 1.6}, + Matrix.Elemental.multiply( + new double[]{1, 2, 3, 4}, + new double[]{0.1, 0.2, 0.3, 0.4}), + 0.001); + } + + @Test + public void vectorMatrixMultiply(){ assertArrayEquals( new double[]{1.4, 1.9, 2.4, 2.9}, Matrix.multiply( diff --git a/test/zutil/ml/LinearRegressionTest.java b/test/zutil/ml/LinearRegressionTest.java index c15a8e4..110a65d 100755 --- a/test/zutil/ml/LinearRegressionTest.java +++ b/test/zutil/ml/LinearRegressionTest.java @@ -33,7 +33,7 @@ public class LinearRegressionTest { @Test public void gradientAscent() { - double[] theta = LinearRegression.gradientDescent( // one iteration + double[] theta = LinearRegression.gradientDescentIteration( // one iteration /* x */ new double[][]{{1, 5},{1, 2},{1, 4},{1, 5}}, /* y */ new double[]{1, 6, 4, 2}, /* theta */ new double[]{0, 0},