Search in sources :

Example 66 with DenseVector

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);
}
Also used : KMeansModel(org.apache.ignite.ml.clustering.kmeans.KMeansModel) HashMap(java.util.HashMap) KMeansTrainer(org.apache.ignite.ml.clustering.kmeans.KMeansTrainer) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 67 with DenseVector

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[] {}));
}
Also used : FeatureMetadata(org.apache.ignite.ml.structures.FeatureMetadata) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) HammingDistance(org.apache.ignite.ml.math.distances.HammingDistance) DatasetRow(org.apache.ignite.ml.structures.DatasetRow) VectorizedViewMatrix(org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix) DelegatingVector(org.apache.ignite.ml.math.primitives.vector.impl.DelegatingVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) ManhattanDistance(org.apache.ignite.ml.math.distances.ManhattanDistance) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Example 68 with DenseVector

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);
}
Also used : KMeansModel(org.apache.ignite.ml.clustering.kmeans.KMeansModel) DoubleArrayVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer) KMeansTrainer(org.apache.ignite.ml.clustering.kmeans.KMeansTrainer) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 69 with DenseVector

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));
}
Also used : Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)

Example 70 with DenseVector

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;
}
Also used : Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)

Aggregations

DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)101 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)59 Test (org.junit.Test)59 Serializable (java.io.Serializable)16 SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)14 HashMap (java.util.HashMap)13 DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)13 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)10 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)10 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)9 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)9 HashSet (java.util.HashSet)7 TrainerTest (org.apache.ignite.ml.common.TrainerTest)7 KMeansModel (org.apache.ignite.ml.clustering.kmeans.KMeansModel)5 LocalDatasetBuilder (org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder)5 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)5 IgniteDifferentiableVectorToDoubleFunction (org.apache.ignite.ml.math.functions.IgniteDifferentiableVectorToDoubleFunction)5 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)5 OneHotEncoderPreprocessor (org.apache.ignite.ml.preprocessing.encoding.onehotencoder.OneHotEncoderPreprocessor)4 Random (java.util.Random)3