Search in sources :

Example 1 with SparseDistributedVector

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

the class DistributedLinearRegressionWithSGDTrainerExample 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(), () -> {
            // Create SparseDistributedMatrix, new cache will be created automagically.
            System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread.");
            SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(data);
            System.out.println(">>> Create new linear regression trainer object.");
            Trainer<LinearRegressionModel, Matrix> trainer = new LinearRegressionSGDTrainer(100_000, 1e-12);
            System.out.println(">>> Perform the training to get the model.");
            LinearRegressionModel model = trainer.train(distributedMatrix);
            System.out.println(">>> Linear regression model: " + model);
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Ground Truth\t|");
            System.out.println(">>> ---------------------------------");
            for (double[] observation : data) {
                Vector inputs = new SparseDistributedVector(Arrays.copyOfRange(observation, 1, observation.length));
                double prediction = model.apply(inputs);
                double groundTruth = observation[0];
                System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
            }
            System.out.println(">>> ---------------------------------");
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixExample(org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) Matrix(org.apache.ignite.ml.math.Matrix) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) LinearRegressionSGDTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionSGDTrainer) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Vector(org.apache.ignite.ml.math.Vector)

Example 2 with SparseDistributedVector

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

the class SparseDistributedMatrix method times.

/**
 * {@inheritDoc}
 */
@Override
public Vector times(Vector vec) {
    if (vec == null)
        throw new IllegalArgumentException("The vector should be not null.");
    if (columnSize() != vec.size())
        throw new CardinalityException(columnSize(), vec.size());
    SparseDistributedMatrix matrixA = this;
    SparseDistributedVector vectorB = (SparseDistributedVector) vec;
    String cacheName = storage().cacheName();
    int rows = this.rowSize();
    SparseDistributedVector vectorC = (SparseDistributedVector) likeVector(rows);
    CacheUtils.bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        Affinity<RowColMatrixKey> affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        SparseDistributedVectorStorage storageC = vectorC.storage();
        Map<ClusterNode, Collection<RowColMatrixKey>> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys());
        Collection<RowColMatrixKey> locKeys = keysCToNodes.get(locNode);
        if (locKeys == null)
            return;
        // compute Cij locally on each node
        // TODO: IGNITE:5114, exec in parallel
        locKeys.forEach(key -> {
            int idx = key.index();
            Vector Aik = matrixA.getRow(idx);
            vectorC.set(idx, Aik.times(vectorB).sum());
        });
    });
    return vectorC;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) SparseDistributedVectorStorage(org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) RowColMatrixKey(org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Vector(org.apache.ignite.ml.math.Vector)

Example 3 with SparseDistributedVector

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

the class SparseDistributedMatrixTest method testLikeVector.

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

Example 4 with SparseDistributedVector

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

the class DistributedLinearRegressionWithQRTrainerExample 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(), () -> {
            // Create SparseDistributedMatrix, new cache will be created automagically.
            System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread.");
            SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(data);
            System.out.println(">>> Create new linear regression trainer object.");
            Trainer<LinearRegressionModel, Matrix> trainer = new LinearRegressionQRTrainer();
            System.out.println(">>> Perform the training to get the model.");
            LinearRegressionModel model = trainer.train(distributedMatrix);
            System.out.println(">>> Linear regression model: " + model);
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Ground Truth\t|");
            System.out.println(">>> ---------------------------------");
            for (double[] observation : data) {
                Vector inputs = new SparseDistributedVector(Arrays.copyOfRange(observation, 1, observation.length));
                double prediction = model.apply(inputs);
                double groundTruth = observation[0];
                System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
            }
            System.out.println(">>> ---------------------------------");
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixExample(org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample) Matrix(org.apache.ignite.ml.math.Matrix) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) LinearRegressionQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionQRTrainer) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) Vector(org.apache.ignite.ml.math.Vector) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector)

Aggregations

Vector (org.apache.ignite.ml.math.Vector)4 SparseDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseDistributedVector)4 Ignite (org.apache.ignite.Ignite)3 SparseDistributedMatrixExample (org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample)2 Matrix (org.apache.ignite.ml.math.Matrix)2 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)2 LinearRegressionModel (org.apache.ignite.ml.regressions.linear.LinearRegressionModel)2 IgniteThread (org.apache.ignite.thread.IgniteThread)2 Collection (java.util.Collection)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 RowColMatrixKey (org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey)1 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 SparseDistributedVectorStorage (org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage)1 LinearRegressionQRTrainer (org.apache.ignite.ml.regressions.linear.LinearRegressionQRTrainer)1 LinearRegressionSGDTrainer (org.apache.ignite.ml.regressions.linear.LinearRegressionSGDTrainer)1