Search in sources :

Example 91 with DenseLocalOnHeapVector

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

the class RPropParameterUpdate method sum.

/**
 * Sums updates returned by different trainings.
 *
 * @param updates Updates.
 * @return Sum of updates during returned by different trainings.
 */
public static RPropParameterUpdate sum(List<RPropParameterUpdate> updates) {
    Vector totalUpdate = updates.stream().filter(Objects::nonNull).map(pu -> VectorUtils.elementWiseTimes(pu.updatesMask().copy(), pu.prevIterationUpdates())).reduce(Vector::plus).orElse(null);
    Vector totalDelta = updates.stream().filter(Objects::nonNull).map(RPropParameterUpdate::deltas).reduce(Vector::plus).orElse(null);
    Vector totalGradient = updates.stream().filter(Objects::nonNull).map(RPropParameterUpdate::prevIterationGradient).reduce(Vector::plus).orElse(null);
    if (totalUpdate != null)
        return new RPropParameterUpdate(totalUpdate, totalGradient, totalDelta, new DenseLocalOnHeapVector(Objects.requireNonNull(totalDelta).size()).assign(1.0));
    return null;
}
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)

Example 92 with DenseLocalOnHeapVector

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

the class AbstractMatrix method getCol.

/**
 * {@inheritDoc}
 */
@Override
public Vector getCol(int col) {
    checkColumnIndex(col);
    Vector res;
    if (isDistributed())
        res = MatrixUtil.likeVector(this, rowSize());
    else
        res = new DenseLocalOnHeapVector(rowSize());
    for (int i = 0; i < rowSize(); i++) res.setX(i, getX(i, col));
    return res;
}
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)

Example 93 with DenseLocalOnHeapVector

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

the class Blas method syr.

/**
 * A := alpha * x * x^T + A.
 *
 * @param alpha a real scalar that will be multiplied to x * x^T^.
 * @param x the vector x that contains the n elements.
 * @param a the symmetric matrix A. Size of n x n.
 */
void syr(Double alpha, Vector x, DenseLocalOnHeapMatrix a) {
    int mA = a.rowSize();
    int nA = a.columnSize();
    if (mA != nA)
        throw new NonSquareMatrixException(mA, nA);
    if (mA != x.size())
        throw new CardinalityException(x.size(), mA);
    // TODO: IGNITE-5535, Process DenseLocalOffHeapVector
    if (x instanceof DenseLocalOnHeapVector)
        syr(alpha, x, a);
    else if (x instanceof SparseLocalVector)
        syr(alpha, x, a);
    else
        throw new IllegalArgumentException("Operation 'syr' does not support vector [class=" + x.getClass().getName() + "].");
}
Also used : NonSquareMatrixException(org.apache.ignite.ml.math.exceptions.NonSquareMatrixException) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) CardinalityException(org.apache.ignite.ml.math.exceptions.CardinalityException) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)

Example 94 with DenseLocalOnHeapVector

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

the class FuzzyCMeansLocalClustererTest method test2DLineClustering.

/**
 * Test FCM on points that forms the line located on the plane.
 */
@Test
public void test2DLineClustering() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 50, null);
    double[][] points = new double[][] { { 1, 2 }, { 3, 6 }, { 5, 10 } };
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    int k = 2;
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, k);
    Vector[] centers = mdl.centers();
    Arrays.sort(centers, Comparator.comparing(vector -> vector.getX(0)));
    Vector[] exp = { new DenseLocalOnHeapVector(new double[] { 1.5, 3 }), new DenseLocalOnHeapVector(new double[] { 4.5, 9 }) };
    for (int i = 0; i < k; i++) {
        Vector center = centers[i];
        for (int j = 0; j < 2; j++) assertEquals(exp[i].getX(j), center.getX(j), 0.5);
    }
}
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) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) 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)

Example 95 with DenseLocalOnHeapVector

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

the class KMeansDistributedClustererTestMultiNode method testClusterizationOnDatasetWithObviousStructure.

/**
 */
public void testClusterizationOnDatasetWithObviousStructure() throws IOException {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    int ptsCnt = 10000;
    int squareSideLen = 10000;
    Random rnd = new Random(123456L);
    // Let centers be in the vertices of square.
    Map<Integer, Vector> centers = new HashMap<>();
    centers.put(100, new DenseLocalOnHeapVector(new double[] { 0.0, 0.0 }));
    centers.put(900, new DenseLocalOnHeapVector(new double[] { squareSideLen, 0.0 }));
    centers.put(3000, new DenseLocalOnHeapVector(new double[] { 0.0, squareSideLen }));
    centers.put(6000, new DenseLocalOnHeapVector(new double[] { squareSideLen, squareSideLen }));
    SparseDistributedMatrix points = new SparseDistributedMatrix(ptsCnt, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
    List<Integer> permutation = IntStream.range(0, ptsCnt).boxed().collect(Collectors.toList());
    Collections.shuffle(permutation, rnd);
    int totalCnt = 0;
    for (Integer count : centers.keySet()) {
        for (int i = 0; i < count; i++) {
            Vector pnt = new DenseLocalOnHeapVector(2).assign(centers.get(count));
            // Perturbate point on random value.
            pnt.map(val -> val + rnd.nextDouble() * squareSideLen / 100);
            points.assignRow(permutation.get(totalCnt), pnt);
            totalCnt++;
        }
    }
    EuclideanDistance dist = new EuclideanDistance();
    KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(dist, 3, 100, 1L);
    clusterer.cluster(points, 4);
    points.destroy();
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) Random(java.util.Random) HashMap(java.util.HashMap) 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)

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