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);
}
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);
}
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();
}
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);
}
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;
}
Aggregations