use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class IgniteLUDecompositionBenchmark method runLUDecomposition.
/**
* Based on LUDecompositionTest.
*/
private void runLUDecomposition() {
Matrix testMatrix = new DenseLocalOnHeapMatrix(new DataChanger.Scale().mutate(new double[][] { { 2.0d, 1.0d, 1.0d, 0.0d }, { 4.0d, 3.0d, 3.0d, 1.0d }, { 8.0d, 7.0d, 9.0d, 5.0d }, { 6.0d, 7.0d, 9.0d, 8.0d } }));
LUDecomposition dec1 = new LUDecomposition(new PivotedMatrixView(testMatrix));
dec1.solve(new DenseLocalOnHeapVector(testMatrix.rowSize()));
dec1.destroy();
LUDecomposition dec2 = new LUDecomposition(new PivotedMatrixView(testMatrix));
dec2.solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize()));
dec2.destroy();
LUDecomposition dec3 = new LUDecomposition(testMatrix);
dec3.getL();
dec3.getU();
dec3.getP();
dec3.getPivot();
dec3.destroy();
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class MLPGroupTrainerTest method doTestXOR.
/**
* Test training of 'xor' by {@link MLPGroupUpdateTrainer}.
*/
private <U extends Serializable> void doTestXOR(UpdatesStrategy<? super MultilayerPerceptron, U> stgy) {
int samplesCnt = 1000;
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);
IgniteCache<Integer, LabeledVector<Vector, Vector>> cache = LabeledVectorsCache.createNew(ignite);
String cacheName = cache.getName();
Random rnd = new Random(12345L);
try (IgniteDataStreamer<Integer, LabeledVector<Vector, Vector>> streamer = ignite.dataStreamer(cacheName)) {
streamer.perNodeBufferSize(10000);
for (int i = 0; i < samplesCnt; i++) {
int col = Math.abs(rnd.nextInt()) % 4;
streamer.addData(i, new LabeledVector<>(xorInputs.getCol(col), xorOutputs.getCol(col)));
}
}
int totalCnt = 30;
int failCnt = 0;
double maxFailRatio = 0.3;
MLPGroupUpdateTrainer<U> trainer = MLPGroupUpdateTrainer.getDefault(ignite).withSyncPeriod(3).withTolerance(0.001).withMaxGlobalSteps(100).withUpdateStrategy(stgy);
for (int i = 0; i < totalCnt; i++) {
MLPGroupUpdateTrainerCacheInput trainerInput = new MLPGroupUpdateTrainerCacheInput(conf, new RandomInitializer(new Random(123L + i)), 6, cache, 10, new Random(123L + i));
MultilayerPerceptron mlp = trainer.train(trainerInput);
Matrix predict = mlp.apply(xorInputs);
Tracer.showAscii(predict);
X.println(xorOutputs.getRow(0).minus(predict.getRow(0)).kNorm(2) + "");
failCnt += TestUtils.checkIsInEpsilonNeighbourhoodBoolean(xorOutputs.getRow(0), predict.getRow(0), 5E-1) ? 0 : 1;
}
double failRatio = (double) failCnt / totalCnt;
System.out.println("Fail percentage: " + (failRatio * 100) + "%.");
assertTrue(failRatio < maxFailRatio);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class MnistMLPTestUtil method createDataset.
/**
*/
static IgniteBiTuple<Matrix, Matrix> createDataset(Stream<DenseLocalOnHeapVector> s, int samplesCnt, int featCnt) {
Matrix vectors = new DenseLocalOnHeapMatrix(featCnt, samplesCnt);
Matrix labels = new DenseLocalOnHeapMatrix(10, samplesCnt);
List<DenseLocalOnHeapVector> sc = s.collect(Collectors.toList());
for (int i = 0; i < samplesCnt; i++) {
DenseLocalOnHeapVector v = sc.get(i);
vectors.assignColumn(i, v.viewPart(0, featCnt));
labels.assignColumn(i, num2Vec((int) v.getX(featCnt), 10));
}
return new IgniteBiTuple<>(vectors, labels);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class GradientDescentTest method testOptimize.
/**
* Test gradient descent optimization on function y = x^2 with gradient function 2 * x.
*/
@Test
public void testOptimize() {
GradientDescent gradientDescent = new GradientDescent((inputs, groundTruth, point) -> point.times(2), new SimpleUpdater(0.01));
Vector res = gradientDescent.optimize(new DenseLocalOnHeapMatrix(new double[1][1]), new DenseLocalOnHeapVector(new double[] { 2.0 }));
TestUtils.assertEquals(0, res.get(0), PRECISION);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class FuzzyCMeansLocalClustererTest method checkCentersOfTheSamePointsTwoDimensions.
/**
* Test FCM on points which have the equal coordinates.
*/
@Test
public void checkCentersOfTheSamePointsTwoDimensions() {
FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_MEMBERSHIPS, 0.01, 10, null);
double[][] points = new double[][] { { 3.3, 10 }, { 3.3, 10 }, { 3.3, 10 }, { 3.3, 10 }, { 3.3, 10 } };
DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
int k = 2;
FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, k);
Vector exp = new DenseLocalOnHeapVector(new double[] { 3.3, 10 });
for (int i = 0; i < k; i++) {
Vector center = mdl.centers()[i];
for (int j = 0; j < 2; j++) assertEquals(exp.getX(j), center.getX(j), 1);
}
}
Aggregations