Struct TensorShape
Represents the shape of a tensor.
Inherited Members
Namespace: Unity.Sentis
Assembly: Unity.Sentis.dll
Syntax
[Serializable]
public struct TensorShape
Constructors
TensorShape(int)
Initializes and returns an instance of TensorShape
with a rank of 1: (d0).
For example (9).
Declaration
public TensorShape(int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d0 | Length of axis 0. |
TensorShape(int, int)
Initializes and returns an instance of TensorShape
with a rank of 2: (d1, d0).
For example (8, 9).
Declaration
public TensorShape(int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d1 | Length of axis 1. |
int | d0 | Length of axis 0. |
TensorShape(int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 3: (d2, d1, d0).
For example (7, 8, 9).
Declaration
public TensorShape(int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d2 | Length of axis 2. |
int | d1 | Length of axis 1. |
int | d0 | Length of axis 0. |
TensorShape(int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 4: (d3, d2, d1, d0).
For example (6, 7, 8, 9).
Declaration
public TensorShape(int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d3 | Length of axis 3. |
int | d2 | Length of axis 2. |
int | d1 | Length of axis 1. |
int | d0 | Length of axis 0. |
TensorShape(int, int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 5: (d4, d3, d2, d1, d0).
For example (5, 6, 7, 8, 9).
Declaration
public TensorShape(int d4, int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d4 | Length of axis 4. |
int | d3 | Length of axis 3. |
int | d2 | Length of axis 2. |
int | d1 | Length of axis 1. |
int | d0 | Length of axis 0. |
TensorShape(int, int, int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 6: (d5, d4, d3, d2, d1, d0).
For example (4, 5, 6, 7, 8, 9).
Declaration
public TensorShape(int d5, int d4, int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d5 | Length of axis 5. |
int | d4 | Length of axis 4. |
int | d3 | Length of axis 3. |
int | d2 | Length of axis 2. |
int | d1 | Length of axis 1. |
int | d0 | Length of axis 0. |
TensorShape(int, int, int, int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 7: (d6, d5, d4, d3, d2, d1, d0).
For example (3, 4, 5, 6, 7, 8, 9).
Declaration
public TensorShape(int d6, int d5, int d4, int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d6 | Length of axis 6. |
int | d5 | Length of axis 5. |
int | d4 | Length of axis 4. |
int | d3 | Length of axis 3. |
int | d2 | Length of axis 2. |
int | d1 | Length of axis 1. |
int | d0 | Length of axis 0. |
TensorShape(int, int, int, int, int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 8: (d7, d6, d5, d4, d3, d2, d1, d0).
For example (2, 3, 4, 5, 6, 7, 8, 9).
Declaration
public TensorShape(int d7, int d6, int d5, int d4, int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d7 | Length of axis 7. |
int | d6 | Length of axis 6. |
int | d5 | Length of axis 5. |
int | d4 | Length of axis 4. |
int | d3 | Length of axis 3. |
int | d2 | Length of axis 2. |
int | d1 | Length of axis 1. |
int | d0 | Length of axis 0. |
TensorShape(ReadOnlySpan<int>)
Initializes and returns an instance of TensorShape
with a given shape. For example: TensorShape(new [] {3, 4, 5, 6})
returns a tensor with a shape of (3, 4, 5, 6).
Declaration
public TensorShape(ReadOnlySpan<int> shape)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<int> | shape | The shape as a span. |
TensorShape(TensorShape)
Returns a copy of another TensorShape
.
Declaration
public TensorShape(TensorShape shape)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | shape | The shape to copy. |
Fields
maxRank
The maximum rank a TensorShape
can have.
Declaration
public const int maxRank = 8
Field Value
Type | Description |
---|---|
int |
Properties
this[int]
Gets or sets the tensor shape at a given axis. A negative axis counts backwards from the inner dimension.
Declaration
public int this[int axis] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | axis | The axis to get or set. |
Property Value
Type | Description |
---|---|
int |
length
The number of elements represented by the TensorShape
. For example a shape of (1, 2, 3, 4) represents 24 elements: 1 * 2 * 3 * 4
.
Declaration
public int length { get; }
Property Value
Type | Description |
---|---|
int |
rank
The rank of a TensorShape
. For example, a tensor of shape (5) has a rank of 1. A tensor of shape (7, 3, 5) has a rank of 3.
Declaration
public int rank { get; }
Property Value
Type | Description |
---|---|
int |
Methods
Axis(int)
Returns the positive axis corresponding to a given axis. Negative axes counts backwards from the inner dimension.
Declaration
public int Axis(int axis)
Parameters
Type | Name | Description |
---|---|---|
int | axis | The axis to wrap. |
Returns
Type | Description |
---|---|
int | The positive axis. |
Broadcast(TensorShape)
Creates a TensorShape
by applying numpy-style broadcasting between this
and other
.
Sentis broadcasts shapes from innermost to outermost dimensions. Two dimensions are compatible when they're equal, or one of the dimensions is 1.
Declaration
public TensorShape Broadcast(TensorShape other)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | other | The other tensor shape which which to broadcast. |
Returns
Type | Description |
---|---|
TensorShape | The broadcast tensor shape. |
BroadcastToRank(int)
Creates a TensorShape
of a given rank and with the same inner dimensions by adding outer dimensions of size 1 when needed.
For example, if the TensorShape
is (256, 256, 3), and the value of rank
is 5, the method returns (1, 1, 256, 256, 3).
Declaration
public TensorShape BroadcastToRank(int rank)
Parameters
Type | Name | Description |
---|---|---|
int | rank | The rank to which to broadcast to. |
Returns
Type | Description |
---|---|
TensorShape | The broadcast tensor shape. |
Concat(TensorShape, int)
Creates a TensorShape
by concatenating this TensorShape
with other
along a given axis. The dimensions of the shapes must be equal, except at axis
.
For example if this
is (2, 3, 4, 5), other
is (2, 2, 4, 5), and the value of axis
is 1, the method returns (2, 5, 4, 5).
Declaration
public TensorShape Concat(TensorShape other, int axis)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | other | The other tensor shape which which to concatenate. |
int | axis | The axis along which to concatenate. |
Returns
Type | Description |
---|---|
TensorShape | The concatenated tensor shape. |
Equals(object)
Determines whether the specified object is equal to the current TensorShape
.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object to compare to the shape. |
Returns
Type | Description |
---|---|
bool | Whether the object is equal to the shape. |
Overrides
Flatten(int)
Creates a TensorShape
by duplicating this
and reshaping to a 2D matrix. The dimensions before axis
are collapsed into the outer dimension and the remaining axes are collapsed into the inner dimension.
For example, if this
is (2, 3, 4), and the value of axis
is 2, the method returns (2 * 3, 4).
Declaration
public TensorShape Flatten(int axis = 1)
Parameters
Type | Name | Description |
---|---|---|
int | axis | The axis at which to flatten. |
Returns
Type | Description |
---|---|
TensorShape | The flattened tensor shape. |
GetHashCode()
Serves as the default hash function.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | The hash code of the tensor shape. |
Overrides
HasZeroDims()
Calculates whether any axes are length 0. In this case the length is also 0.
Declaration
public bool HasZeroDims()
Returns
Type | Description |
---|---|
bool | Whether the shape has any axes that are length 0. |
Length(int)
Returns the number of elements represented by the TensorShape
, starting from a given axis. A negative axis counts backwards from the inner dimension.
Declaration
public int Length(int start)
Parameters
Type | Name | Description |
---|---|---|
int | start | The first axis to count length from. |
Returns
Type | Description |
---|---|
int | The number of elements in the shape. |
Length(int, int)
Returns the number of elements represented by the TensorShape
, between the start and end axes. Negative axes counts backwards from the inner dimension.
Declaration
public int Length(int start, int end)
Parameters
Type | Name | Description |
---|---|---|
int | start | The first axis to count length from. |
int | end | The exclusive final axis to count length to. |
Returns
Type | Description |
---|---|
int | The number of elements in the shape. |
MatMul(TensorShape)
Creates a TensorShape
that results from performing a matrix multiplication between this
and other
with numpy-style broadcasting. For example, if this
is (5, 2, 3), and other
is (1, 3, 4), the method returns (5, 2, 4).
Declaration
public TensorShape MatMul(TensorShape other)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | other | The right hand tensor shape for the MatMul. |
Returns
Type | Description |
---|---|
TensorShape | The resultant tensor shape. |
Ones(int)
Creates a TensorShape
with a given rank where all of the dimensions are 1. For example if rank
is 3, the method returns (1, 1, 1).
Declaration
public static TensorShape Ones(int rank)
Parameters
Type | Name | Description |
---|---|---|
int | rank | The rank of the tensor shape. |
Returns
Type | Description |
---|---|
TensorShape | The created tensor shape. |
Pad(ReadOnlySpan<int>)
Creates a TensorShape
by padding axes. For example, if this
is (1, 2, 3), and pads
is {0, 0, 1, 0, 2, 2}, the method returns (1, 3, 7).
Declaration
public TensorShape Pad(ReadOnlySpan<int> pads)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<int> | pads | The lower and upper padding values for each dimension. For example [pad_left, pad_right] for 1D, or [pad_top, pad_bottom, pad_left, pad_right] for 2D. |
Returns
Type | Description |
---|---|
TensorShape | The padded tensor shape. |
RavelIndex(int, int)
Converts the indexes of the TensorShape
into a flat index.
shape: (6,7) => 1 * (7) + 5 = 12
index: (1,5)
Declaration
public int RavelIndex(int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d1 | The index along axis 1. |
int | d0 | The index along axis 0. |
Returns
Type | Description |
---|---|
int | The raveled index. |
RavelIndex(int, int, int)
Converts the indexes of the TensorShape
into a flat index.
shape: (5,6,7) => 2 * (6*7) + 1 * (7) + 5 = 96
index: (2,1,5)
Declaration
public int RavelIndex(int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d2 | The index along axis 2. |
int | d1 | The index along axis 1. |
int | d0 | The index along axis 0. |
Returns
Type | Description |
---|---|
int | The raveled index. |
RavelIndex(int, int, int, int)
Converts the indexes of the TensorShape
into a flat index.
shape: (4,5,6,7) => 3 * (567) + 2 * (6*7) + 1 * (7) + 5 = 726
index: (3,2,1,5)
Declaration
public int RavelIndex(int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d3 | The index along axis 3. |
int | d2 | The index along axis 2. |
int | d1 | The index along axis 1. |
int | d0 | The index along axis 0. |
Returns
Type | Description |
---|---|
int | The raveled index. |
RavelIndex(int, int, int, int, int)
Converts the indexes of the TensorShape
into a flat index.
shape: (3,4,5,6,7) => 0 * (4567) + 3 * (567) + 2 * (67) + 1 * (7) + 5 = 726
index: (0,3,2,1,5)
Declaration
public int RavelIndex(int d4, int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d4 | The index along axis 4. |
int | d3 | The index along axis 3. |
int | d2 | The index along axis 2. |
int | d1 | The index along axis 1. |
int | d0 | The index along axis 0. |
Returns
Type | Description |
---|---|
int | The raveled index. |
RavelIndex(int, int, int, int, int, int)
Converts the indexes of the TensorShape
into a flat index.
shape: (2,3,4,5,6,7) => 1 * (34567) + 0 * (4567) + 3 * (567) + 2 * (67) + 1 * (7) + 5 = 3246
index: (1,0,3,2,1,5)
Declaration
public int RavelIndex(int d5, int d4, int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d5 | The index along axis 5. |
int | d4 | The index along axis 4. |
int | d3 | The index along axis 3. |
int | d2 | The index along axis 2. |
int | d1 | The index along axis 1. |
int | d0 | The index along axis 0. |
Returns
Type | Description |
---|---|
int | The raveled index. |
RavelIndex(int, int, int, int, int, int, int)
Converts the indexes of the TensorShape
into a flat index.
shape: (1,2,3,4,5,6,7) => 0 * (234567) + 1 * (34567) + 0 * (4567) + 3 * (567) + 2 * (6*7) + 1 * (7) + 5 = 3246
index: (0,1,0,3,2,1,5)
Declaration
public int RavelIndex(int d6, int d5, int d4, int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d6 | The index along axis 6. |
int | d5 | The index along axis 5. |
int | d4 | The index along axis 4. |
int | d3 | The index along axis 3. |
int | d2 | The index along axis 2. |
int | d1 | The index along axis 1. |
int | d0 | The index along axis 0. |
Returns
Type | Description |
---|---|
int | The raveled index. |
RavelIndex(int, int, int, int, int, int, int, int)
Converts the indexes of the TensorShape
into a flat index.
shape: (5,1,2,3,4,5,6,7) => 2 * (1234567) + 0 * (234567) + 1 * (34567) + 0 * (4567) + 3 * (567) + 2 * (6*7) + 1 * (7) + 5 = 13326
index: (2,0,1,0,3,2,1,5)
Declaration
public int RavelIndex(int d7, int d6, int d5, int d4, int d3, int d2, int d1, int d0)
Parameters
Type | Name | Description |
---|---|---|
int | d7 | The index along axis 7. |
int | d6 | The index along axis 6. |
int | d5 | The index along axis 5. |
int | d4 | The index along axis 4. |
int | d3 | The index along axis 3. |
int | d2 | The index along axis 2. |
int | d1 | The index along axis 1. |
int | d0 | The index along axis 0. |
Returns
Type | Description |
---|---|
int | The raveled index. |
Reduce(int, bool)
Removes a dimension at axis
. For example, if this
is (2, 3, 4, 5), the value of axis
is 1, and the value of keepDim
is true
, the method returns (2, 1, 4, 5).
Declaration
public TensorShape Reduce(int axis, bool keepDim = true)
Parameters
Type | Name | Description |
---|---|---|
int | axis | The axis along which to reduce. |
bool | keepDim | When the value is |
Returns
Type | Description |
---|---|
TensorShape | The reduced tensor shape. |
Reduce(ReadOnlySpan<int>, bool)
Creates a TensorShape
by removing the dimensions at axes
. For example, if this
is (2, 3, 4, 5), axis
is {1, 2} and the value of keepDim
is true
, the method returns (2, 1, 1, 5).
Declaration
public TensorShape Reduce(ReadOnlySpan<int> axes, bool keepDim = true)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<int> | axes | The axes along which to reduce. |
bool | keepDim | When the value is |
Returns
Type | Description |
---|---|
TensorShape | The reduced tensor shape. |
Reshape(ReadOnlySpan<int>, bool)
Creates a TensorShape
by duplicating this
and reshaping the dimensions to those given.
If a dimension in the shape array is -1, Sentis infers the value from the size of the TensorShape
and the remaining dimensions. Only one dimension can be -1.
Declaration
public TensorShape Reshape(ReadOnlySpan<int> shape, bool allowZero = false)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<int> | shape | The new shape as a span of integers. |
bool | allowZero | When the value is |
Returns
Type | Description |
---|---|
TensorShape |
Slice(ReadOnlySpan<int>, ReadOnlySpan<int>, ReadOnlySpan<int>, ReadOnlySpan<int>)
Creates a TensorShape
that results from slicing this
along given axes with given starts, ends, and steps.
Declaration
public TensorShape Slice(ReadOnlySpan<int> starts, ReadOnlySpan<int> ends, ReadOnlySpan<int> axes, ReadOnlySpan<int> steps)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<int> | starts | The start indices along each of the |
ReadOnlySpan<int> | ends | The end indices along each of the |
ReadOnlySpan<int> | axes | The optional axes along which to slice. The default value is [0, 1, 2...rank-1]. |
ReadOnlySpan<int> | steps | The optional step sizes for each of the |
Returns
Type | Description |
---|---|
TensorShape | The sliced tensor shape. |
Squeeze()
Creates a TensorShape
by duplicating this
and removing the dimensions of size 1. For example, if this
is (5, 1, 3, 1), the method returns (5, 3).
Declaration
public TensorShape Squeeze()
Returns
Type | Description |
---|---|
TensorShape | The squeezed tensor shape. |
Squeeze(int)
Creates a TensorShape
by duplicating this
and removing the given axis of size 1. For example, if this
is (5, 1, 3, 1), and axis
is 1, the method returns (5, 3, 1).
Declaration
public TensorShape Squeeze(int axis)
Parameters
Type | Name | Description |
---|---|---|
int | axis | The axis to squeeze. |
Returns
Type | Description |
---|---|
TensorShape | The squeezed tensor shape. |
Squeeze(ReadOnlySpan<int>)
Creates a TensorShape
by duplicating this
and removing the given axes of size 1. For example, if this
is (5, 1, 3, 1), and axes
is {1, -1}, the method returns (5, 3).
Declaration
public TensorShape Squeeze(ReadOnlySpan<int> axes)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<int> | axes | The axes to squeeze. |
Returns
Type | Description |
---|---|
TensorShape | The squeezed tensor shape. |
Strides(int)
Returns the product of the dimensions of the tensor shape after a given axis. Negative axes counts backwards from the inner dimension.
The strides of a tensor tell us how many elements we have to skip in flattened memory to move to the next position along a given index.
Declaration
public int Strides(int axis)
Parameters
Type | Name | Description |
---|---|---|
int | axis | The axis to calculate the stride length at. |
Returns
Type | Description |
---|---|
int | The stride length at the axis. |
Tile(ReadOnlySpan<int>)
Creates a TensorShape
by repeating this TensorShape
a number of times along each axis.
Declaration
public TensorShape Tile(ReadOnlySpan<int> repeats)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<int> | repeats | The repeat counts along each axis as a span of integers. |
Returns
Type | Description |
---|---|
TensorShape | The tiled tensor shape. |
ToArray()
Returns the TensorShape
as an array of integers. For example if the TensorShape
is (5, 2, 3, 4), the method returns new[] {5, 2, 3, 4}.
Declaration
public int[] ToArray()
Returns
Type | Description |
---|---|
int[] | An integer array representation of the shape. |
ToString()
Returns a string that represents the TensorShape
.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | String representation of shape. |
Overrides
Transpose()
Creates a TensorShape
by reversing axes. For example, if this
is (6, 7, 8, 9), the method returns (9, 8, 7, 6).
Declaration
public TensorShape Transpose()
Returns
Type | Description |
---|---|
TensorShape | The transposed tensor shape. |
Transpose(ReadOnlySpan<int>)
Creates a TensorShape
by permuting axes. For example, if this
is (6, 7, 8, 9), and permutations
is {3, 0, 1, 2}, the method returns (9, 6, 7, 8).
Declaration
public TensorShape Transpose(ReadOnlySpan<int> permutations)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<int> | permutations | An array indexing the new tensor axis from the old ones. |
Returns
Type | Description |
---|---|
TensorShape | The transposed tensor shape. |
Unsqueeze(int)
Creates a TensorShape
by duplicating this
and inserting a dimension of size one at a given axis. For example if this
is (2), and the value of axis
is 0, the method returns (1, 2).
Declaration
public TensorShape Unsqueeze(int axis)
Parameters
Type | Name | Description |
---|---|---|
int | axis | The axis at which to unsqueeze. |
Returns
Type | Description |
---|---|
TensorShape | The unsqueezed tensor shape. |
Unsqueeze(ReadOnlySpan<int>)
Creates a TensorShape
by duplicating this
and inserting a dimension of size one at a given axis. For example if this
is (2), and the value of axis
is 0, the method returns (1, 2).
Declaration
public TensorShape Unsqueeze(ReadOnlySpan<int> axes)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<int> | axes | The axes at which to unsqueeze. |
Returns
Type | Description |
---|---|
TensorShape | The unsqueezed tensor shape. |
Operators
operator ==(TensorShape, TensorShape)
Compares two TensorShape
objects. Returns true
if the two objects have the same rank, and all their dimensions are equal.
Declaration
public static bool operator ==(TensorShape a, TensorShape b)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | a | The first shape to compare. |
TensorShape | b | The second shape to compare. |
Returns
Type | Description |
---|---|
bool | Whether the two shapes are equal. |
operator !=(TensorShape, TensorShape)
Compares two TensorShape
objects.
Declaration
public static bool operator !=(TensorShape a, TensorShape b)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | a | The first shape to compare. |
TensorShape | b | The second shape to compare. |
Returns
Type | Description |
---|---|
bool | Whether the two shapes are not equal. |