Search in sources :

Example 1 with SparseDistributedMatrixMapReducer

use of org.apache.ignite.ml.optimization.util.SparseDistributedMatrixMapReducer 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)

Aggregations

Matrix (org.apache.ignite.ml.math.Matrix)1 Vector (org.apache.ignite.ml.math.Vector)1 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)1 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)1 FunctionVector (org.apache.ignite.ml.math.impls.vector.FunctionVector)1 SparseDistributedMatrixMapReducer (org.apache.ignite.ml.optimization.util.SparseDistributedMatrixMapReducer)1