Table of Contents

Class OnnxTensorConverter

Namespace
AiDotNet.Onnx
Assembly
AiDotNet.dll

Converts between AiDotNet Tensor types and ONNX Runtime tensor types.

public static class OnnxTensorConverter
Inheritance
OnnxTensorConverter
Inherited Members

Remarks

This class provides static methods for converting tensors between AiDotNet's generic Tensor<T> format and ONNX Runtime's DenseTensor format.

For Beginners: When running ONNX models, we need to convert our data to a format that ONNX Runtime understands. This converter handles that translation:

  • ToOnnx: Converts your AiDotNet tensor to ONNX format for model input
  • FromOnnx: Converts ONNX model output back to AiDotNet tensor

Methods

FromOnnxDouble<T>(Tensor<double>)

Converts an ONNX DenseTensor of doubles to an AiDotNet Tensor.

public static Tensor<T> FromOnnxDouble<T>(Tensor<double> onnxTensor)

Parameters

onnxTensor Tensor<double>

The ONNX tensor to convert.

Returns

Tensor<T>

An AiDotNet Tensor with the same shape and data.

Type Parameters

T

The target tensor's element type.

FromOnnxFloatRemoveBatch<T>(Tensor<float>)

Converts an ONNX tensor and removes the batch dimension if it's 1.

public static Tensor<T> FromOnnxFloatRemoveBatch<T>(Tensor<float> onnxTensor)

Parameters

onnxTensor Tensor<float>

The ONNX tensor to convert.

Returns

Tensor<T>

An AiDotNet Tensor, with batch dimension removed if batch size is 1.

Type Parameters

T

The target tensor's element type.

FromOnnxFloat<T>(Tensor<float>)

Converts an ONNX DenseTensor of floats to an AiDotNet Tensor.

public static Tensor<T> FromOnnxFloat<T>(Tensor<float> onnxTensor)

Parameters

onnxTensor Tensor<float>

The ONNX tensor to convert.

Returns

Tensor<T>

An AiDotNet Tensor with the same shape and data.

Type Parameters

T

The target tensor's element type.

FromOnnxInt<T>(Tensor<int>)

Converts an ONNX DenseTensor of integers to an AiDotNet Tensor.

public static Tensor<T> FromOnnxInt<T>(Tensor<int> onnxTensor)

Parameters

onnxTensor Tensor<int>

The ONNX tensor to convert.

Returns

Tensor<T>

An AiDotNet Tensor with the same shape and data.

Type Parameters

T

The target tensor's element type.

FromOnnxLong<T>(Tensor<long>)

Converts an ONNX DenseTensor of long integers to an AiDotNet Tensor.

public static Tensor<T> FromOnnxLong<T>(Tensor<long> onnxTensor)

Parameters

onnxTensor Tensor<long>

The ONNX tensor to convert.

Returns

Tensor<T>

An AiDotNet Tensor with the same shape and data.

Type Parameters

T

The target tensor's element type.

FromOnnxValue<T>(DisposableNamedOnnxValue)

Converts an ONNX DisposableNamedOnnxValue to an AiDotNet Tensor based on its actual element type.

public static Tensor<T>? FromOnnxValue<T>(DisposableNamedOnnxValue result)

Parameters

result DisposableNamedOnnxValue

The ONNX result value to convert.

Returns

Tensor<T>

An AiDotNet Tensor with the converted data, or null if conversion failed.

Type Parameters

T

The target tensor's element type.

GetOnnxTypeName(Type)

Gets the ONNX element type name for a .NET type.

public static string GetOnnxTypeName(Type type)

Parameters

type Type

The .NET type.

Returns

string

The ONNX element type name.

ToOnnxDouble<T>(Tensor<T>)

Converts an AiDotNet Tensor to an ONNX DenseTensor of doubles.

public static DenseTensor<double> ToOnnxDouble<T>(Tensor<T> tensor)

Parameters

tensor Tensor<T>

The AiDotNet tensor to convert.

Returns

DenseTensor<double>

An ONNX DenseTensor with the same shape and data.

Type Parameters

T

The source tensor's element type.

ToOnnxFloatWithBatch<T>(Tensor<T>)

Creates an ONNX DenseTensor with a prepended batch dimension.

public static DenseTensor<float> ToOnnxFloatWithBatch<T>(Tensor<T> tensor)

Parameters

tensor Tensor<T>

The AiDotNet tensor to convert.

Returns

DenseTensor<float>

An ONNX DenseTensor with shape [1, ...original_shape].

Type Parameters

T

The source tensor's element type.

ToOnnxFloat<T>(Tensor<T>)

Converts an AiDotNet Tensor to an ONNX DenseTensor of floats.

public static DenseTensor<float> ToOnnxFloat<T>(Tensor<T> tensor)

Parameters

tensor Tensor<T>

The AiDotNet tensor to convert.

Returns

DenseTensor<float>

An ONNX DenseTensor with the same shape and data.

Type Parameters

T

The source tensor's element type.

ToOnnxInt<T>(Tensor<T>)

Converts an AiDotNet Tensor to an ONNX DenseTensor of integers.

public static DenseTensor<int> ToOnnxInt<T>(Tensor<T> tensor)

Parameters

tensor Tensor<T>

The AiDotNet tensor to convert.

Returns

DenseTensor<int>

An ONNX DenseTensor with the same shape and data.

Type Parameters

T

The source tensor's element type.

ToOnnxLong<T>(Tensor<T>)

Converts an AiDotNet Tensor to an ONNX DenseTensor of long integers.

public static DenseTensor<long> ToOnnxLong<T>(Tensor<T> tensor)

Parameters

tensor Tensor<T>

The AiDotNet tensor to convert.

Returns

DenseTensor<long>

An ONNX DenseTensor with the same shape and data.

Type Parameters

T

The source tensor's element type.

ToOnnxValue<T>(string, Tensor<T>, string)

Converts an AiDotNet Tensor to an ONNX NamedOnnxValue based on the target element type.

public static NamedOnnxValue ToOnnxValue<T>(string name, Tensor<T> tensor, string elementType)

Parameters

name string

The input name for the ONNX model.

tensor Tensor<T>

The AiDotNet tensor to convert.

elementType string

The target ONNX element type (e.g., "float", "double", "int64").

Returns

NamedOnnxValue

A NamedOnnxValue with the converted tensor.

Type Parameters

T

The source tensor's element type.