use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class MLPGroupTrainerExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) throws InterruptedException {
// IMPL NOTE based on MLPGroupTrainerTest#testXOR
System.out.println(">>> Distributed multilayer perceptron example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
// Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
// because we create ignite cache internally.
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), MLPGroupTrainerExample.class.getSimpleName(), () -> {
int samplesCnt = 10000;
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(100);
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 = 100;
int failCnt = 0;
MLPGroupUpdateTrainer<RPropParameterUpdate> trainer = MLPGroupUpdateTrainer.getDefault(ignite).withSyncPeriod(3).withTolerance(0.001).withMaxGlobalSteps(20);
for (int i = 0; i < totalCnt; i++) {
MLPGroupUpdateTrainerCacheInput trainerInput = new MLPGroupUpdateTrainerCacheInput(conf, new RandomInitializer(rnd), 6, cache, 10);
MultilayerPerceptron mlp = trainer.train(trainerInput);
Matrix predict = mlp.apply(xorInputs);
System.out.println(">>> Prediction data at step " + i + " of total " + totalCnt + ":");
Tracer.showAscii(predict);
System.out.println("Difference estimate: " + xorOutputs.getRow(0).minus(predict.getRow(0)).kNorm(2));
failCnt += closeEnough(xorOutputs.getRow(0), predict.getRow(0)) ? 0 : 1;
}
double failRatio = (double) failCnt / totalCnt;
System.out.println("\n>>> Fail percentage: " + (failRatio * 100) + "%.");
System.out.println("\n>>> Distributed multilayer perceptron example completed.");
});
igniteThread.start();
igniteThread.join();
}
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class TracerExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
*/
public static void main(String[] args) throws IOException {
System.out.println(">>> Tracer utility example started.");
// Tracer is a simple utility class that allows pretty-printing of matrices/vectors.
DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 1.12345, 2.12345 }, { 3.12345, 4.12345 } });
System.out.println("\n>>> Tracer output to console in ASCII.");
Tracer.showAscii(m, "%.3g");
System.out.println("\n>>> Tracer output to browser in HTML.");
Tracer.showHtml(m, COLOR_MAPPER, true);
System.out.println("\n>>> Tracer utility example completed.");
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class KMeansLocalClustererTest method testPerformClusterAnalysisDegenerate.
/**
* Two points, one cluster, one iteration
*/
@Test
public void testPerformClusterAnalysisDegenerate() {
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 });
KMeansModel mdl = clusterer.cluster(points, 1);
Assert.assertEquals(1, mdl.centers().length);
Assert.assertEquals(2, mdl.centers()[0].size());
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class IgniteFuzzyCMeansLocalClustererBenchmark method test.
/**
* {@inheritDoc}
*/
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
// IMPL NOTE originally taken from FuzzyLocalCMeansExample.
// Distance measure that computes distance between two points.
DistanceMeasure distanceMeasure = new EuclideanDistance();
// "Fuzziness" - specific constant that is used in membership calculation (1.0+-eps ~ K-Means).
double exponentialWeight = 2.0;
// Condition that indicated when algorithm must stop.
// In this example algorithm stops if memberships have changed insignificantly.
BaseFuzzyCMeansClusterer.StopCondition stopCond = BaseFuzzyCMeansClusterer.StopCondition.STABLE_MEMBERSHIPS;
// Maximum difference between new and old membership values with which algorithm will continue to work.
double maxDelta = 0.01;
// The maximum number of FCM iterations.
int maxIterations = 50;
// Create new local clusterer with parameters described above.
FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(distanceMeasure, exponentialWeight, stopCond, maxDelta, maxIterations, null);
// Create sample data.
double[][] points = shuffle((int) (DataChanger.next()));
// Create the matrix that contains sample points.
DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
// Call clusterization method with some number of centers.
// It returns model that can predict results for new points.
int numCenters = 4;
FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, numCenters);
// Get centers of clusters that is computed by Fuzzy C-Means algorithm.
mdl.centers();
return true;
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class IgniteEigenDecompositionBenchmark method runEigenDecomposition.
/**
* Based on EigenDecompositionTest.
*/
private void runEigenDecomposition() {
final DataChanger.Scale scale = new DataChanger.Scale();
EigenDecomposition decomposition1 = new EigenDecomposition(new DenseLocalOnHeapMatrix(scale.mutate(new double[][] { { 1.0d, 0.0d, 0.0d, 1.0d }, { 0.0d, 1.0d, 0.0d, 1.0d }, { 0.0d, 0.0d, 2.0d, 0.0d }, { 1.0d, 1.0d, 0.0d, 2.0d } })));
decomposition1.getD();
decomposition1.getV();
decomposition1.destroy();
EigenDecomposition decomposition2 = new EigenDecomposition(new DenseLocalOnHeapMatrix(scale.mutate(new double[][] { { 1.0d, 0.0d, 0.0d }, { 0.0d, 1.0d, 0.0d }, { 0.0d, 0.0d, 2.0d }, { 1.0d, 1.0d, 0.0d } })));
// TODO: IGNITE-5828, find out why decomposition of 3X4 matrix throws row index exception
decomposition2.getD();
decomposition2.getV();
decomposition2.destroy();
}
Aggregations