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: (d0, d1).
For example (8, 9).
Declaration
public TensorShape(int d0, int d1)
Parameters
Type | Name | Description |
---|---|---|
int | d0 | Length of axis 0. |
int | d1 | Length of axis 1. |
TensorShape(int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 3: (d0, d1, d2).
For example (7, 8, 9).
Declaration
public TensorShape(int d0, int d1, int d2)
Parameters
Type | Name | Description |
---|---|---|
int | d0 | Length of axis 0. |
int | d1 | Length of axis 1. |
int | d2 | Length of axis 2. |
TensorShape(int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 4: (d0, d1, d2, d3).
For example (6, 7, 8, 9).
Declaration
public TensorShape(int d0, int d1, int d2, int d3)
Parameters
Type | Name | Description |
---|---|---|
int | d0 | Length of axis 0. |
int | d1 | Length of axis 1. |
int | d2 | Length of axis 2. |
int | d3 | Length of axis 3. |
TensorShape(int, int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 5: (d0, d1, d2, d3, d4).
For example (5, 6, 7, 8, 9).
Declaration
public TensorShape(int d0, int d1, int d2, int d3, int d4)
Parameters
Type | Name | Description |
---|---|---|
int | d0 | Length of axis 0. |
int | d1 | Length of axis 1. |
int | d2 | Length of axis 2. |
int | d3 | Length of axis 3. |
int | d4 | Length of axis 4. |
TensorShape(int, int, int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 6: (d0, d1, d2, d3, d4, d5).
For example (4, 5, 6, 7, 8, 9).
Declaration
public TensorShape(int d0, int d1, int d2, int d3, int d4, int d5)
Parameters
Type | Name | Description |
---|---|---|
int | d0 | Length of axis 0. |
int | d1 | Length of axis 1. |
int | d2 | Length of axis 2. |
int | d3 | Length of axis 3. |
int | d4 | Length of axis 4. |
int | d5 | Length of axis 5. |
TensorShape(int, int, int, int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 7: (d0, d1, d2, d3, d4, d5, d6).
For example (3, 4, 5, 6, 7, 8, 9).
Declaration
public TensorShape(int d0, int d1, int d2, int d3, int d4, int d5, int d6)
Parameters
Type | Name | Description |
---|---|---|
int | d0 | Length of axis 0. |
int | d1 | Length of axis 1. |
int | d2 | Length of axis 2. |
int | d3 | Length of axis 3. |
int | d4 | Length of axis 4. |
int | d5 | Length of axis 5. |
int | d6 | Length of axis 6. |
TensorShape(int, int, int, int, int, int, int, int)
Initializes and returns an instance of TensorShape
with a rank of 8: (d0, d1, d2, d3, d4, d5, d6, d7).
For example (2, 3, 4, 5, 6, 7, 8, 9).
Declaration
public TensorShape(int d0, int d1, int d2, int d3, int d4, int d5, int d6, int d7)
Parameters
Type | Name | Description |
---|---|---|
int | d0 | Length of axis 0. |
int | d1 | Length of axis 1. |
int | d2 | Length of axis 2. |
int | d3 | Length of axis 3. |
int | d4 | Length of axis 4. |
int | d5 | Length of axis 5. |
int | d6 | Length of axis 6. |
int | d7 | Length of axis 7. |
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. |
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
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. |
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()
Creates a TensorShape
by duplicating this
and flattening to a 1D shape of the same length.
For example, if this
is (2, 3, 4), the method returns (2 * 3 * 4).
Declaration
public TensorShape 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. |
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. |
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. |
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. |
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
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. |
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. |