Search in sources :

Example 11 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class BooleanIndexingTest method testMatchConditionAllDimensions1.

@Test
public void testMatchConditionAllDimensions1() throws Exception {
    INDArray array = Nd4j.create(new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    int val = (int) Nd4j.getExecutioner().exec(new MatchCondition(array, Conditions.lessThan(5)), Integer.MAX_VALUE).getDouble(0);
    assertEquals(5, val);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 12 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class BooleanIndexingTest method testEpsEquals1.

@Test
public void testEpsEquals1() throws Exception {
    INDArray array = Nd4j.create(new double[] { -1, -1, -1e-8, 1e-8, 1, 1 });
    MatchCondition condition = new MatchCondition(array, Conditions.epsEquals(0.0));
    int numZeroes = Nd4j.getExecutioner().exec(condition, Integer.MAX_VALUE).getInt(0);
    assertEquals(2, numZeroes);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 13 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class BooleanIndexingTest method testMatchConditionAllDimensions3.

@Test
public void testMatchConditionAllDimensions3() throws Exception {
    INDArray array = Nd4j.create(new double[] { 0, 1, 2, 3, Double.NEGATIVE_INFINITY, 5, 6, 7, 8, 9 });
    int val = (int) Nd4j.getExecutioner().exec(new MatchCondition(array, Conditions.isInfinite()), Integer.MAX_VALUE).getDouble(0);
    assertEquals(1, val);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 14 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class RandomTests method testStepOver1.

@Test
public void testStepOver1() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    log.info("1: ----------------");
    INDArray z0 = Nd4j.getExecutioner().exec(new GaussianDistribution(Nd4j.createUninitialized(1000000), 0.0, 1.0));
    assertEquals(0.0, z0.meanNumber().doubleValue(), 0.01);
    assertEquals(1.0, z0.stdNumber().doubleValue(), 0.01);
    random1.setSeed(119);
    log.info("2: ----------------");
    INDArray z2 = Nd4j.zeros(55000000);
    INDArray z1 = Nd4j.zeros(55000000);
    GaussianDistribution op1 = new GaussianDistribution(z1, 0.0, 1.0);
    Nd4j.getExecutioner().exec(op1, random1);
    log.info("2: ----------------");
    // log.info("End: [{}, {}, {}, {}]", z1.getFloat(29000000), z1.getFloat(29000001), z1.getFloat(29000002), z1.getFloat(29000003));
    log.info("Sum: {}", z1.sumNumber().doubleValue());
    log.info("Sum2: {}", z2.sumNumber().doubleValue());
    INDArray match = Nd4j.getExecutioner().exec(new MatchCondition(z1, Conditions.isNan()), Integer.MAX_VALUE);
    log.info("NaNs: {}", match);
    assertEquals(0.0f, match.getFloat(0), 0.01f);
    /*
        for (int i = 0; i < z1.length(); i++) {
            if (Double.isNaN(z1.getDouble(i)))
                throw new IllegalStateException("NaN value found at " + i);
        
            if (Double.isInfinite(z1.getDouble(i)))
                throw new IllegalStateException("Infinite value found at " + i);
        }
        */
    assertEquals(1.0, z1.stdNumber().doubleValue(), 0.01);
    assertEquals(0.0, z1.meanNumber().doubleValue(), 0.01);
}
Also used : Random(org.nd4j.linalg.api.rng.Random) DefaultRandom(org.nd4j.linalg.api.rng.DefaultRandom) NativeRandom(org.nd4j.rng.NativeRandom) INDArray(org.nd4j.linalg.api.ndarray.INDArray) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest) Test(org.junit.Test)

Example 15 with MatchCondition

use of org.nd4j.linalg.api.ops.impl.accum.MatchCondition in project nd4j by deeplearning4j.

the class BooleanIndexing method and.

/**
 * And over the whole ndarray given some condition, with respect to dimensions
 *
 * @param n    the ndarray to test
 * @param condition the condition to test against
 * @return true if all of the elements meet the specified
 * condition false otherwise
 */
public static boolean[] and(final INDArray n, final Condition condition, int... dimension) {
    if (!(condition instanceof BaseCondition))
        throw new UnsupportedOperationException("Only static Conditions are supported");
    MatchCondition op = new MatchCondition(n, condition);
    INDArray arr = Nd4j.getExecutioner().exec(op, dimension);
    boolean[] result = new boolean[arr.length()];
    long tadLength = Shape.getTADLength(n.shape(), dimension);
    for (int i = 0; i < arr.length(); i++) {
        if (arr.getDouble(i) == tadLength)
            result[i] = true;
        else
            result[i] = false;
    }
    return result;
}
Also used : BaseCondition(org.nd4j.linalg.indexing.conditions.BaseCondition) INDArray(org.nd4j.linalg.api.ndarray.INDArray) MatchCondition(org.nd4j.linalg.api.ops.impl.accum.MatchCondition)

Aggregations

MatchCondition (org.nd4j.linalg.api.ops.impl.accum.MatchCondition)17 INDArray (org.nd4j.linalg.api.ndarray.INDArray)12 Test (org.junit.Test)8 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)6 BaseCondition (org.nd4j.linalg.indexing.conditions.BaseCondition)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CoordinateFunction (org.nd4j.linalg.api.shape.loop.coordinatefunction.CoordinateFunction)2 CompressionDescriptor (org.nd4j.linalg.compression.CompressionDescriptor)2 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)2 IntPointer (org.bytedeco.javacpp.IntPointer)1 IActivation (org.nd4j.linalg.activations.IActivation)1 ActivationSigmoid (org.nd4j.linalg.activations.impl.ActivationSigmoid)1 DataBuffer (org.nd4j.linalg.api.buffer.DataBuffer)1 BernoulliDistribution (org.nd4j.linalg.api.ops.random.impl.BernoulliDistribution)1 DefaultRandom (org.nd4j.linalg.api.rng.DefaultRandom)1 Random (org.nd4j.linalg.api.rng.Random)1 Distribution (org.nd4j.linalg.api.rng.distribution.Distribution)1 CompressedDataBuffer (org.nd4j.linalg.compression.CompressedDataBuffer)1 NativeRandom (org.nd4j.rng.NativeRandom)1