docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class Tensor<T>

    Represents data in a multidimensional array-like structure.

    Inheritance
    object
    Tensor
    Tensor<T>
    Implements
    IDisposable
    Inherited Members
    Tensor.dataType
    Tensor.count
    Tensor.shape
    Tensor.dataOnBackend
    Tensor.backendType
    Tensor.AdoptTensorData(ITensorData, bool)
    Tensor.ReleaseTensorData()
    Tensor.IsReadbackRequestDone()
    Tensor.ReadbackRequest()
    Tensor.CompleteAllPendingOperations()
    Tensor.Dispose()
    Tensor.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    Namespace: Unity.InferenceEngine
    Assembly: Unity.InferenceEngine.dll
    Syntax
    [MovedFrom("Unity.Sentis")]
    public class Tensor<T> : Tensor, IDisposable where T : unmanaged
    Type Parameters
    Name Description
    T
    Remarks

    Tensors are the fundamental data structure used to represent multidimensional arrays of data, such as images and audio. Use them as inputs for models, and to download a copy of the backend output values (backendType).

    Ownership and Lifetime
    Tensors manage native memory resources. You must call Dispose() of a tensor when it is no longer needed. The ownership of the tensor's internal data (dataOnBackend) belongs to the Tensor object itself. Disposing the tensor also disposes its underlying data.

    Data Representation
    A Tensor's structure is defined by its shape (a TensorShape object) and its dataType. The actual data is held by an ITensorData implementation, which dictates the physical storage location BackendType. Data within the tensor is stored in a flattened, row-major format.

    Asynchronous Operations and Data Access
    Tensor data can be pending a device is performing computations, or if the data is stored on a non-readable device-specific type (for example GPU memory). To get a CPU readable copy of the data, use ReadbackAndClone(), or ReadbackAndCloneAsync() for an asynchronous operation. You can check the status of an asynchronous readback request with IsReadbackRequestDone().

    Examples

    The following example demonstrates how to interact with a Tensor object, ensuring its data is available on the CPU for reading, and then properly disposing of resources.

     For a full workflow example, refer to [Workflow example](xref:sentis-workflow-example).
    
     </p>
    
      // Create a tensor
             m_Tensor = new Tensor<float>(new TensorShape(2, 3), new[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f });
             // Alternatively with `using` so you don't need to call `Dispose()` when you are done with the tensor
             using var m_OtherTensor = new Tensor<float>(new TensorShape(2, 3), new[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f });
    
             // Get a CPU-accessible clone of the tensor. This copy owns its own data.
             Tensor<float> cpuCopyTensor = m_Tensor.ReadbackAndClone() as Tensor<float>;
    
             // Release memory.
             cpuCopyTensor.Dispose();
             m_Tensor.Dispose();
    

    Constructors

    Tensor(TensorShape, bool)

    Initializes and returns a tensor with the specified shape.

    Declaration
    public Tensor(TensorShape shape, bool clearOnInit = true)
    Parameters
    Type Name Description
    TensorShape shape

    The shape of the tensor.

    bool clearOnInit

    Whether to clear the tensor data to zeros.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    Tensor(TensorShape, NativeArray<T>, int)

    Initializes and returns a tensor with specified shape and a native T array of srcData data.

    Declaration
    public Tensor(TensorShape shape, NativeArray<T> srcData, int dataStartIndex = 0)
    Parameters
    Type Name Description
    TensorShape shape

    The shape of the tensor.

    NativeArray<T> srcData

    The data elements of the tensor.

    int dataStartIndex

    The index of the first tensor element in the srcData array.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    Tensor(TensorShape, ITensorData)

    Initializes and returns a Tensor with the specified shape, an ITensorData data.

    Declaration
    public Tensor(TensorShape shape, ITensorData data)
    Parameters
    Type Name Description
    TensorShape shape

    The shape of the tensor.

    ITensorData data

    The optional tensor data.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    Tensor(TensorShape, T[], int)

    Initializes and returns a tensor with specified shape and a T[] array of srcData data.

    Declaration
    public Tensor(TensorShape shape, T[] srcData, int dataStartIndex = 0)
    Parameters
    Type Name Description
    TensorShape shape

    The shape of the tensor.

    T[] srcData

    The data elements of the tensor.

    int dataStartIndex

    The index of the first tensor element in the srcData array.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    Properties

    this[int]

    Returns the tensor element at offset d0.

    Declaration
    public T this[int d0] { get; set; }
    Parameters
    Type Name Description
    int d0

    Axis 0.

    Property Value
    Type Description
    T
    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    this[int, int]

    Returns the tensor element at offset (d1, d0), which is position d1 * stride0 + d0.

    Declaration
    public T this[int d1, int d0] { get; set; }
    Parameters
    Type Name Description
    int d1

    Axis 1.

    int d0

    Axis 0.

    Property Value
    Type Description
    T
    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    this[int, int, int]

    Returns the tensor element at offset (d2, d1, d0), which is position d2 * stride1 + d1 * stride0 + d0.

    Declaration
    public T this[int d2, int d1, int d0] { get; set; }
    Parameters
    Type Name Description
    int d2

    Axis 2.

    int d1

    Axis 1.

    int d0

    Axis 0.

    Property Value
    Type Description
    T
    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    this[int, int, int, int]

    Returns the tensor element at offset (d3, d2, d1, d0), which is position d3 * stride2 + d2 * stride1 + d1 * stride0 + d0 in this tensor.

    Declaration
    public T this[int d3, int d2, int d1, int d0] { get; set; }
    Parameters
    Type Name Description
    int d3

    Axis 3.

    int d2

    Axis 2.

    int d1

    Axis 1.

    int d0

    Axis 0.

    Property Value
    Type Description
    T
    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    this[int, int, int, int, int]

    Returns the tensor element at offset (d4, d3, d2, d1, d0), which is position d4 * stride3 + d3 * stride2 + d2 * stride1 + d1 * stride0 + d0.

    Declaration
    public T this[int d4, int d3, int d2, int d1, int d0] { get; set; }
    Parameters
    Type Name Description
    int d4

    Axis 4.

    int d3

    Axis 3.

    int d2

    Axis 2.

    int d1

    Axis 1.

    int d0

    Axis 0.

    Property Value
    Type Description
    T
    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    this[int, int, int, int, int, int]

    Returns the tensor element at offset (d5, d4, d3, d2, d1, d0), which is position d5 * stride4 + d4 * stride3 + d3 * stride2 + d2 * stride1 + d1 * stride0 + d0.

    Declaration
    public T this[int d5, int d4, int d3, int d2, int d1, int d0] { get; set; }
    Parameters
    Type Name Description
    int d5

    Axis 5.

    int d4

    Axis 4.

    int d3

    Axis 3.

    int d2

    Axis 2.

    int d1

    Axis 1.

    int d0

    Axis 0.

    Property Value
    Type Description
    T
    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    this[int, int, int, int, int, int, int]

    Returns the tensor element at offset (d6, d5, d4, d3, d2, d1, d0), which is position d6 * stride5 + d5 * stride4 + d4 * stride3 + d3 * stride2 + d2 * stride1 + d1 * stride0 + d0.

    Declaration
    public T this[int d6, int d5, int d4, int d3, int d2, int d1, int d0] { get; set; }
    Parameters
    Type Name Description
    int d6

    Axis 6.

    int d5

    Axis 5.

    int d4

    Axis 4.

    int d3

    Axis 3.

    int d2

    Axis 2.

    int d1

    Axis 1.

    int d0

    Axis 0.

    Property Value
    Type Description
    T
    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    this[int, int, int, int, int, int, int, int]

    Returns the tensor element at offset (d7, d6, d5, d4, d3, d2, d1, d0), which is position d7 * stride6 + d6 * stride5 + d5 * stride4 + d4 * stride3 + d3 * stride2 + d2 * stride1 + d1 * stride0 + d0.

    Declaration
    public T this[int d7, int d6, int d5, int d4, int d3, int d2, int d1, int d0] { get; set; }
    Parameters
    Type Name Description
    int d7

    Axis 7.

    int d6

    Axis 6.

    int d5

    Axis 5.

    int d4

    Axis 4.

    int d3

    Axis 3.

    int d2

    Axis 2.

    int d1

    Axis 1.

    int d0

    Axis 0.

    Property Value
    Type Description
    T
    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    Methods

    AsReadOnlyNativeArray()

    Exposes the linear memory data representation of this tensor as a readonly NativeArray.

    Declaration
    public NativeArray<T>.ReadOnly AsReadOnlyNativeArray()
    Returns
    Type Description
    NativeArray<T>.ReadOnly

    NativeArray of tensor data.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    AsReadOnlySpan()

    Exposes the linear memory data representation of this tensor as a ReadOnlySpan.

    Declaration
    public ReadOnlySpan<T> AsReadOnlySpan()
    Returns
    Type Description
    ReadOnlySpan<T>

    Span of tensor data.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    DownloadToArray()

    Blocking Call to return a copy of linear memory representation of the data in this tensor.

    the returned array is a deepcopy of the tensor, the caller of this methods is now responsible for it. If you modify the contents of the returned array, it will not modify the underlying tensor

    Declaration
    public T[] DownloadToArray()
    Returns
    Type Description
    T[]

    T array copy of tensor data.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    DownloadToNativeArray()

    Blocking Call to return a copy of linear memory representation of the data in this tensor.

    the returned native array is a deepcopy of the tensor, the caller of this methods is now responsible for it. If you modify the contents of the returned native array, it will not modify the underlying tensor

    Declaration
    public NativeArray<T> DownloadToNativeArray()
    Returns
    Type Description
    NativeArray<T>

    T native array copy of tensor data.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    ReadbackAndClone()

    Blocking download task of the internal data.

    Declaration
    public Tensor<T> ReadbackAndClone()
    Returns
    Type Description
    Tensor<T>

    returns cpu copy of the tensor.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    ReadbackAndCloneAsync()

    Schedules asynchronous download task of the internal data.

    Declaration
    public Awaitable<Tensor<T>> ReadbackAndCloneAsync()
    Returns
    Type Description
    Awaitable<Tensor<T>>

    awaitable tensor on the cpu.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    Reshape(TensorShape)

    Changes the logical shape of the tensor without changing the backing data's physical allocation.

    Declaration
    public override void Reshape(TensorShape shape)
    Parameters
    Type Name Description
    TensorShape shape

    The new shape for the tensor. The total number of elements in the new shape must fit within the currently allocated backend tensor data's capacity.

    Overrides
    Tensor.Reshape(TensorShape)
    Exceptions
    Type Condition
    AssertionException

    Thrown if the new shape's total elements exceed the allocated capacity.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    Upload(NativeArray<T>)

    Uploads a contiguous block of tensor data to internal storage.

    Declaration
    public void Upload(NativeArray<T> srcData)
    Parameters
    Type Name Description
    NativeArray<T> srcData

    The data to upload.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    Upload(T[])

    Uploads a contiguous block of tensor data to internal storage.

    Declaration
    public void Upload(T[] srcData)
    Parameters
    Type Name Description
    T[] srcData

    The data to upload.

    See Also
    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>

    Implements

    IDisposable

    See Also

    ITensorData
    TensorShape
    DataType
    BackendType
    NativeArray<T>
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)