use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class KMeansTrainerTest method testUpdateMdl.
/**
*/
@Test
public void testUpdateMdl() {
KMeansTrainer trainer = createAndCheckTrainer();
Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST);
KMeansModel originalMdl = trainer.withAmountOfClusters(1).fit(new LocalDatasetBuilder<>(data, parts), vectorizer);
KMeansModel updatedMdlOnSameDataset = trainer.update(originalMdl, new LocalDatasetBuilder<>(data, parts), vectorizer);
KMeansModel updatedMdlOnEmptyDataset = trainer.update(originalMdl, new LocalDatasetBuilder<>(new HashMap<>(), parts), vectorizer);
Vector firstVector = new DenseVector(new double[] { 2.0, 2.0 });
Vector secondVector = new DenseVector(new double[] { -2.0, -2.0 });
assertEquals(originalMdl.predict(firstVector), updatedMdlOnSameDataset.predict(firstVector), PRECISION);
assertEquals(originalMdl.predict(secondVector), updatedMdlOnSameDataset.predict(secondVector), PRECISION);
assertEquals(originalMdl.predict(firstVector), updatedMdlOnEmptyDataset.predict(firstVector), PRECISION);
assertEquals(originalMdl.predict(secondVector), updatedMdlOnEmptyDataset.predict(secondVector), PRECISION);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class ExternalizeTest method test.
/**
*/
@Test
@SuppressWarnings("unchecked")
public void test() {
externalizeTest(new DelegatingVector(new DenseVector(1)));
externalizeTest(new VectorizedViewMatrix(new DenseMatrix(2, 2), 1, 1, 1, 1));
externalizeTest(new ManhattanDistance());
externalizeTest(new HammingDistance());
externalizeTest(new EuclideanDistance());
externalizeTest(new FeatureMetadata());
externalizeTest(new VectorizedViewMatrix(new DenseMatrix(2, 2), 1, 1, 1, 1));
externalizeTest(new DatasetRow<>(new DenseVector()));
externalizeTest(new LabeledVector<>(new DenseVector(), null));
externalizeTest(new Dataset<DatasetRow<Vector>>(new DatasetRow[] {}, new FeatureMetadata[] {}));
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class KMeansTrainerTest method findOneClusters.
/**
* A few points, one cluster, one iteration
*/
@Test
public void findOneClusters() {
KMeansTrainer trainer = createAndCheckTrainer();
KMeansModel knnMdl = trainer.withAmountOfClusters(1).fit(new LocalDatasetBuilder<>(data, parts), new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
Vector firstVector = new DenseVector(new double[] { 2.0, 2.0 });
assertEquals(knnMdl.predict(firstVector), 0.0, PRECISION);
Vector secondVector = new DenseVector(new double[] { -2.0, -2.0 });
assertEquals(knnMdl.predict(secondVector), 0.0, PRECISION);
assertEquals(trainer.getMaxIterations(), 1);
assertEquals(trainer.getEpsilon(), PRECISION, PRECISION);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class RPropParameterUpdate method sumLocal.
/**
* Sums updates during one training.
*
* @param updates Updates.
* @return Sum of updates during one training.
*/
private static RPropParameterUpdate sumLocal(List<RPropParameterUpdate> updates) {
List<RPropParameterUpdate> nonNullUpdates = updates.stream().filter(Objects::nonNull).collect(Collectors.toList());
if (nonNullUpdates.isEmpty())
return null;
Vector newDeltas = nonNullUpdates.get(nonNullUpdates.size() - 1).deltas();
Vector newGradient = nonNullUpdates.get(nonNullUpdates.size() - 1).prevIterationGradient();
Vector totalUpdate = nonNullUpdates.stream().map(pu -> VectorUtils.elementWiseTimes(pu.updatesMask().copy(), pu.prevIterationUpdates())).reduce(Vector::plus).orElse(null);
return new RPropParameterUpdate(totalUpdate, newGradient, newDeltas, new DenseVector(newDeltas.size()).assign(1.0));
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class RPropParameterUpdate method sum.
/**
* Sums updates returned by different trainings.
*
* @param updates Updates.
* @return Sum of updates during returned by different trainings.
*/
private static RPropParameterUpdate sum(List<RPropParameterUpdate> updates) {
Vector totalUpdate = updates.stream().filter(Objects::nonNull).map(pu -> VectorUtils.elementWiseTimes(pu.updatesMask().copy(), pu.prevIterationUpdates())).reduce(Vector::plus).orElse(null);
Vector totalDelta = updates.stream().filter(Objects::nonNull).map(RPropParameterUpdate::deltas).reduce(Vector::plus).orElse(null);
Vector totalGradient = updates.stream().filter(Objects::nonNull).map(RPropParameterUpdate::prevIterationGradient).reduce(Vector::plus).orElse(null);
if (totalUpdate != null)
return new RPropParameterUpdate(totalUpdate, totalGradient, totalDelta, new DenseVector(Objects.requireNonNull(totalDelta).size()).assign(1.0));
return null;
}
Aggregations