use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class BlasTest method testSyrDenseDense.
/**
* Tests 'syr' operation for dense vector x and dense matrix A.
*/
@Test
public void testSyrDenseDense() {
double alpha = 2.0;
DenseLocalOnHeapVector x = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 10.0, 20.0 }, { 20.0, 10.0 } });
// alpha * x * x^T + A
DenseLocalOnHeapMatrix exp = (DenseLocalOnHeapMatrix) new DenseLocalOnHeapMatrix(new double[][] { { 1.0, 2.0 }, { 2.0, 4.0 } }).times(alpha).plus(a);
Blas.syr(alpha, x, a);
Assert.assertEquals(exp, a);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class BlasTest method testSprDenseDense.
/**
* Test 'spr' operation for dense vector v and dense matrix A.
*/
@Test
public void testSprDenseDense() {
double alpha = 3.0;
DenseLocalOnHeapVector v = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
DenseLocalOnHeapVector u = new DenseLocalOnHeapVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
// m is alpha * v * v^t
DenseLocalOnHeapMatrix m = (DenseLocalOnHeapMatrix) new DenseLocalOnHeapMatrix(new double[][] { { 1.0, 0.0 }, { 2.0, 4.0 } }, StorageConstants.COLUMN_STORAGE_MODE).times(alpha);
DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 3.0, 0.0 }, { 13.0, 20.0 } }, StorageConstants.COLUMN_STORAGE_MODE);
// m := alpha * v * v.t + A
Blas.spr(alpha, v, u);
DenseLocalOnHeapMatrix mu = fromVector(u, a.rowSize(), StorageConstants.COLUMN_STORAGE_MODE, (i, j) -> i >= j);
Assert.assertEquals(m.plus(a), mu);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class BlasTest method testGemvDenseSparseDense.
/**
* Tests 'gemv' operation for dense matrix A, sparse vector x and dense vector y.
*/
@Test
public void testGemvDenseSparseDense() {
// y := alpha * A * x + beta * y
double alpha = 3.0;
SparseLocalOnHeapMatrix a = (SparseLocalOnHeapMatrix) new SparseLocalOnHeapMatrix(2, 2).assign(new double[][] { { 10.0, 11.0 }, { 0.0, 1.0 } });
SparseLocalVector x = sparseFromArray(new double[] { 1.0, 2.0 });
double beta = 2.0;
DenseLocalOnHeapVector y = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
DenseLocalOnHeapVector exp = (DenseLocalOnHeapVector) y.times(beta).plus(a.times(x).times(alpha));
Blas.gemv(alpha, a, x, beta, y);
Assert.assertEquals(exp, y);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class DistributedLinearRegressionWithLSQRTrainerAndNormalizationExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws InterruptedException {
System.out.println();
System.out.println(">>> Linear regression model over sparse distributed matrix API usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
// Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
// because we create ignite cache internally.
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> {
IgniteCache<Integer, double[]> dataCache = getTestCache(ignite);
System.out.println(">>> Create new normalization trainer object.");
NormalizationTrainer<Integer, double[]> normalizationTrainer = new NormalizationTrainer<>();
System.out.println(">>> Perform the training to get the normalization preprocessor.");
NormalizationPreprocessor<Integer, double[]> preprocessor = normalizationTrainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), (k, v) -> Arrays.copyOfRange(v, 1, v.length), 4);
System.out.println(">>> Create new linear regression trainer object.");
LinearRegressionLSQRTrainer<Integer, double[]> trainer = new LinearRegressionLSQRTrainer<>();
System.out.println(">>> Perform the training to get the model.");
LinearRegressionModel mdl = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), preprocessor, (k, v) -> v[0], 4);
System.out.println(">>> Linear regression model: " + mdl);
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Ground Truth\t|");
System.out.println(">>> ---------------------------------");
try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {
for (Cache.Entry<Integer, double[]> observation : observations) {
Integer key = observation.getKey();
double[] val = observation.getValue();
double groundTruth = val[0];
double prediction = mdl.apply(new DenseLocalOnHeapVector(preprocessor.apply(key, val)));
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
}
System.out.println(">>> ---------------------------------");
});
igniteThread.start();
igniteThread.join();
}
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class SVMBinaryClassificationExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws InterruptedException {
System.out.println();
System.out.println(">>> SVM Binary classification model over cached dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SVMBinaryClassificationExample.class.getSimpleName(), () -> {
IgniteCache<Integer, double[]> dataCache = getTestCache(ignite);
SVMLinearBinaryClassificationTrainer<Integer, double[]> trainer = new SVMLinearBinaryClassificationTrainer<>();
SVMLinearBinaryClassificationModel mdl = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), (k, v) -> Arrays.copyOfRange(v, 1, v.length), (k, v) -> v[0], 4);
System.out.println(">>> SVM model " + mdl);
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Ground Truth\t|");
System.out.println(">>> ---------------------------------");
int amountOfErrors = 0;
int totalAmount = 0;
// Build confusion matrix. See https://en.wikipedia.org/wiki/Confusion_matrix
int[][] confusionMtx = { { 0, 0 }, { 0, 0 } };
try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {
for (Cache.Entry<Integer, double[]> observation : observations) {
double[] val = observation.getValue();
double[] inputs = Arrays.copyOfRange(val, 1, val.length);
double groundTruth = val[0];
double prediction = mdl.apply(new DenseLocalOnHeapVector(inputs));
totalAmount++;
if (groundTruth != prediction)
amountOfErrors++;
int idx1 = (int) prediction == -1.0 ? 0 : 1;
int idx2 = (int) groundTruth == -1.0 ? 0 : 1;
confusionMtx[idx1][idx2]++;
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
System.out.println(">>> ---------------------------------");
System.out.println("\n>>> Absolute amount of errors " + amountOfErrors);
System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double) totalAmount));
}
System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtx));
});
igniteThread.start();
igniteThread.join();
}
}
Aggregations