org.nd4j.linalg.api.rng.Random Java Examples

The following examples show how to use org.nd4j.linalg.api.rng.Random. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testTruncatedNormal1() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z01 = Nd4j.create(10000000).assign(-119119d);
    INDArray z02 = Nd4j.createUninitialized(z01.length());

    TruncatedNormalDistribution distribution01 = new TruncatedNormalDistribution(z01, 0.0, 1.0);

    long time1 = System.currentTimeMillis();
    Nd4j.getExecutioner().exec(distribution01, random1);
    long time2 = System.currentTimeMillis();

    Nd4j.getExecutioner().exec(new GaussianDistribution( z02, 0.0, 1.0));
    long time3 = System.currentTimeMillis();

    log.info("Truncated: {} ms; Gaussian: {} ms", time2 - time1, time3 - time2);

    for (int e = 0; e < z01.length(); e++) {
        assertTrue("Value: " + z01.getDouble(e) + " at " + e,FastMath.abs(z01.getDouble(e)) <= 2.0);
        assertNotEquals(-119119d, z01.getDouble(e), 1e-3);
    }

    assertEquals(0.0, z01.meanNumber().doubleValue(), 1e-3);
}
 
Example #2
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGaussianDistribution1() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.create(100000);
    INDArray z2 = Nd4j.create(100000);
    INDArray zDup = z1.dup();

    GaussianDistribution op1 = new GaussianDistribution(z1, 0.0, 1.0);
    Nd4j.getExecutioner().exec(op1, random1);

    GaussianDistribution op2 = new GaussianDistribution(z2, 0.0, 1.0);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(zDup, z1);
    assertEquals(0.0, z1.meanNumber().doubleValue(), 0.01);

    assertEquals(1.0, z1.stdNumber().doubleValue(), 0.01);

    assertEquals(z1, z2);
}
 
Example #3
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAlphaDropout1() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.ones(300);
    INDArray z2 = Nd4j.ones(300);
    INDArray zDup = z1.dup();

    AlphaDropOut op1 = new AlphaDropOut(z1, z1, 0.10, 0.3, 0.5, 0.7);
    Nd4j.getExecutioner().exec(op1, random1);

    AlphaDropOut op2 = new AlphaDropOut(z2, z2, 0.10, 0.3, 0.5, 0.7);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(zDup, z1);

    assertEquals(z1, z2);
}
 
Example #4
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAlphaDropout1() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.ones(300);
    INDArray z2 = Nd4j.ones(300);
    INDArray zDup = z1.dup();

    AlphaDropOut op1 = new AlphaDropOut(z1, z1, 0.10, 0.3, 0.5, 0.7);
    Nd4j.getExecutioner().exec(op1, random1);

    AlphaDropOut op2 = new AlphaDropOut(z2, z2, 0.10, 0.3, 0.5, 0.7);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(zDup, z1);

    assertEquals(z1, z2);
}
 
Example #5
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDropoutInverted1() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.ones(300);
    INDArray z2 = Nd4j.ones(300);
    INDArray zDup = z1.dup();

    DropOutInverted op1 = new DropOutInverted(z1, z1, 0.10);
    Nd4j.getExecutioner().exec(op1, random1);

    DropOutInverted op2 = new DropOutInverted(z2, z2, 0.10);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(zDup, z1);


    for (int x = 0; x < z1.length(); x++) {
        assertEquals("Failed on element: [" + x + "]", z1.getFloat(x), z2.getFloat(x), 0.01f);
    }
    assertEquals(z1, z2);
}
 
Example #6
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDistribution2() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.create(32);
    INDArray z2 = Nd4j.create(32);
    UniformDistribution distribution = new UniformDistribution(z1, 1.0, 2.0);
    Nd4j.getExecutioner().exec(distribution, random1);
    UniformDistribution distribution2 = new UniformDistribution(z2, 1.0, 2.0);
    Nd4j.getExecutioner().exec(distribution2, random2);

    System.out.println("Data: " + z1);
    System.out.println("Data: " + z2);
    for (int e = 0; e < z1.length(); e++) {
        double val = z1.getDouble(e);
        assertTrue(val >= 1.0 && val <= 2.0);
    }

    assertEquals(z1, z2);
}
 
Example #7
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBinomialDistribution2() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.create(DataType.FLOAT, 1000);
    INDArray z2 = Nd4j.zeros(DataType.FLOAT,1000);
    INDArray z1Dup = Nd4j.zeros(DataType.FLOAT,1000);

    INDArray probs = Nd4j.create(new float[] {0.25f, 0.43f, 0.55f, 0.43f, 0.25f});

    BinomialDistribution op1 = new BinomialDistribution(z1, 5, probs);
    BinomialDistribution op2 = new BinomialDistribution(z2, 5, probs);

    Nd4j.getExecutioner().exec(op1, random1);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(z1Dup, z1);

    assertEquals(z1, z2);

    BooleanIndexing.and(z1, Conditions.lessThanOrEqual(5.0));
    BooleanIndexing.and(z1, Conditions.greaterThanOrEqual(0.0));
}
 
Example #8
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGaussianDistribution3() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.create(DataType.DOUBLE, 1000000);
    INDArray z2 = Nd4j.create(DataType.DOUBLE, 1000000);

    GaussianDistribution op1 = new GaussianDistribution(z1, 1.0, 1.0);
    Nd4j.getExecutioner().exec(op1, random1);

    GaussianDistribution op2 = new GaussianDistribution(z2, -1.0, 2.0);
    Nd4j.getExecutioner().exec(op2, random2);


    assertEquals(1.0, z1.meanNumber().doubleValue(), 0.01);
    assertEquals(1.0, z1.stdNumber().doubleValue(), 0.01);

    // check variance
    assertEquals(-1.0, z2.meanNumber().doubleValue(), 0.01);
    assertEquals(4.0, z2.varNumber().doubleValue(), 0.01);

    assertNotEquals(z1, z2);
}
 
Example #9
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDistribution3() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.create(128);
    INDArray z2 = Nd4j.create(128);
    UniformDistribution distribution = new UniformDistribution(z1, 1.0, 2.0);
    Nd4j.getExecutioner().exec(distribution, random1);
    UniformDistribution distribution2 = new UniformDistribution(z2, 1.0, 2.0);
    Nd4j.getExecutioner().exec(distribution2, random1);

    System.out.println("Data: " + z1);
    System.out.println("Data: " + z2);

    assertNotEquals(z1, z2);
}
 
Example #10
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBernoulliDistribution2() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.zeros(20);
    INDArray z2 = Nd4j.zeros(20);
    INDArray z1Dup = Nd4j.zeros(20);
    INDArray exp = Nd4j.create(new double[] {0,    1.0000,         0,    1.0000,    1.0000,         0,    1.0000,    1.0000,         0,    1.0000,    1.0000,    1.0000,         0,    1.0000,    1.0000,         0,         0,    1.0000,         0,    1.0000});

    BernoulliDistribution op1 = new BernoulliDistribution(z1, 0.50);
    BernoulliDistribution op2 = new BernoulliDistribution(z2, 0.50);

    Nd4j.getExecutioner().exec(op1, random1);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(z1Dup, z1);

    assertEquals(z1, z2);

    assertEquals(exp, z1);
}
 
Example #11
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testDistribution1() {
        Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
        Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

        INDArray z1 = Nd4j.create(1000);
        INDArray z2 = Nd4j.create(1000);
        UniformDistribution distribution = new UniformDistribution(z1, 1.0, 2.0);
        Nd4j.getExecutioner().exec(distribution, random1);
        UniformDistribution distribution2 = new UniformDistribution(z2, 1.0, 2.0);
        Nd4j.getExecutioner().exec(distribution2, random2);

//        System.out.println("Data: " + z1);
//        System.out.println("Data: " + z2);
        for (int e = 0; e < z1.length(); e++) {
            double val = z1.getDouble(e);
            assertTrue(val >= 1.0 && val <= 2.0);
        }

        assertEquals(z1, z2);
    }
 
Example #12
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDropoutInverted1() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.ones(300);
    INDArray z2 = Nd4j.ones(300);
    INDArray zDup = z1.dup();

    DropOutInverted op1 = new DropOutInverted(z1, z1, 0.10);
    Nd4j.getExecutioner().exec(op1, random1);

    DropOutInverted op2 = new DropOutInverted(z2, z2, 0.10);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(zDup, z1);


    for (int x = 0; x < z1.length(); x++) {
        assertEquals("Failed on element: [" + x + "]", z1.getFloat(x), z2.getFloat(x), 0.01f);
    }
    assertEquals(z1, z2);
}
 
Example #13
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBernoulliDistribution2() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.zeros(20);
    INDArray z2 = Nd4j.zeros(20);
    INDArray z1Dup = Nd4j.zeros(20);
    INDArray exp = Nd4j.create(new double[] {1.00, 0.00, 1.00, 0.00, 1.00, 1.00, 0.00, 1.00, 1.00, 0.00, 0.00, 0.00,
                    1.00, 1.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00});

    BernoulliDistribution op1 = new BernoulliDistribution(z1, 0.50);
    BernoulliDistribution op2 = new BernoulliDistribution(z2, 0.50);

    Nd4j.getExecutioner().exec(op1, random1);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(z1Dup, z1);

    assertEquals(z1, z2);

    assertEquals(exp, z1);
}
 
Example #14
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBernoulliDistribution3() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray prob = Nd4j.create(new double[] {1.0, 0.1, 0.2, 0.5, 1.0, 1.0, 0.3, 0.7, 0.34, 0.119});

    INDArray z1 = Nd4j.zeros(10);
    INDArray z2 = Nd4j.zeros(10);
    INDArray z1Dup = Nd4j.zeros(10);
    INDArray exp = Nd4j.create(new double[] {1.00, 0.00, 1.00, 0.00, 1.00, 1.00, 0.00, 1.00, 1.00, 0.00});

    BernoulliDistribution op1 = new BernoulliDistribution(z1, prob);
    BernoulliDistribution op2 = new BernoulliDistribution(z2, prob);

    Nd4j.getExecutioner().exec(op1, random1);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(z1Dup, z1);

    assertEquals(z1, z2);

    assertEquals(exp, z1);
}
 
Example #15
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBinomialDistribution1() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.zeros(1000);
    INDArray z2 = Nd4j.zeros(1000);
    INDArray z1Dup = Nd4j.zeros(1000);

    BinomialDistribution op1 = new BinomialDistribution(z1, 5, 0.25);
    BinomialDistribution op2 = new BinomialDistribution(z2, 5, 0.25);

    Nd4j.getExecutioner().exec(op1, random1);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(z1Dup, z1);

    assertEquals(z1, z2);

    BooleanIndexing.and(z1, Conditions.lessThanOrEqual(5.0));
    BooleanIndexing.and(z1, Conditions.greaterThanOrEqual(0.0));
}
 
Example #16
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBernoulliDistribution3() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray prob = Nd4j.create(new double[] {1.0, 0.1, 0.2, 0.5, 1.0, 1.0, 0.3, 0.7, 0.34, 0.119});

    INDArray z1 = Nd4j.zeros(10);
    INDArray z2 = Nd4j.zeros(10);
    INDArray z1Dup = Nd4j.zeros(10);
    INDArray exp = Nd4j.create(new double[] {1.0000,         0,         0,    1.0000,    1.0000,    1.0000,         0,    1.0000,         0,         0});

    BernoulliDistribution op1 = new BernoulliDistribution(z1, prob);
    BernoulliDistribution op2 = new BernoulliDistribution(z2, prob);

    Nd4j.getExecutioner().exec(op1, random1);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(z1Dup, z1);

    assertEquals(z1, z2);

    assertEquals(exp, z1);
}
 
Example #17
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testTruncatedNormal1() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z01 = Nd4j.create(10000000).assign(-119119d);
    INDArray z02 = Nd4j.createUninitialized(z01.length());

    TruncatedNormalDistribution distribution01 = new TruncatedNormalDistribution(z01, 0.0, 1.0);

    long time1 = System.currentTimeMillis();
    Nd4j.getExecutioner().exec(distribution01, random1);
    long time2 = System.currentTimeMillis();

    Nd4j.getExecutioner().exec(new GaussianDistribution( z02, 0.0, 1.0));
    long time3 = System.currentTimeMillis();

    log.info("Truncated: {} ms; Gaussian: {} ms", time2 - time1, time3 - time2);

    for (int e = 0; e < z01.length(); e++) {
        assertTrue("Value: " + z01.getDouble(e) + " at " + e,FastMath.abs(z01.getDouble(e)) <= 2.0);
        assertNotEquals(-119119d, z01.getDouble(e), 1e-3);
    }

    assertEquals(0.0, z01.meanNumber().doubleValue(), 1e-3);
}
 
Example #18
Source File: RandomTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaSide1() throws Exception {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    float array1[] = new float[1000];
    float array2[] = new float[1000];

    for (int e = 0; e < array1.length; e++) {
        array1[e] = random1.nextFloat();
        array2[e] = random2.nextFloat();

        assertTrue(array1[e] <= 1.0f);
    }

    assertArrayEquals(array1, array2, 1e-5f);
}
 
Example #19
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
    public void testDistribution3() {
        Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);

        INDArray z1 = Nd4j.create(128);
        INDArray z2 = Nd4j.create(128);
        UniformDistribution distribution = new UniformDistribution(z1, 1.0, 2.0);
        Nd4j.getExecutioner().exec(distribution, random1);
        UniformDistribution distribution2 = new UniformDistribution(z2, 1.0, 2.0);
        Nd4j.getExecutioner().exec(distribution2, random1);

//        System.out.println("Data: " + z1);
//        System.out.println("Data: " + z2);

        assertNotEquals(z1, z2);
    }
 
Example #20
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaSide2() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    int array1[] = new int[1000];
    int array2[] = new int[1000];

    for (int e = 0; e < array1.length; e++) {
        array1[e] = random1.nextInt();
        array2[e] = random2.nextInt();

        assertEquals(array1[e], array2[e]);
        assertTrue(array1[e] >= 0);
    }

    assertArrayEquals(array1, array2);
}
 
Example #21
Source File: RandomTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultithreading1() throws Exception {

    final AtomicInteger cnt = new AtomicInteger(0);
    final CopyOnWriteArrayList<float[]> list = new CopyOnWriteArrayList<>();

    Thread[] threads = new Thread[10];
    for (int x = 0; x < threads.length; x++) {
        list.add(null);
    }

    for (int x = 0; x < threads.length; x++) {
        threads[x] = new Thread(new Runnable() {
            @Override
            public void run() {
                Random rnd = Nd4j.getRandom();
                rnd.setSeed(119);
                float[] array = new float[10];

                for (int e = 0; e < array.length; e++) {
                    array[e] = rnd.nextFloat();
                }
                list.set(cnt.getAndIncrement(), array);
            }
        });
        threads[x].start();
    }

    for (int x = 0; x < threads.length; x++) {
        threads[x].join();

        assertNotEquals(null, list.get(x));

        if (x > 0) {
            assertArrayEquals(list.get(0), list.get(x), 1e-5f);
        }
    }
}
 
Example #22
Source File: InMemoryLookupTable.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public InMemoryLookupTable(VocabCache<T> vocab, int vectorLength, boolean useAdaGrad, double lr, Random gen,
                double negative) {
    this.vocab = vocab;
    this.vectorLength = vectorLength;
    this.useAdaGrad = useAdaGrad;
    this.lr.set(lr);
    this.rng = gen;
    this.negative = negative;
    initExpTable();

    if (useAdaGrad) {
        initAdaGrad();
    }
}
 
Example #23
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultithreading2() throws Exception {

    final AtomicInteger cnt = new AtomicInteger(0);
    final CopyOnWriteArrayList<INDArray> list = new CopyOnWriteArrayList<>();

    Thread[] threads = new Thread[10];
    for (int x = 0; x < threads.length; x++) {
        list.add(null);
    }

    for (int x = 0; x < threads.length; x++) {
        threads[x] = new Thread(new Runnable() {
            @Override
            public void run() {
                Random rnd = Nd4j.getRandom();
                rnd.setSeed(119);
                INDArray array = Nd4j.getExecutioner().exec(new UniformDistribution(Nd4j.createUninitialized(25)));

                Nd4j.getExecutioner().commit();

                list.set(cnt.getAndIncrement(), array);
            }
        });
        threads[x].start();
    }

    // we want all threads finished before comparing arrays
    for (int x = 0; x < threads.length; x++)
        threads[x].join();

    for (int x = 0; x < threads.length; x++) {
        assertNotEquals(null, list.get(x));

        if (x > 0) {
            assertEquals(list.get(0), list.get(x));
        }
    }
}
 
Example #24
Source File: QLearningDiscrete.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public QLearningDiscrete(MDP<O, Integer, DiscreteSpace> mdp, IDQN dqn, QLearningConfiguration conf,
                         int epsilonNbStep, ILearningBehavior<Integer> learningBehavior, Random random) {
    this.configuration = conf;
    this.mdp = new LegacyMDPWrapper<>(mdp, null);
    qNetwork = dqn;
    policy = new DQNPolicy(getQNetwork());
    egPolicy = new EpsGreedy(policy, mdp, conf.getUpdateStart(), epsilonNbStep, random, conf.getMinEpsilon(),
            this);

    this.learningBehavior = learningBehavior;
}
 
Example #25
Source File: Nd4jTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRandomSetSeed() {
    Random r = Nd4j.getRandom();
    Random t = Nd4j.getRandom();

    assertEquals(r, t);
    r.setSeed(123);
    assertEquals(r, t);
}
 
Example #26
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGaussianDistribution1() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    INDArray z1 = Nd4j.create(DataType.DOUBLE, 1000000);
    INDArray z2 = Nd4j.create(DataType.DOUBLE, 1000000);
    INDArray zDup = z1.like();

    GaussianDistribution op1 = new GaussianDistribution(z1, 0.0, 1.0);
    Nd4j.getExecutioner().exec(op1, random1);

    GaussianDistribution op2 = new GaussianDistribution(z2, 0.0, 1.0);
    Nd4j.getExecutioner().exec(op2, random2);

    assertNotEquals(zDup, z1);
    assertEquals(0.0, z1.meanNumber().doubleValue(), 0.01);

    assertEquals(1.0, z1.stdNumber().doubleValue(), 0.01);

    val d1 = z1.toDoubleVector();
    val d2 = z2.toDoubleVector();

    assertArrayEquals(d1, d2, 1e-4);

    assertEquals(z1, z2);
}
 
Example #27
Source File: NormalDistribution.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a normal distribution.
 *
 * @param rng                Random number generator.
 * @param mean               Mean for this distribution.
 * @param sd                 Standard deviation for this distribution.
 * @param inverseCumAccuracy Inverse cumulative probability accuracy.
 * @throws NotStrictlyPositiveException if {@code sd <= 0}.
 * @since 3.1
 */
public NormalDistribution(Random rng, double mean, double sd, double inverseCumAccuracy)
                throws NotStrictlyPositiveException {
    super(rng);

    if (sd <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sd);
    }

    this.mean = mean;
    standardDeviation = sd;
    solverAbsoluteAccuracy = inverseCumAccuracy;
}
 
Example #28
Source File: TruncatedNormalDistribution.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a normal distribution.
 *
 * @param rng                Random number generator.
 * @param mean               Mean for this distribution.
 * @param sd                 Standard deviation for this distribution.
 * @param inverseCumAccuracy Inverse cumulative probability accuracy.
 * @throws NotStrictlyPositiveException if {@code sd <= 0}.
 * @since 3.1
 */
public TruncatedNormalDistribution(Random rng, double mean, double sd, double inverseCumAccuracy)
                throws NotStrictlyPositiveException {
    super(rng);

    if (sd <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sd);
    }

    this.mean = mean;
    standardDeviation = sd;
    solverAbsoluteAccuracy = inverseCumAccuracy;
}
 
Example #29
Source File: RandomTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This test checks reSeed mechanics for native side
 *
 * @throws Exception
 */

@Test
public void testJavaSide4() {
    Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
    Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);

    int array1[] = new int[1000];
    int array2[] = new int[1000];

    for (int e = 0; e < array1.length; e++) {
        array1[e] = random1.nextInt();
        array2[e] = random2.nextInt();

        assertEquals(array1[e], array2[e]);
        assertTrue(array1[e] >= 0);
    }

    assertArrayEquals(array1, array2);

    random1.reSeed();
    random1.reSeed();

    int array3[] = new int[1000];
    int array4[] = new int[1000];

    for (int e = 0; e < array1.length; e++) {
        array3[e] = random1.nextInt();
        array4[e] = random2.nextInt();

        assertNotEquals(array3[e], array4[e]);
        assertTrue(array1[e] >= 0);
    }
}
 
Example #30
Source File: RandomProjectionLSH.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private INDArray gaussianRandomMatrix(int[] shape, Random rng){
    INDArray res = Nd4j.create(shape);

    GaussianDistribution op1 = new GaussianDistribution(res, 0.0, 1.0 / Math.sqrt(shape[0]));

    Nd4j.getExecutioner().exec(op1, rng);
    return res;
}