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

The following examples show how to use org.nd4j.linalg.api.shape.Shape#stride() . 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: ShapeBufferTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testShape() {
    long[] shape = {2, 4};
    long[] stride = {1, 2};
    val shapeInfoBuffer = Shape.createShapeInformation(shape, stride, 1, 'c', DataType.DOUBLE, false);
    val buff = shapeInfoBuffer.asNioLong();
    val shapeView = Shape.shapeOf(buff);
    assertTrue(Shape.contentEquals(shape, shapeView));
    val strideView = Shape.stride(buff);
    assertTrue(Shape.contentEquals(stride, strideView));
    assertEquals('c', Shape.order(buff));
    assertEquals(1, Shape.elementWiseStride(buff));
    assertFalse(Shape.isVector(buff));
    assertTrue(Shape.contentEquals(shape, Shape.shapeOf(buff)));
    assertTrue(Shape.contentEquals(stride, Shape.stride(buff)));
}
 
Example 2
Source File: ShapeBufferTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testShape() {
    int[] shape = {2, 4};
    int[] stride = {1, 2};
    IntBuffer buff = Shape.createShapeInformation(shape, stride, 0, 1, 'c').asNioInt();
    IntBuffer shapeView = Shape.shapeOf(buff);
    assertTrue(Shape.contentEquals(shape, shapeView));
    IntBuffer strideView = Shape.stride(buff);
    assertTrue(Shape.contentEquals(stride, strideView));
    assertEquals('c', Shape.order(buff));
    assertEquals(1, Shape.elementWiseStride(buff));
    assertFalse(Shape.isVector(buff));
    assertTrue(Shape.contentEquals(shape, Shape.shapeOf(buff)));
    assertTrue(Shape.contentEquals(stride, Shape.stride(buff)));
}
 
Example 3
Source File: JvmShapeInfo.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public JvmShapeInfo(@NonNull long[] javaShapeInformation) {
    this.javaShapeInformation = javaShapeInformation;
    this.shape = Shape.shape(javaShapeInformation);
    this.stride = Shape.stride(javaShapeInformation);
    this.length = Shape.isEmpty(javaShapeInformation) ? 0 : Shape.length(javaShapeInformation);
    this.ews = Shape.elementWiseStride(javaShapeInformation);
    this.extras = Shape.extras(javaShapeInformation);
    this.order = Shape.order(javaShapeInformation);
    this.rank = Shape.rank(javaShapeInformation);
}
 
Example 4
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
protected DataBuffer strideOf() {
    //        if (stride == null)
    //            stride = Shape.stride(shapeInfoDataBuffer());
    //        return stride;
    return Shape.stride(shapeInfoDataBuffer());
}
 
Example 5
Source File: BaseSparseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
protected DataBuffer strideOf() {
    if (stride == null)
        stride = Shape.stride(shapeInfoDataBuffer());
    return stride;
}
 
Example 6
Source File: BaseSparseNDArray.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public long[] stride() {
    return Shape.stride(javaShapeInformation);
}
 
Example 7
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Override
public INDArray tensorAlongDimension(long index, int... dimension) {
    if (dimension == null || dimension.length == 0)
        throw new IllegalArgumentException("Invalid input: dimensions not specified (null or length 0)");

    Preconditions.checkArgument(!this.isEmpty(), "tensorAlongDimension(...) can't be used on empty tensors");

    if (dimension.length >= rank()  || dimension.length == 1 && dimension[0] == Integer.MAX_VALUE)
        return this;
    for (int i = 0; i < dimension.length; i++)
        if (dimension[i] < 0)
            dimension[i] += rank();

    //dedup
    if (dimension.length > 1)
        dimension = Ints.toArray(new ArrayList<>(new TreeSet<>(Ints.asList(dimension))));

    if (dimension.length > 1) {
        Arrays.sort(dimension);
    }

    long tads = tensorsAlongDimension(dimension);
    if (index >= tads)
        throw new IllegalArgumentException("Illegal index " + index + " out of tads " + tads);


    if (dimension.length == 1) {
        if (dimension[0] == 0 && isColumnVector()) {
            return this.transpose();
        } else if (dimension[0] == 1 && isRowVector()) {
            return this;
        }
    }

    Pair<DataBuffer, DataBuffer> tadInfo = Nd4j.getExecutioner().getTADManager().getTADOnlyShapeInfo(this, dimension);
    DataBuffer shapeInfo = tadInfo.getFirst();
    val jShapeInfo = shapeInfo.asLong();
    val shape = Shape.shape(jShapeInfo);
    val stride = Shape.stride(jShapeInfo);
    long offset = offset() + tadInfo.getSecond().getLong(index);
    val ews = shapeInfo.getLong(jShapeInfo[0] * 2 + 2);
    char tadOrder = (char) shapeInfo.getInt(jShapeInfo[0] * 2 + 3);
    val toTad = Nd4j.create(data(), shape, stride, offset, ews, tadOrder);
    return toTad;
}
 
Example 8
Source File: BaseNDArray.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
protected DataBuffer strideOf() {
    //        if (stride == null)
    //            stride = Shape.stride(shapeInfoDataBuffer());
    //        return stride;
    return Shape.stride(shapeInfoDataBuffer());
}
 
Example 9
Source File: BaseNDArray.java    From nd4j with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the stride(indices along the linear index for which each slice is accessed) of this array
 *
 * @return the stride of this array
 */
@Override
public long[] stride() {
    return Shape.stride(javaShapeInformation);
}