Java Code Examples for org.nd4j.linalg.indexing.NDArrayIndex#all()

The following examples show how to use org.nd4j.linalg.indexing.NDArrayIndex#all() . 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: ShapeResolutionTestsC.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testIndexPointAll() {
    INDArray zeros = Nd4j.zeros(3, 3, 3);
    INDArrayIndex x = NDArrayIndex.point(1);
    INDArrayIndex y = NDArrayIndex.all();
    INDArrayIndex z = NDArrayIndex.point(1);
    INDArray value = Nd4j.ones(1, 3);
    zeros.put(new INDArrayIndex[] {x, y, z}, value);

    String f1 = "[[[0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]\n" + "  [[0,00,1,00,0,00]\n"
                    + " [0,00,1,00,0,00]\n" + " [0,00,1,00,0,00]]\n" + "  [[0,00,0,00,0,00]\n"
                    + " [0,00,0,00,0,00]\n" + " [0,00,0,00,0,00]]]";

    String f2 = "[[[0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]\n" + "  [[0.00,1.00,0.00]\n"
                    + " [0.00,1.00,0.00]\n" + " [0.00,1.00,0.00]]\n" + "  [[0.00,0.00,0.00]\n"
                    + " [0.00,0.00,0.00]\n" + " [0.00,0.00,0.00]]]";

    if (!zeros.toString().equals(f1) && !zeros.toString().equals(f2))
        assertEquals(f2, zeros.toString());
}
 
Example 2
Source File: BatchedInferenceObservable.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
private INDArray[] splitExamples(INDArray netOutput, int firstInputComponent, int lastInputComponent){

        int numSplits = lastInputComponent - firstInputComponent + 1;
        if(numSplits == 1){
            return new INDArray[]{netOutput};
        } else {
            INDArray[] out = new INDArray[numSplits];
            INDArrayIndex[] indices = new INDArrayIndex[netOutput.rank()];
            for(int i=1; i<indices.length; i++ ){
                indices[i] = NDArrayIndex.all();
            }
            int examplesSoFar = 0;
            for( int inNum = 0; inNum < numSplits; inNum++ ){
                val inSizeEx = inputs.get(firstInputComponent + inNum)[0].size(0);
                indices[0] = NDArrayIndex.interval(examplesSoFar, examplesSoFar+inSizeEx);
                out[inNum] = netOutput.get(indices);
                examplesSoFar += inSizeEx;
            }
            return out;
        }
    }
 
Example 3
Source File: CoverageModelEMWorkspace.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * This method applies the Fourier filter on a given bias covariates matrix, applies the Fourier filter on it,
 * partitions the result, and pushes it to compute block(s)
 *
 * @param biasCovariates any T x D bias covariates matrix
 */
@UpdatesRDD
private void updateFilteredBiasCovariates(@Nonnull final INDArray biasCovariates) {
    final INDArray filteredBiasCovariates = Nd4j.create(biasCovariates.shape());

    /* instantiate the Fourier filter */
    final FourierLinearOperatorNDArray regularizerFourierLinearOperator = createRegularizerFourierLinearOperator();

    /* FFT by resolving W_tl on l */
    for (int li = 0; li < numLatents; li++) {
        final INDArrayIndex[] slice = {NDArrayIndex.all(), NDArrayIndex.point(li)};
        filteredBiasCovariates.get(slice).assign(
                regularizerFourierLinearOperator.operate(biasCovariates.get(slice)));
    }

    /* sent the new W to workers */
    switch (config.getBiasCovariatesComputeNodeCommunicationPolicy()) {
        case BROADCAST_HASH_JOIN:
            pushToWorkers(mapINDArrayToBlocks(filteredBiasCovariates),
                    (W, cb) -> cb.cloneWithUpdatedPrimitive(CoverageModelEMComputeBlock
                            .CoverageModelICGCacheNode.F_W_tl, W.get(cb.getTargetSpaceBlock())));
            break;

        case RDD_JOIN:
            joinWithWorkersAndMap(chopINDArrayToBlocks(filteredBiasCovariates),
                    p -> p._1.cloneWithUpdatedPrimitive(CoverageModelEMComputeBlock
                            .CoverageModelICGCacheNode.F_W_tl, p._2));
            break;
    }
}
 
Example 4
Source File: ROCBinaryTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testROCBinary3d() {
    INDArray prediction = Nd4j.rand(DataType.FLOAT, 2, 5, 10);
    INDArray label = Nd4j.rand(DataType.FLOAT, 2, 5, 10);


    List<INDArray> rowsP = new ArrayList<>();
    List<INDArray> rowsL = new ArrayList<>();
    NdIndexIterator iter = new NdIndexIterator(2, 10);
    while (iter.hasNext()) {
        long[] idx = iter.next();
        INDArrayIndex[] idxs = new INDArrayIndex[]{NDArrayIndex.point(idx[0]), NDArrayIndex.all(), NDArrayIndex.point(idx[1])};
        rowsP.add(prediction.get(idxs));
        rowsL.add(label.get(idxs));
    }

    INDArray p2d = Nd4j.vstack(rowsP);
    INDArray l2d = Nd4j.vstack(rowsL);

    ROCBinary e3d = new ROCBinary();
    ROCBinary e2d = new ROCBinary();

    e3d.eval(label, prediction);
    e2d.eval(l2d, p2d);

    for (ROCBinary.Metric m : ROCBinary.Metric.values()) {
        for( int i=0; i<5; i++ ) {
            double d1 = e3d.scoreForMetric(m, i);
            double d2 = e2d.scoreForMetric(m, i);
            assertEquals(m.toString(), d2, d1, 1e-6);
        }
    }
}
 
Example 5
Source File: EvaluationBinaryTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvaluationBinary4d() {
    INDArray prediction = Nd4j.rand(DataType.FLOAT, 2, 3, 10, 10);
    INDArray label = Nd4j.rand(DataType.FLOAT, 2, 3, 10, 10);


    List<INDArray> rowsP = new ArrayList<>();
    List<INDArray> rowsL = new ArrayList<>();
    NdIndexIterator iter = new NdIndexIterator(2, 10, 10);
    while (iter.hasNext()) {
        long[] idx = iter.next();
        INDArrayIndex[] idxs = new INDArrayIndex[]{NDArrayIndex.point(idx[0]), NDArrayIndex.all(), NDArrayIndex.point(idx[1]), NDArrayIndex.point(idx[2])};
        rowsP.add(prediction.get(idxs));
        rowsL.add(label.get(idxs));
    }

    INDArray p2d = Nd4j.vstack(rowsP);
    INDArray l2d = Nd4j.vstack(rowsL);

    EvaluationBinary e4d = new EvaluationBinary();
    EvaluationBinary e2d = new EvaluationBinary();

    e4d.eval(label, prediction);
    e2d.eval(l2d, p2d);

    for (EvaluationBinary.Metric m : EvaluationBinary.Metric.values()) {
        for( int i=0; i<3; i++ ) {
            double d1 = e4d.scoreForMetric(m, i);
            double d2 = e2d.scoreForMetric(m, i);
            assertEquals(m.toString(), d2, d1, 1e-6);
        }
    }
}
 
Example 6
Source File: NDArrayIndexResolveTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolvePoint() {
    INDArray arr = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    INDArrayIndex[] test = NDArrayIndex.resolve(arr.shape(), NDArrayIndex.point(1));
    INDArrayIndex[] assertion = {NDArrayIndex.point(1), NDArrayIndex.all()};
    assertArrayEquals(assertion, test);

    INDArrayIndex[] allAssertion = {NDArrayIndex.all(), NDArrayIndex.all()};
    assertArrayEquals(allAssertion, NDArrayIndex.resolve(arr.shape(), NDArrayIndex.all()));

    INDArrayIndex[] allAndOne = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(1)};
    assertArrayEquals(allAndOne, NDArrayIndex.resolve(arr.shape(), allAndOne));
}
 
Example 7
Source File: IndexShapeTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSinglePoint() {
    /*
    Assumes all indexes are filled out.
    Test simple general point case
     */
    int[] assertion = {2, 1, 4, 5, 1};
    INDArrayIndex[] indexes = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all()};

    int[] testShape = Indices.shape(shape, indexes);
    assertArrayEquals(assertion, testShape);

    int[] secondAssertion = {1, 2, 1, 5, 1};
    INDArrayIndex[] otherCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.point(0)

    };

    assertArrayEquals(secondAssertion, Indices.shape(shape, otherCase));


    int[] thridAssertion = {1, 2, 1, 4, 5, 1};
    INDArrayIndex[] thirdCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.point(0),

    };
    assertArrayEquals(thridAssertion, Indices.shape(shape, thirdCase));

}
 
Example 8
Source File: IndexShapeTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewAxis() {
    //normal prepend
    int[] prependAssertion = {1, 1, 1, 1, 2, 1, 3, 4, 5, 1};
    INDArrayIndex[] prependTest = {NDArrayIndex.newAxis(), NDArrayIndex.newAxis(), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),

    };

    assertArrayEquals(prependAssertion, Indices.shape(shape, prependTest));

    //test setting for particular indexes.
    //when an all is encountered before a new axis,
    //it is assumed that new axis must occur at the destination
    //where the new axis was specified
    int[] addToMiddle = {1, 1, 2, 1, 1, 1, 3, 4, 5, 1};
    INDArrayIndex[] setInMiddleTest = {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),
                    NDArrayIndex.newAxis(), NDArrayIndex.newAxis(), NDArrayIndex.all(), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),};
    assertArrayEquals(addToMiddle, Indices.shape(shape, setInMiddleTest));

    //test prepending AND adding to middle
    int[] prependAndAddToMiddleAssertion = {1, 1, 1, 1, 2, 1, 1, 1, 3, 4, 5, 1};

    INDArrayIndex[] prependAndMiddle = {NDArrayIndex.newAxis(), NDArrayIndex.newAxis(), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.newAxis(), NDArrayIndex.newAxis(),
                    NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.all(),
                    NDArrayIndex.all(),};
    assertArrayEquals(prependAndAddToMiddleAssertion, Indices.shape(shape, prependAndMiddle));

}
 
Example 9
Source File: EvaluationCalibrationTest.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvaluationCalibration3dMasking() {
    INDArray prediction = Nd4j.rand(DataType.FLOAT, 2, 3, 10);
    INDArray label = Nd4j.rand(DataType.FLOAT, 2, 3, 10);

    List<INDArray> rowsP = new ArrayList<>();
    List<INDArray> rowsL = new ArrayList<>();

    //Check "DL4J-style" 2d per timestep masking [minibatch, seqLength] mask shape
    INDArray mask2d = Nd4j.randomBernoulli(0.5, 2, 10);
    NdIndexIterator iter = new NdIndexIterator(2, 10);
    while (iter.hasNext()) {
        long[] idx = iter.next();
        if(mask2d.getDouble(idx[0], idx[1]) != 0.0) {
            INDArrayIndex[] idxs = new INDArrayIndex[]{NDArrayIndex.point(idx[0]), NDArrayIndex.all(), NDArrayIndex.point(idx[1])};
            rowsP.add(prediction.get(idxs));
            rowsL.add(label.get(idxs));
        }
    }
    INDArray p2d = Nd4j.vstack(rowsP);
    INDArray l2d = Nd4j.vstack(rowsL);

    EvaluationCalibration e3d_m2d = new EvaluationCalibration();
    EvaluationCalibration e2d_m2d = new EvaluationCalibration();
    e3d_m2d.eval(label, prediction, mask2d);
    e2d_m2d.eval(l2d, p2d);

    assertEquals(e3d_m2d, e2d_m2d);
}
 
Example 10
Source File: OldConvolution.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Rearrange matrix
 * columns into blocks

 * @param col the column
 *            transposed image to convert
 * @param sy stride y
 * @param sx stride x
 * @param ph padding height
 * @param pw padding width
 * @param h height
 * @param w width
 * @return
 */
public static INDArray col2im(INDArray col, int sy, int sx, int ph, int pw, int h, int w) {
    //number of images
    long n = col.size(0);
    //number of columns
    long c = col.size(1);
    //kernel height
    long kh = col.size(2);
    //kernel width
    long kw = col.size(3);
    //out height
    long outH = col.size(4);
    //out width
    long outW = col.size(5);

    INDArray img = Nd4j.create(n, c, h + 2 * ph + sy - 1, w + 2 * pw + sx - 1);
    for (int i = 0; i < kh; i++) {
        //iterate over the kernel rows
        long iLim = i + sy * outH;
        for (int j = 0; j < kw; j++) {
            //iterate over the kernel columns
            long jLim = j + sx * outW;
            INDArrayIndex[] indices = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(),
                            NDArrayIndex.interval(i, sy, iLim), NDArrayIndex.interval(j, sx, jLim)};

            INDArray get = img.get(indices);

            INDArray colAdd = col.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(i),
                            NDArrayIndex.point(j), NDArrayIndex.all(), NDArrayIndex.all());
            get.addi(colAdd);
            img.put(indices, get);

        }
    }

    //return the subset of the padded image relative to the height/width of the image and the padding width/height
    return img.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(ph, ph + h),
                    NDArrayIndex.interval(pw, pw + w));
}
 
Example 11
Source File: OldConvolution.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Rearrange matrix
 * columns into blocks

 * @param col the column
 *            transposed image to convert
 * @param sy stride y
 * @param sx stride x
 * @param ph padding height
 * @param pw padding width
 * @param h height
 * @param w width
 * @return
 */
public static INDArray col2im(INDArray col, int sy, int sx, int ph, int pw, int h, int w) {
    //number of images
    long n = col.size(0);
    //number of columns
    long c = col.size(1);
    //kernel height
    long kh = col.size(2);
    //kernel width
    long kw = col.size(3);
    //out height
    long outH = col.size(4);
    //out width
    long outW = col.size(5);

    INDArray img = Nd4j.create(n, c, h + 2 * ph + sy - 1, w + 2 * pw + sx - 1);
    for (int i = 0; i < kh; i++) {
        //iterate over the kernel rows
        long iLim = i + sy * outH;
        for (int j = 0; j < kw; j++) {
            //iterate over the kernel columns
            long jLim = j + sx * outW;
            INDArrayIndex[] indices = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all(),
                            NDArrayIndex.interval(i, sy, iLim), NDArrayIndex.interval(j, sx, jLim)};

            INDArray get = img.get(indices);

            INDArray colAdd = col.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.point(i),
                            NDArrayIndex.point(j), NDArrayIndex.all(), NDArrayIndex.all());
            get.addi(colAdd);
            img.put(indices, get);

        }
    }

    //return the subset of the padded image relative to the height/width of the image and the padding width/height
    return img.get(NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(ph, ph + h),
                    NDArrayIndex.interval(pw, pw + w));
}
 
Example 12
Source File: IndexShapeTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterval() {
    int[] basicAssertion = {1, 1, 1, 1, 3, 1, 2, 1};
    INDArrayIndex[] basicTest = {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(0, 1),
                    NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(1, 2),
                    NDArrayIndex.interval(2, 4), NDArrayIndex.all()};
    assertArrayEquals(basicAssertion, Indices.shape(shape, basicTest));

}
 
Example 13
Source File: IndexShapeTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSinglePoint() {
    /*
    Assumes all indexes are filled out.
    Test simple general point case
     */
    int[] assertion = {2, 1, 4, 5, 1};
    INDArrayIndex[] indexes = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all()};

    int[] testShape = Indices.shape(shape, indexes);
    assertArrayEquals(assertion, testShape);

    int[] secondAssertion = {1, 2, 1, 5, 1};
    INDArrayIndex[] otherCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.point(0)

    };

    assertArrayEquals(secondAssertion, Indices.shape(shape, otherCase));


    int[] thridAssertion = {1, 2, 1, 4, 5, 1};
    INDArrayIndex[] thirdCase = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(0), NDArrayIndex.all(),
                    NDArrayIndex.all(), NDArrayIndex.point(0),

    };
    assertArrayEquals(thridAssertion, Indices.shape(shape, thirdCase));

}
 
Example 14
Source File: IndexShapeTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterval() {
    int[] basicAssertion = {1, 1, 1, 1, 3, 1, 2, 1};
    INDArrayIndex[] basicTest = {NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(0, 1),
                    NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(1, 2),
                    NDArrayIndex.interval(2, 4), NDArrayIndex.all()};
    assertArrayEquals(basicAssertion, Indices.shape(shape, basicTest));

}
 
Example 15
Source File: NDArrayIndexResolveTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testResolvePoint() {
    INDArray arr = Nd4j.linspace(1, 4, 4).reshape(2, 2);
    INDArrayIndex[] test = NDArrayIndex.resolve(arr.shape(), NDArrayIndex.point(1));
    INDArrayIndex[] assertion = {NDArrayIndex.point(1), NDArrayIndex.all()};
    assertArrayEquals(assertion, test);

    INDArrayIndex[] allAssertion = {NDArrayIndex.all(), NDArrayIndex.all()};
    assertArrayEquals(allAssertion, NDArrayIndex.resolve(arr.shape(), NDArrayIndex.all()));

    INDArrayIndex[] allAndOne = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.point(1)};
    assertArrayEquals(allAndOne, NDArrayIndex.resolve(arr.shape(), allAndOne));
}
 
Example 16
Source File: NDArrayRecordBatch.java    From DataVec with Apache License 2.0 5 votes vote down vote up
private static INDArray getExample(int idx, INDArray from){
    INDArrayIndex[] idxs = new INDArrayIndex[from.rank()];
    idxs[0] = NDArrayIndex.interval(idx, idx, true);    //Use interval to avoid collapsing point dimension
    for( int i=1; i<from.rank(); i++){
        idxs[i] = NDArrayIndex.all();
    }
    return from.get(idxs);
}
 
Example 17
Source File: IndexingTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testGet() {
    System.out.println("Testing sub-array put and get with a 3D array ...");

    INDArray arr = Nd4j.linspace(0, 124, 125).reshape(5, 5, 5);

    /*
     * Extract elements with the following indices:
     *
     * (2,1,1) (2,1,2) (2,1,3)
     * (2,2,1) (2,2,2) (2,2,3)
     * (2,3,1) (2,3,2) (2,3,3)
     */

    int slice = 2;

    int iStart = 1;
    int jStart = 1;

    int iEnd = 4;
    int jEnd = 4;

    // Method A: Element-wise.

    INDArray subArr_A = Nd4j.create(new int[] {3, 3});

    for (int i = iStart; i < iEnd; i++) {
        for (int j = jStart; j < jEnd; j++) {

            double val = arr.getDouble(slice, i, j);
            int[] sub = new int[] {i - iStart, j - jStart};

            subArr_A.putScalar(sub, val);

        }
    }

    // Method B: Using NDArray get and put with index classes.

    INDArray subArr_B = Nd4j.create(new int[] {3, 3});

    INDArrayIndex ndi_Slice = NDArrayIndex.point(slice);
    INDArrayIndex ndi_J = NDArrayIndex.interval(jStart, jEnd);
    INDArrayIndex ndi_I = NDArrayIndex.interval(iStart, iEnd);

    INDArrayIndex[] whereToGet = new INDArrayIndex[] {ndi_Slice, ndi_I, ndi_J};

    INDArray whatToPut = arr.get(whereToGet);
    assertEquals(subArr_A, whatToPut);
    System.out.println(whatToPut);
    INDArrayIndex[] whereToPut = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all()};

    subArr_B.put(whereToPut, whatToPut);

    assertEquals(subArr_A, subArr_B);
    System.out.println("... done");
}
 
Example 18
Source File: IndexingTestsC.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testGet() {
    System.out.println("Testing sub-array put and get with a 3D array ...");

    INDArray arr = Nd4j.linspace(0, 124, 125).reshape(5, 5, 5);

    /*
     * Extract elements with the following indices:
     *
     * (2,1,1) (2,1,2) (2,1,3)
     * (2,2,1) (2,2,2) (2,2,3)
     * (2,3,1) (2,3,2) (2,3,3)
     */

    int slice = 2;

    int iStart = 1;
    int jStart = 1;

    int iEnd = 4;
    int jEnd = 4;

    // Method A: Element-wise.

    INDArray subArr_A = Nd4j.create(new int[] {3, 3});

    for (int i = iStart; i < iEnd; i++) {
        for (int j = jStart; j < jEnd; j++) {

            double val = arr.getDouble(slice, i, j);
            int[] sub = new int[] {i - iStart, j - jStart};

            subArr_A.putScalar(sub, val);
        }
    }

    // Method B: Using NDArray get and put with index classes.

    INDArray subArr_B = Nd4j.create(new int[] {3, 3});

    INDArrayIndex ndi_Slice = NDArrayIndex.point(slice);
    INDArrayIndex ndi_J = NDArrayIndex.interval(jStart, jEnd);
    INDArrayIndex ndi_I = NDArrayIndex.interval(iStart, iEnd);

    INDArrayIndex[] whereToGet = new INDArrayIndex[] {ndi_Slice, ndi_I, ndi_J};

    INDArray whatToPut = arr.get(whereToGet);
    System.out.println(whatToPut);
    INDArrayIndex[] whereToPut = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all()};

    subArr_B.put(whereToPut, whatToPut);

    assertEquals(subArr_A, subArr_B);

    System.out.println("... done");
}
 
Example 19
Source File: IndexingTestsC.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testGet() {
//        System.out.println("Testing sub-array put and get with a 3D array ...");

        INDArray arr = Nd4j.linspace(0, 124, 125).reshape(5, 5, 5);

        /*
         * Extract elements with the following indices:
         *
         * (2,1,1) (2,1,2) (2,1,3)
         * (2,2,1) (2,2,2) (2,2,3)
         * (2,3,1) (2,3,2) (2,3,3)
         */

        int slice = 2;

        int iStart = 1;
        int jStart = 1;

        int iEnd = 4;
        int jEnd = 4;

        // Method A: Element-wise.

        INDArray subArr_A = Nd4j.create(new int[] {3, 3});

        for (int i = iStart; i < iEnd; i++) {
            for (int j = jStart; j < jEnd; j++) {

                double val = arr.getDouble(slice, i, j);
                int[] sub = new int[] {i - iStart, j - jStart};

                subArr_A.putScalar(sub, val);
            }
        }

        // Method B: Using NDArray get and put with index classes.

        INDArray subArr_B = Nd4j.create(new int[] {3, 3});

        INDArrayIndex ndi_Slice = NDArrayIndex.point(slice);
        INDArrayIndex ndi_J = NDArrayIndex.interval(jStart, jEnd);
        INDArrayIndex ndi_I = NDArrayIndex.interval(iStart, iEnd);

        INDArrayIndex[] whereToGet = new INDArrayIndex[] {ndi_Slice, ndi_I, ndi_J};

        INDArray whatToPut = arr.get(whereToGet);
//        System.out.println(whatToPut);
        INDArrayIndex[] whereToPut = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all()};

        subArr_B.put(whereToPut, whatToPut);

        assertEquals(subArr_A, subArr_B);

//        System.out.println("... done");
    }
 
Example 20
Source File: IndexingTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testGet() {
//        System.out.println("Testing sub-array put and get with a 3D array ...");

        INDArray arr = Nd4j.linspace(0, 124, 125).reshape(5, 5, 5);

        /*
         * Extract elements with the following indices:
         *
         * (2,1,1) (2,1,2) (2,1,3)
         * (2,2,1) (2,2,2) (2,2,3)
         * (2,3,1) (2,3,2) (2,3,3)
         */

        int slice = 2;

        int iStart = 1;
        int jStart = 1;

        int iEnd = 4;
        int jEnd = 4;

        // Method A: Element-wise.

        INDArray subArr_A = Nd4j.create(new int[] {3, 3});

        for (int i = iStart; i < iEnd; i++) {
            for (int j = jStart; j < jEnd; j++) {

                double val = arr.getDouble(slice, i, j);
                int[] sub = new int[] {i - iStart, j - jStart};

                subArr_A.putScalar(sub, val);

            }
        }

        // Method B: Using NDArray get and put with index classes.

        INDArray subArr_B = Nd4j.create(new int[] {3, 3});

        INDArrayIndex ndi_Slice = NDArrayIndex.point(slice);
        INDArrayIndex ndi_J = NDArrayIndex.interval(jStart, jEnd);
        INDArrayIndex ndi_I = NDArrayIndex.interval(iStart, iEnd);

        INDArrayIndex[] whereToGet = new INDArrayIndex[] {ndi_Slice, ndi_I, ndi_J};

        INDArray whatToPut = arr.get(whereToGet);
        assertEquals(subArr_A, whatToPut);
//        System.out.println(whatToPut);
        INDArrayIndex[] whereToPut = new INDArrayIndex[] {NDArrayIndex.all(), NDArrayIndex.all()};

        subArr_B.put(whereToPut, whatToPut);

        assertEquals(subArr_A, subArr_B);
//        System.out.println("... done");
    }