Search in sources :

Example 16 with DenseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix in project ignite by apache.

the class GmmModelTest method testTwoComponents.

/**
 */
@Test
public void testTwoComponents() {
    Vector mean1 = VectorUtils.of(1., 2.);
    DenseMatrix covariance1 = MatrixUtil.fromList(Arrays.asList(VectorUtils.of(1, -0.25), VectorUtils.of(-0.25, 1)), true);
    Vector mean2 = VectorUtils.of(2., 1.);
    DenseMatrix covariance2 = MatrixUtil.fromList(Arrays.asList(VectorUtils.of(1, 0.5), VectorUtils.of(0.5, 1)), true);
    GmmModel gmm = new GmmModel(VectorUtils.of(0.5, 0.5), Arrays.asList(new MultivariateGaussianDistribution(mean1, covariance1), new MultivariateGaussianDistribution(mean2, covariance2)));
    Assert.assertEquals(0., gmm.predict(mean1), 0.01);
    Assert.assertEquals(1., gmm.predict(mean2), 0.01);
    Assert.assertEquals(0., gmm.predict(VectorUtils.of(1.5, 1.5)), 0.01);
    Assert.assertEquals(1., gmm.predict(VectorUtils.of(3., 0.)), 0.01);
}
Also used : MultivariateGaussianDistribution(org.apache.ignite.ml.math.stat.MultivariateGaussianDistribution) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Example 17 with DenseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix 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 18 with DenseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix in project ignite by apache.

the class MLPTest method testDifferentiation.

/**
 * Test differentiation.
 */
@Test
public void testDifferentiation() {
    int inputSize = 2;
    int firstLayerNeuronsCnt = 1;
    double w10 = 0.1;
    double w11 = 0.2;
    MLPArchitecture conf = new MLPArchitecture(inputSize).withAddedLayer(firstLayerNeuronsCnt, false, Activators.SIGMOID);
    MultilayerPerceptron mlp1 = new MultilayerPerceptron(conf);
    mlp1.setWeight(1, 0, 0, w10);
    MultilayerPerceptron mlp = mlp1.setWeight(1, 1, 0, w11);
    double x0 = 1.0;
    double x1 = 3.0;
    Matrix inputs = new DenseMatrix(new double[][] { { x0, x1 } }).transpose();
    double ytt = 1.0;
    Matrix truth = new DenseMatrix(new double[][] { { ytt } }).transpose();
    Vector grad = mlp.differentiateByParameters(LossFunctions.MSE, inputs, truth);
    // Let yt be y ground truth value.
    // d/dw1i [(yt - sigma(w10 * x0 + w11 * x1))^2] =
    // 2 * (yt - sigma(w10 * x0 + w11 * x1)) * (-1) * (sigma(w10 * x0 + w11 * x1)) * (1 - sigma(w10 * x0 + w11 * x1)) * xi =
    // let z = sigma(w10 * x0 + w11 * x1)
    // - 2* (yt - z) * (z) * (1 - z) * xi.
    IgniteTriFunction<Double, Vector, Vector, Vector> partialDer = (yt, w, x) -> {
        Double z = Activators.SIGMOID.apply(w.dot(x));
        return x.copy().map(xi -> -2 * (yt - z) * z * (1 - z) * xi);
    };
    Vector weightsVec = mlp.weights(1).getRow(0);
    Tracer.showAscii(weightsVec);
    Vector trueGrad = partialDer.andThen(x -> x).apply(ytt, weightsVec, inputs.getCol(0));
    Tracer.showAscii(trueGrad);
    Tracer.showAscii(grad);
    Assert.assertEquals(mlp.architecture().parametersCount(), grad.size());
    Assert.assertEquals(trueGrad, grad);
}
Also used : Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) TestUtils(org.apache.ignite.ml.TestUtils) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) LossFunctions(org.apache.ignite.ml.optimization.LossFunctions) Tracer(org.apache.ignite.ml.math.Tracer) Test(org.junit.Test) MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) Assert(org.junit.Assert) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Example 19 with DenseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix in project ignite by apache.

the class MLPTest method testSimpleMLPPrediction.

/**
 * Tests that MLP with 2 layer, 1 neuron in each layer and weight equal to 1 is equivalent to sigmoid function.
 */
@Test
public void testSimpleMLPPrediction() {
    MLPArchitecture conf = new MLPArchitecture(1).withAddedLayer(1, false, Activators.SIGMOID);
    MultilayerPerceptron mlp = new MultilayerPerceptron(conf, new MLPConstInitializer(1));
    int input = 2;
    Matrix predict = mlp.predict(new DenseMatrix(new double[][] { { input } }));
    Assert.assertEquals(predict, new DenseMatrix(new double[][] { { Activators.SIGMOID.apply(input) } }));
}
Also used : Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Example 20 with DenseMatrix

use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix 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)

Aggregations

DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)39 Test (org.junit.Test)28 Matrix (org.apache.ignite.ml.math.primitives.matrix.Matrix)14 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)14 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)9 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)8 SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)4 MultivariateGaussianDistribution (org.apache.ignite.ml.math.stat.MultivariateGaussianDistribution)3 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)3 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)2 HammingDistance (org.apache.ignite.ml.math.distances.HammingDistance)2 ManhattanDistance (org.apache.ignite.ml.math.distances.ManhattanDistance)2 ViewMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.ViewMatrix)2 VectorizedViewMatrix (org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix)2 MLPTrainer (org.apache.ignite.ml.nn.MLPTrainer)2 SimpleGDParameterUpdate (org.apache.ignite.ml.optimization.updatecalculators.SimpleGDParameterUpdate)2 SimpleGDUpdateCalculator (org.apache.ignite.ml.optimization.updatecalculators.SimpleGDUpdateCalculator)2 Path (java.nio.file.Path)1