Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#broadcast()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#broadcast() . 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: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBroadcastingGenerated() {
    int[][] broadcastShape = NDArrayCreationUtil.getRandomBroadCastShape(7, 6, 10);
    List<List<Pair<INDArray, String>>> broadCastList = new ArrayList<>(broadcastShape.length);
    for (int[] shape : broadcastShape) {
        List<Pair<INDArray, String>> arrShape = NDArrayCreationUtil.get6dPermutedWithShape(7, shape);
        broadCastList.add(arrShape);
        broadCastList.add(NDArrayCreationUtil.get6dReshapedWithShape(7, shape));
        broadCastList.add(NDArrayCreationUtil.getAll6dTestArraysWithShape(7, shape));
    }

    for (List<Pair<INDArray, String>> b : broadCastList) {
        for (Pair<INDArray, String> val : b) {
            INDArray inputArrBroadcast = val.getFirst();
            val destShape = NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7);
            INDArray output = inputArrBroadcast
                            .broadcast(NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7));
            assertArrayEquals(destShape, output.shape());
        }
    }



}
 
Example 2
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBroadCast() {
    INDArray n = Nd4j.linspace(1, 4, 4);
    INDArray broadCasted = n.broadcast(5, 4);
    for (int i = 0; i < broadCasted.rows(); i++) {
        assertEquals(n, broadCasted.getRow(i));
    }

    INDArray broadCast2 = broadCasted.getRow(0).broadcast(5, 4);
    assertEquals(broadCasted, broadCast2);


    INDArray columnBroadcast = n.transpose().broadcast(4, 5);
    for (int i = 0; i < columnBroadcast.columns(); i++) {
        assertEquals(columnBroadcast.getColumn(i), n.transpose());
    }

    INDArray fourD = Nd4j.create(1, 2, 1, 1);
    INDArray broadCasted3 = fourD.broadcast(1, 2, 36, 36);
    assertTrue(Arrays.equals(new long[] {1, 2, 36, 36}, broadCasted3.shape()));
}
 
Example 3
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBroadcastingGenerated() {
    int[][] broadcastShape = NDArrayCreationUtil.getRandomBroadCastShape(7, 6, 10);
    List<List<Pair<INDArray, String>>> broadCastList = new ArrayList<>(broadcastShape.length);
    for (int[] shape : broadcastShape) {
        List<Pair<INDArray, String>> arrShape = NDArrayCreationUtil.get6dPermutedWithShape(7, shape, DataType.DOUBLE);
        broadCastList.add(arrShape);
        broadCastList.add(NDArrayCreationUtil.get6dReshapedWithShape(7, shape, DataType.DOUBLE));
        broadCastList.add(NDArrayCreationUtil.getAll6dTestArraysWithShape(7, shape, DataType.DOUBLE));
    }

    for (List<Pair<INDArray, String>> b : broadCastList) {
        for (Pair<INDArray, String> val : b) {
            INDArray inputArrBroadcast = val.getFirst();
            val destShape = NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7);
            INDArray output = inputArrBroadcast
                            .broadcast(NDArrayCreationUtil.broadcastToShape(inputArrBroadcast.shape(), 7));
            assertArrayEquals(destShape, output.shape());
        }
    }



}
 
Example 4
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBroadCast() {
    INDArray n = Nd4j.linspace(1, 4, 4, DataType.DOUBLE);
    INDArray broadCasted = n.broadcast(5, 4);
    for (int i = 0; i < broadCasted.rows(); i++) {
        assertEquals(n, broadCasted.getRow(i));
    }

    INDArray broadCast2 = broadCasted.getRow(0).broadcast(5, 4);
    assertEquals(broadCasted, broadCast2);


    INDArray columnBroadcast = n.reshape(4,1).broadcast(4, 5);
    for (int i = 0; i < columnBroadcast.columns(); i++) {
        assertEquals(columnBroadcast.getColumn(i), n.reshape(4));
    }

    INDArray fourD = Nd4j.create(1, 2, 1, 1);
    INDArray broadCasted3 = fourD.broadcast(1, 2, 36, 36);
    assertTrue(Arrays.equals(new long[] {1, 2, 36, 36}, broadCasted3.shape()));
}
 
Example 5
Source File: IndexingTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void broadcastBug() throws Exception {
    INDArray a = Nd4j.create(new double[] {1.0, 2.0, 3.0, 4.0}, new int[] {2, 2});
    final INDArray col = a.get(NDArrayIndex.all(), NDArrayIndex.point(0));

    final INDArray aBad = col.broadcast(2, 2);
    final INDArray aGood = col.dup().broadcast(2, 2);
    System.out.println(aBad);
    System.out.println(aGood);
    assertTrue(Transforms.abs(aGood.sub(aBad).div(aGood)).maxNumber().doubleValue() < 0.01);
}
 
Example 6
Source File: NDArrayTestsFortran.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testBroadCasting() {
    INDArray first = Nd4j.arange(0, 3).reshape(3, 1);
    INDArray ret = first.broadcast(3, 4);
    INDArray testRet = Nd4j.create(new double[][] {{0, 0, 0, 0}, {1, 1, 1, 1}, {2, 2, 2, 2}});
    assertEquals(testRet, ret);
    INDArray r = Nd4j.arange(0, 4).reshape(1, 4);
    INDArray r2 = r.broadcast(4, 4);
    INDArray testR2 = Nd4j.create(new double[][] {{0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}});
    assertEquals(testR2, r2);

}
 
Example 7
Source File: IndexingTestsC.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void broadcastBug() {
        INDArray a = Nd4j.create(new double[] {1.0, 2.0, 3.0, 4.0}, new int[] {2, 2});
        final INDArray col = a.get(NDArrayIndex.all(), NDArrayIndex.point(0));

        final INDArray aBad = col.broadcast(2, 2);
        final INDArray aGood = col.dup().broadcast(2, 2);
//        System.out.println(aBad);
//        System.out.println(aGood);
        assertTrue(Transforms.abs(aGood.sub(aBad).div(aGood)).maxNumber().doubleValue() < 0.01);
    }
 
Example 8
Source File: NDArrayTestsFortran.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testBroadCasting() {
    INDArray first = Nd4j.arange(0, 3).reshape(3, 1).castTo(DataType.DOUBLE);
    INDArray ret = first.broadcast(3, 4);
    INDArray testRet = Nd4j.create(new double[][] {{0, 0, 0, 0}, {1, 1, 1, 1}, {2, 2, 2, 2}});
    assertEquals(testRet, ret);
    INDArray r = Nd4j.arange(0, 4).reshape(1, 4).castTo(DataType.DOUBLE);
    INDArray r2 = r.broadcast(4, 4);
    INDArray testR2 = Nd4j.create(new double[][] {{0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}, {0, 1, 2, 3}});
    assertEquals(testR2, r2);

}