Do operations on tensors
To do operations on tensors, follow these steps:
- Create an
ITensorAllocator
allocator, which manages a pool of allocated tensors. - Use
WorkerFactory.CreateOps
to create anOps
object with the allocator. - Use a method of the
Ops
object to do an operation on a tensor.
The following example uses the ArgMax
operation. ArgMax
returns the index of the maximum value in a tensor, for example the prediction with the highest probability in a classification model.
using UnityEngine;
using Unity.Sentis;
public class RunOperatorOnTensor : MonoBehaviour
{
TensorFloat inputTensor;
ITensorAllocator allocator;
Ops ops;
void Start()
{
// Create a one-dimensional input tensor with four values.
inputTensor = new TensorFloat(new TensorShape(4), new[] { 2.0f, 1.0f, 3.0f, 0.0f });
// Create an allocator.
allocator = new TensorCachingAllocator();
// Create an Ops object. The object uses Sentis compute shaders to do operations on the GPU.
ops = WorkerFactory.CreateOps(BackendType.GPUCompute, allocator);
// Run the ArgMax operator on the input tensor.
TensorInt result = ops.ArgMax(inputTensor, axis: 0, keepdim: true);
// Log the first item of the result.
Debug.Log(result[0]);
}
void OnDisable()
{
// Tell the GPU we're finished with the memory the input tensor, allocator and Ops object used.
inputTensor.Dispose();
allocator.Dispose();
ops.Dispose();
}
}
Refer to the ExecuteOperatorOnTensor
example in the sample scripts for an example.
Refer to Create an engine to run a model for more information about back end types.