Search in sources :

Example 26 with DenseMatrix

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

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

the class MultilayerPerceptron method readFromVector.

/**
 * Read matrix with given dimensions from vector starting with offset and return new offset position
 * which is last matrix entry position + 1.
 *
 * @param v Vector to read from.
 * @param rows Count of rows of matrix to read.
 * @param cols Count of columns of matrix to read.
 * @param off Start read position.
 * @return New offset position which is last matrix entry position + 1.
 */
private IgniteBiTuple<Integer, Matrix> readFromVector(Vector v, int rows, int cols, int off) {
    Matrix mtx = new DenseMatrix(rows, cols);
    int size = rows * cols;
    for (int i = 0; i < size; i++) mtx.setX(i / cols, i % cols, v.getX(off + i));
    return new IgniteBiTuple<>(off + size, mtx);
}
Also used : Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)

Example 28 with DenseMatrix

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

the class ReplicatedVectorMatrix method assignRow.

/**
 * {@inheritDoc}
 */
@Override
public Matrix assignRow(int row, Vector vec) {
    int rows = asCol ? vector.size() : replicationCnt;
    int cols = asCol ? replicationCnt : vector.size();
    int times = asCol ? cols : rows;
    Matrix res = new DenseMatrix(rows, cols);
    IgniteBiConsumer<Integer, Vector> replicantAssigner = asCol ? res::assignColumn : res::assignRow;
    IgniteBiConsumer<Integer, Vector> assigner = res::assignRow;
    assign(replicantAssigner, assigner, vector, vec, times, row);
    return res;
}
Also used : Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)

Example 29 with DenseMatrix

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

the class MLPTest method testXOR.

/**
 * Test that MLP with parameters that should produce function close to 'XOR' is close to 'XOR' on 'XOR' domain.
 */
@Test
public void testXOR() {
    MLPArchitecture conf = new MLPArchitecture(2).withAddedLayer(2, true, Activators.SIGMOID).withAddedLayer(1, true, Activators.SIGMOID);
    MultilayerPerceptron mlp1 = new MultilayerPerceptron(conf, new MLPConstInitializer(1, 2));
    mlp1.setWeights(1, new DenseMatrix(new double[][] { { 20.0, 20.0 }, { -20.0, -20.0 } }));
    mlp1.setBiases(1, new DenseVector(new double[] { -10.0, 30.0 }));
    MultilayerPerceptron mlp2 = mlp1.setWeights(2, new DenseMatrix(new double[][] { { 20.0, 20.0 } }));
    MultilayerPerceptron mlp = mlp2.setBiases(2, new DenseVector(new double[] { -30.0 }));
    Matrix input = new DenseMatrix(new double[][] { { 0.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 0.0 }, { 1.0, 1.0 } });
    Matrix predict = mlp.predict(input);
    Matrix truth = new DenseMatrix(new double[][] { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } });
    TestUtils.checkIsInEpsilonNeighbourhood(predict.getRow(0), truth.getRow(0), 1E-4);
}
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) 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 30 with DenseMatrix

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

the class MLPTest method testStackedMLP.

/**
 * Test that two layer MLP is equivalent to it's subparts stacked on each other.
 */
@Test
public void testStackedMLP() {
    int firstLayerNeuronsCnt = 3;
    int secondLayerNeuronsCnt = 2;
    MLPConstInitializer initer = new MLPConstInitializer(1, 2);
    MLPArchitecture conf = new MLPArchitecture(4).withAddedLayer(firstLayerNeuronsCnt, false, Activators.SIGMOID).withAddedLayer(secondLayerNeuronsCnt, false, Activators.SIGMOID);
    MultilayerPerceptron mlp = new MultilayerPerceptron(conf, initer);
    MLPArchitecture mlpLayer1Conf = new MLPArchitecture(4).withAddedLayer(firstLayerNeuronsCnt, false, Activators.SIGMOID);
    MLPArchitecture mlpLayer2Conf = new MLPArchitecture(firstLayerNeuronsCnt).withAddedLayer(secondLayerNeuronsCnt, false, Activators.SIGMOID);
    MultilayerPerceptron mlp1 = new MultilayerPerceptron(mlpLayer1Conf, initer);
    MultilayerPerceptron mlp2 = new MultilayerPerceptron(mlpLayer2Conf, initer);
    MultilayerPerceptron stackedMLP = mlp1.add(mlp2);
    Matrix predict = mlp.predict(new DenseMatrix(new double[][] { { 1 }, { 2 }, { 3 }, { 4 } }).transpose());
    Matrix stackedPredict = stackedMLP.predict(new DenseMatrix(new double[][] { { 1 }, { 2 }, { 3 }, { 4 } }).transpose());
    Assert.assertEquals(predict, stackedPredict);
}
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)

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