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

The following examples show how to use org.nd4j.linalg.indexing.NDArrayIndex#interval() . 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 testIndexPointInterval() {
    INDArray zeros = Nd4j.zeros(3, 3, 3);
    INDArrayIndex x = NDArrayIndex.point(1);
    INDArrayIndex y = NDArrayIndex.interval(1, 2, true);
    INDArrayIndex z = NDArrayIndex.point(1);
    INDArray value = Nd4j.ones(1, 2);
    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,0,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,0.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(f2) && !zeros.toString().equals(f1))
        assertEquals(f2, zeros.toString());

}
 
Example 2
Source File: ShapeResolutionTestsC.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testIndexIntervalAll() {
    INDArray zeros = Nd4j.zeros(3, 3, 3);
    INDArrayIndex x = NDArrayIndex.interval(0, 1, true);
    INDArrayIndex y = NDArrayIndex.all();
    INDArrayIndex z = NDArrayIndex.interval(1, 2, true);
    INDArray value = Nd4j.ones(2, 6);
    zeros.put(new INDArrayIndex[] {x, y, z}, value);

    String f1 = "[[[0,00,1,00,1,00]\n" + " [0,00,1,00,1,00]\n" + " [0,00,1,00,1,00]]\n" + "  [[0,00,1,00,1,00]\n"
                    + " [0,00,1,00,1,00]\n" + " [0,00,1,00,1,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,1.00,1.00]\n" + " [0.00,1.00,1.00]\n" + " [0.00,1.00,1.00]]\n" + "  [[0.00,1.00,1.00]\n"
                    + " [0.00,1.00,1.00]\n" + " [0.00,1.00,1.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 3
Source File: ShapeResolutionTestsC.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testIndexPointIntervalAll() {
    INDArray zeros = Nd4j.zeros(3, 3, 3);
    INDArrayIndex x = NDArrayIndex.point(1);
    INDArrayIndex y = NDArrayIndex.all();
    INDArrayIndex z = NDArrayIndex.interval(1, 2, true);
    INDArray value = Nd4j.ones(3, 2);
    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,1,00]\n"
                    + " [0,00,1,00,1,00]\n" + " [0,00,1,00,1,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,1.00]\n"
                    + " [0.00,1.00,1.00]\n" + " [0.00,1.00,1.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 4
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 5
Source File: IndexingTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPointIndexing() {
    int slices = 5;
    int rows = 5;
    int cols = 5;
    int l = slices * rows * cols;
    INDArray A = Nd4j.linspace(1, l, l).reshape(slices, rows, cols);

    for (int s = 0; s < slices; s++) {
        INDArrayIndex ndi_Slice = NDArrayIndex.point(s);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                log.info("Running for ( {}, {} - {} , {} - {} )", s, i, rows, j, cols);
                INDArrayIndex ndi_I = NDArrayIndex.interval(i, rows);
                INDArrayIndex ndi_J = NDArrayIndex.interval(j, cols);
                INDArray aView = A.get(ndi_Slice).get(ndi_I, ndi_J);
                INDArray sameView = A.get(ndi_Slice, ndi_I, ndi_J);
                String failureMessage = String.format("Fails for (%d , %d - %d, %d - %d)\n", s, i, rows, j, cols);
                try {
                    assertEquals(failureMessage, aView, sameView);
                } catch (Throwable t) {
                    collector.addError(t);
                }
            }
        }
    }
}
 
Example 6
Source File: IndexingTestsC.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testPointIndexing() {
        int slices = 5;
        int rows = 5;
        int cols = 5;
        int l = slices * rows * cols;
        INDArray A = Nd4j.linspace(1, l, l).reshape(slices, rows, cols);

        for (int s = 0; s < slices; s++) {
            INDArrayIndex ndi_Slice = NDArrayIndex.point(s);
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
//                    log.info("Running for ( {}, {} - {} , {} - {} )", s, i, rows, j, cols);
                    INDArrayIndex ndi_I = NDArrayIndex.interval(i, rows);
                    INDArrayIndex ndi_J = NDArrayIndex.interval(j, cols);
                    INDArray aView = A.get(ndi_Slice, NDArrayIndex.all(), NDArrayIndex.all()).get(ndi_I, ndi_J);
                    INDArray sameView = A.get(ndi_Slice, ndi_I, ndi_J);
                    String failureMessage = String.format("Fails for (%d , %d - %d, %d - %d)\n", s, i, rows, j, cols);
                    try {
                        assertEquals(failureMessage, aView, sameView);
                    } catch (Throwable t) {
                        collector.addError(t);
                    }
                }
            }
        }
    }
 
Example 7
Source File: BaseNDArrayList.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private void moveForward(int index) {
    int numMoved = size - index - 1;
    INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index,index + numMoved)};
    INDArray get = container.get(getRange);
    INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1,index + 1 + get.length())};
    container.put(first,get);
}
 
Example 8
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 9
Source File: NDArrayList.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private void moveForward(int index) {
    int numMoved = size - index - 1;
    INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index,index + numMoved)};
    INDArray get = container.get(getRange).dup();
    INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1,index + 1 + get.length())};
    container.put(first,get);
}
 
Example 10
Source File: MergeVertex.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private INDArrayIndex[] indices(int num, int axis, long from, long to){
    INDArrayIndex[] out = new INDArrayIndex[num];
    for( int i=0; i<num; i++ ){
        if(i == axis){
            out[i] = NDArrayIndex.interval(from, to);
        } else {
            out[i] = NDArrayIndex.all();
        }
    }
    return out;
}
 
Example 11
Source File: BaseNDArrayList.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private void moveForward(int index) {
    int numMoved = size - index - 1;
    INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index,index + numMoved)};
    INDArray get = container.get(getRange);
    INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1,index + 1 + get.length())};
    container.put(first,get);
}
 
Example 12
Source File: BaseNDArrayList.java    From nd4j with Apache License 2.0 5 votes vote down vote up
private void moveBackward(int index) {
    int numMoved = size - index - 1;
    INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index  ,index  + numMoved)};
    INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.point(0), NDArrayIndex.interval(index + 1 ,index + 1  + numMoved)};
    INDArray get = container.get(getRange);
    container.put(first,get);
}
 
Example 13
Source File: CpuLapack.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
 public void dgeqrf(int M, int N, INDArray A, INDArray R, INDArray INFO)  {
     INDArray tau = Nd4j.create(DataType.DOUBLE, N ) ;

     int status = LAPACKE_dgeqrf(getColumnOrder(A), M, N,
          (DoublePointer)A.data().addressPointer(), getLda(A),
          (DoublePointer)tau.data().addressPointer()
          );
     if( status != 0 ) {
         throw new BlasException( "Failed to execute dgeqrf", status ) ;
     }
     
     // Copy R ( upper part of Q ) into result
     if( R != null ) {
         R.assign( A.get( NDArrayIndex.interval( 0, A.columns() ), NDArrayIndex.all() ) ) ; 
INDArrayIndex ix[] = new INDArrayIndex[ 2 ] ;

for( int i=1 ; i<Math.min( A.rows(), A.columns() ) ; i++ ) {
	ix[0] = NDArrayIndex.point( i ) ;
	ix[1] = NDArrayIndex.interval( 0, i ) ;				
	R.put(ix, 0) ;
}
     }

     status = LAPACKE_dorgqr( getColumnOrder(A), M, N, N, 
          (DoublePointer)A.data().addressPointer(), getLda(A),
          (DoublePointer)tau.data().addressPointer()
          );
     if( status != 0 ) {
         throw new BlasException( "Failed to execute dorgqr", status ) ;
     }
 }
 
Example 14
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 15
Source File: ShapeResolutionTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlatIndexPointInterval() {
    INDArray zeros = Nd4j.zeros(1, 4);
    INDArrayIndex x = NDArrayIndex.point(0);
    INDArrayIndex y = NDArrayIndex.interval(1, 2, true);
    INDArray value = Nd4j.ones(1, 2);
    zeros.put(new INDArrayIndex[] {x, y}, value);

    INDArray assertion = Nd4j.create(new double[] {0.0, 1.0, 1.0, 0.0});
    assertEquals(assertion, zeros);
}
 
Example 16
Source File: IndexingTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testNegativeBounds() {
   INDArray arr = Nd4j.linspace(1,10,10).reshape(2,5);
   INDArrayIndex interval = NDArrayIndex.interval(0,1,-2,arr.size(1));
   INDArray get = arr.get(NDArrayIndex.all(),interval);
   INDArray assertion = Nd4j.create(new double[][]{
           {1,2,3},
           {6,7,8}
   });
   assertEquals(assertion,get);
}
 
Example 17
Source File: NDArrayList.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private void moveForward(int index) {
    int numMoved = size - index - 1;
    INDArrayIndex[] getRange = new INDArrayIndex[] {NDArrayIndex.interval(index,index + numMoved)};
    INDArray get = container.get(getRange).dup();
    INDArrayIndex[] first = new INDArrayIndex[] {NDArrayIndex.interval(index + 1,index + 1 + get.length())};
    container.put(first,get);
}
 
Example 18
Source File: CpuLapack.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
 public void sgeqrf(int M, int N, INDArray A, INDArray R, INDArray INFO) {
     INDArray tau = Nd4j.create( N ) ;

     int status = LAPACKE_sgeqrf(getColumnOrder(A), M, N, 
          (FloatPointer)A.data().addressPointer(), getLda(A),
          (FloatPointer)tau.data().addressPointer()
          );
     if( status != 0 ) {
         throw new BlasException( "Failed to execute sgeqrf", status ) ;
     }

     // Copy R ( upper part of Q ) into result
     if( R != null ) {
         R.assign( A.get( NDArrayIndex.interval( 0, A.columns() ), NDArrayIndex.all() ) ) ; 
INDArrayIndex ix[] = new INDArrayIndex[ 2 ] ;

for( int i=1 ; i<Math.min( A.rows(), A.columns() ) ; i++ ) {
	ix[0] = NDArrayIndex.point( i ) ;
	ix[1] = NDArrayIndex.interval( 0, i ) ;				
	R.put(ix, 0) ;
}
     }

     status = LAPACKE_sorgqr( getColumnOrder(A), M, N, N, 
          (FloatPointer)A.data().addressPointer(), getLda(A),
          (FloatPointer)tau.data().addressPointer()
          );
     if( status != 0 ) {
         throw new BlasException( "Failed to execute sorgqr", status ) ;
     }
 }
 
Example 19
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 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");
    }