Search in sources :

Example 86 with DenseLocalOnHeapVector

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

the class DistanceTest method setup.

/**
 */
@Before
public void setup() {
    v1 = new DenseLocalOnHeapVector(new double[] { 0.0, 0.0, 0.0 });
    v2 = new DenseLocalOnHeapVector(new double[] { 2.0, 1.0, 0.0 });
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Before(org.junit.Before)

Example 87 with DenseLocalOnHeapVector

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

the class MatrixImplementationsTest method testAssignRow.

/**
 */
@Test
public void testAssignRow() {
    consumeSampleMatrix((m, desc) -> {
        if (ignore(m.getClass()))
            return;
        fillMatrix(m);
        int rowIdx = m.rowSize() / 2;
        double[] newValues = fillArray(m.columnSize());
        m.assignRow(rowIdx, new DenseLocalOnHeapVector(newValues));
        for (int col = 0; col < m.columnSize(); col++) assertEquals("Unexpected value for " + desc + " at " + col, newValues[col], m.get(rowIdx, col), 0d);
        testInvalidCardinality(() -> m.assignRow(rowIdx, new DenseLocalOnHeapVector(m.columnSize() + 1)), desc);
    });
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 88 with DenseLocalOnHeapVector

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

the class MatrixImplementationsTest method testAssignColumn.

/**
 */
@Test
public void testAssignColumn() {
    consumeSampleMatrix((m, desc) -> {
        if (ignore(m.getClass()))
            return;
        fillMatrix(m);
        int colIdx = m.columnSize() / 2;
        double[] newValues = fillArray(m.rowSize());
        m.assignColumn(colIdx, new DenseLocalOnHeapVector(newValues));
        for (int row = 0; row < m.rowSize(); row++) assertEquals("Unexpected value for " + desc + " at " + row, newValues[row], m.get(row, colIdx), 0d);
    });
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 89 with DenseLocalOnHeapVector

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

the class GradientDescent method calculateDistributedGradient.

/**
 * Calculates gradient based in distributed matrix using {@link SparseDistributedMatrixMapReducer}.
 *
 * @param data Distributed matrix
 * @param weights Point to calculate gradient
 * @return Gradient
 */
private Vector calculateDistributedGradient(SparseDistributedMatrix data, Vector weights) {
    SparseDistributedMatrixMapReducer mapReducer = new SparseDistributedMatrixMapReducer(data);
    return mapReducer.mapReduce((matrix, args) -> {
        Matrix inputs = extractInputs(matrix);
        Vector groundTruth = extractGroundTruth(matrix);
        return lossGradient.compute(inputs, groundTruth, args);
    }, gradients -> {
        int cnt = 0;
        Vector resGradient = new DenseLocalOnHeapVector(data.columnSize());
        for (Vector gradient : gradients) {
            if (gradient != null) {
                resGradient = resGradient.plus(gradient);
                cnt++;
            }
        }
        return resGradient.divide(cnt);
    }, weights);
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) FunctionVector(org.apache.ignite.ml.math.impls.vector.FunctionVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) SparseDistributedMatrixMapReducer(org.apache.ignite.ml.optimization.util.SparseDistributedMatrixMapReducer)

Example 90 with DenseLocalOnHeapVector

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

the class RPropParameterUpdate method sumLocal.

/**
 * Sums updates during one training.
 *
 * @param updates Updates.
 * @return Sum of updates during one training.
 */
public static RPropParameterUpdate sumLocal(List<RPropParameterUpdate> updates) {
    List<RPropParameterUpdate> nonNullUpdates = updates.stream().filter(Objects::nonNull).collect(Collectors.toList());
    if (nonNullUpdates.isEmpty())
        return null;
    Vector newDeltas = nonNullUpdates.get(nonNullUpdates.size() - 1).deltas();
    Vector newGradient = nonNullUpdates.get(nonNullUpdates.size() - 1).prevIterationGradient();
    Vector totalUpdate = nonNullUpdates.stream().map(pu -> VectorUtils.elementWiseTimes(pu.updatesMask().copy(), pu.prevIterationUpdates())).reduce(Vector::plus).orElse(null);
    return new RPropParameterUpdate(totalUpdate, newGradient, newDeltas, new DenseLocalOnHeapVector(newDeltas.size()).assign(1.0));
}
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)

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