Create a new model
You can use the Sentis Functional API to create a new runtime model without an ONNX or LiteRT file. For example, if you want to perform a series of tensor operations without weights or build your own model serialization from another model format.
Using the functional API
To create a model with the functional API, follow these steps:
- Create a
FunctionalGraphobject. - Add inputs to the graph by specifying their data type, shape, name. This returns
FunctionalTensorobjects for each input. - Apply a series of functional API methods to the functional tensors objects to create the desired output functional tensors.
- Add the outputs to the graph by calling the
AddOutputmethod with the output functional tensor and name. - Compile the model with the
Compilemethod.
The following example shows the creation of a simple model to calculate the dot product of two vectors.
using System;
using Unity.InferenceEngine;
using UnityEngine;
public class CreateNewModel : MonoBehaviour
{
Model model;
void Start()
{
// Create the functional graph.
FunctionalGraph graph = new FunctionalGraph();
// Add two inputs to the graph with data types and shapes.
// Our dot product operates on two vector tensors of the same size `6`.
FunctionalTensor x = graph.AddInput<float>(new TensorShape(6), "input_x");
FunctionalTensor y = graph.AddInput<float>(new TensorShape(6), "input_y");
// Calculate the elementwise product of the input `FunctionalTensor`s using an operator.
FunctionalTensor prod = x * y;
// Sum the product along the first axis flattening the summed dimension.
FunctionalTensor reduce = Functional.ReduceSum(prod, dim: 0, keepdim: false);
graph.AddOutput(reduce, "output_reduce");
graph.AddOutput(prod, "output_prod");
// Build the model using the `Compile` method.
model = graph.Compile();
}
}
To debug your code, use ToString to retrieve information, such as its shape and datatype.
You can then create an engine to run a model.
Model inputs and outputs
When you compile a model with the Compile method, the resulting model includes the inputs and outputs you defined with AddInput and AddOutput.
You can assign names to inputs and outputs when you add them to the graph. If you don’t specify a name, Sentis automatically assigns a default name in the format input_0, input_1, output_0, output_1, and so forth in numerical order.