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