use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class KMeansDistributedClusterer method initClusterCenters.
/**
* Initialize cluster centers.
*/
private Vector[] initClusterCenters(SparseDistributedMatrix points, int k) {
// Initialize empty centers and point costs.
int ptsCnt = points.rowSize();
String cacheName = ((SparseDistributedMatrixStorage) points.getStorage()).cacheName();
// Initialize the first center to a random point.
Vector sample = localCopyOf(points.viewRow(rnd.nextInt(ptsCnt)));
List<Vector> centers = new ArrayList<>();
List<Vector> newCenters = new ArrayList<>();
newCenters.add(sample);
centers.add(sample);
final ConcurrentHashMap<Integer, Double> costs = new ConcurrentHashMap<>();
// On each step, sample 2 * k points on average with probability proportional
// to their squared distance from the centers. Note that only distances between points
// and new centers are computed in each iteration.
int step = 0;
UUID uid = points.getUUID();
while (step < initSteps) {
// We assume here that costs can fit into memory of one node.
ConcurrentHashMap<Integer, Double> newCosts = getNewCosts(points, newCenters, cacheName);
// Merge costs with new costs.
for (Integer ind : newCosts.keySet()) costs.merge(ind, newCosts.get(ind), Math::min);
double sumCosts = costs.values().stream().mapToDouble(Double::valueOf).sum();
newCenters = getNewCenters(k, costs, uid, sumCosts, cacheName);
centers.addAll(newCenters);
step++;
}
List<Vector> distinctCenters = centers.stream().distinct().collect(Collectors.toList());
if (distinctCenters.size() <= k)
return distinctCenters.toArray(new Vector[] {});
else {
// Finally, we might have a set of more than k distinct candidate centers; weight each
// candidate by the number of points in the dataset mapping to it and run a local k-means++
// on the weighted centers to pick k of them
ConcurrentHashMap<Integer, Integer> centerInd2Weight = weightCenters(uid, distinctCenters, cacheName);
List<Double> weights = new ArrayList<>(centerInd2Weight.size());
for (int i = 0; i < distinctCenters.size(); i++) weights.add(i, Double.valueOf(centerInd2Weight.getOrDefault(i, 0)));
DenseLocalOnHeapMatrix dCenters = MatrixUtil.fromList(distinctCenters, true);
return new KMeansLocalClusterer(getDistanceMeasure(), 30, seed).cluster(dCenters, k, weights).centers();
}
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class LocalModelsTest method getClusterModel.
/**
*/
private KMeansModel getClusterModel() {
KMeansLocalClusterer clusterer = new KMeansLocalClusterer(new EuclideanDistance(), 1, 1L);
double[] v1 = new double[] { 1959, 325100 };
double[] v2 = new double[] { 1960, 373200 };
DenseLocalOnHeapMatrix points = new DenseLocalOnHeapMatrix(new double[][] { v1, v2 });
return clusterer.cluster(points, 1);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class MLPLocalTrainerTest method xorTest.
/**
* Common method for testing 'XOR' with various updaters.
* @param updaterSupplier Updater supplier.
* @param <P> Updater parameters type.
*/
private <P> void xorTest(IgniteSupplier<ParameterUpdateCalculator<? super MultilayerPerceptron, P>> updaterSupplier) {
Matrix xorInputs = new DenseLocalOnHeapMatrix(new double[][] { { 0.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 0.0 }, { 1.0, 1.0 } }, StorageConstants.ROW_STORAGE_MODE).transpose();
Matrix xorOutputs = new DenseLocalOnHeapMatrix(new double[][] { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } }, StorageConstants.ROW_STORAGE_MODE).transpose();
MLPArchitecture conf = new MLPArchitecture(2).withAddedLayer(10, true, Activators.RELU).withAddedLayer(1, false, Activators.SIGMOID);
SimpleMLPLocalBatchTrainerInput trainerInput = new SimpleMLPLocalBatchTrainerInput(conf, new Random(123L), xorInputs, xorOutputs, 4);
MultilayerPerceptron mlp = new MLPLocalBatchTrainer<>(LossFunctions.MSE, updaterSupplier, 0.0001, 16000).train(trainerInput);
Matrix predict = mlp.apply(xorInputs);
Tracer.showAscii(predict);
X.println(xorOutputs.getRow(0).minus(predict.getRow(0)).kNorm(2) + "");
TestUtils.checkIsInEpsilonNeighbourhood(xorOutputs.getRow(0), predict.getRow(0), 1E-1);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class GradientDescentTest method testOptimizeWithOffset.
/**
* Test gradient descent optimization on function y = (x - 2)^2 with gradient function 2 * (x - 2).
*/
@Test
public void testOptimizeWithOffset() {
GradientDescent gradientDescent = new GradientDescent((inputs, groundTruth, point) -> point.minus(new DenseLocalOnHeapVector(new double[] { 2.0 })).times(2.0), new SimpleUpdater(0.01));
Vector res = gradientDescent.optimize(new DenseLocalOnHeapMatrix(new double[1][1]), new DenseLocalOnHeapVector(new double[] { 2.0 }));
TestUtils.assertEquals(2, res.get(0), PRECISION);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class CholeskyDecompositionTest method basicTest.
/**
*/
private void basicTest(Matrix m) {
// This decomposition is useful when dealing with systems of linear equations of the form
// m x = b where m is a Hermitian matrix.
// For such systems Cholesky decomposition provides
// more effective method of solving compared to LU decomposition.
// Suppose we want to solve system
// m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs
// as a matrix of the form
// (b1, b2, ..., bm)
// to the method Cholesky::solve which returns solutions in the form
// (sol1, sol2, ..., solm)
CholeskyDecomposition dec = new CholeskyDecomposition(m);
assertEquals("Unexpected value for decomposition determinant.", 4d, dec.getDeterminant(), 0d);
Matrix l = dec.getL();
Matrix lt = dec.getLT();
assertNotNull("Matrix l is expected to be not null.", l);
assertNotNull("Matrix lt is expected to be not null.", lt);
for (int row = 0; row < l.rowSize(); row++) for (int col = 0; col < l.columnSize(); col++) assertEquals("Unexpected value transposed matrix at (" + row + "," + col + ").", l.get(row, col), lt.get(col, row), 0d);
Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } }).transpose();
Matrix sol = dec.solve(bs);
assertNotNull("Solution matrix is expected to be not null.", sol);
assertEquals("Solution rows are not as expected.", bs.rowSize(), sol.rowSize());
assertEquals("Solution columns are not as expected.", bs.columnSize(), sol.columnSize());
for (int i = 0; i < sol.columnSize(); i++) assertNotNull("Solution matrix column is expected to be not null at index " + i, sol.viewColumn(i));
Vector b = new DenseLocalOnHeapVector(new double[] { 4.0, -6.0, 7.0 });
Vector solVec = dec.solve(b);
for (int idx = 0; idx < b.size(); idx++) assertEquals("Unexpected value solution vector at " + idx, b.get(idx), solVec.get(idx), 0d);
dec.destroy();
}
Aggregations