public class Shape
extends java.lang.Object
NDArray
's shape information.Constructor and Description |
---|
Shape(java.util.List<java.lang.Long> shape)
Constructs and initializes a
Shape with specified dimension. |
Shape(long... shape)
Constructs and initializes a
Shape with specified dimension as {@code (long... |
Shape(long[] shape,
LayoutType[] layout)
Constructs and initializes a
Shape with specified dimension and layout. |
Shape(long[] shape,
java.lang.String layout)
Constructs and initializes a
Shape with specified dimension and layout. |
Shape(ai.djl.util.PairList<java.lang.Long,LayoutType> shape)
Constructs and initializes a
Shape with specified shape and layout pairList. |
Modifier and Type | Method and Description |
---|---|
Shape |
add(long... axes)
Joins this shape with axes.
|
Shape |
addAll(Shape other)
Joins this shape with specified
other shape. |
static Shape |
decode(java.io.DataInputStream dis)
Decodes the data in the given
DataInputStream and converts it into the corresponding
Shape object. |
int |
dimension()
Returns the number of dimensions of this
Shape . |
boolean |
equals(java.lang.Object o) |
Shape |
filterByLayoutType(java.util.function.Predicate<LayoutType> predicate)
Returns only the axes of the Shape whose layout types match the predicate.
|
long |
get(int dimension)
Returns the shape in the given dimension.
|
byte[] |
getEncoded()
Gets the byte array representation of this
Shape for serialization. |
LayoutType[] |
getLayout()
Returns the layout type for each axis in this shape.
|
LayoutType |
getLayoutType(int dimension)
Returns the layout type in the given dimension.
|
int |
getLeadingOnes()
Returns the number of leading ones in the array shape.
|
long[] |
getShape()
Returns the dimensions of the
Shape . |
int |
getTrailingOnes()
Returns the number of trailing ones in the array shape.
|
long |
getUnknownValueCount()
Return the count of unknown value in this
Shape . |
int |
hashCode() |
boolean |
hasZeroDimension()
Returns
true if the NDArray contains zero dimensions. |
long |
head()
Returns the head index of the shape.
|
boolean |
isLayoutKnown()
Returns
true if a layout is set. |
boolean |
isScalar()
Returns
true if the NDArray is a scalar. |
Shape |
map(java.util.function.Function<ai.djl.util.Pair<java.lang.Long,LayoutType>,ai.djl.util.Pair<java.lang.Long,LayoutType>> mapper)
Returns a mapped shape.
|
long |
size()
Returns the total size.
|
long |
size(int... dimensions)
Returns the size of a specific dimension or several specific dimensions.
|
Shape |
slice(int beginIndex)
Creates a new
Shape whose content is a slice of this shape. |
Shape |
slice(int beginIndex,
int endIndex)
Creates a new
Shape whose content is a slice of this shape. |
java.util.stream.Stream<ai.djl.util.Pair<java.lang.Long,LayoutType>> |
stream()
Returns a stream of the Shape.
|
long |
tail()
Returns the tail index of the shape.
|
java.lang.String |
toLayoutString()
Returns the string layout type for each axis in this shape.
|
java.lang.String |
toString() |
static Shape |
update(Shape shape,
int dimension,
long value)
Returns a new shape altering the given dimension.
|
public Shape(long... shape)
Shape
with specified dimension as (long...
shape)
.shape
- the dimensions of the shapejava.lang.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.public Shape(java.util.List<java.lang.Long> shape)
Shape
with specified dimension.shape
- the dimensions of the shapejava.lang.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.public Shape(ai.djl.util.PairList<java.lang.Long,LayoutType> shape)
Shape
with specified shape and layout pairList.shape
- the dimensions and layout of the shapejava.lang.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.public Shape(long[] shape, java.lang.String layout)
Shape
with specified dimension and layout.shape
- the size of each axis of the shapelayout
- the LayoutType
of each axis in the shapejava.lang.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.public Shape(long[] shape, LayoutType[] layout)
Shape
with specified dimension and layout.shape
- the size of each axis of the shapelayout
- the LayoutType
of each axis in the shapejava.lang.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.public static Shape update(Shape shape, int dimension, long value)
shape
- the shape to updatedimension
- the dimension to get the shape invalue
- the value to set the dimension topublic long[] getShape()
Shape
.Shape
public long get(int dimension)
dimension
- the dimension to get the shape inpublic LayoutType getLayoutType(int dimension)
dimension
- the dimension to get the layout type inpublic long size(int... dimensions)
dimensions
- the dimension or dimensions to find the size ofjava.lang.IllegalArgumentException
- thrown if passed an invalid dimensionpublic long size()
public int dimension()
Shape
.Shape
public long getUnknownValueCount()
Shape
.Shape
public Shape slice(int beginIndex)
Shape
whose content is a slice of this shape.
The sub shape begins at the specified beginIndex
and extends to endIndex -
1
.
beginIndex
- the beginning index, inclusiveShape
whose content is a slice of this shapepublic Shape slice(int beginIndex, int endIndex)
Shape
whose content is a slice of this shape.
The sub shape begins at the specified beginIndex
and extends to endIndex -
1
.
beginIndex
- the beginning index, inclusiveendIndex
- the ending index, exclusiveShape
whose content is a slice of this shapepublic Shape filterByLayoutType(java.util.function.Predicate<LayoutType> predicate)
predicate
- the predicate to compare the axes of the Shape withpublic Shape map(java.util.function.Function<ai.djl.util.Pair<java.lang.Long,LayoutType>,ai.djl.util.Pair<java.lang.Long,LayoutType>> mapper)
mapper
- the function to map each element of the Shape bypublic java.util.stream.Stream<ai.djl.util.Pair<java.lang.Long,LayoutType>> stream()
public Shape add(long... axes)
axes
- the axes to joinShape
public Shape addAll(Shape other)
other
shape.other
- the shape to joinShape
public long head()
java.lang.IndexOutOfBoundsException
- Thrown if the shape is emptypublic long tail()
java.lang.IndexOutOfBoundsException
- Thrown if the shape is emptypublic int getTrailingOnes()
For example, a rank 3 array with shape [10, 1, 1] would return 2 for this method
public int getLeadingOnes()
For example, a rank 3 array with shape [1, 10, 1] would return value 1 for this method
public boolean isScalar()
true
if the NDArray is a scalar.public boolean hasZeroDimension()
true
if the NDArray contains zero dimensions.public boolean isLayoutKnown()
true
if a layout is set.public LayoutType[] getLayout()
public java.lang.String toLayoutString()
public byte[] getEncoded()
Shape
for serialization.Shape
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public static Shape decode(java.io.DataInputStream dis) throws java.io.IOException
DataInputStream
and converts it into the corresponding
Shape
object.dis
- the inputstream to read fromShape
objectjava.io.IOException
- when an I/O error occurs