docs.unity3d.com
    Show / Hide Table of Contents

    Struct TensorShape

    Represents the shape of a tensor.

    Inherited Members
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetType()
    Namespace: Unity.Sentis
    Syntax
    [Serializable]
    public struct TensorShape

    Constructors

    TensorShape(ReadOnlySpan<Int32>)

    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<Int32> shape

    TensorShape(Int32)

    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
    Int32 d0

    TensorShape(Int32, Int32)

    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
    Int32 d1
    Int32 d0

    TensorShape(Int32, Int32, Int32)

    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
    Int32 d2
    Int32 d1
    Int32 d0

    TensorShape(Int32, Int32, Int32, Int32)

    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
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0

    TensorShape(Int32, Int32, Int32, Int32, Int32)

    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
    Int32 d4
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0

    TensorShape(Int32, Int32, Int32, Int32, Int32, Int32)

    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
    Int32 d5
    Int32 d4
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0

    TensorShape(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

    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
    Int32 d6
    Int32 d5
    Int32 d4
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0

    TensorShape(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

    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
    Int32 d7
    Int32 d6
    Int32 d5
    Int32 d4
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0

    TensorShape(TensorShape)

    Returns a copy of another TensorShape.

    Declaration
    public TensorShape(TensorShape shape)
    Parameters
    Type Name Description
    TensorShape shape

    Fields

    maxRank

    The maximum rank a TensorShape can have.

    Declaration
    public const int maxRank = 8
    Field Value
    Type Description
    Int32

    Properties

    Item[Int32]

    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
    Int32 axis
    Property Value
    Type Description
    Int32

    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 readonly int length { get; }
    Property Value
    Type Description
    Int32

    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 readonly int rank { get; }
    Property Value
    Type Description
    Int32

    Methods

    Axis(Int32)

    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
    Int32 axis
    Returns
    Type Description
    Int32

    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
    Returns
    Type Description
    TensorShape

    BroadcastToRank(Int32)

    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
    Int32 rank
    Returns
    Type Description
    TensorShape

    Concat(TensorShape, Int32)

    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
    Int32 axis
    Returns
    Type Description
    TensorShape

    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
    Returns
    Type Description
    Boolean
    Overrides
    ValueType.Equals(Object)

    Flatten(Int32)

    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
    Int32 axis
    Returns
    Type Description
    TensorShape

    GetHashCode()

    Serves as the default hash function.

    Declaration
    public override int GetHashCode()
    Returns
    Type Description
    Int32
    Overrides
    ValueType.GetHashCode()

    HasZeroDims()

    Whether any axes are size 0. In this case the length is also 0.

    Declaration
    public bool HasZeroDims()
    Returns
    Type Description
    Boolean

    Length(Int32)

    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
    Int32 start
    Returns
    Type Description
    Int32

    Length(Int32, Int32)

    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
    Int32 start
    Int32 end
    Returns
    Type Description
    Int32

    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
    Returns
    Type Description
    TensorShape

    Ones(Int32)

    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
    Int32 rank
    Returns
    Type Description
    TensorShape

    Pad(ReadOnlySpan<Int32>)

    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<Int32> 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

    RavelIndex(Int32, Int32)

    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
    Int32 d1
    Int32 d0
    Returns
    Type Description
    Int32

    RavelIndex(Int32, Int32, Int32)

    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
    Int32 d2
    Int32 d1
    Int32 d0
    Returns
    Type Description
    Int32

    RavelIndex(Int32, Int32, Int32, Int32)

    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
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0
    Returns
    Type Description
    Int32

    RavelIndex(Int32, Int32, Int32, Int32, Int32)

    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
    Int32 d4
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0
    Returns
    Type Description
    Int32

    RavelIndex(Int32, Int32, Int32, Int32, Int32, Int32)

    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
    Int32 d5
    Int32 d4
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0
    Returns
    Type Description
    Int32

    RavelIndex(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

    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
    Int32 d6
    Int32 d5
    Int32 d4
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0
    Returns
    Type Description
    Int32

    RavelIndex(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

    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
    Int32 d7
    Int32 d6
    Int32 d5
    Int32 d4
    Int32 d3
    Int32 d2
    Int32 d1
    Int32 d0
    Returns
    Type Description
    Int32

    Reduce(ReadOnlySpan<Int32>, Boolean)

    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<Int32> axes
    Boolean keepDim

    When the value is true, Sentis replaces the reduced axes with 1. Otherwise Sentis removes the reduced axes.

    Returns
    Type Description
    TensorShape

    Reduce(Int32, Boolean)

    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
    Int32 axis
    Boolean keepDim

    When the value is true, Sentis replaces the dimension with 1.

    Returns
    Type Description
    TensorShape

    Reshape(ReadOnlySpan<Int32>, Boolean)

    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<Int32> shape
    Boolean allowZero

    When the value is true, Sentis sets a dimension to zero if the new shape includes a zero. Otherwise Sentis retains the corresponding size at that axis from the original shape.

    Returns
    Type Description
    TensorShape

    Slice(ReadOnlySpan<Int32>, ReadOnlySpan<Int32>, ReadOnlySpan<Int32>, ReadOnlySpan<Int32>)

    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<Int32> starts
    ReadOnlySpan<Int32> ends
    ReadOnlySpan<Int32> axes
    ReadOnlySpan<Int32> steps
    Returns
    Type Description
    TensorShape

    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

    Squeeze(ReadOnlySpan<Int32>)

    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<Int32> axes
    Returns
    Type Description
    TensorShape

    Squeeze(Int32)

    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
    Int32 axis
    Returns
    Type Description
    TensorShape

    Strides(Int32)

    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
    Int32 axis
    Returns
    Type Description
    Int32

    Tile(ReadOnlySpan<Int32>)

    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<Int32> repeats
    Returns
    Type Description
    TensorShape

    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
    Int32[]

    ToString()

    Returns a string that represents the TensorShape.

    Declaration
    public override string ToString()
    Returns
    Type Description
    String
    Overrides
    ValueType.ToString()

    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

    Transpose(Int32[])

    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(int[] permutations)
    Parameters
    Type Name Description
    Int32[] permutations
    Returns
    Type Description
    TensorShape

    Unsqueeze(ReadOnlySpan<Int32>)

    Creates a TensorShape by duplicating this and inserting dimensions of size one at the given axes. For example if this is (2), and axes is {0, 1}, the method returns (1, 1, 2).

    Declaration
    public TensorShape Unsqueeze(ReadOnlySpan<int> axes)
    Parameters
    Type Name Description
    ReadOnlySpan<Int32> axes
    Returns
    Type Description
    TensorShape

    Unsqueeze(Int32)

    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
    Int32 axis
    Returns
    Type Description
    TensorShape

    Operators

    Equality(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
    TensorShape b
    Returns
    Type Description
    Boolean

    Inequality(TensorShape, TensorShape)

    Declaration
    public static bool operator !=(TensorShape a, TensorShape b)
    Parameters
    Type Name Description
    TensorShape a
    TensorShape b
    Returns
    Type Description
    Boolean

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Constructors
      • TensorShape(ReadOnlySpan<Int32>)
      • TensorShape(Int32)
      • TensorShape(Int32, Int32)
      • TensorShape(Int32, Int32, Int32)
      • TensorShape(Int32, Int32, Int32, Int32)
      • TensorShape(Int32, Int32, Int32, Int32, Int32)
      • TensorShape(Int32, Int32, Int32, Int32, Int32, Int32)
      • TensorShape(Int32, Int32, Int32, Int32, Int32, Int32, Int32)
      • TensorShape(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)
      • TensorShape(TensorShape)
    • Fields
      • maxRank
    • Properties
      • Item[Int32]
      • length
      • rank
    • Methods
      • Axis(Int32)
      • Broadcast(TensorShape)
      • BroadcastToRank(Int32)
      • Concat(TensorShape, Int32)
      • Equals(Object)
      • Flatten(Int32)
      • GetHashCode()
      • HasZeroDims()
      • Length(Int32)
      • Length(Int32, Int32)
      • MatMul(TensorShape)
      • Ones(Int32)
      • Pad(ReadOnlySpan<Int32>)
      • RavelIndex(Int32, Int32)
      • RavelIndex(Int32, Int32, Int32)
      • RavelIndex(Int32, Int32, Int32, Int32)
      • RavelIndex(Int32, Int32, Int32, Int32, Int32)
      • RavelIndex(Int32, Int32, Int32, Int32, Int32, Int32)
      • RavelIndex(Int32, Int32, Int32, Int32, Int32, Int32, Int32)
      • RavelIndex(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)
      • Reduce(ReadOnlySpan<Int32>, Boolean)
      • Reduce(Int32, Boolean)
      • Reshape(ReadOnlySpan<Int32>, Boolean)
      • Slice(ReadOnlySpan<Int32>, ReadOnlySpan<Int32>, ReadOnlySpan<Int32>, ReadOnlySpan<Int32>)
      • Squeeze()
      • Squeeze(ReadOnlySpan<Int32>)
      • Squeeze(Int32)
      • Strides(Int32)
      • Tile(ReadOnlySpan<Int32>)
      • ToArray()
      • ToString()
      • Transpose()
      • Transpose(Int32[])
      • Unsqueeze(ReadOnlySpan<Int32>)
      • Unsqueeze(Int32)
    • Operators
      • Equality(TensorShape, TensorShape)
      • Inequality(TensorShape, TensorShape)
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023