use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class CollectionsTest method test.
/**
*/
@Test
@SuppressWarnings("unchecked")
public void test() {
test(new VectorizedViewMatrix(new DenseMatrix(2, 2), 1, 1, 1, 1), new VectorizedViewMatrix(new DenseMatrix(3, 2), 2, 1, 1, 1));
specialTest(new ManhattanDistance(), new ManhattanDistance());
specialTest(new HammingDistance(), new HammingDistance());
specialTest(new EuclideanDistance(), new EuclideanDistance());
FeatureMetadata data = new FeatureMetadata("name2");
data.setName("name1");
test(data, new FeatureMetadata("name2"));
test(new DatasetRow<>(new DenseVector()), new DatasetRow<>(new DenseVector(1)));
test(new LabeledVector<>(new DenseVector(), null), new LabeledVector<>(new DenseVector(1), null));
test(new Dataset<DatasetRow<Vector>>(new DatasetRow[] {}, new FeatureMetadata[] {}), new Dataset<DatasetRow<Vector>>(new DatasetRow[] { new DatasetRow() }, new FeatureMetadata[] { new FeatureMetadata() }));
test(new LogisticRegressionModel(new DenseVector(), 1.0), new LogisticRegressionModel(new DenseVector(), 0.5));
test(new KMeansModelFormat(new Vector[] {}, new ManhattanDistance()), new KMeansModelFormat(new Vector[] {}, new HammingDistance()));
test(new KMeansModel(new Vector[] {}, new ManhattanDistance()), new KMeansModel(new Vector[] {}, new HammingDistance()));
test(new SVMLinearClassificationModel(null, 1.0), new SVMLinearClassificationModel(null, 0.5));
test(new ANNClassificationModel(new LabeledVectorSet<>(), new ANNClassificationTrainer.CentroidStat()), new ANNClassificationModel(new LabeledVectorSet<>(1, 1), new ANNClassificationTrainer.CentroidStat()));
test(new ANNModelFormat(1, new ManhattanDistance(), false, new LabeledVectorSet<>(), new ANNClassificationTrainer.CentroidStat()), new ANNModelFormat(2, new ManhattanDistance(), false, new LabeledVectorSet<>(), new ANNClassificationTrainer.CentroidStat()));
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class KMeansModelTest method predictClusters.
/**
*/
@Test
public void predictClusters() {
DistanceMeasure distanceMeasure = new EuclideanDistance();
Vector[] centers = new DenseVector[4];
centers[0] = new DenseVector(new double[] { 1.0, 1.0 });
centers[1] = new DenseVector(new double[] { -1.0, 1.0 });
centers[2] = new DenseVector(new double[] { 1.0, -1.0 });
centers[3] = new DenseVector(new double[] { -1.0, -1.0 });
KMeansModel mdl = new KMeansModel(centers, distanceMeasure);
Assert.assertTrue(mdl.toString().contains("KMeansModel"));
Assert.assertEquals(mdl.predict(new DenseVector(new double[] { 1.1, 1.1 })), 0.0, PRECISION);
Assert.assertEquals(mdl.predict(new DenseVector(new double[] { -1.1, 1.1 })), 1.0, PRECISION);
Assert.assertEquals(mdl.predict(new DenseVector(new double[] { 1.1, -1.1 })), 2.0, PRECISION);
Assert.assertEquals(mdl.predict(new DenseVector(new double[] { -1.1, -1.1 })), 3.0, PRECISION);
Assert.assertEquals(mdl.distanceMeasure(), distanceMeasure);
Assert.assertEquals(mdl.amountOfClusters(), 4);
Assert.assertArrayEquals(mdl.centers(), centers);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class MnistUtils method mnistAsStream.
/**
* Read random {@code count} samples from MNIST dataset from two files (images and labels) into a stream of labeled
* vectors.
*
* @param imagesPath Path to the file with images.
* @param labelsPath Path to the file with labels.
* @param rnd Random numbers generator.
* @param cnt Count of samples to read.
* @return Stream of MNIST samples.
* @throws IgniteException In case of exception.
*/
public static Stream<DenseVector> mnistAsStream(String imagesPath, String labelsPath, Random rnd, int cnt) throws IOException {
FileInputStream isImages = new FileInputStream(imagesPath);
FileInputStream isLabels = new FileInputStream(labelsPath);
// Skip magic number.
read4Bytes(isImages);
int numOfImages = read4Bytes(isImages);
int imgHeight = read4Bytes(isImages);
int imgWidth = read4Bytes(isImages);
// Skip magic number.
read4Bytes(isLabels);
// Skip number of labels.
read4Bytes(isLabels);
int numOfPixels = imgHeight * imgWidth;
double[][] vecs = new double[numOfImages][numOfPixels + 1];
for (int imgNum = 0; imgNum < numOfImages; imgNum++) {
vecs[imgNum][numOfPixels] = isLabels.read();
for (int p = 0; p < numOfPixels; p++) {
int c = 128 - isImages.read();
vecs[imgNum][p] = (double) c / 128;
}
}
List<double[]> lst = Arrays.asList(vecs);
Collections.shuffle(lst, rnd);
isImages.close();
isLabels.close();
return lst.subList(0, cnt).stream().map(DenseVector::new);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class MLPTrainerIntegrationTest method xorTest.
/**
* Common method for testing 'XOR' with various updaters.
*
* @param updatesStgy Update strategy.
* @param <P> Updater parameters type.
*/
private <P extends Serializable> void xorTest(UpdatesStrategy<? super MultilayerPerceptron, P> updatesStgy) {
CacheConfiguration<Integer, LabeledVector<double[]>> xorCacheCfg = new CacheConfiguration<>();
xorCacheCfg.setName("XorData");
xorCacheCfg.setAffinity(new RendezvousAffinityFunction(false, 5));
IgniteCache<Integer, LabeledVector<double[]>> xorCache = ignite.createCache(xorCacheCfg);
try {
xorCache.put(0, VectorUtils.of(0.0, 0.0).labeled(new double[] { 0.0 }));
xorCache.put(1, VectorUtils.of(0.0, 1.0).labeled(new double[] { 1.0 }));
xorCache.put(2, VectorUtils.of(1.0, 0.0).labeled(new double[] { 1.0 }));
xorCache.put(3, VectorUtils.of(1.0, 1.0).labeled(new double[] { 0.0 }));
MLPArchitecture arch = new MLPArchitecture(2).withAddedLayer(10, true, Activators.RELU).withAddedLayer(1, false, Activators.SIGMOID);
MLPTrainer<P> trainer = new MLPTrainer<>(arch, LossFunctions.MSE, updatesStgy, 2500, 4, 50, 123L);
MultilayerPerceptron mlp = trainer.fit(ignite, xorCache, new LabeledDummyVectorizer<>());
Matrix predict = mlp.predict(new DenseMatrix(new double[][] { { 0.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 0.0 }, { 1.0, 1.0 } }));
Tracer.showAscii(predict);
X.println(new DenseVector(new double[] { 0.0 }).minus(predict.getRow(0)).kNorm(2) + "");
TestUtils.checkIsInEpsilonNeighbourhood(new DenseVector(new double[] { 0.0 }), predict.getRow(0), 1E-1);
} finally {
xorCache.destroy();
}
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class MnistMLPTestUtil method loadMnist.
/**
*/
static IgniteBiTuple<Stream<DenseVector>, Stream<DenseVector>> loadMnist(int samplesCnt) throws IOException {
Properties props = loadMNISTProperties();
Stream<DenseVector> trainingMnistStream = MnistUtils.mnistAsStream(props.getProperty(PROP_TRAINING_IMAGES), props.getProperty(PROP_TRAINING_LABELS), new Random(123L), samplesCnt);
Stream<DenseVector> testMnistStream = MnistUtils.mnistAsStream(props.getProperty(PROP_TEST_IMAGES), props.getProperty(PROP_TEST_LABELS), new Random(123L), 10_000);
return new IgniteBiTuple<>(trainingMnistStream, testMnistStream);
}
Aggregations