use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class MnistDistributed method testMNISTDistributed.
/**
*/
public void testMNISTDistributed() throws IOException {
int samplesCnt = 60_000;
int hiddenNeuronsCnt = 100;
IgniteBiTuple<Stream<DenseLocalOnHeapVector>, Stream<DenseLocalOnHeapVector>> trainingAndTest = loadMnist(samplesCnt);
// Load training mnist part into a cache.
Stream<DenseLocalOnHeapVector> trainingMnist = trainingAndTest.get1();
List<DenseLocalOnHeapVector> trainingMnistLst = trainingMnist.collect(Collectors.toList());
IgniteCache<Integer, LabeledVector<Vector, Vector>> labeledVectorsCache = LabeledVectorsCache.createNew(ignite);
loadIntoCache(trainingMnistLst, labeledVectorsCache);
MLPGroupUpdateTrainer<RPropParameterUpdate> trainer = MLPGroupUpdateTrainer.getDefault(ignite).withMaxGlobalSteps(35).withSyncPeriod(2);
MLPArchitecture arch = new MLPArchitecture(FEATURES_CNT).withAddedLayer(hiddenNeuronsCnt, true, Activators.SIGMOID).withAddedLayer(10, false, Activators.SIGMOID);
MultilayerPerceptron mdl = trainer.train(new MLPGroupUpdateTrainerCacheInput(arch, 9, labeledVectorsCache, 2000));
IgniteBiTuple<Matrix, Matrix> testDs = createDataset(trainingAndTest.get2(), 10_000, FEATURES_CNT);
Vector truth = testDs.get2().foldColumns(VectorUtils::vec2Num);
Vector predicted = mdl.apply(testDs.get1()).foldColumns(VectorUtils::vec2Num);
Tracer.showAscii(truth);
Tracer.showAscii(predicted);
X.println("Accuracy: " + VectorUtils.zipWith(predicted, truth, (x, y) -> x.equals(y) ? 1.0 : 0.0).sum() / truth.size() * 100 + "%.");
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class MnistLocal method tstMNISTLocal.
/**
* Run nn classifier on MNIST using bi-indexed cache as a storage for dataset.
* To run this test rename this method so it starts from 'test'.
*
* @throws IOException In case of loading MNIST dataset errors.
*/
@Test
public void tstMNISTLocal() throws IOException {
int samplesCnt = 60_000;
int featCnt = 28 * 28;
int hiddenNeuronsCnt = 100;
IgniteBiTuple<Stream<DenseLocalOnHeapVector>, Stream<DenseLocalOnHeapVector>> trainingAndTest = loadMnist(samplesCnt);
Stream<DenseLocalOnHeapVector> trainingMnistStream = trainingAndTest.get1();
Stream<DenseLocalOnHeapVector> testMnistStream = trainingAndTest.get2();
IgniteBiTuple<Matrix, Matrix> ds = createDataset(trainingMnistStream, samplesCnt, featCnt);
IgniteBiTuple<Matrix, Matrix> testDs = createDataset(testMnistStream, 10000, featCnt);
MLPArchitecture conf = new MLPArchitecture(featCnt).withAddedLayer(hiddenNeuronsCnt, true, Activators.SIGMOID).withAddedLayer(10, false, Activators.SIGMOID);
SimpleMLPLocalBatchTrainerInput input = new SimpleMLPLocalBatchTrainerInput(conf, new Random(), ds.get1(), ds.get2(), 2000);
MultilayerPerceptron mdl = new MLPLocalBatchTrainer<>(LossFunctions.MSE, () -> new RPropUpdateCalculator(0.1, 1.2, 0.5), 1E-7, 200).train(input);
X.println("Training started");
long before = System.currentTimeMillis();
X.println("Training finished in " + (System.currentTimeMillis() - before));
Vector predicted = mdl.apply(testDs.get1()).foldColumns(VectorUtils::vec2Num);
Vector truth = testDs.get2().foldColumns(VectorUtils::vec2Num);
Tracer.showAscii(truth);
Tracer.showAscii(predicted);
X.println("Accuracy: " + VectorUtils.zipWith(predicted, truth, (x, y) -> x.equals(y) ? 1.0 : 0.0).sum() / truth.size() * 100 + "%.");
}
use of org.apache.ignite.ml.math.Vector 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.Vector in project ignite by apache.
the class GenericLinearRegressionTrainerTest method testTrainOnBostonDataset.
/**
* Test trainer on boston dataset.
*/
@Test
public void testTrainOnBostonDataset() {
Matrix data = loadDataset("datasets/regression/boston.csv", 506, 13);
LinearRegressionModel mdl = trainer.train(data);
Vector expWeights = vectorCreator.apply(new double[] { -1.07170557e-01, 4.63952195e-02, 2.08602395e-02, 2.68856140e+00, -1.77957587e+01, 3.80475246e+00, 7.51061703e-04, -1.47575880e+00, 3.05655038e-01, -1.23293463e-02, -9.53463555e-01, 9.39251272e-03, -5.25466633e-01 });
double expIntercept = 36.4911032804;
TestUtils.assertEquals("Wrong weights", expWeights, mdl.getWeights(), precision);
TestUtils.assertEquals("Wrong intercept", expIntercept, mdl.getIntercept(), precision);
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class SVMModelTest method testPredictWithMultiClasses.
/**
*/
@Test
public void testPredictWithMultiClasses() {
Vector weights1 = new DenseLocalOnHeapVector(new double[] { 10.0, 0.0 });
Vector weights2 = new DenseLocalOnHeapVector(new double[] { 0.0, 10.0 });
Vector weights3 = new DenseLocalOnHeapVector(new double[] { -1.0, -1.0 });
SVMLinearMultiClassClassificationModel mdl = new SVMLinearMultiClassClassificationModel();
mdl.add(1, new SVMLinearBinaryClassificationModel(weights1, 0.0).withRawLabels(true));
mdl.add(2, new SVMLinearBinaryClassificationModel(weights2, 0.0).withRawLabels(true));
mdl.add(2, new SVMLinearBinaryClassificationModel(weights3, 0.0).withRawLabels(true));
Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
}
Aggregations