Java Code Examples for org.nd4j.linalg.factory.Nd4j#averageAndPropagate()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#averageAndPropagate() . 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: ParallelWrapper.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private double getScore(AtomicInteger locker) {
    wasAveraged = true;
    double score = 0.0;

    List<INDArray> params = new ArrayList<>();
    for (int cnt = 0; cnt < workers && cnt < locker.get(); cnt++) {
        params.add(zoo[cnt].getModel().params());
        score += zoo[cnt].getModel().score();
    }

    Nd4j.averageAndPropagate(null, params);


    score /= Math.min(workers, locker.get());

    // TODO: improve this
    if (reportScore)
        log.info("Averaged score: " + score);

    return score;
}
 
Example 2
Source File: AveragingTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleDeviceAveraging() throws Exception {
    INDArray array1 = Nd4j.valueArrayOf(LENGTH, 1.0);
    INDArray array2 = Nd4j.valueArrayOf(LENGTH, 2.0);
    INDArray array3 = Nd4j.valueArrayOf(LENGTH, 3.0);
    INDArray array4 = Nd4j.valueArrayOf(LENGTH, 4.0);
    INDArray array5 = Nd4j.valueArrayOf(LENGTH, 5.0);
    INDArray array6 = Nd4j.valueArrayOf(LENGTH, 6.0);
    INDArray array7 = Nd4j.valueArrayOf(LENGTH, 7.0);
    INDArray array8 = Nd4j.valueArrayOf(LENGTH, 8.0);
    INDArray array9 = Nd4j.valueArrayOf(LENGTH, 9.0);
    INDArray array10 = Nd4j.valueArrayOf(LENGTH, 10.0);
    INDArray array11 = Nd4j.valueArrayOf(LENGTH, 11.0);
    INDArray array12 = Nd4j.valueArrayOf(LENGTH, 12.0);
    INDArray array13 = Nd4j.valueArrayOf(LENGTH, 13.0);
    INDArray array14 = Nd4j.valueArrayOf(LENGTH, 14.0);
    INDArray array15 = Nd4j.valueArrayOf(LENGTH, 15.0);
    INDArray array16 = Nd4j.valueArrayOf(LENGTH, 16.0);


    long time1 = System.currentTimeMillis();
    INDArray arrayMean = Nd4j.averageAndPropagate(new INDArray[] {array1, array2, array3, array4, array5, array6, array7, array8, array9, array10, array11, array12, array13, array14, array15, array16});
    long time2 = System.currentTimeMillis();
    System.out.println("Execution time: " + (time2 - time1));

    assertNotEquals(null, arrayMean);

    assertEquals(8.5f, arrayMean.getFloat(12), 0.1f);
    assertEquals(8.5f, arrayMean.getFloat(150), 0.1f);
    assertEquals(8.5f, arrayMean.getFloat(475), 0.1f);


    assertEquals(8.5f, array1.getFloat(475), 0.1f);
    assertEquals(8.5f, array2.getFloat(475), 0.1f);
    assertEquals(8.5f, array3.getFloat(475), 0.1f);
    assertEquals(8.5f, array5.getFloat(475), 0.1f);
    assertEquals(8.5f, array16.getFloat(475), 0.1f);
}
 
Example 3
Source File: AveragingTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleDeviceAveraging2() throws Exception {
    INDArray exp = Nd4j.linspace(1, LENGTH, LENGTH);
    List<INDArray> arrays = new ArrayList<>();
    for (int i = 0; i < THREADS; i++)
        arrays.add(exp.dup());

    INDArray mean = Nd4j.averageAndPropagate(arrays);

    assertEquals(exp, mean);

    for (int i = 0; i < THREADS; i++)
        assertEquals(exp, arrays.get(i));
}
 
Example 4
Source File: AveragingTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleDeviceAveraging2() {
    INDArray exp = Nd4j.linspace(1, LENGTH, LENGTH);
    List<INDArray> arrays = new ArrayList<>();
    for (int i = 0; i < THREADS; i++)
        arrays.add(exp.dup());

    INDArray mean = Nd4j.averageAndPropagate(arrays);

    assertEquals(exp, mean);

    for (int i = 0; i < THREADS; i++)
        assertEquals(exp, arrays.get(i));
}
 
Example 5
Source File: AveragingTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
     * This test should be run on multi-gpu system only. On single-gpu system this test will fail
     * @throws Exception
     */
    @Test
    public void testMultiDeviceAveraging() throws Exception {
        final List<Pair<INDArray, INDArray>> pairs = new ArrayList<>();

        int numDevices = Nd4j.getAffinityManager().getNumberOfDevices();
        AtomicAllocator allocator = AtomicAllocator.getInstance();


        for (int i = 0; i < THREADS; i++) {
            final int order = i;
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    pairs.add(new Pair<INDArray, INDArray>(Nd4j.valueArrayOf(LENGTH, (double) order), null));

                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                        //
                    }
                }
            });

            thread.start();
            thread.join();
        }

        assertEquals(THREADS, pairs.size());
        final List<INDArray> arrays = new ArrayList<>();

        AtomicBoolean hasNonZero = new AtomicBoolean(false);

        for (int i = 0; i < THREADS; i++) {
            INDArray array = pairs.get(i).getKey();
            AllocationPoint point = allocator.getAllocationPoint(array.data());

            if (point.getDeviceId() != 0 )
                hasNonZero.set(true);

            arrays.add(array);
        }

        assertEquals(true, hasNonZero.get());

/*
        // old way of averaging, without further propagation
        INDArray z = Nd4j.create(LENGTH);
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < THREADS; i++) {
            z.addi(arrays.get(i));
        }
        z.divi((float) THREADS);
        CudaContext context = (CudaContext) allocator.getDeviceContext().getContext();
        context.syncOldStream();
        long time2 = System.currentTimeMillis();
        System.out.println("Execution time: " + (time2 - time1));

*/

        long time1 = System.currentTimeMillis();
        INDArray z = Nd4j.averageAndPropagate(arrays);
        long time2 = System.currentTimeMillis();
        System.out.println("Execution time: " + (time2 - time1));


        assertEquals(7.5f, z.getFloat(0), 0.01f);
        assertEquals(7.5f, z.getFloat(10), 0.01f);

        for (int i = 0; i < THREADS; i++) {

            for (int x = 0; x < LENGTH; x++) {
                assertEquals("Failed on array [" +i+ "], element [" +x+ "]",z.getFloat(0), arrays.get(i).getFloat(x), 0.01f);
            }
        }



    }
 
Example 6
Source File: AveragingTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSingleDeviceAveraging1() throws Exception {
    INDArray array1 = Nd4j.valueArrayOf(LENGTH, 1.0);
    INDArray array2 = Nd4j.valueArrayOf(LENGTH, 2.0);
    INDArray array3 = Nd4j.valueArrayOf(LENGTH, 3.0);
    INDArray array4 = Nd4j.valueArrayOf(LENGTH, 4.0);
    INDArray array5 = Nd4j.valueArrayOf(LENGTH, 5.0);
    INDArray array6 = Nd4j.valueArrayOf(LENGTH, 6.0);
    INDArray array7 = Nd4j.valueArrayOf(LENGTH, 7.0);
    INDArray array8 = Nd4j.valueArrayOf(LENGTH, 8.0);
    INDArray array9 = Nd4j.valueArrayOf(LENGTH, 9.0);
    INDArray array10 = Nd4j.valueArrayOf(LENGTH, 10.0);
    INDArray array11 = Nd4j.valueArrayOf(LENGTH, 11.0);
    INDArray array12 = Nd4j.valueArrayOf(LENGTH, 12.0);
    INDArray array13 = Nd4j.valueArrayOf(LENGTH, 13.0);
    INDArray array14 = Nd4j.valueArrayOf(LENGTH, 14.0);
    INDArray array15 = Nd4j.valueArrayOf(LENGTH, 15.0);
    INDArray array16 = Nd4j.valueArrayOf(LENGTH, 16.0);


    long time1 = System.currentTimeMillis();
    INDArray arrayMean = Nd4j.averageAndPropagate(new INDArray[] {array1, array2, array3, array4, array5, array6,
                    array7, array8, array9, array10, array11, array12, array13, array14, array15, array16});
    long time2 = System.currentTimeMillis();
    System.out.println("Execution time: " + (time2 - time1));

    assertNotEquals(null, arrayMean);

    assertEquals(8.5f, arrayMean.getFloat(12), 0.1f);
    assertEquals(8.5f, arrayMean.getFloat(150), 0.1f);
    assertEquals(8.5f, arrayMean.getFloat(475), 0.1f);


    assertEquals(8.5f, array1.getFloat(475), 0.1f);
    assertEquals(8.5f, array2.getFloat(475), 0.1f);
    assertEquals(8.5f, array3.getFloat(475), 0.1f);
    assertEquals(8.5f, array5.getFloat(475), 0.1f);
    assertEquals(8.5f, array16.getFloat(475), 0.1f);


    assertEquals(8.5, arrayMean.meanNumber().doubleValue(), 0.01);
    assertEquals(8.5, array1.meanNumber().doubleValue(), 0.01);
    assertEquals(8.5, array2.meanNumber().doubleValue(), 0.01);

    assertEquals(arrayMean, array16);
}
 
Example 7
Source File: AveragingTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSingleDeviceAveraging1() {
    INDArray array1 = Nd4j.valueArrayOf(LENGTH, 1.0);
    INDArray array2 = Nd4j.valueArrayOf(LENGTH, 2.0);
    INDArray array3 = Nd4j.valueArrayOf(LENGTH, 3.0);
    INDArray array4 = Nd4j.valueArrayOf(LENGTH, 4.0);
    INDArray array5 = Nd4j.valueArrayOf(LENGTH, 5.0);
    INDArray array6 = Nd4j.valueArrayOf(LENGTH, 6.0);
    INDArray array7 = Nd4j.valueArrayOf(LENGTH, 7.0);
    INDArray array8 = Nd4j.valueArrayOf(LENGTH, 8.0);
    INDArray array9 = Nd4j.valueArrayOf(LENGTH, 9.0);
    INDArray array10 = Nd4j.valueArrayOf(LENGTH, 10.0);
    INDArray array11 = Nd4j.valueArrayOf(LENGTH, 11.0);
    INDArray array12 = Nd4j.valueArrayOf(LENGTH, 12.0);
    INDArray array13 = Nd4j.valueArrayOf(LENGTH, 13.0);
    INDArray array14 = Nd4j.valueArrayOf(LENGTH, 14.0);
    INDArray array15 = Nd4j.valueArrayOf(LENGTH, 15.0);
    INDArray array16 = Nd4j.valueArrayOf(LENGTH, 16.0);


    long time1 = System.currentTimeMillis();
    INDArray arrayMean = Nd4j.averageAndPropagate(new INDArray[] {array1, array2, array3, array4, array5, array6,
                    array7, array8, array9, array10, array11, array12, array13, array14, array15, array16});
    long time2 = System.currentTimeMillis();
    System.out.println("Execution time: " + (time2 - time1));

    assertNotEquals(null, arrayMean);

    assertEquals(8.5f, arrayMean.getFloat(12), 0.1f);
    assertEquals(8.5f, arrayMean.getFloat(150), 0.1f);
    assertEquals(8.5f, arrayMean.getFloat(475), 0.1f);


    assertEquals(8.5f, array1.getFloat(475), 0.1f);
    assertEquals(8.5f, array2.getFloat(475), 0.1f);
    assertEquals(8.5f, array3.getFloat(475), 0.1f);
    assertEquals(8.5f, array5.getFloat(475), 0.1f);
    assertEquals(8.5f, array16.getFloat(475), 0.1f);


    assertEquals(8.5, arrayMean.meanNumber().doubleValue(), 0.01);
    assertEquals(8.5, array1.meanNumber().doubleValue(), 0.01);
    assertEquals(8.5, array2.meanNumber().doubleValue(), 0.01);

    assertEquals(arrayMean, array16);
}