Package ai.djl.ndarray.types
Class Shape
java.lang.Object
ai.djl.ndarray.types.Shape
A class that presents the
NDArray's shape information.-
Constructor Summary
ConstructorsConstructorDescriptionShape(long... shape) Constructs and initializes aShapewith specified dimension as(long... shape).Shape(long[] shape, LayoutType[] layout) Constructs and initializes aShapewith specified dimension and layout.Constructs and initializes aShapewith specified dimension and layout.Shape(ai.djl.util.PairList<Long, LayoutType> shape) Constructs and initializes aShapewith specified shape and layout pairList.Constructs and initializes aShapewith specified dimension. -
Method Summary
Modifier and TypeMethodDescriptionadd(long... axes) Joins this shape with axes.Joins this shape with specifiedothershape.static Shapedecode(DataInputStream dis) Decodes the data in the givenDataInputStreamand converts it into the correspondingShapeobject.static Shapedecode(ByteBuffer bb) Decodes the data in the givenByteBufferand converts it into the correspondingShapeobject.intReturns the number of dimensions of thisShape.booleanfilterByLayoutType(Predicate<LayoutType> predicate) Returns only the axes of the Shape whose layout types match the predicate.longget(int dimension) Returns the shape in the given dimension.byte[]Gets the byte array representation of thisShapefor serialization.longReturns the last index.Returns the layout type for each axis in this shape.getLayoutType(int dimension) Returns the layout type in the given dimension.intReturns the number of leading ones in the array shape.long[]getShape()Returns the dimensions of theShape.intReturns the number of trailing ones in the array shape.longReturn the count of unknown value in thisShape.inthashCode()booleanReturnstrueif the NDArray contains zero dimensions.longhead()Returns the head index of the shape.booleanReturnstrueif a layout is set.booleanReturns if the array is rank-1 which is inferred from the shape.booleanisScalar()Returnstrueif the NDArray is a scalar.map(Function<ai.djl.util.Pair<Long, LayoutType>, ai.djl.util.Pair<Long, LayoutType>> mapper) Returns a mapped shape.parseShapes(String value) Parses a string representation of shapes for NDList.longsize()Returns the total size.longsize(int... dimensions) Returns the size of a specific dimension or several specific dimensions.slice(int beginIndex) Creates a newShapewhose content is a slice of this shape.slice(int beginIndex, int endIndex) Creates a newShapewhose content is a slice of this shape.Stream<ai.djl.util.Pair<Long,LayoutType>> stream()Returns a stream of the Shape.longtail()Returns the tail index of the shape.Returns the string layout type for each axis in this shape.toString()static ShapeReturns a new shape altering the given dimension.
-
Constructor Details
-
Shape
public Shape(long... shape) Constructs and initializes aShapewith specified dimension as(long... shape).- Parameters:
shape- the dimensions of the shape- Throws:
IllegalArgumentException- Thrown if any element in Shape is invalid. It should not be less than -1. Also thrown if the shape and layout do not have equal sizes.
-
Shape
Constructs and initializes aShapewith specified dimension.- Parameters:
shape- the dimensions of the shape- Throws:
IllegalArgumentException- Thrown if any element in Shape is invalid. It should not be less than -1. Also thrown if the shape and layout do not have equal sizes.
-
Shape
Constructs and initializes aShapewith specified shape and layout pairList.- Parameters:
shape- the dimensions and layout of the shape- Throws:
IllegalArgumentException- Thrown if any element in Shape is invalid. It should not be less than -1 .Also thrown if the shape and layout do not have equal sizes.
-
Shape
Constructs and initializes aShapewith specified dimension and layout.- Parameters:
shape- the size of each axis of the shapelayout- theLayoutTypeof each axis in the shape- Throws:
IllegalArgumentException- Thrown if any element in Shape is invalid. It should not be less than -1. Also thrown for an invalid layout. Also thrown if the shape and layout do not have equal sizes.
-
Shape
Constructs and initializes aShapewith specified dimension and layout.- Parameters:
shape- the size of each axis of the shapelayout- theLayoutTypeof each axis in the shape- Throws:
IllegalArgumentException- Thrown if any element in Shape is invalid. It should not be less than -1. Also thrown if the shape and layout do not have equal sizes.
-
-
Method Details
-
update
Returns a new shape altering the given dimension.- Parameters:
shape- the shape to updatedimension- the dimension to get the shape invalue- the value to set the dimension to- Returns:
- a new shape with the update applied
-
getShape
public long[] getShape()Returns the dimensions of theShape.- Returns:
- the dimensions of the
Shape
-
get
public long get(int dimension) Returns the shape in the given dimension.- Parameters:
dimension- the dimension to get the shape in- Returns:
- the shape in the given dimension
-
getLastDimension
public long getLastDimension()Returns the last index.- Returns:
- the last index
-
getLayoutType
Returns the layout type in the given dimension.- Parameters:
dimension- the dimension to get the layout type in- Returns:
- the layout type in the given dimension
-
size
public long size(int... dimensions) Returns the size of a specific dimension or several specific dimensions.- Parameters:
dimensions- the dimension or dimensions to find the size of- Returns:
- the size of specific dimension(s) or -1 for indeterminate size
- Throws:
IllegalArgumentException- thrown if passed an invalid dimension
-
size
public long size()Returns the total size.- Returns:
- the total size or -1 for indeterminate size
-
dimension
public int dimension()Returns the number of dimensions of thisShape.- Returns:
- the number of dimensions of this
Shape
-
getUnknownValueCount
public long getUnknownValueCount()Return the count of unknown value in thisShape.- Returns:
- the number of unknown value in this
Shape
-
slice
Creates a newShapewhose content is a slice of this shape.The sub shape begins at the specified
beginIndexand extends toendIndex - 1.- Parameters:
beginIndex- the beginning index, inclusive- Returns:
- a new
Shapewhose content is a slice of this shape
-
slice
Creates a newShapewhose content is a slice of this shape.The sub shape begins at the specified
beginIndexand extends toendIndex - 1.- Parameters:
beginIndex- the beginning index, inclusiveendIndex- the ending index, exclusive- Returns:
- a new
Shapewhose content is a slice of this shape
-
filterByLayoutType
Returns only the axes of the Shape whose layout types match the predicate.- Parameters:
predicate- the predicate to compare the axes of the Shape with- Returns:
- a new filtered Shape
-
map
public Shape map(Function<ai.djl.util.Pair<Long, LayoutType>, ai.djl.util.Pair<Long, LayoutType>> mapper) Returns a mapped shape.- Parameters:
mapper- the function to map each element of the Shape by- Returns:
- a new mapped Shape
-
stream
Returns a stream of the Shape.- Returns:
- the stream of the Shape
-
add
Joins this shape with axes.- Parameters:
axes- the axes to join- Returns:
- the joined
Shape
-
addAll
Joins this shape with specifiedothershape.- Parameters:
other- the shape to join- Returns:
- the joined
Shape
-
head
public long head()Returns the head index of the shape.- Returns:
- the head index of the shape
- Throws:
IndexOutOfBoundsException- Thrown if the shape is empty
-
tail
public long tail()Returns the tail index of the shape.- Returns:
- the tail index of the shape
- Throws:
IndexOutOfBoundsException- Thrown if the shape is empty
-
getTrailingOnes
public int getTrailingOnes()Returns the number of trailing ones in the array shape.For example, a rank 3 array with shape [10, 1, 1] would return 2 for this method
- Returns:
- the number of trailing ones in the shape
-
getLeadingOnes
public int getLeadingOnes()Returns the number of leading ones in the array shape.For example, a rank 3 array with shape [1, 10, 1] would return value 1 for this method
- Returns:
- the number of leading ones in the shape
-
isScalar
public boolean isScalar()Returnstrueif the NDArray is a scalar.- Returns:
- whether the NDArray is a scalar
-
hasZeroDimension
public boolean hasZeroDimension()Returnstrueif the NDArray contains zero dimensions.- Returns:
- whether the NDArray contain zero dimensions
-
isLayoutKnown
public boolean isLayoutKnown()Returnstrueif a layout is set.- Returns:
- whether a layout has been set
-
getLayout
Returns the layout type for each axis in this shape.- Returns:
- the layout type for each axis in this shape
-
toLayoutString
Returns the string layout type for each axis in this shape.- Returns:
- the string layout type for each axis in this shape
-
getEncoded
public byte[] getEncoded()Gets the byte array representation of thisShapefor serialization.- Returns:
- a byte array representation of this
Shape
-
equals
-
hashCode
public int hashCode() -
toString
-
decode
Decodes the data in the givenDataInputStreamand converts it into the correspondingShapeobject.- Parameters:
dis- the inputstream to read from- Returns:
- the corresponding
Shapeobject - Throws:
IOException- when an I/O error occurs
-
decode
Decodes the data in the givenByteBufferand converts it into the correspondingShapeobject.- Parameters:
bb- the ByteBuffer to read from- Returns:
- the corresponding
Shapeobject
-
isRankOne
public boolean isRankOne()Returns if the array is rank-1 which is inferred from the shape.For example, an array with shape [1, 10, 1] returns true. Array with indeterminate size -1 returns false.
- Returns:
- if the array is rank-1
-
parseShapes
Parses a string representation of shapes for NDList.- Parameters:
value- a string representation of shapes for NDList- Returns:
- a list of Shape and datatype pairs
-