About the Tensor data type in .NET

Posted: (EET/GMT+2)

 

With machine learning workloads becoming more common, .NET has introduced a standard representation for multi-dimensional numeric data: the Tensor type. You will see this type primarily when working with ML.NET or when exchanging data with ONNX models (Open Neural Network Exchange).

A tensor is essentially a multi-dimensional array with a defined shape. For example:

var t = new DenseTensor<float>(new[] { 2, 3 });

This creates a tensor with two rows and three columns. You can access the data similar to how you would handle arrays:

t[0, 0] = 1.0f;
t[1, 2] = 5.0f;

Tensors become especially useful when you need to process batches of numeric data for classification, regression, or image processing models. They provide a common format that many .NET machine learning components can understand.

You may not need tensors every day, but knowing the basics helps when you step into .NET machine learning or start working with ONNX model inputs and outputs.