Class OnnxTensorConverter
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
Returns
- Tensor<T>
An AiDotNet Tensor with the same shape and data.
Type Parameters
TThe 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
Returns
- Tensor<T>
An AiDotNet Tensor, with batch dimension removed if batch size is 1.
Type Parameters
TThe 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
Returns
- Tensor<T>
An AiDotNet Tensor with the same shape and data.
Type Parameters
TThe 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
Returns
- Tensor<T>
An AiDotNet Tensor with the same shape and data.
Type Parameters
TThe 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
Returns
- Tensor<T>
An AiDotNet Tensor with the same shape and data.
Type Parameters
TThe 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
resultDisposableNamedOnnxValueThe ONNX result value to convert.
Returns
- Tensor<T>
An AiDotNet Tensor with the converted data, or null if conversion failed.
Type Parameters
TThe target tensor's element type.
GetOnnxTypeName(Type)
Gets the ONNX element type name for a .NET type.
public static string GetOnnxTypeName(Type type)
Parameters
typeTypeThe .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
tensorTensor<T>The AiDotNet tensor to convert.
Returns
- DenseTensor<double>
An ONNX DenseTensor with the same shape and data.
Type Parameters
TThe 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
tensorTensor<T>The AiDotNet tensor to convert.
Returns
- DenseTensor<float>
An ONNX DenseTensor with shape [1, ...original_shape].
Type Parameters
TThe 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
tensorTensor<T>The AiDotNet tensor to convert.
Returns
- DenseTensor<float>
An ONNX DenseTensor with the same shape and data.
Type Parameters
TThe 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
tensorTensor<T>The AiDotNet tensor to convert.
Returns
- DenseTensor<int>
An ONNX DenseTensor with the same shape and data.
Type Parameters
TThe 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
tensorTensor<T>The AiDotNet tensor to convert.
Returns
- DenseTensor<long>
An ONNX DenseTensor with the same shape and data.
Type Parameters
TThe 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
namestringThe input name for the ONNX model.
tensorTensor<T>The AiDotNet tensor to convert.
elementTypestringThe target ONNX element type (e.g., "float", "double", "int64").
Returns
- NamedOnnxValue
A NamedOnnxValue with the converted tensor.
Type Parameters
TThe source tensor's element type.