Create input for a model
A model requires input tensors with certain shapes and data types. Use this information to find model inputs and create input tensors for your model.
Understand the required input
Before you can create input tensors for a model, you must first understand the shape and data types of the model inputs. For more information, refer to Model inputs.
The TensorShape
of the [
Tensor](xref:Unity.Sentis.Tensor)
you create must be compatible with the SymbolicTensorShape
, which defines the shape of the model input.
Convert an array to a tensor
To create a tensor from a one-dimensional data array, follow these steps:
- Create a
TensorShape
object that has the length of each axis. - Create a
Tensor
object with theTensorShape
object and the data array.
For example, the following code creates a tensor for a model that takes an input tensor of shape 3 × 1 × 3.
using UnityEngine;
using Unity.Sentis;
public class ConvertArrayToTensor : MonoBehaviour
{
void Start()
{
// Create a data array with 9 values
float[] data = new float[] { 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f };
// Create a 3D tensor shape with size 3 × 1 × 3
TensorShape shape = new TensorShape(3, 1, 3);
// Create a new tensor from the array
TensorFloat tensor = new TensorFloat(shape, data);
}
}
Create multiple inputs
If a model needs multiple input tensors, create a dictionary that contains the inputs.
For example:
Dictionary<string, Tensor> inputTensors = new Dictionary<string, Tensor>()
{
{ "x", xTensor },
{ "y", yTensor },
};
To avoid handling a dictionary, set inputs manually with SetInput
.
For example:
worker.SetInput("x", xTensor);
worker.SetInput("y", yTensor);
Edit a model
Use the functional API to add operations to your model inputs. For more information, refer to Edit a model.