use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class SVMModelTest method testPredictWithMultiClasses.
/**
*/
@Test
public void testPredictWithMultiClasses() {
Vector weights1 = new DenseLocalOnHeapVector(new double[] { 10.0, 0.0 });
Vector weights2 = new DenseLocalOnHeapVector(new double[] { 0.0, 10.0 });
Vector weights3 = new DenseLocalOnHeapVector(new double[] { -1.0, -1.0 });
SVMLinearMultiClassClassificationModel mdl = new SVMLinearMultiClassClassificationModel();
mdl.add(1, new SVMLinearBinaryClassificationModel(weights1, 0.0).withRawLabels(true));
mdl.add(2, new SVMLinearBinaryClassificationModel(weights2, 0.0).withRawLabels(true));
mdl.add(2, new SVMLinearBinaryClassificationModel(weights3, 0.0).withRawLabels(true));
Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class SVMMultiClassTrainerTest method testTrainWithTheLinearlySeparableCase.
/**
* Test trainer on classification model y = x.
*/
@Test
public void testTrainWithTheLinearlySeparableCase() {
Map<Integer, double[]> data = new HashMap<>();
ThreadLocalRandom rndX = ThreadLocalRandom.current();
ThreadLocalRandom rndY = ThreadLocalRandom.current();
for (int i = 0; i < AMOUNT_OF_OBSERVATIONS; i++) {
double x = rndX.nextDouble(-1000, 1000);
double y = rndY.nextDouble(-1000, 1000);
double[] vec = new double[AMOUNT_OF_FEATURES + 1];
// assign label.
vec[0] = y - x > 0 ? 1 : -1;
vec[1] = x;
vec[2] = y;
data.put(i, vec);
}
SVMLinearMultiClassClassificationTrainer<Integer, double[]> trainer = new SVMLinearMultiClassClassificationTrainer<Integer, double[]>().withLambda(0.3).withAmountOfLocIterations(100).withAmountOfIterations(20);
SVMLinearMultiClassClassificationModel mdl = trainer.fit(new LocalDatasetBuilder<>(data, 10), (k, v) -> Arrays.copyOfRange(v, 1, v.length), (k, v) -> v[0], AMOUNT_OF_FEATURES);
TestUtils.assertEquals(-1, mdl.apply(new DenseLocalOnHeapVector(new double[] { 100, 10 })), PRECISION);
TestUtils.assertEquals(1, mdl.apply(new DenseLocalOnHeapVector(new double[] { 10, 100 })), PRECISION);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class BlasTest method testAxpySparseArray.
/**
* Test 'axpy' operation for sparse vector and array-based vector.
*/
@Test
public void testAxpySparseArray() {
DenseLocalOnHeapVector y = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
double a = 2.0;
SparseLocalVector x = sparseFromArray(new double[] { 1.0, 2.0 });
SparseLocalVector exp = (SparseLocalVector) x.times(a).plus(y);
Blas.axpy(a, x, y);
Assert.assertTrue(elementsEqual(exp, y));
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class BlasTest method testAxpyArrayArray.
/**
* Test 'axpy' operation for two array-based vectors.
*/
@Test
public void testAxpyArrayArray() {
Vector y = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
double a = 2.0;
Vector x = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
Vector exp = x.times(a).plus(y);
Blas.axpy(a, x, y);
Assert.assertEquals(y, exp);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class BlasTest method testSprSparseDense1.
/**
* Test 'spr' operation for sparse vector v (sparse in representation, dense in fact) and dense matrix A.
*/
@Test
public void testSprSparseDense1() {
double alpha = 3.0;
SparseLocalVector v = sparseFromArray(new double[] { 1.0, 2.0 });
DenseLocalOnHeapVector u = new DenseLocalOnHeapVector(new double[] { 3.0, 13.0, 20.0, 0.0 });
DenseLocalOnHeapMatrix a = new DenseLocalOnHeapMatrix(new double[][] { { 3.0, 0.0 }, { 13.0, 20.0 } }, StorageConstants.COLUMN_STORAGE_MODE);
DenseLocalOnHeapMatrix exp = (DenseLocalOnHeapMatrix) new DenseLocalOnHeapMatrix(new double[][] { { 1.0, 0.0 }, { 2.0, 4.0 } }, StorageConstants.COLUMN_STORAGE_MODE).times(alpha).plus(a);
// 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(exp, mu);
}
Aggregations