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);
}
Aggregations