Search in sources :

Example 76 with DenseLocalOnHeapVector

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

the class SVMModelTest method testPredictWithMultiClasses.

/**
 */
@Test
public void testPredictWithMultiClasses() {
    Vector weights1 = new DenseLocalOnHeapVector(new double[] { 10.0, 0.0 });
    Vector weights2 = new DenseLocalOnHeapVector(new double[] { 0.0, 10.0 });
    Vector weights3 = new DenseLocalOnHeapVector(new double[] { -1.0, -1.0 });
    SVMLinearMultiClassClassificationModel mdl = new SVMLinearMultiClassClassificationModel();
    mdl.add(1, new SVMLinearBinaryClassificationModel(weights1, 0.0).withRawLabels(true));
    mdl.add(2, new SVMLinearBinaryClassificationModel(weights2, 0.0).withRawLabels(true));
    mdl.add(2, new SVMLinearBinaryClassificationModel(weights3, 0.0).withRawLabels(true));
    Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
    TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 77 with DenseLocalOnHeapVector

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

the class SVMMultiClassTrainerTest method testTrainWithTheLinearlySeparableCase.

/**
 * Test trainer on classification model y = x.
 */
@Test
public void testTrainWithTheLinearlySeparableCase() {
    Map<Integer, double[]> data = new HashMap<>();
    ThreadLocalRandom rndX = ThreadLocalRandom.current();
    ThreadLocalRandom rndY = ThreadLocalRandom.current();
    for (int i = 0; i < AMOUNT_OF_OBSERVATIONS; i++) {
        double x = rndX.nextDouble(-1000, 1000);
        double y = rndY.nextDouble(-1000, 1000);
        double[] vec = new double[AMOUNT_OF_FEATURES + 1];
        // assign label.
        vec[0] = y - x > 0 ? 1 : -1;
        vec[1] = x;
        vec[2] = y;
        data.put(i, vec);
    }
    SVMLinearMultiClassClassificationTrainer<Integer, double[]> trainer = new SVMLinearMultiClassClassificationTrainer<Integer, double[]>().withLambda(0.3).withAmountOfLocIterations(100).withAmountOfIterations(20);
    SVMLinearMultiClassClassificationModel mdl = trainer.fit(new LocalDatasetBuilder<>(data, 10), (k, v) -> Arrays.copyOfRange(v, 1, v.length), (k, v) -> v[0], AMOUNT_OF_FEATURES);
    TestUtils.assertEquals(-1, mdl.apply(new DenseLocalOnHeapVector(new double[] { 100, 10 })), PRECISION);
    TestUtils.assertEquals(1, mdl.apply(new DenseLocalOnHeapVector(new double[] { 10, 100 })), PRECISION);
}
Also used : HashMap(java.util.HashMap) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 78 with DenseLocalOnHeapVector

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

the class BlasTest method testAxpySparseArray.

/**
 * Test 'axpy' operation for sparse vector and array-based vector.
 */
@Test
public void testAxpySparseArray() {
    DenseLocalOnHeapVector y = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
    double a = 2.0;
    SparseLocalVector x = sparseFromArray(new double[] { 1.0, 2.0 });
    SparseLocalVector exp = (SparseLocalVector) x.times(a).plus(y);
    Blas.axpy(a, x, y);
    Assert.assertTrue(elementsEqual(exp, y));
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) Test(org.junit.Test)

Example 79 with DenseLocalOnHeapVector

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

the class BlasTest method testAxpyArrayArray.

/**
 * Test 'axpy' operation for two array-based vectors.
 */
@Test
public void testAxpyArrayArray() {
    Vector y = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
    double a = 2.0;
    Vector x = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
    Vector exp = x.times(a).plus(y);
    Blas.axpy(a, x, y);
    Assert.assertEquals(y, exp);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 80 with DenseLocalOnHeapVector

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

the class BlasTest method testSprSparseDense1.

/**
 * Test 'spr' operation for sparse vector v (sparse in representation, dense in fact) and dense matrix A.
 */
@Test
public void testSprSparseDense1() {
    double alpha = 3.0;
    SparseLocalVector v = sparseFromArray(new double[] { 1.0, 2.0 });
    DenseLocalOnHeapVector u = new DenseLocalOnHeapVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
    DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 3.0, 0.0 }, { 13.0, 20.0 } }, StorageConstants.COLUMN_STORAGE_MODE);
    DenseLocalOnHeapMatrix exp = (DenseLocalOnHeapMatrix) new DenseLocalOnHeapMatrix(new double[][] { { 1.0, 0.0 }, { 2.0, 4.0 } }, StorageConstants.COLUMN_STORAGE_MODE).times(alpha).plus(a);
    // 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(exp, mu);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) Test(org.junit.Test)

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