在对涉及到使用NDArray类型的信息的Debug中,总是会对debug信息里面出现的shapeInfo的长度刚到疑惑。
经过阅读源码发现:
/**
* Creates the shape information buffer
* given the shape,stride
* @param shape the shape for the buffer
* @param stride the stride for the buffer
* @param offset the offset for the buffer
* @param elementWiseStride the element wise stride for the buffer
* @param order the order for the buffer
* @return the shape information buffer given the parameters
*/
public static DataBuffer createShapeInformation(int[] shape, int[] stride, long offset, int elementWiseStride,
char order) {
if (shape.length != stride.length)
throw new IllegalStateException("Shape and stride must be the same length");
int rank = shape.length;
int shapeBuffer[] = new int[rank * 2 + 4];
shapeBuffer[0] = rank;
int count = 1;
for (int e = 0; e < shape.length; e++)
shapeBuffer[count++] = shape[e];
for (int e = 0; e < stride.length; e++)
shapeBuffer[count++] = stride[e];
shapeBuffer[count++] = (int) offset;
shapeBuffer[count++] = elementWiseStride;
shapeBuffer[count] = (int) order;
DataBuffer ret = Nd4j.createBufferDetached(shapeBuffer);
ret.setConstant(true);
return ret;
}
shapeInfo的信息如此构成:
- 长度为 rank * 2 + 4
- 第一位为 rank, 后面紧跟的数据为 数组的shape,后面为stride,再紧接着依次为 offset, elementWiseStride和order