use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class JensenShannonDistance method compute.
/**
* {@inheritDoc}
*/
@Override
public double compute(Vector a, Vector b) throws CardinalityException {
Vector aNormalized = MatrixUtil.localCopyOf(a).divide(a.sum());
Vector bNormalized = MatrixUtil.localCopyOf(b).divide(b.sum());
Vector mean = aNormalized.plus(bNormalized).divide(2d);
double js = aNormalized.map(mean, this::relativeEntropy).sum() + bNormalized.map(mean, this::relativeEntropy).sum();
js /= Math.log(base);
return Math.sqrt(js / 2d);
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class StackingTest method testSimpleStack.
/**
* Tests simple stack training.
*/
@Test
public void testSimpleStack() {
StackedDatasetTrainer<Vector, Vector, Double, LinearRegressionModel, Double> trainer = new StackedDatasetTrainer<>();
UpdatesStrategy<SmoothParametrized, SimpleGDParameterUpdate> updatesStgy = new UpdatesStrategy<>(new SimpleGDUpdateCalculator(0.2), SimpleGDParameterUpdate.SUM_LOCAL, SimpleGDParameterUpdate.AVG);
MLPArchitecture arch = new MLPArchitecture(2).withAddedLayer(10, true, Activators.RELU).withAddedLayer(1, false, Activators.SIGMOID);
MLPTrainer<SimpleGDParameterUpdate> trainer1 = new MLPTrainer<>(arch, LossFunctions.MSE, updatesStgy, 3000, 10, 50, 123L);
// Convert model trainer to produce Vector -> Vector model
DatasetTrainer<AdaptableDatasetModel<Vector, Vector, Matrix, Matrix, MultilayerPerceptron>, Double> mlpTrainer = AdaptableDatasetTrainer.of(trainer1).beforeTrainedModel((Vector v) -> new DenseMatrix(v.asArray(), 1)).afterTrainedModel((Matrix mtx) -> mtx.getRow(0)).withConvertedLabels(VectorUtils::num2Arr);
final double factor = 3;
StackedModel<Vector, Vector, Double, LinearRegressionModel> mdl = trainer.withAggregatorTrainer(new LinearRegressionLSQRTrainer().withConvertedLabels(x -> x * factor)).addTrainer(mlpTrainer).withAggregatorInputMerger(VectorUtils::concat).withSubmodelOutput2VectorConverter(IgniteFunction.identity()).withVector2SubmodelInputConverter(IgniteFunction.identity()).withOriginalFeaturesKept(IgniteFunction.identity()).withEnvironmentBuilder(TestUtils.testEnvBuilder()).fit(getCacheMock(xor), parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
assertEquals(0.0 * factor, mdl.predict(VectorUtils.of(0.0, 0.0)), 0.3);
assertEquals(1.0 * factor, mdl.predict(VectorUtils.of(0.0, 1.0)), 0.3);
assertEquals(1.0 * factor, mdl.predict(VectorUtils.of(1.0, 0.0)), 0.3);
assertEquals(0.0 * factor, mdl.predict(VectorUtils.of(1.0, 1.0)), 0.3);
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class GDBTrainerTest method testClassifier.
/**
*/
private void testClassifier(BiFunction<GDBTrainer, Map<Integer, double[]>, IgniteModel<Vector, Double>> fitter) {
int sampleSize = 100;
double[] xs = new double[sampleSize];
double[] ys = new double[sampleSize];
for (int i = 0; i < sampleSize; i++) {
xs[i] = i;
ys[i] = ((int) (xs[i] / 10.0) % 2) == 0 ? -1.0 : 1.0;
}
Map<Integer, double[]> learningSample = new HashMap<>();
for (int i = 0; i < sampleSize; i++) learningSample.put(i, new double[] { xs[i], ys[i] });
GDBTrainer trainer = new GDBBinaryClassifierOnTreesTrainer(0.3, 500, 3, 0.0).withUsingIdx(true).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.3));
IgniteModel<Vector, Double> mdl = fitter.apply(trainer, learningSample);
int errorsCnt = 0;
for (int j = 0; j < sampleSize; j++) {
double x = xs[j];
double y = ys[j];
double p = mdl.predict(VectorUtils.of(x));
if (p != y)
errorsCnt++;
}
assertEquals(0, errorsCnt);
assertTrue(mdl instanceof ModelsComposition);
ModelsComposition composition = (ModelsComposition) mdl;
composition.getModels().forEach(m -> assertTrue(m instanceof DecisionTreeModel));
assertTrue(composition.getModels().size() < 500);
assertTrue(composition.getPredictionsAggregator() instanceof WeightedPredictionsAggregator);
trainer = trainer.withCheckConvergenceStgyFactory(new ConvergenceCheckerStubFactory());
assertEquals(500, ((ModelsComposition) fitter.apply(trainer, learningSample)).getModels().size());
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class GDBTrainerTest method testUpdate.
/**
*/
@Test
public void testUpdate() {
int sampleSize = 100;
double[] xs = new double[sampleSize];
double[] ys = new double[sampleSize];
for (int i = 0; i < sampleSize; i++) {
xs[i] = i;
ys[i] = ((int) (xs[i] / 10.0) % 2) == 0 ? -1.0 : 1.0;
}
Map<Integer, double[]> learningSample = new HashMap<>();
for (int i = 0; i < sampleSize; i++) learningSample.put(i, new double[] { xs[i], ys[i] });
IgniteBiFunction<Integer, double[], Vector> fExtr = (k, v) -> VectorUtils.of(v[0]);
IgniteBiFunction<Integer, double[], Double> lExtr = (k, v) -> v[1];
GDBTrainer classifTrainer = new GDBBinaryClassifierOnTreesTrainer(0.3, 500, 3, 0.0).withUsingIdx(true).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.3));
GDBTrainer regressTrainer = new GDBRegressionOnTreesTrainer(0.3, 500, 3, 0.0).withUsingIdx(true).withCheckConvergenceStgyFactory(new MeanAbsValueConvergenceCheckerFactory(0.3));
// testUpdate(learningSample, fExtr, lExtr, classifTrainer);
// testUpdate(learningSample, fExtr, lExtr, regressTrainer);
}
use of org.apache.ignite.ml.math.primitives.vector.Vector in project ignite by apache.
the class IgniteModelStorageUtilTest method testSaveRemoveSaveModel.
/**
*/
@Test
public void testSaveRemoveSaveModel() throws Exception {
try (Ignite ignite = startGrid(cfg)) {
IgniteModelStorageUtil.saveModel(ignite, i -> 0.42, "mdl");
IgniteModelStorageUtil.removeModel(ignite, "mdl");
IgniteModelStorageUtil.saveModel(ignite, i -> 0.43, "mdl");
Model<Vector, Double> infMdl = IgniteModelStorageUtil.getModel(ignite, "mdl");
assertEquals(0.43, infMdl.predict(VectorUtils.of()));
}
}
Aggregations