Search in sources :

Example 1 with SparseBlockDistributedVector

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

the class KNNMultipleLinearRegressionTest method testSimpleRegressionWithOneNeighbour.

/**
 */
public void testSimpleRegressionWithOneNeighbour() {
    y = new double[] { 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 };
    x = new double[6][];
    x[0] = new double[] { 0, 0, 0, 0, 0 };
    x[1] = new double[] { 2.0, 0, 0, 0, 0 };
    x[2] = new double[] { 0, 3.0, 0, 0, 0 };
    x[3] = new double[] { 0, 0, 4.0, 0, 0 };
    x[4] = new double[] { 0, 0, 0, 5.0, 0 };
    x[5] = new double[] { 0, 0, 0, 0, 6.0 };
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    LabeledDataset training = new LabeledDataset(x, y);
    KNNMultipleLinearRegression knnMdl = new KNNMultipleLinearRegression(1, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
    Vector vector = new SparseBlockDistributedVector(new double[] { 0, 0, 0, 5.0, 0.0 });
    System.out.println(knnMdl.apply(vector));
    Assert.assertEquals(15, knnMdl.apply(vector), 1E-12);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNMultipleLinearRegression(org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) 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 2 with SparseBlockDistributedVector

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

the class SparseBlockDistributedMatrix method getCol.

/**
 * {@inheritDoc}
 */
@Override
public Vector getCol(int col) {
    checkColumnIndex(col);
    Vector res = new SparseBlockDistributedVector(rowSize());
    for (int i = 0; i < rowSize(); i++) res.setX(i, getX(i, col));
    return res;
}
Also used : SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) Vector(org.apache.ignite.ml.math.Vector) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector)

Example 3 with SparseBlockDistributedVector

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

the class SparseBlockDistributedMatrix method times.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked" })
@Override
public Vector times(final Vector vec) {
    if (vec == null)
        throw new IllegalArgumentException("The vector should be not null.");
    if (columnSize() != vec.size())
        throw new CardinalityException(columnSize(), vec.size());
    SparseBlockDistributedMatrix matrixA = this;
    SparseBlockDistributedVector vectorB = (SparseBlockDistributedVector) vec;
    String cacheName = this.storage().cacheName();
    SparseBlockDistributedVector vectorC = new SparseBlockDistributedVector(matrixA.rowSize());
    CacheUtils.bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        Affinity<VectorBlockKey> affinity = ignite.affinity(cacheName);
        IgniteCache<VectorBlockKey, VectorBlockEntry> cache = ignite.getOrCreateCache(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        BlockVectorStorage storageC = vectorC.storage();
        Map<ClusterNode, Collection<VectorBlockKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
        Collection<VectorBlockKey> locKeys = keysCToNodes.get(locNode);
        if (locKeys == null)
            return;
        // compute Cij locally on each node
        // TODO: IGNITE:5114, exec in parallel
        locKeys.forEach(key -> {
            long newBlockId = key.blockId();
            IgnitePair<Long> newBlockIdForMtx = new IgnitePair<>(newBlockId, 0L);
            VectorBlockEntry blockC = null;
            List<MatrixBlockEntry> aRow = matrixA.storage().getRowForBlock(newBlockIdForMtx);
            List<VectorBlockEntry> bCol = vectorB.storage().getColForBlock(newBlockId);
            for (int i = 0; i < aRow.size(); i++) {
                MatrixBlockEntry blockA = aRow.get(i);
                VectorBlockEntry blockB = bCol.get(i);
                VectorBlockEntry tmpBlock = new VectorBlockEntry(blockA.times(blockB));
                blockC = blockC == null ? tmpBlock : new VectorBlockEntry(blockC.plus(tmpBlock));
            }
            cache.put(storageC.getCacheKey(newBlockId), blockC);
        });
    });
    return vectorC;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) VectorBlockKey(org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey) VectorBlockEntry(org.apache.ignite.ml.math.impls.vector.VectorBlockEntry) BlockVectorStorage(org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Collection(java.util.Collection) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) Ignite(org.apache.ignite.Ignite) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException)

Example 4 with SparseBlockDistributedVector

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

the class SparseBlockDistributedMatrix method getRow.

/**
 * {@inheritDoc}
 */
@Override
public Vector getRow(int row) {
    checkRowIndex(row);
    Vector res = new SparseBlockDistributedVector(columnSize());
    for (int i = 0; i < columnSize(); i++) res.setX(i, getX(row, i));
    return res;
}
Also used : SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) Vector(org.apache.ignite.ml.math.Vector) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector)

Example 5 with SparseBlockDistributedVector

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

the class SparseDistributedBlockMatrixTest method testLikeVector.

/**
 */
public void testLikeVector() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    cacheMatrix = new SparseBlockDistributedMatrix(rows, cols);
    Vector v = cacheMatrix.likeVector(1);
    assert v.size() == 1;
    assert v instanceof SparseBlockDistributedVector;
}
Also used : SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) Vector(org.apache.ignite.ml.math.Vector) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector)

Aggregations

SparseBlockDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector)6 Vector (org.apache.ignite.ml.math.Vector)5 Collection (java.util.Collection)1 Ignite (org.apache.ignite.Ignite)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 IgnitePair (org.apache.ignite.internal.util.lang.IgnitePair)1 KNNMultipleLinearRegression (org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression)1 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)1 VectorBlockKey (org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey)1 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 BlockVectorStorage (org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage)1 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)1 VectorBlockEntry (org.apache.ignite.ml.math.impls.vector.VectorBlockEntry)1 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)1