public final class Shape extends Object
NdArray.
A Shape defines sizes along its axes. It may contain an unknown size for one of the
axes or may be totally unknown, in which case not even the number of axes is known. If the size
of an axis is unknown, UNKNOWN_SIZE should be used as its size.
| Modifier and Type | Field and Description |
|---|---|
static long |
UNKNOWN_SIZE
The size of an unknown axis or the total unknown size for an unknown Shape.
|
| Modifier and Type | Method and Description |
|---|---|
Shape |
append(long lastDimension)
Returns a new Shape, with a new last dimension added.
|
Shape |
append(Shape other)
Returns a new Shape, with another Shapes' dimensions appended.
|
long[] |
asArray()
Returns a defensive copy of the this Shape's axes.
|
boolean |
equals(Object obj)
Equals implementation for Shapes.
|
int |
hashCode() |
boolean |
hasUnknownDimension()
Returns whether one or more dimensions of this Shape have an unknown size.
|
Shape |
head()
Returns a 1-dimensional Shape with first dimension matching the first dimension of this Shape.
|
boolean |
isMatrix()
Returns whether this Shape is the shape of a matrix
|
boolean |
isScalar()
Returns whether this Shape represents a scalar.
|
boolean |
isUnknown()
Returns whether the number of dimensions of this Shape is unknown.
|
boolean |
isVector()
Returns whether this Shape is the shape of a vector.
|
int |
numDimensions()
Returns the number of dimensions of this Shape.
|
static Shape |
of(long... dimensionSizes)
Create a Shape representing a scalar or an N-dimensional value.
|
Shape |
prepend(long firstDimension)
Returns a new Shape, with a new first dimension added.
|
Shape |
prepend(Shape other)
Returns a new Shape, with another Shape's dimensions prepended.
|
static Shape |
scalar()
Creates a Shape representing a scalar value.
|
long |
size()
Returns the total number of elements a Tensor with this Shape would have.
|
long |
size(int i)
The size of the dimension with the given index.
|
Shape |
tail()
Returns a new Shape, with this Shape's first dimension removed.
|
Shape |
take(int n)
Returns an n-dimensional Shape with the dimensions matching the first n dimensions
of this shape
|
Shape |
takeLast(int n)
Returns an n-dimensional Shape with the dimensions matching the last n dimensions
of this Shape.
|
String |
toString()
Succinct description of the Shape meant for debugging.
|
static Shape |
unknown()
Creates a Shape representing an unknown number of dimensions.
|
public static long UNKNOWN_SIZE
public static Shape unknown()
isUnknown() is true, never null.public static Shape scalar()
isScalar() is true, never null.public static Shape of(long... dimensionSizes)
Creates a Shape representing a scalar or an N-dimensional value (N being at least 1), with the provided size for each dimension. A -1 indicates that the size of the corresponding dimension is unknown. If no sizes are provided, a Shape representing a scalar is created. For example:
// A 2-element vector.
Shape vector = Shape.of(2);
// A 2x3 matrix.
Shape matrix = Shape.of(2, 3);
// A matrix with 4 columns but an unknown number of rows.
// This is typically used to indicate the shape of tensors that represent
// a variable-sized batch of values. The Shape below might represent a
// variable-sized batch of 4-element vectors.
Shape batch = Shape.of(-1, 4);
// A scalar. For readability, you should prefer calling Shape.scalar()
Shape scalar = Shape.of()
dimensionSizes - number of elements in each dimension of this shape, if any, or
UNKNOWN_SIZE if unknown.public long size()
If isUnknown() is true or hasUnknownDimension() is true,
UNKNOWN_SIZE is returned.
UNKNOWN_SIZE.public long size(int i)
If isUnknown() is true or the size of the dimension with the given index has
an unknown size, UNKNOWN_SIZE is returned.
i - the index of the dimension to get the size for. If this Shape has a known number of
dimensions, it must be < numDimensions(). The index may be negative,
in which case the position is counted from the end of the shape. E.g.:
size(-1) returns the size of the last dimension, size(-2) the size of
the second to last dimension etc.UNKNOWN_SIZE
otherwise.public int numDimensions()
public boolean hasUnknownDimension()
public boolean isScalar()
public boolean isVector()
public boolean isMatrix()
public boolean isUnknown()
public long[] asArray()
isUnknown() is true.public boolean equals(Object obj)
If either Shape has unknown dimensions (even if they are the same in both) or if either
shape has an unknown number of dimensions (even if both return true for
isUnknown()), they are not considered equal! However, a shape will always
equal itself, even if it is unknown or contains unknown dimensions.
public String toString()
public Shape head()
public Shape take(int n)
n - the number of leading dimensions to get, must be <= than numDimensions()public Shape tail()
public Shape takeLast(int n)
n - the number of trailing dimensions to get, must be <= than
numDimensions()public Shape prepend(long firstDimension)
isUnknown() must be false.firstDimension - the dimension to prependpublic Shape append(long lastDimension)
isUnknown() must be false.lastDimension - the dimension to appendpublic Shape prepend(Shape other)
isUnknown() must return false.
E.g. Shape.of(3,4).prepend(Shape.of(1,2)) => Shape.of(1,2,3,4) other - another Shape, must not be null, must not be unknownpublic Shape append(Shape other)
isUnknown() must return false.
e.g. Shape.of(3,4).append(Shape.of(1,2)) => Shape.of(3,4,1,2) other - another Shape, must not be null, must not be unknownCopyright © 2015–2020. All rights reserved.