Search in sources :

Example 1 with VectorKeyMapper

use of org.apache.ignite.ml.math.distributed.VectorKeyMapper in project ignite by apache.

the class CacheVectorExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> CacheVector example started.");
        CacheConfiguration<Integer, Double> cfg = new CacheConfiguration<>();
        cfg.setName(CACHE_NAME);
        try (IgniteCache<Integer, Double> cache = ignite.getOrCreateCache(cfg)) {
            double[] testValues1 = { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            double[] testValues2 = { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
            ValueMapper valMapper = new IdentityValueMapper();
            // Map vector element index to cache keys.
            VectorKeyMapper<Integer> keyMapper1 = new VectorKeyMapper<Integer>() {

                @Override
                public Integer apply(int i) {
                    return i;
                }

                @Override
                public boolean isValid(Integer integer) {
                    return integer >= 0 && CARDINALITY > integer;
                }
            };
            // Map vector element index to cache keys with shift.
            VectorKeyMapper<Integer> keyMapper2 = new VectorKeyMapper<Integer>() {

                @Override
                public Integer apply(int i) {
                    return i + CARDINALITY;
                }

                @Override
                public boolean isValid(Integer integer) {
                    return integer >= 0 && CARDINALITY > integer;
                }
            };
            // Create two cache vectors over one cache.
            CacheVector cacheVector1 = new CacheVector(CARDINALITY, cache, keyMapper1, valMapper);
            System.out.println(">>> First cache vector created.");
            CacheVector cacheVector2 = new CacheVector(CARDINALITY, cache, keyMapper2, valMapper);
            System.out.println(">>> Second cache vector created.");
            cacheVector1.assign(testValues1);
            cacheVector2.assign(testValues2);
            // Dot product for orthogonal vectors is 0.0.
            assert cacheVector1.dot(cacheVector2) == 0.0;
            System.out.println(">>>");
            System.out.println(">>> Finished executing Ignite \"CacheVector\" example.");
            System.out.println(">>> Dot product is 0.0 for orthogonal vectors.");
            System.out.println(">>>");
        }
    }
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) ValueMapper(org.apache.ignite.ml.math.distributed.ValueMapper) IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) CacheVector(org.apache.ignite.ml.math.impls.vector.CacheVector) VectorKeyMapper(org.apache.ignite.ml.math.distributed.VectorKeyMapper) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

Ignite (org.apache.ignite.Ignite)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 IdentityValueMapper (org.apache.ignite.ml.math.IdentityValueMapper)1 ValueMapper (org.apache.ignite.ml.math.distributed.ValueMapper)1 VectorKeyMapper (org.apache.ignite.ml.math.distributed.VectorKeyMapper)1 CacheVector (org.apache.ignite.ml.math.impls.vector.CacheVector)1