Java Code Examples for org.nd4j.linalg.api.shape.Shape#newShapeNoCopy()

The following examples show how to use org.nd4j.linalg.api.shape.Shape#newShapeNoCopy() . 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: AdaDeltaUpdater.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.msg = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.msdx = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.msg = Shape.newShapeNoCopy(this.msg, gradientShape, gradientOrder == 'f');
    this.msdx = Shape.newShapeNoCopy(this.msdx, gradientShape, gradientOrder == 'f');
    if (msg == null || msdx == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");
}
 
Example 2
Source File: AdaMaxUpdater.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.u = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.u = Shape.newShapeNoCopy(this.u, gradientShape, gradientOrder == 'f');
    if (m == null || u == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 3
Source File: BasicWorkspaceTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoShape1() {
    int outDepth = 50;
    int miniBatch = 64;
    int outH = 8;
    int outW = 8;

    try (Nd4jWorkspace wsI =
                    (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "ITER")) {
        INDArray delta = Nd4j.create(new int[] {50, 64, 8, 8}, new int[] {64, 3200, 8, 1}, 'c');
        delta = delta.permute(1, 0, 2, 3);

        assertArrayEquals(new long[] {64, 50, 8, 8}, delta.shape());
        assertArrayEquals(new long[] {3200, 64, 8, 1}, delta.stride());

        INDArray delta2d = Shape.newShapeNoCopy(delta, new int[] {outDepth, miniBatch * outH * outW}, false);

        assertNotNull(delta2d);
    }
}
 
Example 4
Source File: DeepFMProductVertex.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray doForward(boolean training, LayerWorkspaceMgr workspaceMgr) {
    if (!canDoForward()) {
        throw new IllegalStateException("Cannot do forward pass: inputs not set");
    }
    // inputs[index] => {batchSize, numberOfEmbeds}
    INDArray left = inputs[0];
    INDArray right = inputs[1];
    long size = inputs[0].shape()[0];
    INDArray value = workspaceMgr.createUninitialized(ArrayType.ACTIVATIONS, size);
    // 求两个行向量的点积
    for (int index = 0; index < size; index++) {
        INDArray product = left.getRow(index).mmul(right.getRow(index).transpose());
        value.put(index, product);
    }
    // outputs[index] => {batchSize, 1}
    return Shape.newShapeNoCopy(value, new long[] { value.length(), 1L }, value.ordering() == 'f');
}
 
Example 5
Source File: AdamUpdater.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    if (m == null || v == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 6
Source File: AMSGradUpdater.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    val n = viewArray.length() / 3;
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, n));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(n, 2*n));
    this.vHat = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(2*n, 3*n));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    this.vHat = Shape.newShapeNoCopy(this.vHat, gradientShape, gradientOrder == 'f');
    if (m == null || v == null || vHat == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 7
Source File: BasicWorkspaceTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoShape1() {
    int outDepth = 50;
    int miniBatch = 64;
    int outH = 8;
    int outW = 8;

    try (Nd4jWorkspace wsI =
                    (Nd4jWorkspace) Nd4j.getWorkspaceManager().getAndActivateWorkspace(basicConfig, "ITER")) {
        INDArray delta = Nd4j.create(new int[] {50, 64, 8, 8}, new int[] {64, 3200, 8, 1}, 'c');
        delta = delta.permute(1, 0, 2, 3);

        assertArrayEquals(new int[] {64, 50, 8, 8}, delta.shape());
        assertArrayEquals(new int[] {3200, 64, 8, 1}, delta.stride());

        INDArray delta2d = Shape.newShapeNoCopy(delta, new int[] {outDepth, miniBatch * outH * outW}, false);

        assertNotNull(delta2d);
    }
}
 
Example 8
Source File: ElementWiseStrideTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testEWS1() throws Exception {
    List<Pair<INDArray,String>> list = NDArrayCreationUtil.getAllTestMatricesWithShape(4,5,12345);
    list.addAll(NDArrayCreationUtil.getAll3dTestArraysWithShape(12345,4,5,6));
    list.addAll(NDArrayCreationUtil.getAll4dTestArraysWithShape(12345,4,5,6,7));
    list.addAll(NDArrayCreationUtil.getAll5dTestArraysWithShape(12345,4,5,6,7,8));
    list.addAll(NDArrayCreationUtil.getAll6dTestArraysWithShape(12345,4,5,6,7,8,9));


    for(Pair<INDArray,String> p : list){
        int ewsBefore = Shape.elementWiseStride(p.getFirst().shapeInfo());
        INDArray reshapeAttempt = Shape.newShapeNoCopy(p.getFirst(),new int[]{1,p.getFirst().length()}, Nd4j.order() == 'f');

        if (reshapeAttempt != null && ewsBefore == -1 && reshapeAttempt.elementWiseStride() != -1 ) {
            System.out.println("NDArrayCreationUtil." + p.getSecond());
            System.out.println("ews before: " + ewsBefore);
            System.out.println(p.getFirst().shapeInfoToString());
            System.out.println("ews returned by elementWiseStride(): " + p.getFirst().elementWiseStride());
            System.out.println("ews returned by reshape(): " + reshapeAttempt.elementWiseStride());
            System.out.println();
      //      assertTrue(false);
        } else {
      //      System.out.println("FAILED: " + p.getFirst().shapeInfoToString());
        }
    }
}
 
Example 9
Source File: AdamUpdater.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);
    long length = viewArray.length();
    this.m = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(0, length / 2));
    this.v = viewArray.get(NDArrayIndex.point(0), NDArrayIndex.interval(length / 2, length));

    //Reshape to match the expected shape of the input gradient arrays
    this.m = Shape.newShapeNoCopy(this.m, gradientShape, gradientOrder == 'f');
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    if (m == null || v == null)
        throw new IllegalStateException("Could not correctly reshape gradient view arrays");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 10
Source File: AdaGrad.java    From nd4j with Apache License 2.0 5 votes vote down vote up
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector() && !(viewArray.rank() == 2 && viewArray.columns() == 1 && viewArray.rows() == 1))
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(epsilon);
    this.historicalGradient = viewArray;
    //Reshape to match the expected shape of the input gradient arrays
    this.historicalGradient = Shape.newShapeNoCopy(this.historicalGradient, gradientShape, gradientOrder == 'f');
    if (historicalGradient == null)
        throw new IllegalStateException("Could not correctly reshape gradient view array");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 11
Source File: NesterovsUpdater.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVectorOrScalar())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(0);

    this.v = viewArray;

    //Reshape to match the expected shape of the input gradient arrays
    this.v = Shape.newShapeNoCopy(this.v, gradientShape, gradientOrder == 'f');
    if (v == null)
        throw new IllegalStateException("Could not correctly reshape gradient view array");
    this.gradientReshapeOrder = gradientOrder;
}
 
Example 12
Source File: AdaGradUpdater.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(epsilon);
    this.historicalGradient = viewArray;
    //Reshape to match the expected shape of the input gradient arrays
    this.historicalGradient = Shape.newShapeNoCopy(this.historicalGradient, gradientShape, gradientOrder == 'f');
    if (historicalGradient == null)
        throw new IllegalStateException("Could not correctly reshape gradient view array");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 13
Source File: RmsPropUpdater.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector())
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(config.getEpsilon());
    this.lastGradient = viewArray;

    //Reshape to match the expected shape of the input gradient arrays
    this.lastGradient = Shape.newShapeNoCopy(this.lastGradient, gradientShape, gradientOrder == 'f');
    if (lastGradient == null)
        throw new IllegalStateException("Could not correctly reshape gradient view array");

    gradientReshapeOrder = gradientOrder;
}
 
Example 14
Source File: AdaGrad.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public void setStateViewArray(INDArray viewArray, long[] gradientShape, char gradientOrder, boolean initialize) {
    if (!viewArray.isRowVector() && !(viewArray.rank() == 2 && viewArray.columns() == 1 && viewArray.rows() == 1))
        throw new IllegalArgumentException("Invalid input: expect row vector input");
    if (initialize)
        viewArray.assign(epsilon);
    this.historicalGradient = viewArray;
    //Reshape to match the expected shape of the input gradient arrays
    this.historicalGradient = Shape.newShapeNoCopy(this.historicalGradient, gradientShape, gradientOrder == 'f');
    if (historicalGradient == null)
        throw new IllegalStateException("Could not correctly reshape gradient view array");

    this.gradientReshapeOrder = gradientOrder;
}
 
Example 15
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray reshape(char order, long... newShape) {
    Nd4j.getCompressor().autoDecompress(this);

    if (newShape == null || newShape.length < 1)
        throw new ND4JIllegalStateException(
                "Can't reshape(int...) without shape arguments. Got empty shape instead.");

    // TODO: maybe toFlatten() makes more sense here?
    // reshape(-1) special case
    if (newShape.length == 1 && newShape[0] == -1)
        newShape[0] = this.length();

    int numberNegativesOnes = 0;
    long[] shape = ArrayUtil.copy(newShape);


    for (int i = 0; i < shape.length; i++) {
        if (shape[i] < 0) {
            if (numberNegativesOnes >= 1)
                throw new IllegalArgumentException("Only one dimension can be negative ones. Got shape "
                        + Arrays.toString(newShape));

            numberNegativesOnes++;

            int shapeLength = 1;
            for (int j = 0; j < shape.length; j++)
                if (shape[j] >= 1)
                    shapeLength *= shape[j];
            long realShape = Math.abs(length() / shapeLength);
            long[] thisNewShape = new long[shape.length];
            for (int j = 0; j < shape.length; j++) {
                if (i != j) {
                    thisNewShape[j] = shape[j];
                } else
                    thisNewShape[j] = realShape;
            }

            shape = thisNewShape;
            break;

        }
    }

    long prod = ArrayUtil.prodLong(shape);

    if (prod != this.lengthLong()){
        throw new ND4JIllegalStateException("New shape length doesn't match original length: [" + prod + "] vs [" + this.lengthLong() + "]. Original shape: "+Arrays.toString(this.shape())+" New Shape: "+Arrays.toString(newShape));
    }





    INDArray reshapeAttempt = Shape.newShapeNoCopy(this, shape, order == 'f');
    if (reshapeAttempt != null) {
        // kinda strange get/set usage
        //  reshapeAttempt.setOrder(Shape.getOrder(reshapeAttempt));
        return reshapeAttempt;
    }


    INDArray ret = Nd4j.createUninitialized(shape, order);
    if (order != ordering()) {
        ret.setData(dup(order).data());
    } else
        ret.assign(this);
    return ret;
}
 
Example 16
Source File: ShapeTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoCopy() {
    INDArray threeTwoTwo = Nd4j.linspace(1, 12, 12);
    INDArray arr = Shape.newShapeNoCopy(threeTwoTwo, new long[] {3, 2, 2}, true);
    assertArrayEquals(arr.shape(), new long[] {3, 2, 2});
}
 
Example 17
Source File: ConvolutionLayerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testWeightReshaping() {
    //Test assumptions of weight reshaping
    //Weights: originally c order, shape [outDepth, inDepth, kH, kw]
    //permute (3,2,1,0)

    int depthOut = 2;
    int depthIn = 3;
    int kH = 2;
    int kW = 2;

    /*
     ----- Weights -----
     - dOut 0 -
    dIn 0      dIn 1        dIn 2
    [ 0  1      [ 4  5      [ 8  9
      2  3]       6  7]      10 11]
     - dOut 1 -
    [12 13      [16 17      [20 21
     14 15]      18 19]      22 23]
     */

    INDArray weightOrig = Nd4j.create(new int[] {depthOut, depthIn, kH, kW}, 'c');
    weightOrig.put(new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{0, 1}, {2, 3}}));
    weightOrig.put(new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(1), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{4, 5}, {6, 7}}));
    weightOrig.put(new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(2), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{8, 9}, {10, 11}}));
    weightOrig.put(new INDArrayIndex[] {NDArrayIndex.point(1), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{12, 13}, {14, 15}}));
    weightOrig.put(new INDArrayIndex[] {NDArrayIndex.point(1), NDArrayIndex.point(1), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{16, 17}, {18, 19}}));
    weightOrig.put(new INDArrayIndex[] {NDArrayIndex.point(1), NDArrayIndex.point(2), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{20, 21}, {22, 23}}));

    INDArray weightPermute = weightOrig.permute(3, 2, 1, 0);
    INDArray w2d = Shape.newShapeNoCopy(weightPermute, new int[] {depthIn * kH * kW, depthOut}, true);

    assertNotNull(w2d);

    //Expected order of weight rows, after reshaping: (kw0,kh0,din0), (kw1,kh0,din0), (kw0,kh1,din0), (kw1,kh1,din0), (kw0,kh0,din1), ...
    INDArray wExp = Nd4j.create(new double[][] {{0, 12}, {1, 13}, {2, 14}, {3, 15}, {4, 16}, {5, 17}, {6, 18},
                    {7, 19}, {8, 20}, {9, 21}, {10, 22}, {11, 23}}).castTo(DataType.FLOAT);

    assertEquals(wExp, w2d);
}
 
Example 18
Source File: ConvolutionLayerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeltaReshaping() {
    //As per above test: testing assumptions of cnn implementation...

    //Delta: initially shape [miniBatch,dOut,outH,outW]
    //permute to [dOut,miniB,outH,outW]
    //then reshape to [dOut,miniB*outH*outW]
    //Expect columns of delta2d to be like: (mb0,h0,w0), (mb0,h0,w1), (mb1,h0,w2), (mb0,h1,w0), ... (mb1,...), ..., (mb2,...)
    int miniBatch = 3;
    int depth = 2;
    int outW = 3;
    int outH = 3;

    /*
     ----- Input delta -----
    example 0:
    channels 0     channels 1
    [ 0  1  2      [ 9 10 11
      3  4  5       12 13 14
      6  7  8]      15 16 17]
    example 1:
    [18 19 20      [27 28 29
     21 22 23       30 31 32
     24 25 26]      33 34 35]
    example 2:
    [36 37 38      [45 46 47
     39 40 41       48 49 50
     42 43 44]      51 52 53]
     */

    INDArray deltaOrig = Nd4j.create(new int[] {miniBatch, depth, outH, outW}, 'c');
    deltaOrig.put(new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}));
    deltaOrig.put(new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(1), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{9, 10, 11}, {12, 13, 14}, {15, 16, 17}}));
    deltaOrig.put(new INDArrayIndex[] {NDArrayIndex.point(1), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{18, 19, 20}, {21, 22, 23}, {24, 25, 26}}));
    deltaOrig.put(new INDArrayIndex[] {NDArrayIndex.point(1), NDArrayIndex.point(1), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{27, 28, 29}, {30, 31, 32}, {33, 34, 35}}));
    deltaOrig.put(new INDArrayIndex[] {NDArrayIndex.point(2), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{36, 37, 38}, {39, 40, 41}, {42, 43, 44}}));
    deltaOrig.put(new INDArrayIndex[] {NDArrayIndex.point(2), NDArrayIndex.point(1), NDArrayIndex.all(),
                    NDArrayIndex.all()}, Nd4j.create(new double[][] {{45, 46, 47}, {48, 49, 50}, {51, 52, 53}}));


    INDArray deltaPermute = deltaOrig.permute(1, 0, 2, 3).dup('c');
    INDArray delta2d = Shape.newShapeNoCopy(deltaPermute, new int[] {depth, miniBatch * outW * outH}, false);

    INDArray exp = Nd4j.create(new double[][] {
                    {0, 1, 2, 3, 4, 5, 6, 7, 8, 18, 19, 20, 21, 22, 23, 24, 25, 26, 36, 37, 38, 39, 40, 41, 42, 43,
                                    44}, //depth0
                    {9, 10, 11, 12, 13, 14, 15, 16, 17, 27, 28, 29, 30, 31, 32, 33, 34, 35, 45, 46, 47, 48, 49, 50,
                                    51, 52, 53} //depth1
    }).castTo(delta2d.dataType());

    assertEquals(exp, delta2d);
}
 
Example 19
Source File: ConvolutionLayerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCnnIm2ColReshaping() {
    //This test: a bit unusual in that it tests the *assumptions* of the CNN implementation rather than the implementation itself
    //Specifically, it tests the row and column orders after reshaping on im2col is reshaped (both forward and backward pass)
    INDArray input = getInput();

    //im2col in the required order: want [outW,outH,miniBatch,depthIn,kH,kW], but need to input [miniBatch,channels,kH,kW,outH,outW]
    // given the current im2col implementation
    //To get this: create an array of the order we want, permute it to the order required by im2col implementation, and then do im2col on that
    //to get old order from required order: permute(2,3,4,5,1,2)
    INDArray col = Nd4j.create(new int[] {miniBatch, outH, outW, inDepth, kH, kW}, 'c');
    INDArray col2 = col.permute(0, 3, 4, 5, 1, 2);
    Convolution.im2col(input, kH, kW, strides[0], strides[1], pad[0], pad[1], false, col2);

    /*
    Expected Output, im2col
    - example 0 -
        channels 0                        channels 1
    h0,w0      h0,w1               h0,w0      h0,w1
    0  1     1  2                 9 10      10 11
    3  4     4  5                12 13      13 14
    
    h1,w0      h1,w1               h1,w0      h1,w1
    3  4     4  5                12 13      13 14
    6  7     7  8                15 16      16 17
    
    - example 1 -
        channels 0                        channels 1
    h0,w0      h0,w1               h0,w0      h0,w1
    18 19     19 20               27 28      28 29
    21 22     22 23               30 31      31 32
    
    h1,w0      h1,w1               h1,w0      h1,w1
    21 22     22 23               30 31      31 32
    24 25     25 26               33 34      34 35
    */

    //Now, after reshaping im2col to 2d, we expect:
    //Rows with order (wOut0,hOut0,mb0), (wOut1,hOut0,mb0), (wOut0,hOut1,mb0), (wOut1,hOut1,mb0), (wOut0,hOut0,mb1), ...
    //Columns with order (d0,kh0,kw0), (d0,kh0,kw1), (d0,kh1,kw0), (d0,kh1,kw1), (d1,kh0,kw0), ...

    INDArray reshapedCol = Shape.newShapeNoCopy(col, new int[] {miniBatch * outH * outW, inDepth * kH * kW}, false);

    INDArray exp2d = Nd4j.create(outW * outH * miniBatch, inDepth * kH * kW);
    exp2d.putRow(0, Nd4j.create(new double[] {0, 1, 3, 4, 9, 10, 12, 13})); //wOut0,hOut0,mb0 -> both depths, in order (d0,kh0,kw0), (d0,kh0,kw1), (d0,kh1,kw0), (d0,kh1,kw1), (d1,kh0,kw0), (d1,kh0,kw1), (d1,kh1,kw0), (d1,kh1,kw1)
    exp2d.putRow(1, Nd4j.create(new double[] {1, 2, 4, 5, 10, 11, 13, 14})); //wOut1,hOut0,mb0
    exp2d.putRow(2, Nd4j.create(new double[] {3, 4, 6, 7, 12, 13, 15, 16})); //wOut0,hOut1,mb0
    exp2d.putRow(3, Nd4j.create(new double[] {4, 5, 7, 8, 13, 14, 16, 17})); //wOut1,hOut1,mb0
    exp2d.putRow(4, Nd4j.create(new double[] {18, 19, 21, 22, 27, 28, 30, 31})); //wOut0,hOut0,mb1
    exp2d.putRow(5, Nd4j.create(new double[] {19, 20, 22, 23, 28, 29, 31, 32})); //wOut1,hOut0,mb1
    exp2d.putRow(6, Nd4j.create(new double[] {21, 22, 24, 25, 30, 31, 33, 34})); //wOut0,hOut1,mb1
    exp2d.putRow(7, Nd4j.create(new double[] {22, 23, 25, 26, 31, 32, 34, 35})); //wOut1,hOut1,mb1

    assertEquals(exp2d, reshapedCol);

    //Check the same thing for the backprop im2col (different order)
    INDArray colBackprop = Nd4j.create(new int[] {miniBatch, outH, outW, inDepth, kH, kW}, 'c');
    INDArray colBackprop2 = colBackprop.permute(0, 3, 4, 5, 1, 2);

    Convolution.im2col(input, kH, kW, strides[0], strides[1], pad[0], pad[1], false, colBackprop2);

    INDArray reshapedColBackprop = Shape.newShapeNoCopy(colBackprop,
                    new int[] {miniBatch * outH * outW, inDepth * kH * kW}, false);

    //Rows with order (mb0,h0,w0), (mb0,h0,w1), (mb0,h1,w0), (mb0,h1,w1), (mb1,h0,w0), (mb1,h0,w1), (mb1,h1,w0), (mb1,h1,w1)
    //Columns with order (d0,kh0,kw0), (d0,kh0,kw1), (d0,kh1,kw0), (d0,kh1,kw1), (d1,kh0,kw0), ...

    INDArray exp2dv2 = Nd4j.create(outW * outH * miniBatch, inDepth * kH * kW);
    exp2dv2.putRow(0, Nd4j.create(new double[] {0, 1, 3, 4, 9, 10, 12, 13})); //wOut0,hOut0,mb0 -> both depths, in order (d0,kh0,kw0), (d0,kh0,kw1), (d0,kh1,kw0), (d0,kh1,kw1), (d1,kh0,kw0), (d1,kh0,kw1), (d1,kh1,kw0), (d1,kh1,kw1)
    exp2dv2.putRow(1, Nd4j.create(new double[] {1, 2, 4, 5, 10, 11, 13, 14})); //wOut1,hOut0,mb0
    exp2dv2.putRow(2, Nd4j.create(new double[] {3, 4, 6, 7, 12, 13, 15, 16})); //wOut0,hOut1,mb0
    exp2dv2.putRow(3, Nd4j.create(new double[] {4, 5, 7, 8, 13, 14, 16, 17})); //wOut1,hOut1,mb0
    exp2dv2.putRow(4, Nd4j.create(new double[] {18, 19, 21, 22, 27, 28, 30, 31})); //wOut0,hOut0,mb1
    exp2dv2.putRow(5, Nd4j.create(new double[] {19, 20, 22, 23, 28, 29, 31, 32})); //wOut1,hOut0,mb1
    exp2dv2.putRow(6, Nd4j.create(new double[] {21, 22, 24, 25, 30, 31, 33, 34})); //wOut0,hOut1,mb1
    exp2dv2.putRow(7, Nd4j.create(new double[] {22, 23, 25, 26, 31, 32, 34, 35})); //wOut1,hOut1,mb1

    assertEquals(exp2dv2, reshapedColBackprop);
}
 
Example 20
Source File: ShapeTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoCopy() {
    INDArray threeTwoTwo = Nd4j.linspace(1, 12, 12, DataType.DOUBLE);
    INDArray arr = Shape.newShapeNoCopy(threeTwoTwo, new long[] {3, 2, 2}, true);
    assertArrayEquals(arr.shape(), new long[] {3, 2, 2});
}