Search in sources :

Example 41 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class MLPLocalTrainerExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    // IMPL NOTE based on MLPLocalTrainerTest#testXORRProp
    System.out.println(">>> Local multilayer perceptron example started.");
    Matrix xorInputs = new DenseLocalOnHeapMatrix(new double[][] { { 0.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 0.0 }, { 1.0, 1.0 } }, StorageConstants.ROW_STORAGE_MODE).transpose();
    System.out.println("\n>>> Input data:");
    Tracer.showAscii(xorInputs);
    Matrix xorOutputs = new DenseLocalOnHeapMatrix(new double[][] { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } }, StorageConstants.ROW_STORAGE_MODE).transpose();
    MLPArchitecture conf = new MLPArchitecture(2).withAddedLayer(10, true, Activators.RELU).withAddedLayer(1, false, Activators.SIGMOID);
    SimpleMLPLocalBatchTrainerInput trainerInput = new SimpleMLPLocalBatchTrainerInput(conf, new Random(1234L), xorInputs, xorOutputs, 4);
    System.out.println("\n>>> Perform training.");
    MultilayerPerceptron mlp = new MLPLocalBatchTrainer<>(LossFunctions.MSE, RPropUpdateCalculator::new, 0.0001, 16000).train(trainerInput);
    System.out.println("\n>>> Apply model.");
    Matrix predict = mlp.apply(xorInputs);
    System.out.println("\n>>> Predicted data:");
    Tracer.showAscii(predict);
    System.out.println("\n>>> Reference expected data:");
    Tracer.showAscii(xorOutputs);
    System.out.println("\n>>> Difference estimate: " + xorOutputs.getRow(0).minus(predict.getRow(0)).kNorm(2));
    System.out.println("\n>>> Local multilayer perceptron example completed.");
}
Also used : MultilayerPerceptron(org.apache.ignite.ml.nn.MultilayerPerceptron) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) MLPArchitecture(org.apache.ignite.ml.nn.architecture.MLPArchitecture) Random(java.util.Random) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 42 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class QRDecompositionExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println(">>> QR decomposition example started.");
    Matrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } });
    System.out.println("\n>>> Input matrix:");
    Tracer.showAscii(m);
    QRDecomposition dec = new QRDecomposition(m);
    System.out.println("\n>>> Value for full rank in decomposition: [" + dec.hasFullRank() + "].");
    Matrix q = dec.getQ();
    Matrix r = dec.getR();
    System.out.println("\n>>> Orthogonal matrix Q:");
    Tracer.showAscii(q);
    System.out.println("\n>>> Upper triangular matrix R:");
    Tracer.showAscii(r);
    Matrix qSafeCp = safeCopy(q);
    Matrix identity = qSafeCp.times(qSafeCp.transpose());
    System.out.println("\n>>> Identity matrix obtained from Q:");
    Tracer.showAscii(identity);
    Matrix recomposed = qSafeCp.times(r);
    System.out.println("\n>>> Recomposed input matrix:");
    Tracer.showAscii(recomposed);
    Matrix sol = dec.solve(new DenseLocalOnHeapMatrix(3, 10));
    System.out.println("\n>>> Solved matrix:");
    Tracer.showAscii(sol);
    dec.destroy();
    System.out.println("\n>>> QR decomposition example completed.");
}
Also used : QRDecomposition(org.apache.ignite.ml.math.decompositions.QRDecomposition) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 43 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class MatrixExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> Basic Matrix API usage example started.");
    System.out.println("\n>>> Creating a matrix to be transposed.");
    double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
    Matrix m = new DenseLocalOnHeapMatrix(data);
    Matrix transposed = m.transpose();
    System.out.println(">>> Matrix: ");
    MatrixExampleUtil.print(m);
    System.out.println(">>> Transposed matrix: ");
    MatrixExampleUtil.print(transposed);
    MatrixExampleUtil.verifyTransposition(m, transposed);
    System.out.println("\n>>> Creating matrices to be multiplied.");
    double[][] data1 = new double[][] { { 1, 2 }, { 3, 4 } };
    double[][] data2 = new double[][] { { 5, 6 }, { 7, 8 } };
    Matrix m1 = new DenseLocalOnHeapMatrix(data1);
    Matrix m2 = new DenseLocalOnHeapMatrix(data2);
    Matrix mult = m1.times(m2);
    System.out.println(">>> First matrix: ");
    MatrixExampleUtil.print(m1);
    System.out.println(">>> Second matrix: ");
    MatrixExampleUtil.print(m2);
    System.out.println(">>> Matrix product: ");
    MatrixExampleUtil.print(mult);
    System.out.println("\n>>> Calculating matrices determinants.");
    double det1 = m1.determinant();
    double det2 = m2.determinant();
    double detMult = mult.determinant();
    boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d;
    System.out.println(">>> First matrix determinant: [" + det1 + "].");
    System.out.println(">>> Second matrix determinant: [" + det2 + "].");
    System.out.println(">>> Matrix product determinant: [" + detMult + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "].");
    System.out.println("Determinant of product matrix [" + detMult + "] should be equal to product of determinants [" + (det1 * det2) + "].");
    System.out.println("\n>>> Basic Matrix API usage example completed.");
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 44 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class FuzzyCMeansLocalExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println(">>> Local Fuzzy C-Means usage example started.");
    // Distance measure that computes distance between two points.
    DistanceMeasure distanceMeasure = new EuclideanDistance();
    // "Fuzziness" - specific constant that is used in membership calculation (1.0+-eps ~ K-Means).
    double exponentialWeight = 2.0;
    // Condition that indicated when algorithm must stop.
    // In this example algorithm stops if memberships have changed insignificantly.
    BaseFuzzyCMeansClusterer.StopCondition stopCond = BaseFuzzyCMeansClusterer.StopCondition.STABLE_MEMBERSHIPS;
    // Maximum difference between new and old membership values with which algorithm will continue to work.
    double maxDelta = 0.01;
    // The maximum number of FCM iterations.
    int maxIterations = 50;
    // Value that is used to initialize random numbers generator. You can choose it randomly.
    Long seed = null;
    // Create new distributed clusterer with parameters described above.
    System.out.println(">>> Create new Local Fuzzy C-Means clusterer.");
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(distanceMeasure, exponentialWeight, stopCond, maxDelta, maxIterations, seed);
    // Create sample data.
    double[][] points = new double[][] { { -10, -10 }, { -9, -11 }, { -10, -9 }, { -11, -9 }, { 10, 10 }, { 9, 11 }, { 10, 9 }, { 11, 9 }, { -10, 10 }, { -9, 11 }, { -10, 9 }, { -11, 9 }, { 10, -10 }, { 9, -11 }, { 10, -9 }, { 11, -9 } };
    // Initialize matrix of data points. Each row contains one point.
    System.out.println(">>> Create the matrix that contains sample points.");
    // Store points into matrix.
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    // Call clusterization method with some number of centers.
    // It returns model that can predict results for new points.
    System.out.println(">>> Perform clusterization.");
    int numCenters = 4;
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, numCenters);
    // You can also get centers of clusters that is computed by Fuzzy C-Means algorithm.
    Vector[] centers = mdl.centers();
    String res = ">>> Results:\n" + ">>> 1st center: " + centers[0].get(0) + " " + centers[0].get(1) + "\n" + ">>> 2nd center: " + centers[1].get(0) + " " + centers[1].get(1) + "\n" + ">>> 3rd center: " + centers[2].get(0) + " " + centers[2].get(1) + "\n" + ">>> 4th center: " + centers[3].get(0) + " " + centers[3].get(1) + "\n";
    System.out.println(res);
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DistanceMeasure(org.apache.ignite.ml.math.distances.DistanceMeasure) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) FuzzyCMeansModel(org.apache.ignite.ml.clustering.FuzzyCMeansModel) FuzzyCMeansLocalClusterer(org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer) BaseFuzzyCMeansClusterer(org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer) Vector(org.apache.ignite.ml.math.Vector)

Example 45 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class CholeskyDecompositionExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println(">>> Cholesky decomposition example started.");
    // Let's compute a Cholesky decomposition of Hermitian matrix m:
    // m = l l^{*}, where
    // l is a lower triangular matrix
    // l^{*} is its conjugate transpose
    DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } });
    System.out.println("\n>>> Matrix m for decomposition: ");
    Tracer.showAscii(m);
    // This decomposition is useful when dealing with systems of linear equations of the form
    // m x = b where m is a Hermitian matrix.
    // For such systems Cholesky decomposition provides
    // more effective method of solving compared to LU decomposition.
    // Suppose we want to solve system
    // m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs
    // as a matrix of the form
    // (b1, b2, ..., bm)
    // to the method Cholesky::solve which returns solutions in the form
    // (sol1, sol2, ..., solm)
    CholeskyDecomposition dec = new CholeskyDecomposition(m);
    System.out.println("\n>>> Made decomposition m = l * l^{*}.");
    System.out.println(">>> Matrix l is ");
    Tracer.showAscii(dec.getL());
    System.out.println(">>> Matrix l^{*} is ");
    Tracer.showAscii(dec.getLT());
    Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } }).transpose();
    System.out.println("\n>>> Solving systems of linear equations of the form m x = b for various bs represented by columns of matrix");
    Tracer.showAscii(bs);
    Matrix sol = dec.solve(bs);
    System.out.println("\n>>> List of solutions: ");
    for (int i = 0; i < sol.columnSize(); i++) Tracer.showAscii(sol.viewColumn(i));
    System.out.println("\n>>> Cholesky decomposition example completed.");
}
Also used : CholeskyDecomposition(org.apache.ignite.ml.math.decompositions.CholeskyDecomposition) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Aggregations

DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)67 Test (org.junit.Test)33 Matrix (org.apache.ignite.ml.math.Matrix)28 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)19 Vector (org.apache.ignite.ml.math.Vector)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)8 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)6 Random (java.util.Random)5 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Ignite (org.apache.ignite.Ignite)3 KMeansLocalClusterer (org.apache.ignite.ml.clustering.KMeansLocalClusterer)3 BaseFuzzyCMeansClusterer (org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer)2 FuzzyCMeansLocalClusterer (org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer)2