Search in sources :

Example 56 with DenseLocalOnHeapMatrix

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

the class QRDecompositionTest method basicTest.

/**
 */
private void basicTest(Matrix m) {
    QRDecomposition dec = new QRDecomposition(m);
    assertTrue("Unexpected value for full rank in decomposition " + dec, dec.hasFullRank());
    Matrix q = dec.getQ();
    Matrix r = dec.getR();
    assertNotNull("Matrix q is expected to be not null.", q);
    assertNotNull("Matrix r is expected to be not null.", r);
    Matrix qSafeCp = safeCopy(q);
    Matrix expIdentity = qSafeCp.times(qSafeCp.transpose());
    final double delta = 0.0001;
    for (int row = 0; row < expIdentity.rowSize(); row++) for (int col = 0; col < expIdentity.columnSize(); col++) assertEquals("Unexpected identity matrix value at (" + row + "," + col + ").", row == col ? 1d : 0d, expIdentity.get(col, row), delta);
    for (int row = 0; row < r.rowSize(); row++) for (int col = 0; col < row - 1; col++) assertEquals("Unexpected upper triangular matrix value at (" + row + "," + col + ").", 0d, r.get(row, col), delta);
    Matrix recomposed = qSafeCp.times(r);
    for (int row = 0; row < m.rowSize(); row++) for (int col = 0; col < m.columnSize(); col++) assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", m.get(row, col), recomposed.get(row, col), delta);
    Matrix sol = dec.solve(new DenseLocalOnHeapMatrix(3, 10));
    assertEquals("Unexpected rows in solution matrix.", 3, sol.rowSize());
    assertEquals("Unexpected cols in solution matrix.", 10, sol.columnSize());
    for (int row = 0; row < sol.rowSize(); row++) for (int col = 0; col < sol.columnSize(); col++) assertEquals("Unexpected solution matrix value at (" + row + "," + col + ").", 0d, sol.get(row, col), delta);
    dec.destroy();
    QRDecomposition dec1 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d }, { -1.0d, 2.0d }, { 0.0d, -1.0d } }));
    assertTrue("Unexpected value for full rank in decomposition " + dec1, dec1.hasFullRank());
    dec1.destroy();
    QRDecomposition dec2 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d, 0.0d }, { -1.0d, 2.0d, -1.0d, 0.0d }, { 0.0d, -1.0d, 2.0d, 0.0d } }));
    assertTrue("Unexpected value for full rank in decomposition " + dec2, dec2.hasFullRank());
    dec2.destroy();
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 57 with DenseLocalOnHeapMatrix

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

the class MLPGroupUpdateTrainerCacheInput method batchSupplier.

/**
 * {@inheritDoc}
 */
@Override
public IgniteSupplier<IgniteBiTuple<Matrix, Matrix>> batchSupplier() {
    String cName = cache.getName();
    // This line is for prohibiting of 'this' object be caught into serialization context of lambda.
    int bs = batchSize;
    // This line is for prohibiting of 'this' object be caught into serialization context of lambda.
    Random r = rand;
    return () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<Integer, LabeledVector<Vector, Vector>> cache = ignite.getOrCreateCache(cName);
        int total = cache.size();
        Affinity<Integer> affinity = ignite.affinity(cName);
        List<Integer> allKeys = IntStream.range(0, total).boxed().collect(Collectors.toList());
        List<Integer> keys = new ArrayList<>(affinity.mapKeysToNodes(allKeys).get(ignite.cluster().localNode()));
        int locKeysCnt = keys.size();
        int[] selected = Utils.selectKDistinct(locKeysCnt, Math.min(bs, locKeysCnt), r);
        // Get dimensions of vectors in cache. We suppose that every feature vector has
        // same dimension d 1 and every label has the same dimension d2.
        LabeledVector<Vector, Vector> dimEntry = cache.get(keys.get(selected[0]));
        Matrix inputs = new DenseLocalOnHeapMatrix(dimEntry.features().size(), bs);
        Matrix groundTruth = new DenseLocalOnHeapMatrix(dimEntry.label().size(), bs);
        for (int i = 0; i < selected.length; i++) {
            LabeledVector<Vector, Vector> labeled = cache.get(keys.get(selected[i]));
            inputs.assignColumn(i, labeled.features());
            groundTruth.assignColumn(i, labeled.label());
        }
        return new IgniteBiTuple<>(inputs, groundTruth);
    };
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) 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) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) ArrayList(java.util.ArrayList) List(java.util.List) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) Vector(org.apache.ignite.ml.math.Vector)

Example 58 with DenseLocalOnHeapMatrix

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

the class SparseDistributedMatrixMapReducer method mapReduce.

/**
 */
public <R, T> R mapReduce(IgniteBiFunction<Matrix, T, R> mapper, IgniteFunction<Collection<R>, R> reducer, T args) {
    Ignite ignite = Ignition.localIgnite();
    SparseDistributedMatrixStorage storage = (SparseDistributedMatrixStorage) distributedMatrix.getStorage();
    int colSize = distributedMatrix.columnSize();
    Collection<R> results = ignite.compute(ignite.cluster().forDataNodes(storage.cacheName())).broadcast(arguments -> {
        Ignite locIgnite = Ignition.localIgnite();
        Affinity<RowColMatrixKey> affinity = locIgnite.affinity(storage.cacheName());
        ClusterNode locNode = locIgnite.cluster().localNode();
        Map<ClusterNode, Collection<RowColMatrixKey>> keys = affinity.mapKeysToNodes(storage.getAllKeys());
        Collection<RowColMatrixKey> locKeys = keys.get(locNode);
        if (locKeys != null) {
            int idx = 0;
            Matrix locMatrix = new DenseLocalOnHeapMatrix(locKeys.size(), colSize);
            for (RowColMatrixKey key : locKeys) {
                Map<Integer, Double> row = storage.cache().get(key);
                for (Map.Entry<Integer, Double> cell : row.entrySet()) locMatrix.set(idx, cell.getKey(), cell.getValue());
                idx++;
            }
            return mapper.apply(locMatrix, arguments);
        }
        return null;
    }, args);
    return reducer.apply(results);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) Map(java.util.Map)

Example 59 with DenseLocalOnHeapMatrix

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

the class DecompositionSupport method copy.

/**
 * Create the copy of matrix with read-only matrices support.
 *
 * @param matrix Matrix for copy.
 * @return Copy.
 */
protected Matrix copy(Matrix matrix) {
    if (isCopyLikeSupport(matrix)) {
        DenseLocalOnHeapMatrix cp = new DenseLocalOnHeapMatrix(matrix.rowSize(), matrix.columnSize());
        cp.assign(matrix);
        return cp;
    } else
        return matrix.copy();
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 60 with DenseLocalOnHeapMatrix

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

the class FuzzyCMeansLocalClustererTest method equalWeightsOneDimension.

/**
 * Test FCM on points that forms three clusters on the line.
 */
@Test
public void equalWeightsOneDimension() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 10, null);
    double[][] points = new double[][] { { -10 }, { -9 }, { -8 }, { -7 }, { 7 }, { 8 }, { 9 }, { 10 }, { -1 }, { 0 }, { 1 } };
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, 3);
    Vector[] centers = mdl.centers();
    Arrays.sort(centers, Comparator.comparing(vector -> vector.getX(0)));
    assertEquals(-8.5, centers[0].getX(0), 2);
    assertEquals(0, centers[1].getX(0), 2);
    assertEquals(8.5, centers[2].getX(0), 2);
}
Also used : Arrays(java.util.Arrays) Assert.assertTrue(org.junit.Assert.assertTrue) DistanceMeasure(org.apache.ignite.ml.math.distances.DistanceMeasure) Test(org.junit.Test) ArrayList(java.util.ArrayList) Vector(org.apache.ignite.ml.math.Vector) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) Comparator(java.util.Comparator) Collections(java.util.Collections) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Assert.assertEquals(org.junit.Assert.assertEquals) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) 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