Shape

public final class Shape

The possibly partially known shape of a tensor produced by an operation.

Public Methods

boolean
equals(Object obj)
int
static Shape
make(long firstDimensionSize, long... otherDimensionSizes)
Create a Shape representing an N-dimensional value.
int
numDimensions()
Number of dimensions represented by this shape.
static Shape
scalar()
Create a Shape representing a scalar value.
long
size(int i)
The size of the i-th dimension.
String
toString()
Succinct description of the shape meant for debugging.
static Shape
unknown()
Create a Shape representing an unknown number of dimensions.

Inherited Methods

Public Methods

public boolean equals (Object obj)

public int hashCode ()

public static Shape make (long firstDimensionSize, long... otherDimensionSizes)

Create a Shape representing an N-dimensional value.

Creates a Shape representing 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. For example:

// A 2-element vector.
 Shape vector = Shape.create(2);

 // A 2x3 matrix.
 Shape matrix = Shape.create(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.create(-1, 4);
 

public int numDimensions ()

Number of dimensions represented by this shape.

Returns
  • -1 if the number of dimensions is unknown, 0 if the shape represents a scalar, 1 for a vector, 2 for a matrix etc.

public static Shape scalar ()

Create a Shape representing a scalar value.

public long size (int i)

The size of the i-th dimension.

Returns
  • The size of the requested dimension or -1 if it is unknown.

public String toString ()

Succinct description of the shape meant for debugging.

public static Shape unknown ()

Create a Shape representing an unknown number of dimensions.