use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix in project ignite by apache.
the class GmmModelTest method testTwoComponents.
/**
*/
@Test
public void testTwoComponents() {
Vector mean1 = VectorUtils.of(1., 2.);
DenseMatrix covariance1 = MatrixUtil.fromList(Arrays.asList(VectorUtils.of(1, -0.25), VectorUtils.of(-0.25, 1)), true);
Vector mean2 = VectorUtils.of(2., 1.);
DenseMatrix covariance2 = MatrixUtil.fromList(Arrays.asList(VectorUtils.of(1, 0.5), VectorUtils.of(0.5, 1)), true);
GmmModel gmm = new GmmModel(VectorUtils.of(0.5, 0.5), Arrays.asList(new MultivariateGaussianDistribution(mean1, covariance1), new MultivariateGaussianDistribution(mean2, covariance2)));
Assert.assertEquals(0., gmm.predict(mean1), 0.01);
Assert.assertEquals(1., gmm.predict(mean2), 0.01);
Assert.assertEquals(0., gmm.predict(VectorUtils.of(1.5, 1.5)), 0.01);
Assert.assertEquals(1., gmm.predict(VectorUtils.of(3., 0.)), 0.01);
}
use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix 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.matrix.impl.DenseMatrix in project ignite by apache.
the class MLPTest method testDifferentiation.
/**
* Test differentiation.
*/
@Test
public void testDifferentiation() {
int inputSize = 2;
int firstLayerNeuronsCnt = 1;
double w10 = 0.1;
double w11 = 0.2;
MLPArchitecture conf = new MLPArchitecture(inputSize).withAddedLayer(firstLayerNeuronsCnt, false, Activators.SIGMOID);
MultilayerPerceptron mlp1 = new MultilayerPerceptron(conf);
mlp1.setWeight(1, 0, 0, w10);
MultilayerPerceptron mlp = mlp1.setWeight(1, 1, 0, w11);
double x0 = 1.0;
double x1 = 3.0;
Matrix inputs = new DenseMatrix(new double[][] { { x0, x1 } }).transpose();
double ytt = 1.0;
Matrix truth = new DenseMatrix(new double[][] { { ytt } }).transpose();
Vector grad = mlp.differentiateByParameters(LossFunctions.MSE, inputs, truth);
// Let yt be y ground truth value.
// d/dw1i [(yt - sigma(w10 * x0 + w11 * x1))^2] =
// 2 * (yt - sigma(w10 * x0 + w11 * x1)) * (-1) * (sigma(w10 * x0 + w11 * x1)) * (1 - sigma(w10 * x0 + w11 * x1)) * xi =
// let z = sigma(w10 * x0 + w11 * x1)
// - 2* (yt - z) * (z) * (1 - z) * xi.
IgniteTriFunction<Double, Vector, Vector, Vector> partialDer = (yt, w, x) -> {
Double z = Activators.SIGMOID.apply(w.dot(x));
return x.copy().map(xi -> -2 * (yt - z) * z * (1 - z) * xi);
};
Vector weightsVec = mlp.weights(1).getRow(0);
Tracer.showAscii(weightsVec);
Vector trueGrad = partialDer.andThen(x -> x).apply(ytt, weightsVec, inputs.getCol(0));
Tracer.showAscii(trueGrad);
Tracer.showAscii(grad);
Assert.assertEquals(mlp.architecture().parametersCount(), grad.size());
Assert.assertEquals(trueGrad, grad);
}
use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix in project ignite by apache.
the class MLPTest method testSimpleMLPPrediction.
/**
* Tests that MLP with 2 layer, 1 neuron in each layer and weight equal to 1 is equivalent to sigmoid function.
*/
@Test
public void testSimpleMLPPrediction() {
MLPArchitecture conf = new MLPArchitecture(1).withAddedLayer(1, false, Activators.SIGMOID);
MultilayerPerceptron mlp = new MultilayerPerceptron(conf, new MLPConstInitializer(1));
int input = 2;
Matrix predict = mlp.predict(new DenseMatrix(new double[][] { { input } }));
Assert.assertEquals(predict, new DenseMatrix(new double[][] { { Activators.SIGMOID.apply(input) } }));
}
use of org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix 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();
}
}
Aggregations