Search in sources :

Example 26 with Matrix

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

the class AbstractVector method toMatrixPlusOne.

/**
 * {@inheritDoc}
 */
@Override
public Matrix toMatrixPlusOne(boolean rowLike, double zeroVal) {
    Matrix res = likeMatrix(rowLike ? 1 : size() + 1, rowLike ? size() + 1 : 1);
    if (res == null)
        return null;
    res.set(0, 0, zeroVal);
    if (rowLike)
        new ViewMatrix(res, 0, 1, 1, size()).assignRow(0, this);
    else
        new ViewMatrix(res, 1, 0, size(), 1).assignColumn(0, this);
    return res;
}
Also used : Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) ViewMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.ViewMatrix) ViewMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.ViewMatrix)

Example 27 with Matrix

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

the class VectorToMatrixTest method availableForTesting.

/**
 */
private boolean availableForTesting(Vector v) {
    assertNotNull("Error in test: vector is null", v);
    final boolean availableForTesting = typesMap.get(v.getClass()) != null;
    final Matrix actualLikeMatrix = v.likeMatrix(1, 1);
    assertTrue("Need to enable matrix testing for vector type " + v.getClass().getSimpleName(), availableForTesting || actualLikeMatrix == null);
    return availableForTesting;
}
Also used : Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) VectorizedViewMatrix(org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix)

Example 28 with Matrix

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

the class VectorToMatrixTest method testLikeMatrix.

/**
 */
@Test
public void testLikeMatrix() {
    consumeSampleVectors((v, desc) -> {
        if (!availableForTesting(v))
            return;
        final Matrix matrix = v.likeMatrix(1, 1);
        Class<? extends Vector> key = v.getClass();
        Class<? extends Matrix> expMatrixType = typesMap.get(key);
        assertNotNull("Expect non-null matrix for " + key.getSimpleName() + " in " + desc, matrix);
        Class<? extends Matrix> actualMatrixType = matrix.getClass();
        assertTrue("Expected matrix type " + expMatrixType.getSimpleName() + " should be assignable from actual type " + actualMatrixType.getSimpleName() + " in " + desc, expMatrixType.isAssignableFrom(actualMatrixType));
        for (int rows : new int[] { 1, 2 }) for (int cols : new int[] { 1, 2 }) {
            final Matrix actualMatrix = v.likeMatrix(rows, cols);
            String details = "rows " + rows + " cols " + cols;
            assertNotNull("Expect non-null matrix for " + details + " in " + desc, actualMatrix);
            assertEquals("Unexpected number of rows in " + desc, rows, actualMatrix.rowSize());
            assertEquals("Unexpected number of cols in " + desc, cols, actualMatrix.columnSize());
        }
    });
}
Also used : Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) VectorizedViewMatrix(org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix) Test(org.junit.Test)

Example 29 with Matrix

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

the class VectorToMatrixTest method assertCross.

/**
 */
private void assertCross(Vector v1, Vector v2, String desc) {
    assertNotNull(v1);
    assertNotNull(v2);
    final Matrix res = v1.cross(v2);
    assertNotNull("Cross matrix is expected to be not null in " + desc, res);
    assertEquals("Unexpected number of rows in cross Matrix in " + desc, v1.size(), res.rowSize());
    assertEquals("Unexpected number of cols in cross Matrix in " + desc, v2.size(), res.columnSize());
    for (int row = 0; row < v1.size(); row++) for (int col = 0; col < v2.size(); col++) {
        final Metric metric = new Metric(v1.get(row) * v2.get(col), res.get(row, col));
        assertTrue("Not close enough cross " + metric + " at row " + row + " at col " + col + " in " + desc, metric.closeEnough());
    }
}
Also used : Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) VectorizedViewMatrix(org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) SparseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix)

Example 30 with Matrix

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

the class TracerTest method testAsciiMatrixTracer.

/**
 */
@Test
public void testAsciiMatrixTracer() {
    Matrix mtx = makeRandomMatrix(10, 10);
    Tracer.showAscii(mtx);
    Tracer.showAscii(mtx, "%2f");
    Tracer.showAscii(mtx, "%.3g");
}
Also used : Matrix(org.apache.ignite.ml.math.primitives.matrix.Matrix) DenseMatrix(org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix) Test(org.junit.Test)

Aggregations

Matrix (org.apache.ignite.ml.math.primitives.matrix.Matrix)31 DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)24 Test (org.junit.Test)12 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)8 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)8 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)7 SparseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.SparseMatrix)6 VectorizedViewMatrix (org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix)5 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)2 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)2 ViewMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.ViewMatrix)2 DelegatingVector (org.apache.ignite.ml.math.primitives.vector.impl.DelegatingVector)2 SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)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 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1