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[] {}));
}
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);
}
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;
}
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);
}
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);
}
Aggregations