Skip to main content
Tensors are multi-dimensional arrays used for input and output data in ONNX Runtime.

Namespace

Tensor Element Types

TensorElementType Enum

Supported tensor data types:

DenseTensor<T>

The main tensor class for dense multi-dimensional arrays.

Constructors

DenseTensor(T[], int[])

Creates a tensor from an array and shape.
Example:

DenseTensor(int[])

Creates an empty tensor with specified dimensions.
Example:

DenseTensor(Memory<T>, int[])

Creates a tensor backed by Memory<T>.
Example:

Properties

Dimensions

Gets the tensor dimensions.
Example:

Length

Gets total number of elements.
Example:

Rank

Gets the number of dimensions.

Methods

Clone

Creates a deep copy of the tensor.
Example:

Reshape

Reshapes the tensor to new dimensions.
Example:

ToArray

Converts tensor to flat array.
Example:

NamedOnnxValue

Wrapper for named tensor inputs/outputs.

Creating Named Values

CreateFromTensor

Creates a named value from a tensor.
Example:

Accessing Tensor Data

AsTensor<T>

Extracts tensor from named value.
Example:

Common Tensor Operations

Creating Image Tensors

Batch Processing

Working with Sequences

String Tensors

Advanced Tensor Usage

Memory-Efficient Tensors

Slicing Tensors

Type Conversions

Performance Tips

  1. Reuse tensors: Avoid creating new tensors in hot paths
  2. Use Memory<T>: For zero-copy scenarios
  3. Batch operations: Process multiple items together
  4. Pre-allocate: Create tensors with known sizes upfront
  5. Avoid ToArray(): Access elements directly when possible

Complete Example: NLP Model

See Also