Search in sources :

Example 31 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class CollectionsTest method test.

/**
 */
@Test
@SuppressWarnings("unchecked")
public void test() {
    test(new VectorizedViewMatrix(new DenseMatrix(2, 2), 1, 1, 1, 1), new VectorizedViewMatrix(new DenseMatrix(3, 2), 2, 1, 1, 1));
    specialTest(new ManhattanDistance(), new ManhattanDistance());
    specialTest(new HammingDistance(), new HammingDistance());
    specialTest(new EuclideanDistance(), new EuclideanDistance());
    FeatureMetadata data = new FeatureMetadata("name2");
    data.setName("name1");
    test(data, new FeatureMetadata("name2"));
    test(new DatasetRow<>(new DenseVector()), new DatasetRow<>(new DenseVector(1)));
    test(new LabeledVector<>(new DenseVector(), null), new LabeledVector<>(new DenseVector(1), null));
    test(new Dataset<DatasetRow<Vector>>(new DatasetRow[] {}, new FeatureMetadata[] {}), new Dataset<DatasetRow<Vector>>(new DatasetRow[] { new DatasetRow() }, new FeatureMetadata[] { new FeatureMetadata() }));
    test(new LogisticRegressionModel(new DenseVector(), 1.0), new LogisticRegressionModel(new DenseVector(), 0.5));
    test(new KMeansModelFormat(new Vector[] {}, new ManhattanDistance()), new KMeansModelFormat(new Vector[] {}, new HammingDistance()));
    test(new KMeansModel(new Vector[] {}, new ManhattanDistance()), new KMeansModel(new Vector[] {}, new HammingDistance()));
    test(new SVMLinearClassificationModel(null, 1.0), new SVMLinearClassificationModel(null, 0.5));
    test(new ANNClassificationModel(new LabeledVectorSet<>(), new ANNClassificationTrainer.CentroidStat()), new ANNClassificationModel(new LabeledVectorSet<>(1, 1), new ANNClassificationTrainer.CentroidStat()));
    test(new ANNModelFormat(1, new ManhattanDistance(), false, new LabeledVectorSet<>(), new ANNClassificationTrainer.CentroidStat()), new ANNModelFormat(2, new ManhattanDistance(), false, new LabeledVectorSet<>(), new ANNClassificationTrainer.CentroidStat()));
}
Also used : FeatureMetadata(org.apache.ignite.ml.structures.FeatureMetadata) HammingDistance(org.apache.ignite.ml.math.distances.HammingDistance) KMeansModel(org.apache.ignite.ml.clustering.kmeans.KMeansModel) LogisticRegressionModel(org.apache.ignite.ml.regressions.logistic.LogisticRegressionModel) ANNModelFormat(org.apache.ignite.ml.knn.ann.ANNModelFormat) LabeledVectorSet(org.apache.ignite.ml.structures.LabeledVectorSet) KMeansModelFormat(org.apache.ignite.ml.clustering.kmeans.KMeansModelFormat) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DatasetRow(org.apache.ignite.ml.structures.DatasetRow) VectorizedViewMatrix(org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix) ANNClassificationModel(org.apache.ignite.ml.knn.ann.ANNClassificationModel) SVMLinearClassificationModel(org.apache.ignite.ml.svm.SVMLinearClassificationModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) ManhattanDistance(org.apache.ignite.ml.math.distances.ManhattanDistance) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Example 32 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class KMeansModelTest method predictClusters.

/**
 */
@Test
public void predictClusters() {
    DistanceMeasure distanceMeasure = new EuclideanDistance();
    Vector[] centers = new DenseVector[4];
    centers[0] = new DenseVector(new double[] { 1.0, 1.0 });
    centers[1] = new DenseVector(new double[] { -1.0, 1.0 });
    centers[2] = new DenseVector(new double[] { 1.0, -1.0 });
    centers[3] = new DenseVector(new double[] { -1.0, -1.0 });
    KMeansModel mdl = new KMeansModel(centers, distanceMeasure);
    Assert.assertTrue(mdl.toString().contains("KMeansModel"));
    Assert.assertEquals(mdl.predict(new DenseVector(new double[] { 1.1, 1.1 })), 0.0, PRECISION);
    Assert.assertEquals(mdl.predict(new DenseVector(new double[] { -1.1, 1.1 })), 1.0, PRECISION);
    Assert.assertEquals(mdl.predict(new DenseVector(new double[] { 1.1, -1.1 })), 2.0, PRECISION);
    Assert.assertEquals(mdl.predict(new DenseVector(new double[] { -1.1, -1.1 })), 3.0, PRECISION);
    Assert.assertEquals(mdl.distanceMeasure(), distanceMeasure);
    Assert.assertEquals(mdl.amountOfClusters(), 4);
    Assert.assertArrayEquals(mdl.centers(), centers);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KMeansModel(org.apache.ignite.ml.clustering.kmeans.KMeansModel) DistanceMeasure(org.apache.ignite.ml.math.distances.DistanceMeasure) 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) Test(org.junit.Test)

Example 33 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class MnistUtils method mnistAsStream.

/**
 * Read random {@code count} samples from MNIST dataset from two files (images and labels) into a stream of labeled
 * vectors.
 *
 * @param imagesPath Path to the file with images.
 * @param labelsPath Path to the file with labels.
 * @param rnd Random numbers generator.
 * @param cnt Count of samples to read.
 * @return Stream of MNIST samples.
 * @throws IgniteException In case of exception.
 */
public static Stream<DenseVector> mnistAsStream(String imagesPath, String labelsPath, Random rnd, int cnt) throws IOException {
    FileInputStream isImages = new FileInputStream(imagesPath);
    FileInputStream isLabels = new FileInputStream(labelsPath);
    // Skip magic number.
    read4Bytes(isImages);
    int numOfImages = read4Bytes(isImages);
    int imgHeight = read4Bytes(isImages);
    int imgWidth = read4Bytes(isImages);
    // Skip magic number.
    read4Bytes(isLabels);
    // Skip number of labels.
    read4Bytes(isLabels);
    int numOfPixels = imgHeight * imgWidth;
    double[][] vecs = new double[numOfImages][numOfPixels + 1];
    for (int imgNum = 0; imgNum < numOfImages; imgNum++) {
        vecs[imgNum][numOfPixels] = isLabels.read();
        for (int p = 0; p < numOfPixels; p++) {
            int c = 128 - isImages.read();
            vecs[imgNum][p] = (double) c / 128;
        }
    }
    List<double[]> lst = Arrays.asList(vecs);
    Collections.shuffle(lst, rnd);
    isImages.close();
    isLabels.close();
    return lst.subList(0, cnt).stream().map(DenseVector::new);
}
Also used : FileInputStream(java.io.FileInputStream) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)

Example 34 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class MLPTrainerIntegrationTest method xorTest.

/**
 * Common method for testing 'XOR' with various updaters.
 *
 * @param updatesStgy Update strategy.
 * @param <P> Updater parameters type.
 */
private <P extends Serializable> void xorTest(UpdatesStrategy<? super MultilayerPerceptron, P> updatesStgy) {
    CacheConfiguration<Integer, LabeledVector<double[]>> xorCacheCfg = new CacheConfiguration<>();
    xorCacheCfg.setName("XorData");
    xorCacheCfg.setAffinity(new RendezvousAffinityFunction(false, 5));
    IgniteCache<Integer, LabeledVector<double[]>> xorCache = ignite.createCache(xorCacheCfg);
    try {
        xorCache.put(0, VectorUtils.of(0.0, 0.0).labeled(new double[] { 0.0 }));
        xorCache.put(1, VectorUtils.of(0.0, 1.0).labeled(new double[] { 1.0 }));
        xorCache.put(2, VectorUtils.of(1.0, 0.0).labeled(new double[] { 1.0 }));
        xorCache.put(3, VectorUtils.of(1.0, 1.0).labeled(new double[] { 0.0 }));
        MLPArchitecture arch = new MLPArchitecture(2).withAddedLayer(10, true, Activators.RELU).withAddedLayer(1, false, Activators.SIGMOID);
        MLPTrainer<P> trainer = new MLPTrainer<>(arch, LossFunctions.MSE, updatesStgy, 2500, 4, 50, 123L);
        MultilayerPerceptron mlp = trainer.fit(ignite, xorCache, new LabeledDummyVectorizer<>());
        Matrix predict = mlp.predict(new DenseMatrix(new double[][] { { 0.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 0.0 }, { 1.0, 1.0 } }));
        Tracer.showAscii(predict);
        X.println(new DenseVector(new double[] { 0.0 }).minus(predict.getRow(0)).kNorm(2) + "");
        TestUtils.checkIsInEpsilonNeighbourhood(new DenseVector(new double[] { 0.0 }), predict.getRow(0), 1E-1);
    } finally {
        xorCache.destroy();
    }
}
Also used : MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) LabeledVector(org.apache.ignite.ml.structures.LabeledVector) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)

Example 35 with DenseVector

use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.

the class MnistMLPTestUtil method loadMnist.

/**
 */
static IgniteBiTuple<Stream<DenseVector>, Stream<DenseVector>> loadMnist(int samplesCnt) throws IOException {
    Properties props = loadMNISTProperties();
    Stream<DenseVector> trainingMnistStream = MnistUtils.mnistAsStream(props.getProperty(PROP_TRAINING_IMAGES), props.getProperty(PROP_TRAINING_LABELS), new Random(123L), samplesCnt);
    Stream<DenseVector> testMnistStream = MnistUtils.mnistAsStream(props.getProperty(PROP_TEST_IMAGES), props.getProperty(PROP_TEST_LABELS), new Random(123L), 10_000);
    return new IgniteBiTuple<>(trainingMnistStream, testMnistStream);
}
Also used : Random(java.util.Random) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Properties(java.util.Properties) 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