Search in sources :

Example 31 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class KNNClassificationTest method testBinaryClassificationFarPointsWithSimpleStrategy.

/**
 */
public void testBinaryClassificationFarPointsWithSimpleStrategy() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    double[][] mtx = new double[][] { { 10.0, 10.0 }, { 10.0, 20.0 }, { -1, -1 }, { -2, -2 }, { -1.0, -2.0 }, { -2.0, -1.0 } };
    double[] lbs = new double[] { 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 };
    LabeledDataset training = new LabeledDataset(mtx, lbs);
    KNNModel knnMdl = new KNNModel(3, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
    Vector vector = new DenseLocalOnHeapVector(new double[] { -1.01, -1.01 });
    assertEquals(knnMdl.apply(vector), 2.0);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNModel(org.apache.ignite.ml.knn.models.KNNModel) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 32 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class KNNMultipleLinearRegressionTest method testLonglyWithWeightedStrategyAndNormalization.

/**
 */
public void testLonglyWithWeightedStrategyAndNormalization() {
    y = new double[] { 60323, 61122, 60171, 61187, 63221, 63639, 64989, 63761, 66019, 68169, 66513, 68655, 69564, 69331, 70551 };
    x = new double[15][];
    x[0] = new double[] { 83.0, 234289, 2356, 1590, 107608, 1947 };
    x[1] = new double[] { 88.5, 259426, 2325, 1456, 108632, 1948 };
    x[2] = new double[] { 88.2, 258054, 3682, 1616, 109773, 1949 };
    x[3] = new double[] { 89.5, 284599, 3351, 1650, 110929, 1950 };
    x[4] = new double[] { 96.2, 328975, 2099, 3099, 112075, 1951 };
    x[5] = new double[] { 98.1, 346999, 1932, 3594, 113270, 1952 };
    x[6] = new double[] { 99.0, 365385, 1870, 3547, 115094, 1953 };
    x[7] = new double[] { 100.0, 363112, 3578, 3350, 116219, 1954 };
    x[8] = new double[] { 101.2, 397469, 2904, 3048, 117388, 1955 };
    x[9] = new double[] { 108.4, 442769, 2936, 2798, 120445, 1957 };
    x[10] = new double[] { 110.8, 444546, 4681, 2637, 121950, 1958 };
    x[11] = new double[] { 112.6, 482704, 3813, 2552, 123366, 1959 };
    x[12] = new double[] { 114.2, 502601, 3931, 2514, 125368, 1960 };
    x[13] = new double[] { 115.7, 518173, 4806, 2572, 127852, 1961 };
    x[14] = new double[] { 116.9, 554894, 4007, 2827, 130081, 1962 };
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    LabeledDataset training = new LabeledDataset(x, y);
    final LabeledDataset normalizedTrainingDataset = (LabeledDataset) Normalizer.normalizeWithMiniMax(training);
    KNNMultipleLinearRegression knnMdl = new KNNMultipleLinearRegression(5, new EuclideanDistance(), KNNStrategy.WEIGHTED, normalizedTrainingDataset);
    Vector vector = new DenseLocalOnHeapVector(new double[] { 104.6, 419180, 2822, 2857, 118734, 1956 });
    System.out.println(knnMdl.apply(vector));
    Assert.assertEquals(67857, knnMdl.apply(vector), 2000);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNMultipleLinearRegression(org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Vector(org.apache.ignite.ml.math.Vector) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 33 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class KNNMultipleLinearRegressionTest method testLonglyWithNormalization.

/**
 */
public void testLonglyWithNormalization() {
    y = new double[] { 60323, 61122, 60171, 61187, 63221, 63639, 64989, 63761, 66019, 68169, 66513, 68655, 69564, 69331, 70551 };
    x = new double[15][];
    x[0] = new double[] { 83.0, 234289, 2356, 1590, 107608, 1947 };
    x[1] = new double[] { 88.5, 259426, 2325, 1456, 108632, 1948 };
    x[2] = new double[] { 88.2, 258054, 3682, 1616, 109773, 1949 };
    x[3] = new double[] { 89.5, 284599, 3351, 1650, 110929, 1950 };
    x[4] = new double[] { 96.2, 328975, 2099, 3099, 112075, 1951 };
    x[5] = new double[] { 98.1, 346999, 1932, 3594, 113270, 1952 };
    x[6] = new double[] { 99.0, 365385, 1870, 3547, 115094, 1953 };
    x[7] = new double[] { 100.0, 363112, 3578, 3350, 116219, 1954 };
    x[8] = new double[] { 101.2, 397469, 2904, 3048, 117388, 1955 };
    x[9] = new double[] { 108.4, 442769, 2936, 2798, 120445, 1957 };
    x[10] = new double[] { 110.8, 444546, 4681, 2637, 121950, 1958 };
    x[11] = new double[] { 112.6, 482704, 3813, 2552, 123366, 1959 };
    x[12] = new double[] { 114.2, 502601, 3931, 2514, 125368, 1960 };
    x[13] = new double[] { 115.7, 518173, 4806, 2572, 127852, 1961 };
    x[14] = new double[] { 116.9, 554894, 4007, 2827, 130081, 1962 };
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    LabeledDataset training = new LabeledDataset(x, y);
    final LabeledDataset normalizedTrainingDataset = (LabeledDataset) Normalizer.normalizeWithMiniMax(training);
    KNNMultipleLinearRegression knnMdl = new KNNMultipleLinearRegression(5, new EuclideanDistance(), KNNStrategy.SIMPLE, normalizedTrainingDataset);
    Vector vector = new DenseLocalOnHeapVector(new double[] { 104.6, 419180, 2822, 2857, 118734, 1956 });
    System.out.println(knnMdl.apply(vector));
    Assert.assertEquals(67857, knnMdl.apply(vector), 2000);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNMultipleLinearRegression(org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Vector(org.apache.ignite.ml.math.Vector) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 34 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class MLPTest method testDifferentiation.

/**
 * Test differentiation.
 */
@Test
public void testDifferentiation() {
    int inputSize = 2;
    int firstLayerNeuronsCnt = 1;
    double w10 = 0.1;
    double w11 = 0.2;
    MLPArchitecture conf = new MLPArchitecture(inputSize).withAddedLayer(firstLayerNeuronsCnt, false, Activators.SIGMOID);
    MultilayerPerceptron mlp = new MultilayerPerceptron(conf);
    mlp.setWeight(1, 0, 0, w10);
    mlp.setWeight(1, 1, 0, w11);
    double x0 = 1.0;
    double x1 = 3.0;
    Matrix inputs = new DenseLocalOnHeapMatrix(new double[][] { { x0, x1 } }).transpose();
    double ytt = 1.0;
    Matrix truth = new DenseLocalOnHeapMatrix(new double[][] { { ytt } }).transpose();
    Vector grad = mlp.differentiateByParameters(LossFunctions.MSE, inputs, truth);
    // Let yt be y ground truth value.
    // d/dw1i [(yt - sigma(w10 * x0 + w11 * x1))^2] =
    // 2 * (yt - sigma(w10 * x0 + w11 * x1)) * (-1) * (sigma(w10 * x0 + w11 * x1)) * (1 - sigma(w10 * x0 + w11 * x1)) * xi =
    // let z = sigma(w10 * x0 + w11 * x1)
    // - 2* (yt - z) * (z) * (1 - z) * xi.
    IgniteTriFunction<Double, Vector, Vector, Vector> partialDer = (yt, w, x) -> {
        Double z = Activators.SIGMOID.apply(w.dot(x));
        return x.copy().map(xi -> -2 * (yt - z) * z * (1 - z) * xi);
    };
    Vector weightsVec = mlp.weights(1).getRow(0);
    Tracer.showAscii(weightsVec);
    Vector trueGrad = partialDer.apply(ytt, weightsVec, inputs.getCol(0));
    Tracer.showAscii(trueGrad);
    Tracer.showAscii(grad);
    Assert.assertEquals(mlp.architecture().parametersCount(), grad.size());
    Assert.assertEquals(trueGrad, grad);
}
Also used : TestUtils(org.apache.ignite.ml.TestUtils) Vector(org.apache.ignite.ml.math.Vector) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) LossFunctions(org.apache.ignite.ml.optimization.LossFunctions) Tracer(org.apache.ignite.ml.math.Tracer) Test(org.junit.Test) MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) Assert(org.junit.Assert) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 35 with Vector

use of org.apache.ignite.ml.math.Vector in project ignite by apache.

the class GradientDescentTest method testOptimize.

/**
 * Test gradient descent optimization on function y = x^2 with gradient function 2 * x.
 */
@Test
public void testOptimize() {
    GradientDescent gradientDescent = new GradientDescent((inputs, groundTruth, point) -> point.times(2), new SimpleUpdater(0.01));
    Vector res = gradientDescent.optimize(new DenseLocalOnHeapMatrix(new double[1][1]), new DenseLocalOnHeapVector(new double[] { 2.0 }));
    TestUtils.assertEquals(0, res.get(0), PRECISION);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Aggregations

Vector (org.apache.ignite.ml.math.Vector)144 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)82 Test (org.junit.Test)37 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)26 Matrix (org.apache.ignite.ml.math.Matrix)25 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)22 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)17 ArrayList (java.util.ArrayList)12 Random (java.util.Random)12 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)12 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)11 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)10 Arrays (java.util.Arrays)9 Ignite (org.apache.ignite.Ignite)9 SparseDistributedMatrixStorage (org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage)9 UUID (java.util.UUID)8 Collections (java.util.Collections)7 List (java.util.List)7 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)7 DenseLocalOffHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector)7