Infer.NET user guide : The Infer.NET Modelling API
In many models data will not be in the form of a flat single dimensional or a multi-dimension array. For example, items in a data array may have a variable number of features. In this case the observed data is represented as an array of arrays of values; in C# this is referred to as a jagged array, and Infer.NET has an API which allows creation of jagged variable arrays.
The simplest jagged array is a 1D array of 1D arrays. The outer array can be indexed by a standard range, but the inner array is variable in size, and the size itself is a function of the outer index. Here is an example the illustrates the syntax:
In this case, we specify the sizes as a constant variable array as shown in the first three lines. The inner range can then be defined using the natural expression for the size of the inner range. The jagged array is then created on the final line using the standard Array constructor where the first argument is the array element (created as variable array of doubles ranging over the inner range), and the second argument is the outer range. Here we have used implicit typing, but we could also be explicit about its type:
The .NET type of a jagged array in Infer.NET is a VariableArray with two generic type parameters. The first parameter represents the type of element in the VariableArray. In this case we have a VariableArray of VariableArray<double>, so the first type argument is VariableArray<double>. The second type parameter is the .NET array type of the jagged array - in this case we have an array of array of doubles.
We can now use the ranges to index and loop over our jagged array in a natural manner:
If you use ForEach blocks rather than the inline ForEach method, the outer ForEach block must always be over the outer Range.
Constant jagged arrays
You can define a constant jagged array initialised to its equivalent .NET array directly in the definition. For example:
Here we have used implicit typing again. The explicit type of aConst in this example is:
Observed jagged arrays
It is more efficient and flexible to use an observed jagged array rather than a constant jagged array. One way to do this is to change the last line in the code above to use Variable.Observed rather than Variable.Constant. Another approach is to use Variable.Array and then set the ObservedValue property. However, note that this example uses fixed ranges; if the observed array is going to change its size in any way then you should use ranges that are also variable. The following snippet of code shows how to do this.
The observed values can then be set before inference, making sure that the jagged sizes, and the jagged array itself are consistently set:
More complex jagged arrays
The Infer.NET API also supports more complex jagged arrays such as a 2D array of arrays, or an array of array of arrays. Here is an example of a 2D array of arrays. In this example, the inner range is of fixed size which simplifies the definition.
where the type of zVar is
If, instead, we were to build an array of 2D arrays, the type would be
This can be continued to any depth. This code creates a jagged array of depth 4:
Code like this can be simplified by using type aliases. For example: