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