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

The following examples show how to use org.nd4j.linalg.api.shape.Shape#getOffset() . 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: BaseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray putScalar(int[] indexes, double value) {
    Nd4j.getCompressor().autoDecompress(this);


    for (int i = 0; i < indexes.length; i++) {
        if (indexes[i] < 0)
            indexes[i] += rank();
    }

    if (indexes.length == 1) {
        return putScalar(indexes[0], value);
    } else if (indexes.length == 2) {
        return putScalar(indexes[0], indexes[1], value);
    } else if (indexes.length == 3) {
        return putScalar(indexes[0], indexes[1], indexes[2], value);
    } else if (indexes.length == 4) {
        return putScalar(indexes[0], indexes[1], indexes[2], indexes[3], value);
    } else {
        autoProcessScalarCall();
        long offset = Shape.getOffset(javaShapeInformation, indexes);
        data.put(offset, value);
    }
    return this;
}
 
Example 2
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray putScalar(long[] indexes, double value) {
    Nd4j.getCompressor().autoDecompress(this);


    for (int i = 0; i < indexes.length; i++) {
        if (indexes[i] < 0)
            indexes[i] += rank();
    }

    if (indexes.length == 1) {
        return putScalar(indexes[0], value);
    } else if (indexes.length == 2) {
        return putScalar(indexes[0], indexes[1], value);
    } else if (indexes.length == 3) {
        return putScalar(indexes[0], indexes[1], indexes[2], value);
    } else if (indexes.length == 4) {
        return putScalar(indexes[0], indexes[1], indexes[2], indexes[3], value);
    } else {
        autoProcessScalarCall();
        long offset = Shape.getOffset(javaShapeInformation, indexes);
        data.put(offset, value);
    }
    return this;
}
 
Example 3
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray putScalar(int[] indexes, double value) {
    Nd4j.getCompressor().autoDecompress(this);

    Preconditions.checkArgument(dataType() != DataType.BOOL || value == 0.0 || value == 1.0, "Cannot put value %s into boolean array" +
            " - only putScalar with values 0 or 1 is allowed on boolean arrays", value);

    for (int i = 0; i < indexes.length; i++) {
        if (indexes[i] < 0)
            indexes[i] += this.size(i);
    }

    if (indexes.length == 1) {
        return putScalar(indexes[0], value);
    } else if (indexes.length == 2) {
        return putScalar(indexes[0], indexes[1], value);
    } else if (indexes.length == 3) {
        return putScalar(indexes[0], indexes[1], indexes[2], value);
    } else if (indexes.length == 4) {
        return putScalar(indexes[0], indexes[1], indexes[2], indexes[3], value);
    } else {
        autoProcessScalarCall();
        long offset = Shape.getOffset(jvmShapeInfo.javaShapeInformation, indexes);
        data.put(offset, value);
    }
    return this;
}
 
Example 4
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray putScalar(long[] indexes, double value) {
    Nd4j.getCompressor().autoDecompress(this);

    Preconditions.checkArgument(dataType() != DataType.BOOL || value == 0.0 || value == 1.0, "Cannot put value %s into boolean array" +
            " - only putScalar with values 0 or 1 is allowed on boolean arrays", value);


    for (int i = 0; i < indexes.length; i++) {
        if (indexes[i] < 0)
            indexes[i] += size(i);
    }

    if (indexes.length == 1) {
        return putScalar(indexes[0], value);
    } else if (indexes.length == 2) {
        return putScalar(indexes[0], indexes[1], value);
    } else if (indexes.length == 3) {
        return putScalar(indexes[0], indexes[1], indexes[2], value);
    } else if (indexes.length == 4) {
        return putScalar(indexes[0], indexes[1], indexes[2], indexes[3], value);
    } else {
        autoProcessScalarCall();
        long offset = Shape.getOffset(jvmShapeInfo.javaShapeInformation, indexes);
        data.put(offset, value);
    }
    return this;
}
 
Example 5
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray getScalar(int[] indexes) {
    if (indexes.length > rank())
        throw new ND4JIllegalStateException("Indexes can't be longer then array rank");

    for (int i = 0; i < indexes.length; i++) {
        if (indexes[i] < 0)
            indexes[i] += this.size(i);
    }
    long idx = Shape.getOffset(jvmShapeInfo.javaShapeInformation, indexes);
    val buffer = Nd4j.createBuffer(this.data(), idx, 1);
    val shape = Nd4j.getShapeInfoProvider().createShapeInformation(new long[0], new long[0],1, 'c', this.dataType(), false);
    return Nd4j.createArrayFromShapeBuffer(buffer, shape);
}
 
Example 6
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public INDArray getScalar(long... indexes) {
    if (indexes.length > rank())
        throw new ND4JIllegalStateException("Indexes can't be longer then array rank");

    for (int i = 0; i < indexes.length; i++) {
        if (indexes[i] < 0)
            indexes[i] += this.size(i);
    }

    long idx = Shape.getOffset(jvmShapeInfo.javaShapeInformation, indexes);
    val buffer = Nd4j.createBuffer(this.data(), idx, 1);
    val shape = Nd4j.getShapeInfoProvider().createShapeInformation(new long[0], new long[0],1,'c', this.dataType(), false);
    return Nd4j.createArrayFromShapeBuffer(buffer, shape);
}
 
Example 7
Source File: StaticShapeTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testBufferToIntShapeStrideMethods() {
    //Specifically: Shape.shape(IntBuffer), Shape.shape(DataBuffer)
    //.isRowVectorShape(DataBuffer), .isRowVectorShape(IntBuffer)
    //Shape.size(DataBuffer,int), Shape.size(IntBuffer,int)
    //Also: Shape.stride(IntBuffer), Shape.stride(DataBuffer)
    //Shape.stride(DataBuffer,int), Shape.stride(IntBuffer,int)

    List<List<Pair<INDArray, String>>> lists = new ArrayList<>();
    lists.add(NDArrayCreationUtil.getAllTestMatricesWithShape(3, 4, 12345));
    lists.add(NDArrayCreationUtil.getAllTestMatricesWithShape(1, 4, 12345));
    lists.add(NDArrayCreationUtil.getAllTestMatricesWithShape(3, 1, 12345));
    lists.add(NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, 3, 4, 5));
    lists.add(NDArrayCreationUtil.getAll4dTestArraysWithShape(12345, 3, 4, 5, 6));
    lists.add(NDArrayCreationUtil.getAll4dTestArraysWithShape(12345, 3, 1, 5, 1));
    lists.add(NDArrayCreationUtil.getAll5dTestArraysWithShape(12345, 3, 4, 5, 6, 7));
    lists.add(NDArrayCreationUtil.getAll6dTestArraysWithShape(12345, 3, 4, 5, 6, 7, 8));

    val shapes = new long[][] {{3, 4}, {1, 4}, {3, 1}, {3, 4, 5}, {3, 4, 5, 6}, {3, 1, 5, 1}, {3, 4, 5, 6, 7},
                    {3, 4, 5, 6, 7, 8}};

    for (int i = 0; i < shapes.length; i++) {
        List<Pair<INDArray, String>> list = lists.get(i);
        val shape = shapes[i];

        for (Pair<INDArray, String> p : list) {
            INDArray arr = p.getFirst();

            assertArrayEquals(shape, arr.shape());

            val thisStride = arr.stride();

            val ib = arr.shapeInfo();
            DataBuffer db = arr.shapeInfoDataBuffer();

            //Check shape calculation
            assertEquals(shape.length, Shape.rank(ib));
            assertEquals(shape.length, Shape.rank(db));

            assertArrayEquals(shape, Shape.shape(ib));
            assertArrayEquals(shape, Shape.shape(db));

            for (int j = 0; j < shape.length; j++) {
                assertEquals(shape[j], Shape.size(ib, j));
                assertEquals(shape[j], Shape.size(db, j));

                assertEquals(thisStride[j], Shape.stride(ib, j));
                assertEquals(thisStride[j], Shape.stride(db, j));
            }

            //Check base offset
            assertEquals(Shape.offset(ib), Shape.offset(db));

            //Check offset calculation:
            NdIndexIterator iter = new NdIndexIterator(shape);
            while (iter.hasNext()) {
                val next = iter.next();
                long offset1 = Shape.getOffset(ib, next);

                assertEquals(offset1, Shape.getOffset(db, next));

                switch (shape.length) {
                    case 2:
                        assertEquals(offset1, Shape.getOffset(ib, next[0], next[1]));
                        assertEquals(offset1, Shape.getOffset(db, next[0], next[1]));
                        break;
                    case 3:
                        assertEquals(offset1, Shape.getOffset(ib, next[0], next[1], next[2]));
                        assertEquals(offset1, Shape.getOffset(db, next[0], next[1], next[2]));
                        break;
                    case 4:
                        assertEquals(offset1, Shape.getOffset(ib, next[0], next[1], next[2], next[3]));
                        assertEquals(offset1, Shape.getOffset(db, next[0], next[1], next[2], next[3]));
                        break;
                    case 5:
                    case 6:
                        //No 5 and 6d getOffset overloads
                        break;
                    default:
                        throw new RuntimeException();
                }
            }
        }
    }
}
 
Example 8
Source File: StaticShapeTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testBufferToIntShapeStrideMethods() {
    //Specifically: Shape.shape(IntBuffer), Shape.shape(DataBuffer)
    //.isRowVectorShape(DataBuffer), .isRowVectorShape(IntBuffer)
    //Shape.size(DataBuffer,int), Shape.size(IntBuffer,int)
    //Also: Shape.stride(IntBuffer), Shape.stride(DataBuffer)
    //Shape.stride(DataBuffer,int), Shape.stride(IntBuffer,int)

    List<List<Pair<INDArray, String>>> lists = new ArrayList<>();
    lists.add(NDArrayCreationUtil.getAllTestMatricesWithShape(3, 4, 12345, DataType.DOUBLE));
    lists.add(NDArrayCreationUtil.getAllTestMatricesWithShape(1, 4, 12345, DataType.DOUBLE));
    lists.add(NDArrayCreationUtil.getAllTestMatricesWithShape(3, 1, 12345, DataType.DOUBLE));
    lists.add(NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, new long[]{3, 4, 5}, DataType.DOUBLE));
    lists.add(NDArrayCreationUtil.getAll4dTestArraysWithShape(12345, new int[]{3, 4, 5, 6}, DataType.DOUBLE));
    lists.add(NDArrayCreationUtil.getAll4dTestArraysWithShape(12345, new int[]{3, 1, 5, 1}, DataType.DOUBLE));
    lists.add(NDArrayCreationUtil.getAll5dTestArraysWithShape(12345, new int[]{3, 4, 5, 6, 7}, DataType.DOUBLE));
    lists.add(NDArrayCreationUtil.getAll6dTestArraysWithShape(12345, new int[]{3, 4, 5, 6, 7, 8}, DataType.DOUBLE));

    val shapes = new long[][] {{3, 4}, {1, 4}, {3, 1}, {3, 4, 5}, {3, 4, 5, 6}, {3, 1, 5, 1}, {3, 4, 5, 6, 7}, {3, 4, 5, 6, 7, 8}};

    for (int i = 0; i < shapes.length; i++) {
        List<Pair<INDArray, String>> list = lists.get(i);
        val shape = shapes[i];

        for (Pair<INDArray, String> p : list) {
            INDArray arr = p.getFirst();

            assertArrayEquals(shape, arr.shape());

            val thisStride = arr.stride();

            val ib = arr.shapeInfo();
            DataBuffer db = arr.shapeInfoDataBuffer();

            //Check shape calculation
            assertEquals(shape.length, Shape.rank(ib));
            assertEquals(shape.length, Shape.rank(db));

            assertArrayEquals(shape, Shape.shape(ib));
            assertArrayEquals(shape, Shape.shape(db));

            for (int j = 0; j < shape.length; j++) {
                assertEquals(shape[j], Shape.size(ib, j));
                assertEquals(shape[j], Shape.size(db, j));

                assertEquals(thisStride[j], Shape.stride(ib, j));
                assertEquals(thisStride[j], Shape.stride(db, j));
            }

            //Check base offset
            assertEquals(Shape.offset(ib), Shape.offset(db));

            //Check offset calculation:
            NdIndexIterator iter = new NdIndexIterator(shape);
            while (iter.hasNext()) {
                val next = iter.next();
                long offset1 = Shape.getOffset(ib, next);

                assertEquals(offset1, Shape.getOffset(db, next));

                switch (shape.length) {
                    case 2:
                        assertEquals(offset1, Shape.getOffset(ib, next[0], next[1]));
                        assertEquals(offset1, Shape.getOffset(db, next[0], next[1]));
                        break;
                    case 3:
                        assertEquals(offset1, Shape.getOffset(ib, next[0], next[1], next[2]));
                        assertEquals(offset1, Shape.getOffset(db, next[0], next[1], next[2]));
                        break;
                    case 4:
                        assertEquals(offset1, Shape.getOffset(ib, next[0], next[1], next[2], next[3]));
                        assertEquals(offset1, Shape.getOffset(db, next[0], next[1], next[2], next[3]));
                        break;
                    case 5:
                    case 6:
                        //No 5 and 6d getOffset overloads
                        break;
                    default:
                        throw new RuntimeException();
                }
            }
        }
    }
}