This document describes with examples the JSON formats accepted when reading tensor constants from a file.
For convenience, compactness, and readability there are various formats that can be used depending on the detailed tensor type:
A tensor type can be declared with its dimension in any order, but
internally they will always be sorted in alphabetical order.
So the type "tensor(category{}, brand{}, a[3], x[768], d0[1])" has
the canonical string representation "tensor(a[3],brand{},category{},d0[1],x[768])"
and the "x" dimension with size 768 is the innermost.
For constants, all indexed dimensions must have a known size.
Dense tensors
Tensors using only indexed dimensions are used for storing a vector,
a matrix, and so on and are collectively known as "dense" tensors.
These are particularly easy to handle, as they always have a known
number of cells in a well-defined order.
They can be input as nested arrays of numerical values.
Example with vector of size 5:
Note that the arrays must have exactly the declared number of
elements for each dimension, and be correctly nested.
Example of an ONNX model input where we have an extra "batch" dimension
which is unused (size 1) for this particular input, but still requires
extra brackets:
Tensors using only mapped dimensions are collectively known as "sparse" tensors.
JSON input for these will list the cells directly.
Tensors with only one mapped dimension can use as simple JSON object as input:
The "type" field is optional.
This format is similar to "Short form for tensors with a single mapped dimension"
in the document JSON format.
Tensors with multiple mapped dimensions must use an array of objects,
where each object has an "address" containing the labels for all
dimensions, and a "value" with the cell value:
This format is also known as the
general verbose form,
and it's possible to use it for any tensor type.
Mixed tensors
Tensors with both mapped and indexed dimensions can use
a "blocks" format; this is similar to the "cells" formats for sparse tensors,
but instead of a single cell value you get a block of
values for each address.
With one mapped dimension and two indexed dimensions: