use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class QRDecompositionTest method basicTest.
/**
*/
private void basicTest(Matrix m) {
QRDecomposition dec = new QRDecomposition(m);
assertTrue("Unexpected value for full rank in decomposition " + dec, dec.hasFullRank());
Matrix q = dec.getQ();
Matrix r = dec.getR();
assertNotNull("Matrix q is expected to be not null.", q);
assertNotNull("Matrix r is expected to be not null.", r);
Matrix qSafeCp = safeCopy(q);
Matrix expIdentity = qSafeCp.times(qSafeCp.transpose());
final double delta = 0.0001;
for (int row = 0; row < expIdentity.rowSize(); row++) for (int col = 0; col < expIdentity.columnSize(); col++) assertEquals("Unexpected identity matrix value at (" + row + "," + col + ").", row == col ? 1d : 0d, expIdentity.get(col, row), delta);
for (int row = 0; row < r.rowSize(); row++) for (int col = 0; col < row - 1; col++) assertEquals("Unexpected upper triangular matrix value at (" + row + "," + col + ").", 0d, r.get(row, col), delta);
Matrix recomposed = qSafeCp.times(r);
for (int row = 0; row < m.rowSize(); row++) for (int col = 0; col < m.columnSize(); col++) assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", m.get(row, col), recomposed.get(row, col), delta);
Matrix sol = dec.solve(new DenseLocalOnHeapMatrix(3, 10));
assertEquals("Unexpected rows in solution matrix.", 3, sol.rowSize());
assertEquals("Unexpected cols in solution matrix.", 10, sol.columnSize());
for (int row = 0; row < sol.rowSize(); row++) for (int col = 0; col < sol.columnSize(); col++) assertEquals("Unexpected solution matrix value at (" + row + "," + col + ").", 0d, sol.get(row, col), delta);
dec.destroy();
QRDecomposition dec1 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d }, { -1.0d, 2.0d }, { 0.0d, -1.0d } }));
assertTrue("Unexpected value for full rank in decomposition " + dec1, dec1.hasFullRank());
dec1.destroy();
QRDecomposition dec2 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d, 0.0d }, { -1.0d, 2.0d, -1.0d, 0.0d }, { 0.0d, -1.0d, 2.0d, 0.0d } }));
assertTrue("Unexpected value for full rank in decomposition " + dec2, dec2.hasFullRank());
dec2.destroy();
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class MLPGroupUpdateTrainerCacheInput method batchSupplier.
/**
* {@inheritDoc}
*/
@Override
public IgniteSupplier<IgniteBiTuple<Matrix, Matrix>> batchSupplier() {
String cName = cache.getName();
// This line is for prohibiting of 'this' object be caught into serialization context of lambda.
int bs = batchSize;
// This line is for prohibiting of 'this' object be caught into serialization context of lambda.
Random r = rand;
return () -> {
Ignite ignite = Ignition.localIgnite();
IgniteCache<Integer, LabeledVector<Vector, Vector>> cache = ignite.getOrCreateCache(cName);
int total = cache.size();
Affinity<Integer> affinity = ignite.affinity(cName);
List<Integer> allKeys = IntStream.range(0, total).boxed().collect(Collectors.toList());
List<Integer> keys = new ArrayList<>(affinity.mapKeysToNodes(allKeys).get(ignite.cluster().localNode()));
int locKeysCnt = keys.size();
int[] selected = Utils.selectKDistinct(locKeysCnt, Math.min(bs, locKeysCnt), r);
// Get dimensions of vectors in cache. We suppose that every feature vector has
// same dimension d 1 and every label has the same dimension d2.
LabeledVector<Vector, Vector> dimEntry = cache.get(keys.get(selected[0]));
Matrix inputs = new DenseLocalOnHeapMatrix(dimEntry.features().size(), bs);
Matrix groundTruth = new DenseLocalOnHeapMatrix(dimEntry.label().size(), bs);
for (int i = 0; i < selected.length; i++) {
LabeledVector<Vector, Vector> labeled = cache.get(keys.get(selected[i]));
inputs.assignColumn(i, labeled.features());
groundTruth.assignColumn(i, labeled.label());
}
return new IgniteBiTuple<>(inputs, groundTruth);
};
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class SparseDistributedMatrixMapReducer method mapReduce.
/**
*/
public <R, T> R mapReduce(IgniteBiFunction<Matrix, T, R> mapper, IgniteFunction<Collection<R>, R> reducer, T args) {
Ignite ignite = Ignition.localIgnite();
SparseDistributedMatrixStorage storage = (SparseDistributedMatrixStorage) distributedMatrix.getStorage();
int colSize = distributedMatrix.columnSize();
Collection<R> results = ignite.compute(ignite.cluster().forDataNodes(storage.cacheName())).broadcast(arguments -> {
Ignite locIgnite = Ignition.localIgnite();
Affinity<RowColMatrixKey> affinity = locIgnite.affinity(storage.cacheName());
ClusterNode locNode = locIgnite.cluster().localNode();
Map<ClusterNode, Collection<RowColMatrixKey>> keys = affinity.mapKeysToNodes(storage.getAllKeys());
Collection<RowColMatrixKey> locKeys = keys.get(locNode);
if (locKeys != null) {
int idx = 0;
Matrix locMatrix = new DenseLocalOnHeapMatrix(locKeys.size(), colSize);
for (RowColMatrixKey key : locKeys) {
Map<Integer, Double> row = storage.cache().get(key);
for (Map.Entry<Integer, Double> cell : row.entrySet()) locMatrix.set(idx, cell.getKey(), cell.getValue());
idx++;
}
return mapper.apply(locMatrix, arguments);
}
return null;
}, args);
return reducer.apply(results);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class DecompositionSupport method copy.
/**
* Create the copy of matrix with read-only matrices support.
*
* @param matrix Matrix for copy.
* @return Copy.
*/
protected Matrix copy(Matrix matrix) {
if (isCopyLikeSupport(matrix)) {
DenseLocalOnHeapMatrix cp = new DenseLocalOnHeapMatrix(matrix.rowSize(), matrix.columnSize());
cp.assign(matrix);
return cp;
} else
return matrix.copy();
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class FuzzyCMeansLocalClustererTest method equalWeightsOneDimension.
/**
* Test FCM on points that forms three clusters on the line.
*/
@Test
public void equalWeightsOneDimension() {
FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 10, null);
double[][] points = new double[][] { { -10 }, { -9 }, { -8 }, { -7 }, { 7 }, { 8 }, { 9 }, { 10 }, { -1 }, { 0 }, { 1 } };
DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, 3);
Vector[] centers = mdl.centers();
Arrays.sort(centers, Comparator.comparing(vector -> vector.getX(0)));
assertEquals(-8.5, centers[0].getX(0), 2);
assertEquals(0, centers[1].getX(0), 2);
assertEquals(8.5, centers[2].getX(0), 2);
}
Aggregations