Search in sources :

Example 16 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class IgniteLUDecompositionBenchmark method runLUDecomposition.

/**
 * Based on LUDecompositionTest.
 */
private void runLUDecomposition() {
    Matrix testMatrix = new DenseLocalOnHeapMatrix(new DataChanger.Scale().mutate(new double[][] { { 2.0d, 1.0d, 1.0d, 0.0d }, { 4.0d, 3.0d, 3.0d, 1.0d }, { 8.0d, 7.0d, 9.0d, 5.0d }, { 6.0d, 7.0d, 9.0d, 8.0d } }));
    LUDecomposition dec1 = new LUDecomposition(new PivotedMatrixView(testMatrix));
    dec1.solve(new DenseLocalOnHeapVector(testMatrix.rowSize()));
    dec1.destroy();
    LUDecomposition dec2 = new LUDecomposition(new PivotedMatrixView(testMatrix));
    dec2.solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize()));
    dec2.destroy();
    LUDecomposition dec3 = new LUDecomposition(testMatrix);
    dec3.getL();
    dec3.getU();
    dec3.getP();
    dec3.getPivot();
    dec3.destroy();
}
Also used : PivotedMatrixView(org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) LUDecomposition(org.apache.ignite.ml.math.decompositions.LUDecomposition) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 17 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class MLPGroupTrainerTest method doTestXOR.

/**
 * Test training of 'xor' by {@link MLPGroupUpdateTrainer}.
 */
private <U extends Serializable> void doTestXOR(UpdatesStrategy<? super MultilayerPerceptron, U> stgy) {
    int samplesCnt = 1000;
    Matrix xorInputs = new DenseLocalOnHeapMatrix(new double[][] { { 0.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 0.0 }, { 1.0, 1.0 } }, StorageConstants.ROW_STORAGE_MODE).transpose();
    Matrix xorOutputs = new DenseLocalOnHeapMatrix(new double[][] { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } }, StorageConstants.ROW_STORAGE_MODE).transpose();
    MLPArchitecture conf = new MLPArchitecture(2).withAddedLayer(10, true, Activators.RELU).withAddedLayer(1, false, Activators.SIGMOID);
    IgniteCache<Integer, LabeledVector<Vector, Vector>> cache = LabeledVectorsCache.createNew(ignite);
    String cacheName = cache.getName();
    Random rnd = new Random(12345L);
    try (IgniteDataStreamer<Integer, LabeledVector<Vector, Vector>> streamer = ignite.dataStreamer(cacheName)) {
        streamer.perNodeBufferSize(10000);
        for (int i = 0; i < samplesCnt; i++) {
            int col = Math.abs(rnd.nextInt()) % 4;
            streamer.addData(i, new LabeledVector<>(xorInputs.getCol(col), xorOutputs.getCol(col)));
        }
    }
    int totalCnt = 30;
    int failCnt = 0;
    double maxFailRatio = 0.3;
    MLPGroupUpdateTrainer<U> trainer = MLPGroupUpdateTrainer.getDefault(ignite).withSyncPeriod(3).withTolerance(0.001).withMaxGlobalSteps(100).withUpdateStrategy(stgy);
    for (int i = 0; i < totalCnt; i++) {
        MLPGroupUpdateTrainerCacheInput trainerInput = new MLPGroupUpdateTrainerCacheInput(conf, new RandomInitializer(new Random(123L + i)), 6, cache, 10, new Random(123L + i));
        MultilayerPerceptron mlp = trainer.train(trainerInput);
        Matrix predict = mlp.apply(xorInputs);
        Tracer.showAscii(predict);
        X.println(xorOutputs.getRow(0).minus(predict.getRow(0)).kNorm(2) + "");
        failCnt += TestUtils.checkIsInEpsilonNeighbourhoodBoolean(xorOutputs.getRow(0), predict.getRow(0), 5E-1) ? 0 : 1;
    }
    double failRatio = (double) failCnt / totalCnt;
    System.out.println("Fail percentage: " + (failRatio * 100) + "%.");
    assertTrue(failRatio < maxFailRatio);
}
Also used : MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Random(java.util.Random) RandomInitializer(org.apache.ignite.ml.nn.initializers.RandomInitializer)

Example 18 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class MnistMLPTestUtil method createDataset.

/**
 */
static IgniteBiTuple<Matrix, Matrix> createDataset(Stream<DenseLocalOnHeapVector> s, int samplesCnt, int featCnt) {
    Matrix vectors = new DenseLocalOnHeapMatrix(featCnt, samplesCnt);
    Matrix labels = new DenseLocalOnHeapMatrix(10, samplesCnt);
    List<DenseLocalOnHeapVector> sc = s.collect(Collectors.toList());
    for (int i = 0; i < samplesCnt; i++) {
        DenseLocalOnHeapVector v = sc.get(i);
        vectors.assignColumn(i, v.viewPart(0, featCnt));
        labels.assignColumn(i, num2Vec((int) v.getX(featCnt), 10));
    }
    return new IgniteBiTuple<>(vectors, labels);
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 19 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix 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)

Example 20 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class FuzzyCMeansLocalClustererTest method checkCentersOfTheSamePointsTwoDimensions.

/**
 * Test FCM on points which have the equal coordinates.
 */
@Test
public void checkCentersOfTheSamePointsTwoDimensions() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_MEMBERSHIPS, 0.01, 10, null);
    double[][] points = new double[][] { { 3.3, 10 }, { 3.3, 10 }, { 3.3, 10 }, { 3.3, 10 }, { 3.3, 10 } };
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    int k = 2;
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, k);
    Vector exp = new DenseLocalOnHeapVector(new double[] { 3.3, 10 });
    for (int i = 0; i < k; i++) {
        Vector center = mdl.centers()[i];
        for (int j = 0; j < 2; j++) assertEquals(exp.getX(j), center.getX(j), 1);
    }
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) 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

DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)67 Test (org.junit.Test)33 Matrix (org.apache.ignite.ml.math.Matrix)28 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)19 Vector (org.apache.ignite.ml.math.Vector)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)8 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)6 Random (java.util.Random)5 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Ignite (org.apache.ignite.Ignite)3 KMeansLocalClusterer (org.apache.ignite.ml.clustering.KMeansLocalClusterer)3 BaseFuzzyCMeansClusterer (org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer)2 FuzzyCMeansLocalClusterer (org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer)2