Search in sources :

Example 31 with DenseMatrix

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

the class MLPTest method setParamsFlattening.

/**
 * Test methods related to parameters flattening.
 */
@Test
public void setParamsFlattening() {
    int inputSize = 3;
    int firstLayerNeuronsCnt = 2;
    int secondLayerNeurons = 1;
    DenseVector paramsVector = new DenseVector(new double[] { // First layer weight matrix.
    1.0, // First layer weight matrix.
    2.0, // First layer weight matrix.
    3.0, // First layer weight matrix.
    4.0, // First layer weight matrix.
    5.0, // First layer weight matrix.
    6.0, // Second layer weight matrix.
    7.0, // Second layer weight matrix.
    8.0, // Second layer biases.
    9.0 });
    DenseMatrix firstLayerWeights = new DenseMatrix(new double[][] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } });
    DenseMatrix secondLayerWeights = new DenseMatrix(new double[][] { { 7.0, 8.0 } });
    DenseVector secondLayerBiases = new DenseVector(new double[] { 9.0 });
    MLPArchitecture conf = new MLPArchitecture(inputSize).withAddedLayer(firstLayerNeuronsCnt, false, Activators.SIGMOID).withAddedLayer(secondLayerNeurons, true, Activators.SIGMOID);
    MultilayerPerceptron mlp = new MultilayerPerceptron(conf, new MLPConstInitializer(100, 200));
    mlp.setParameters(paramsVector);
    Assert.assertEquals(paramsVector, mlp.parameters());
    Assert.assertEquals(mlp.weights(1), firstLayerWeights);
    Assert.assertEquals(mlp.weights(2), secondLayerWeights);
    Assert.assertEquals(mlp.biases(2), secondLayerBiases);
}
Also used : 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 32 with DenseMatrix

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

the class MultivariateGaussianDistributionTest method testApply.

/**
 */
@Test
public void testApply() {
    MultivariateGaussianDistribution distribution = new MultivariateGaussianDistribution(VectorUtils.of(1, 2), new DenseMatrix(new double[][] { new double[] { 1, -0.5 }, new double[] { -0.5, 1 } }));
    Assert.assertEquals(0.183, distribution.prob(VectorUtils.of(1, 2)), 0.01);
    Assert.assertEquals(0.094, distribution.prob(VectorUtils.of(0, 2)), 0.01);
}
Also used : DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Example 33 with DenseMatrix

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

the class MatrixUtil method copy.

/**
 * Create the copy of matrix with read-only matrices support.
 *
 * @param matrix Matrix for copy.
 * @return Copy.
 */
public static Matrix copy(Matrix matrix) {
    if (isCopyLikeSupport(matrix)) {
        DenseMatrix cp = new DenseMatrix(matrix.rowSize(), matrix.columnSize());
        cp.assign(matrix);
        return cp;
    } else
        return matrix.copy();
}
Also used : DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)

Example 34 with DenseMatrix

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

the class TracerTest method testWriteMatrixToCSVFile.

/**
 */
@Test
public void testWriteMatrixToCSVFile() throws IOException {
    DenseMatrix matrix = new DenseMatrix(MathTestConstants.STORAGE_SIZE, MathTestConstants.STORAGE_SIZE);
    for (int i = 0; i < matrix.rowSize(); i++) for (int j = 0; j < matrix.columnSize(); j++) matrix.set(i, j, Math.random());
    Path file = createTempFile("matrix", ".csv");
    Tracer.saveAsCsv(matrix, DEFAULT_FORMAT, file.toString());
    System.out.println("Matrix exported: " + file.getFileName());
    List<String> strings = Files.readAllLines(file);
    Optional<String> reduce = strings.stream().reduce((s1, s2) -> s1 + s2);
    String[] csvVals = reduce.orElse("").split(",");
    for (int i = 0; i < matrix.rowSize(); i++) for (int j = 0; j < matrix.columnSize(); j++) {
        Double csvVal = Double.valueOf(csvVals[i * matrix.rowSize() + j]);
        assertEquals("Unexpected value.", csvVal, matrix.get(i, j), DEFAULT_DELTA);
    }
    Files.deleteIfExists(file);
}
Also used : Path(java.nio.file.Path) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Example 35 with DenseMatrix

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

the class TracerTest method makeRandomMatrix.

/**
 * @param rows Amount of rows in matrix.
 * @param cols Amount of columns in matrix.
 */
private Matrix makeRandomMatrix(int rows, int cols) {
    DenseMatrix mtx = new DenseMatrix(rows, cols);
    // Missing assign(f)?
    mtx.map((d) -> Math.random());
    return mtx;
}
Also used : DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)

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