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

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#getRows() . 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: IndexingTestsC.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetIndices2d() throws Exception {
    INDArray twoByTwo = Nd4j.linspace(1, 6, 6).reshape(3, 2);
    INDArray firstRow = twoByTwo.getRow(0);
    INDArray secondRow = twoByTwo.getRow(1);
    INDArray firstAndSecondRow = twoByTwo.getRows(new int[] {1, 2});
    INDArray firstRowViaIndexing = twoByTwo.get(interval(0, 1));
    assertEquals(firstRow, firstRowViaIndexing);
    INDArray secondRowViaIndexing = twoByTwo.get(point(1));
    assertEquals(secondRow, secondRowViaIndexing);

    INDArray firstAndSecondRowTest = twoByTwo.get(interval(1, 3));
    assertEquals(firstAndSecondRow, firstAndSecondRowTest);

    INDArray individualElement = twoByTwo.get(interval(1, 2), interval(1, 2));
    assertEquals(Nd4j.create(new float[] {4}), individualElement);
}
 
Example 2
Source File: ShapeTestC.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testToOffsetZero() {
    INDArray matrix = Nd4j.rand(3, 5);
    INDArray rowOne = matrix.getRow(1);
    INDArray row1Copy = Shape.toOffsetZero(rowOne);
    assertEquals(rowOne, row1Copy);
    INDArray rows = matrix.getRows(1, 2);
    INDArray rowsOffsetZero = Shape.toOffsetZero(rows);
    assertEquals(rows, rowsOffsetZero);

    INDArray tensor = Nd4j.rand(new int[] {3, 3, 3});
    INDArray getTensor = tensor.slice(1).slice(1);
    INDArray getTensorZero = Shape.toOffsetZero(getTensor);
    assertEquals(getTensor, getTensorZero);


}
 
Example 3
Source File: ShapeTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testToOffsetZero() {
    INDArray matrix = Nd4j.rand(3, 5);
    INDArray rowOne = matrix.getRow(1);
    INDArray row1Copy = Shape.toOffsetZero(rowOne);
    assertEquals(rowOne, row1Copy);
    INDArray rows = matrix.getRows(1, 2);
    INDArray rowsOffsetZero = Shape.toOffsetZero(rows);
    assertEquals(rows, rowsOffsetZero);

    INDArray tensor = Nd4j.rand(new int[] {3, 3, 3});
    INDArray getTensor = tensor.slice(1).slice(1);
    INDArray getTensorZero = Shape.toOffsetZero(getTensor);
    assertEquals(getTensor, getTensorZero);



}
 
Example 4
Source File: IndexingTestsC.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetIndices2d() {
    INDArray twoByTwo = Nd4j.linspace(1, 6, 6, DataType.DOUBLE).reshape(3, 2);
    INDArray firstRow = twoByTwo.getRow(0);
    INDArray secondRow = twoByTwo.getRow(1);
    INDArray firstAndSecondRow = twoByTwo.getRows(1, 2);
    INDArray firstRowViaIndexing = twoByTwo.get(interval(0, 1), NDArrayIndex.all());
    assertEquals(firstRow.reshape(1,2), firstRowViaIndexing);
    INDArray secondRowViaIndexing = twoByTwo.get(point(1), NDArrayIndex.all());
    assertEquals(secondRow, secondRowViaIndexing);

    INDArray firstAndSecondRowTest = twoByTwo.get(interval(1, 3), NDArrayIndex.all());
    assertEquals(firstAndSecondRow, firstAndSecondRowTest);

    INDArray individualElement = twoByTwo.get(interval(1, 2), interval(1, 2));
    assertEquals(Nd4j.create(new double[] {4}, new int[]{1,1}), individualElement);
}
 
Example 5
Source File: ShapeTestC.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testToOffsetZero() {
    INDArray matrix = Nd4j.rand(3, 5);
    INDArray rowOne = matrix.getRow(1);
    INDArray row1Copy = Shape.toOffsetZero(rowOne);
    assertEquals(rowOne, row1Copy);
    INDArray rows = matrix.getRows(1, 2);
    INDArray rowsOffsetZero = Shape.toOffsetZero(rows);
    assertEquals(rows, rowsOffsetZero);

    INDArray tensor = Nd4j.rand(new int[] {3, 3, 3});
    INDArray getTensor = tensor.slice(1).slice(1);
    INDArray getTensorZero = Shape.toOffsetZero(getTensor);
    assertEquals(getTensor, getTensorZero);


}
 
Example 6
Source File: ShapeTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testToOffsetZero() {
    INDArray matrix = Nd4j.rand(3, 5);
    INDArray rowOne = matrix.getRow(1);
    INDArray row1Copy = Shape.toOffsetZero(rowOne);
    assertEquals(rowOne, row1Copy);
    INDArray rows = matrix.getRows(1, 2);
    INDArray rowsOffsetZero = Shape.toOffsetZero(rows);
    assertEquals(rows, rowsOffsetZero);

    INDArray tensor = Nd4j.rand(new int[] {3, 3, 3});
    INDArray getTensor = tensor.slice(1).slice(1);
    INDArray getTensorZero = Shape.toOffsetZero(getTensor);
    assertEquals(getTensor, getTensorZero);



}
 
Example 7
Source File: Nd4jGetAndSetParts.java    From dl4j-tutorials with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    INDArray nd = Nd4j.create(new float[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, new int[]{2, 6});
    System.out.println("原始数组");
    System.out.println(nd);

    /*
        获取一行
     */
    System.out.println("获取数组中的一行");
    INDArray singleRow = nd.getRow(0);
    System.out.println(singleRow);

    /*
        获取多行
     */
    System.out.println("获取数组中的多行");
    INDArray multiRows = nd.getRows(0, 1);
    System.out.println(multiRows);

    /*
        替换其中的一行
     */
    System.out.println("替换原有数组中的一行");
    INDArray replaceRow = Nd4j.create(new float[]{1, 3, 5, 7, 9, 11});
    nd.putRow(0, replaceRow);
    System.out.println(nd);
}
 
Example 8
Source File: IndexingTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRow() {
    Nd4j.getRandom().setSeed(12345);
    INDArray in = Nd4j.linspace(0, 14, 15).reshape(3, 5);
    int[] toGet = {0, 1};
    INDArray out = in.getRows(toGet);
    assertEquals(in.getRow(0), out.getRow(0));
    assertEquals(in.getRow(1), out.getRow(1));

    int[] toGet2 = {0, 1, 2, 0, 1, 2};
    INDArray out2 = in.getRows(toGet2);
    for (int i = 0; i < toGet2.length; i++) {
        assertEquals(in.getRow(toGet2[i]), out2.getRow(i));
    }
}
 
Example 9
Source File: IndexingTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRowsColumnsMatrix() {
    INDArray arr = Nd4j.linspace(1, 24, 24).reshape(4, 6);
    INDArray firstAndSecondColumnsAssertion = Nd4j.create(new double[][] {{1, 5}, {2, 6}, {3, 7}, {4, 8}});

    System.out.println(arr);
    INDArray firstAndSecondColumns = arr.getColumns(0, 1);
    assertEquals(firstAndSecondColumnsAssertion, firstAndSecondColumns);

    INDArray firstAndSecondRows = Nd4j.create(new double[][] {{1.00, 5.00, 9.00, 13.00, 17.00, 21.00},
            {1.00, 5.00, 9.00, 13.00, 17.00, 21.00}, {2.00, 6.00, 10.00, 14.00, 18.00, 22.00}});

    INDArray rows = arr.getRows(new int[] {0, 0, 1});
    assertEquals(firstAndSecondRows, rows);
}
 
Example 10
Source File: IndexingTestsC.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRow() {
    Nd4j.getRandom().setSeed(12345);
    INDArray in = Nd4j.linspace(0, 14, 15, DataType.DOUBLE).reshape(3, 5);
    int[] toGet = {0, 1};
    INDArray out = in.getRows(toGet);
    assertEquals(in.getRow(0), out.getRow(0));
    assertEquals(in.getRow(1), out.getRow(1));

    int[] toGet2 = {0, 1, 2, 0, 1, 2};
    INDArray out2 = in.getRows(toGet2);
    for (int i = 0; i < toGet2.length; i++) {
        assertEquals(in.getRow(toGet2[i]), out2.getRow(i));
    }
}
 
Example 11
Source File: IndexingTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testGetRowsColumnsMatrix() {
        INDArray arr = Nd4j.linspace(1, 24, 24, DataType.DOUBLE).reshape(4, 6);
        INDArray firstAndSecondColumnsAssertion = Nd4j.create(new double[][] {{1, 5}, {2, 6}, {3, 7}, {4, 8}});

//        System.out.println(arr);
        INDArray firstAndSecondColumns = arr.getColumns(0, 1);
        assertEquals(firstAndSecondColumnsAssertion, firstAndSecondColumns);

        INDArray firstAndSecondRows = Nd4j.create(new double[][] {{1.00, 5.00, 9.00, 13.00, 17.00, 21.00},
                {1.00, 5.00, 9.00, 13.00, 17.00, 21.00}, {2.00, 6.00, 10.00, 14.00, 18.00, 22.00}});

        INDArray rows = arr.getRows(0, 0, 1);
        assertEquals(firstAndSecondRows, rows);
    }