MLArrayFeature
class NatML.Features.MLArrayFeature<T> : MLFeature, IMLEdgeFeature, IMLCloudFeature
This is a multidimensional array feature, a.k.a a tensor. Most models which accept tensors will accept an array feature in NatML.
The array data type T
is always unmanaged
.
Array features are always views of a pre-allocated array or buffer. As such, creating an array feature will never allocate tensor memory.
Creating the Feature
The array feature can be created from any numeric array or buffer representation:
From a Managed Array
An array feature can be created from a managed array. In rare cases where it is useful to fully specify the feature type, there is an overload which accepts the array type:
From a Native Array
The array feature provides variants of the managed array constructors where the data
argument is a NativeArray<T>
. This can be useful for working with certain performance-critical Unity API's like Burst:
Similarly, in cases where it is necessary to explicitly specify the array feature type, there is a corresponding overload:
From a Native Buffer
The array feature provides variants of the managed array constructors where the data
argument is a typed native buffer. These can be useful for making predictions on arrays from native code or external libraries like OpenCV:
There is a corresponding constructor for specifying the feature type:
The data
buffer MUST remain valid for the lifetime of the array feature.
From an Edge Feature
An array feature can be created from an MLEdgeFeature
. This is useful for making predictions with an MLEdgeModel
.
The feature
MUST have a numeric data type.
From a Cloud Feature
The array feature can be created from an MLCloudFeature
. This is useful for making predictions with an MLCloudModel
.
The feature
MUST have a numeric data type.
Inspecting the Feature
The array feature provides information about its shape.
Feature Type
Refer to the Inspecting the Feature section of the MLFeature
class for more information.
The type
is always an MLArrayType
.
Feature Shape
The feature shape is a convenience property which provides the shape from the feature type
.
The shape
can be null
if the feature was created with no shape.
Element Count
The element count is a convenience property which provides the element count from the feature type
.
The elementCount
will be zero when the feature does not have a shape
.
Accessing Feature Data
The array feature provides accessors for reading and writing feature data.
Multi-Indexing
The array feature provides support for multi-indexing. This is a common pattern for working with tensors in machine learning:
Multiple indexing requires the array feature to have a valid shape
, but linear indexing (i.e. indexing with a single number) does not.
The array feature does not perform bounds checking for performance, so make sure to always index correctly.
The array feature also supports linear indexing, in which case it accesses the elements of the feature assuming a flat shape:
Copying
The array feature can copy it data into another array feature.
This method copies destination.elementCount * sizeof(U)
bytes into the destination
feature.
This method does not respect any permutations that have been applied to the array feature.
To copy data into a managed array, native buffer, or other destination, first create an MLArrayFeature
to wrap the destination, then use the CopyTo
method.
Converting to Array
The array feature can copy its data into an array and return the array. The destination array type can also be specified:
This method does not respect any permutations that have been applied to the array feature.
Pinning
The array feature supports pinning, allowing direct access to the underlying feature data while bypassing all checks. This can be used in unsafe
context with the fixed
statement:
Pinning is mostly used for bulk read or write operations. Use multi-indexing for individual element access.
Do not use pinning unless you know exactly what you are doing. Mistakes are almost guaranteed to result in segmentation faults and hard crashes.
Viewing Operations
The array feature supports viewing operations. Each of these operations do not allocate any memory; they simply return a shallow feature that has the proper shape.
Permute
The array feature's dimensions can be permuted, allowing different dimensions to be swapped (like transposing):
View
The array feature supports creating a view of the feature data with a different shape:
The element count of the view shape must match the element count of the feature.
Creating an Edge Feature
INCOMPLETE.
Creating a Cloud Feature
INCOMPLETE.
Last updated