Search in sources :

Example 41 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class BlasTest method testSyrDenseDense.

/**
 * Tests 'syr' operation for dense vector x and dense matrix A.
 */
@Test
public void testSyrDenseDense() {
    double alpha = 2.0;
    DenseLocalOnHeapVector x = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
    DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 10.0, 20.0 }, { 20.0, 10.0 } });
    // alpha * x * x^T + A
    DenseLocalOnHeapMatrix exp = (DenseLocalOnHeapMatrix) new DenseLocalOnHeapMatrix(new double[][] { { 1.0, 2.0 }, { 2.0, 4.0 } }).times(alpha).plus(a);
    Blas.syr(alpha, x, a);
    Assert.assertEquals(exp, a);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 42 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class BlasTest method testSprDenseDense.

/**
 * Test 'spr' operation for dense vector v and dense matrix A.
 */
@Test
public void testSprDenseDense() {
    double alpha = 3.0;
    DenseLocalOnHeapVector v = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
    DenseLocalOnHeapVector u = new DenseLocalOnHeapVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
    // m is alpha * v * v^t
    DenseLocalOnHeapMatrix m = (DenseLocalOnHeapMatrix) new DenseLocalOnHeapMatrix(new double[][] { { 1.0, 0.0 }, { 2.0, 4.0 } }, StorageConstants.COLUMN_STORAGE_MODE).times(alpha);
    DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 3.0, 0.0 }, { 13.0, 20.0 } }, StorageConstants.COLUMN_STORAGE_MODE);
    // m := alpha * v * v.t + A
    Blas.spr(alpha, v, u);
    DenseLocalOnHeapMatrix mu = fromVector(u, a.rowSize(), StorageConstants.COLUMN_STORAGE_MODE, (i, j) -> i >= j);
    Assert.assertEquals(m.plus(a), mu);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 43 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class BlasTest method testGemvDenseSparseDense.

/**
 * Tests 'gemv' operation for dense matrix A, sparse vector x and dense vector y.
 */
@Test
public void testGemvDenseSparseDense() {
    // y := alpha * A * x + beta * y
    double alpha = 3.0;
    SparseLocalOnHeapMatrix a = (SparseLocalOnHeapMatrix) new SparseLocalOnHeapMatrix(2, 2).assign(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } });
    SparseLocalVector x = sparseFromArray(new double[] { 1.0, 2.0 });
    double beta = 2.0;
    DenseLocalOnHeapVector y = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
    DenseLocalOnHeapVector exp = (DenseLocalOnHeapVector) y.times(beta).plus(a.times(x).times(alpha));
    Blas.gemv(alpha, a, x, beta, y);
    Assert.assertEquals(exp, y);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) SparseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix) Test(org.junit.Test)

Example 44 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class DistributedLinearRegressionWithLSQRTrainerAndNormalizationExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws InterruptedException {
    System.out.println();
    System.out.println(">>> Linear regression model over sparse distributed matrix API usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
        // because we create ignite cache internally.
        IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> {
            IgniteCache<Integer, double[]> dataCache = getTestCache(ignite);
            System.out.println(">>> Create new normalization trainer object.");
            NormalizationTrainer<Integer, double[]> normalizationTrainer = new NormalizationTrainer<>();
            System.out.println(">>> Perform the training to get the normalization preprocessor.");
            NormalizationPreprocessor<Integer, double[]> preprocessor = normalizationTrainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), (k, v) -> Arrays.copyOfRange(v, 1, v.length), 4);
            System.out.println(">>> Create new linear regression trainer object.");
            LinearRegressionLSQRTrainer<Integer, double[]> trainer = new LinearRegressionLSQRTrainer<>();
            System.out.println(">>> Perform the training to get the model.");
            LinearRegressionModel mdl = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), preprocessor, (k, v) -> v[0], 4);
            System.out.println(">>> Linear regression model: " + mdl);
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Ground Truth\t|");
            System.out.println(">>> ---------------------------------");
            try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {
                for (Cache.Entry<Integer, double[]> observation : observations) {
                    Integer key = observation.getKey();
                    double[] val = observation.getValue();
                    double groundTruth = val[0];
                    double prediction = mdl.apply(new DenseLocalOnHeapVector(preprocessor.apply(key, val)));
                    System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
                }
            }
            System.out.println(">>> ---------------------------------");
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) NormalizationTrainer(org.apache.ignite.ml.preprocessing.normalization.NormalizationTrainer) LinearRegressionLSQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer) SparseDistributedMatrixExample(org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 45 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class SVMBinaryClassificationExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws InterruptedException {
    System.out.println();
    System.out.println(">>> SVM Binary classification model over cached dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SVMBinaryClassificationExample.class.getSimpleName(), () -> {
            IgniteCache<Integer, double[]> dataCache = getTestCache(ignite);
            SVMLinearBinaryClassificationTrainer<Integer, double[]> trainer = new SVMLinearBinaryClassificationTrainer<>();
            SVMLinearBinaryClassificationModel mdl = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), (k, v) -> Arrays.copyOfRange(v, 1, v.length), (k, v) -> v[0], 4);
            System.out.println(">>> SVM model " + mdl);
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Ground Truth\t|");
            System.out.println(">>> ---------------------------------");
            int amountOfErrors = 0;
            int totalAmount = 0;
            // Build confusion matrix. See https://en.wikipedia.org/wiki/Confusion_matrix
            int[][] confusionMtx = { { 0, 0 }, { 0, 0 } };
            try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {
                for (Cache.Entry<Integer, double[]> observation : observations) {
                    double[] val = observation.getValue();
                    double[] inputs = Arrays.copyOfRange(val, 1, val.length);
                    double groundTruth = val[0];
                    double prediction = mdl.apply(new DenseLocalOnHeapVector(inputs));
                    totalAmount++;
                    if (groundTruth != prediction)
                        amountOfErrors++;
                    int idx1 = (int) prediction == -1.0 ? 0 : 1;
                    int idx2 = (int) groundTruth == -1.0 ? 0 : 1;
                    confusionMtx[idx1][idx2]++;
                    System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
                }
                System.out.println(">>> ---------------------------------");
                System.out.println("\n>>> Absolute amount of errors " + amountOfErrors);
                System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double) totalAmount));
            }
            System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtx));
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : SVMLinearBinaryClassificationTrainer(org.apache.ignite.ml.svm.SVMLinearBinaryClassificationTrainer) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SVMLinearBinaryClassificationModel(org.apache.ignite.ml.svm.SVMLinearBinaryClassificationModel) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Aggregations

DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)98 Vector (org.apache.ignite.ml.math.Vector)49 Test (org.junit.Test)44 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)26 Random (java.util.Random)18 HashMap (java.util.HashMap)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 Matrix (org.apache.ignite.ml.math.Matrix)12 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)11 IgniteCache (org.apache.ignite.IgniteCache)8 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)8 Arrays (java.util.Arrays)7 Collections (java.util.Collections)6 List (java.util.List)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6 Ignite (org.apache.ignite.Ignite)6 IgniteUtils (org.apache.ignite.internal.util.IgniteUtils)6 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)6