Struct TensorShape
TensorShape are immutable representation of a Tensor dimensions and rank. At the moment a TensorShape is always of rank 4 and channels last ie NHWC. However an axis can be of size 1. For example a tensor without spatial information will be N,1,1,C
Inherited Members
Namespace: Unity.Barracuda
Assembly: Unity.Barracuda.dll
Syntax
[Serializable]
public struct TensorShape
Constructors
TensorShape(int)
Create a TensorShape of shape [1,1,N,1,1,1,1,1].
Declaration
public TensorShape(int n)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
TensorShape(int, int)
Create a TensorShape of shape [1,1,N,1,1,1,1,C].
Declaration
public TensorShape(int n, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | c | channels |
TensorShape(int, int, int)
Create a TensorShape of shape [1,1,N,1,1,1,W,C].
Declaration
public TensorShape(int n, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | w | width |
| int | c | channels |
TensorShape(int, int, int, int)
Create a TensorShape of shape [1,1,N,1,1,H,W,C].
Declaration
public TensorShape(int n, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | h | height |
| int | w | width |
| int | c | channels |
TensorShape(int, int, int, int, int)
Create a TensorShape of shape [1,1,N,1,D,H,W,C].
Declaration
public TensorShape(int n, int d, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | d | depth |
| int | h | height |
| int | w | width |
| int | c | channels |
TensorShape(int, int, int, int, int, int, int, int)
Create a TensorShape of shape [S,R,N,T,D,H,W,C]. Currently seqLen must be 1.
Declaration
public TensorShape(int s, int r, int n, int t, int d, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | s | sequence |
| int | r | direction |
| int | n | batch |
| int | t | time |
| int | d | depth |
| int | h | height |
| int | w | width |
| int | c | channels |
TensorShape(int[])
Create a TensorShape of arbitrary shape.
shape must be of length 4 [N,H,W,C] or 8 [S,R,N,T,D,H,W,C].
If shape.length is 4 then the dimensions S,R,T and D will be defaulted to 1.
Declaration
public TensorShape(int[] shape)
Parameters
| Type | Name | Description |
|---|---|---|
| int[] | shape | shape as int array |
Fields
C
Channels dimension index number
Declaration
public const int C = 7
Field Value
| Type | Description |
|---|---|
| int |
D
Depth dimension index number
Declaration
public const int D = 4
Field Value
| Type | Description |
|---|---|
| int |
DataBatch
Batch dimension index number
Declaration
public const int DataBatch = 2
Field Value
| Type | Description |
|---|---|
| int |
DataChannel
Data channel dimension index number
Declaration
public const int DataChannel = 7
Field Value
| Type | Description |
|---|---|
| int |
DataFeature0
Data feature 0 dimension index number
Declaration
public const int DataFeature0 = 6
Field Value
| Type | Description |
|---|---|
| int |
DataFeature1
Data feature 1 dimension index number
Declaration
public const int DataFeature1 = 5
Field Value
| Type | Description |
|---|---|
| int |
DataFeature2
Data feature 2 dimension index number
Declaration
public const int DataFeature2 = 4
Field Value
| Type | Description |
|---|---|
| int |
DataFeature3
Data feature 3 dimension index number
Declaration
public const int DataFeature3 = 3
Field Value
| Type | Description |
|---|---|
| int |
DataFeatures
Data features
Declaration
public static readonly int[] DataFeatures
Field Value
| Type | Description |
|---|---|
| int[] |
H
Height dimension index number
Declaration
public const int H = 5
Field Value
| Type | Description |
|---|---|
| int |
KernelInChannel
Kernel input channel dimension
Declaration
public const int KernelInChannel = 6
Field Value
| Type | Description |
|---|---|
| int |
KernelOutChannel
Kernel output channel dimension
Declaration
public const int KernelOutChannel = 7
Field Value
| Type | Description |
|---|---|
| int |
KernelSpatial0
Kernel spatial dimension 0
Declaration
public const int KernelSpatial0 = 5
Field Value
| Type | Description |
|---|---|
| int |
KernelSpatial1
Kernel spatial dimension 1
Declaration
public const int KernelSpatial1 = 2
Field Value
| Type | Description |
|---|---|
| int |
KernelSpatial2
Kernel spatial dimension 2
Declaration
public const int KernelSpatial2 = 1
Field Value
| Type | Description |
|---|---|
| int |
KernelSpatial3
Kernel spatial dimension 3
Declaration
public const int KernelSpatial3 = 0
Field Value
| Type | Description |
|---|---|
| int |
KernelSpatials
Kernel spatial dimensions
Declaration
public static readonly int[] KernelSpatials
Field Value
| Type | Description |
|---|---|
| int[] |
MaxRank
Max rank
Declaration
public const int MaxRank = 8
Field Value
| Type | Description |
|---|---|
| int |
NumberOfDirections
Sequence length dimension index number
Declaration
public const int NumberOfDirections = 1
Field Value
| Type | Description |
|---|---|
| int |
SequenceLength
Sequence length dimension index number
Declaration
public const int SequenceLength = 0
Field Value
| Type | Description |
|---|---|
| int |
W
Width dimension index number
Declaration
public const int W = 6
Field Value
| Type | Description |
|---|---|
| int |
batch
Return the number of batch.
Declaration
public readonly int batch
Field Value
| Type | Description |
|---|---|
| int |
channels
Return the number of channels.
Declaration
public readonly int channels
Field Value
| Type | Description |
|---|---|
| int |
depth
Return the spatial depth (axis is DataFeature2).
Declaration
public readonly int depth
Field Value
| Type | Description |
|---|---|
| int |
height
Return the spatial height (axis is DataFeature1).
Declaration
public readonly int height
Field Value
| Type | Description |
|---|---|
| int |
numberOfDirections
Return the number of direction.
Declaration
public readonly int numberOfDirections
Field Value
| Type | Description |
|---|---|
| int |
sequenceLength
Return the number of sequence.
Declaration
public readonly int sequenceLength
Field Value
| Type | Description |
|---|---|
| int |
width
Return the spatial width (axis is DataFeature0).
Declaration
public readonly int width
Field Value
| Type | Description |
|---|---|
| int |
Properties
this[int]
Indexer to return a dimension of this tensorShape as [S,R,N,T,D,H,W,C] Prefer this over ToArray() to avoid GC allocation/collection.
Declaration
public int this[int axis] { get; }
Parameters
| Type | Name | Description |
|---|---|---|
| int | axis | axis |
Property Value
| Type | Description |
|---|---|
| int |
dimensions
Return the count of non-unit dimension of this shape. For example [N,1,1,C] dimensions is 2.
Declaration
public int dimensions { get; }
Property Value
| Type | Description |
|---|---|
| int |
flatHeight
Return the number of batch.
Declaration
public int flatHeight { get; }
Property Value
| Type | Description |
|---|---|
| int |
flatWidth
Return the TDHWC.
Declaration
public int flatWidth { get; }
Property Value
| Type | Description |
|---|---|
| int |
kernelCount
Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose. Return kernel count (aka the number of output channels of the associated operator).
Declaration
public int kernelCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
kernelDepth
Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose. Return kernel depth (aka the number of input channels of the associated operator).
Declaration
public int kernelDepth { get; }
Property Value
| Type | Description |
|---|---|
| int |
kernelHeight
Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose. Return kernel height.
Declaration
public int kernelHeight { get; }
Property Value
| Type | Description |
|---|---|
| int |
kernelSpatialDepth
Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose. Return kernel intermediate dimension 0.
Declaration
public int kernelSpatialDepth { get; }
Property Value
| Type | Description |
|---|---|
| int |
kernelWidth
Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose. Return kernel width.
Declaration
public int kernelWidth { get; }
Property Value
| Type | Description |
|---|---|
| int |
length
Return the total number of elements represented by this shape.
Declaration
public int length { get; }
Property Value
| Type | Description |
|---|---|
| int |
rank
Always 8, look also at the dimensions property.
Declaration
public int rank { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
Axis(int)
Allow to use negative axis to access tensorShape backward.
axis should be from -rank to rank (exclusive).
Declaration
public int Axis(int axis)
Parameters
| Type | Name | Description |
|---|---|---|
| int | axis | axis |
Returns
| Type | Description |
|---|---|
| int | adjusted axis |
Equals(object)
Compares this object to other object
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | other object |
Returns
| Type | Description |
|---|---|
| bool |
|
Overrides
Flatten()
Return a TensorShape of dimensions [S,R,N,1,1,1,1,TDHWC]
Declaration
public TensorShape Flatten()
Returns
| Type | Description |
|---|---|
| TensorShape | new TensorShape |
GetHashCode()
Object hash code
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | object hash code |
Overrides
GetPositionsFromIndex(int, ref int, ref int, ref int, ref int)
Given an offset in memory return the dimensions indices of the element as [,,N,,,H,W,C].
Declaration
public void GetPositionsFromIndex(int index, ref int n, ref int h, ref int w, ref int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | one dimensional index (offset) in the memory |
| int | n | batch |
| int | h | height |
| int | w | width |
| int | c | channels |
GetPositionsFromIndex(int, ref int, ref int, ref int, ref int, ref int, ref int, ref int, ref int)
Given an offset in memory return the dimensions indices of the element as [S,R,N,T,D,H,W,C].
Declaration
public void GetPositionsFromIndex(int index, ref int s, ref int r, ref int n, ref int t, ref int d, ref int h, ref int w, ref int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | one dimensional index (offset) in the memory |
| int | s | sequence |
| int | r | direction |
| int | n | batch |
| int | t | time |
| int | d | depth |
| int | h | height |
| int | w | width |
| int | c | channels |
Index(int, int)
Given an element dimensions indices [0,0,N,0,0,0,0,C] return this element offset in memory.
Declaration
public int Index(int n, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | c | channels |
Returns
| Type | Description |
|---|---|
| int | one dimensional index (offset in the flat memory region) |
Index(int, int, int, int)
Given an element dimensions indices [0,0,N,0,0,H,W,C] return this element offset in memory.
Declaration
public int Index(int n, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | h | height |
| int | w | width |
| int | c | channels |
Returns
| Type | Description |
|---|---|
| int | one dimensional index (offset in the flat memory region) |
Index(int, int, int, int, int)
Given an element dimensions indices [0,0,N,0,D,H,W,C] return this element offset in memory.
Declaration
public int Index(int n, int d, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | d | depth |
| int | h | height |
| int | w | width |
| int | c | channels |
Returns
| Type | Description |
|---|---|
| int | one dimensional index (offset in the flat memory region) |
Index(int, int, int, int, int, int, int, int)
Given an element dimensions indices [S,R,N,T,D,H,W,C] return this element offset in memory.
Declaration
public int Index(int s, int r, int n, int t, int d, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | s | sequence |
| int | r | direction |
| int | n | batch |
| int | t | time |
| int | d | depth |
| int | h | height |
| int | w | width |
| int | c | channels |
Returns
| Type | Description |
|---|---|
| int | one dimensional index (offset in the flat memory region) |
IndexWithBroadcast(int, int, int, int)
Given an element dimensions indices [0,0,N,0,0,H,W,C] with broadcast support, return this element offset in memory.
Declaration
public int IndexWithBroadcast(int n, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | h | height |
| int | w | width |
| int | c | channels |
Returns
| Type | Description |
|---|---|
| int |
IndexWithBroadcast(int, int, int, int, int, int, int, int)
Given an element dimensions indices [S,R,N,T,D,H,W,C] with broadcast support, return this element offset in memory.
Declaration
public int IndexWithBroadcast(int s, int r, int n, int t, int d, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | s | sequence |
| int | r | direction |
| int | n | batch |
| int | t | time |
| int | d | depth |
| int | h | height |
| int | w | width |
| int | c | channels |
Returns
| Type | Description |
|---|---|
| int | one dimensional index (offset in the flat memory region) |
IndexWithClamp(int, int, int, int)
Given an element dimensions indices [1,N,1,1,1,H,W,C] return this element offset in memory, clamping indices to tensor dimensions.
Declaration
public int IndexWithClamp(int n, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | h | height |
| int | w | width |
| int | c | channels |
Returns
| Type | Description |
|---|---|
| int | one dimensional index (offset in the flat memory region) |
IndexWithClamp(int, int, int, int, int)
Given an element dimensions indices [1,N,1,1,D,H,W,C] return this element offset in memory, clamping indices to tensor dimensions.
Declaration
public int IndexWithClamp(int n, int d, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | batch |
| int | d | depth |
| int | h | height |
| int | w | width |
| int | c | channels |
Returns
| Type | Description |
|---|---|
| int | one dimensional index (offset in the flat memory region) |
IndexWithClamp(int, int, int, int, int, int, int, int)
Given an element dimensions indices [S,R,N,T,D,H,W,C] return this element offset in memory, clamping indices to tensor dimensions.
Declaration
public int IndexWithClamp(int s, int r, int n, int t, int d, int h, int w, int c)
Parameters
| Type | Name | Description |
|---|---|---|
| int | s | sequence |
| int | r | direction |
| int | n | batch |
| int | t | time |
| int | d | depth |
| int | h | height |
| int | w | width |
| int | c | channels |
Returns
| Type | Description |
|---|---|
| int | one dimensional index (offset in the flat memory region) |
Squeeze()
Remove single-dimensional entries from the shape. [s=1,r=1,b=4,t=1,d=1h=1,w=1,c=128] => [s=1,r=1,b=1,t=1,d=1,h=1,w=4,c=128]
Declaration
public TensorShape Squeeze()
Returns
| Type | Description |
|---|---|
| TensorShape | new TensorShape |
ToArray()
Return an array representation of this tensorShape as [S,R,N,T,D,H,W,C] Prefer tensorShape[x] to avoid GC allocation/collection.
Declaration
public int[] ToArray()
Returns
| Type | Description |
|---|---|
| int[] | shape as int array |
ToString()
Object summary
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | object summary as a string |
Overrides
Operators
operator ==(TensorShape, TensorShape)
Compares two TensorShape objects
Declaration
public static bool operator ==(TensorShape a, TensorShape b)
Parameters
| Type | Name | Description |
|---|---|---|
| TensorShape | a | left object |
| TensorShape | b | right object |
Returns
| Type | Description |
|---|---|
| bool |
|
operator !=(TensorShape, TensorShape)
Compares two TensorShape objects
Declaration
public static bool operator !=(TensorShape a, TensorShape b)
Parameters
| Type | Name | Description |
|---|---|---|
| TensorShape | a | left object |
| TensorShape | b | right object |
Returns
| Type | Description |
|---|---|
| bool |
|