Structured data fusion: specification of domain specific language

An extensive, more technical overview of the domain specific language (DSL) to express structured data fusion (SDF) models is given in this section. The specification allows the user to get insight in how the language works, enabling him or her to unlock the full potential of the SDF framework.

This chapter is structured as follows. First we give an overview of important definitions w.r.t. naming, referencing and auto-wrapping. Next the model and its different fields (variables, factors, factorizations, transform and options) are explained.

Definitions

To simplify and shorten the specification of the DSL, we first define some frequently used concepts. The following concepts are explained:

  • Named and indexed definitions and references
  • Valid references
  • Auto-wrapping

Named and indexed definitions

The variables, factors, transform and factorizations fields can be defined in two ways: as a struct or as a cell. The following definitions are equivalent:

% named definition
model.variables.A = A;
model.variables.B = B;

% indexed definition
model.variables{1} = A;
model.variables{2} = B;

If the field is a struct, it is converted to a cell using struct2cell. This means that the order in which the variables appear first remains unchanged. Hence the following two blocks are equivalent:

% named definition
model.variables.B = B;
model.variables.A = A;
model.variables.B = B; % B is already defined, and therefore is still the
                       % first variable

% indexed definition
model.variables{1} = B;
model.variables{2} = A;

The fields are not sorted and redefining a field (see B) does not change the order of the fields.

Variables, factors, transforms and factorizations can be defined using names or indexes. References to variables and factors can also be named or indexed. For example:

% first define named variables
model.variables.A = A;
model.variables.B = B;

% named references
model.factors.f1 = {'A', 'A'};
model.factors.f2 = 'B';

% indexed references (equivalent to the above)
model.factors.f1 = {1, 1};
model.factors.f2 = 2;

% mixing named and index references (also equivalent)
model.factors.f1 = {1, 'A'};
model.factors.f2 = 2;

Named references can only be used if named definitions are used for the variable or factor that is being referred to. For example:

% indexed definition for variables
model.variables{1} = A;
model.variables{2} = B;
% named definitions for factors, only indexed references to variables can be
% used.
model.factors.A = 1;
model.factors.B = 2;
% indexed definition of factorizations (both named and indexed references)
model.factorizations{1}.regL1 = {'A', 2};

In the remaining part of this section, named and indexed definitions/references are used interchangeably.

Valid references

A valid reference is a named or indexed reference to a named or indexed variable or factor that can be resolved, i.e., a variable or factor with the given name or index exists, regardless of how it is constructed. Recall that factors can be constructed using a factors or a transform field, or can be defined implicitly. Named variables or factors also have indices. As an example, consider

model.factors.F = {'a', 2};

The factor F has two references a and 2. The named reference a is valid if the variables are defined using named definitions (model.variables is a struct) and model.variables contains a field with name a. Names are case sensitive. The indexed reference 2 is valid if model.variables{2} exists or if model.variables is a struct and at least two variables are defined. Hence, 2 is a valid reference if length(model.variables) >= 2. The same holds for factor references in factorizations. Note that the transform field generates new factors as if they are defined using the factors field. Also note that the ImplicitFactors option (see Implicit factors) allows factors to be defined implicitly, i.e., if a factor is not defined and if there exists a variable with a matching name or index, a new factor is created automatically.

Auto-wrapping

The auto-wrapping function automatically adds missing braces {} around content, or calling num2cell if this can be done unambiguously. Some examples:

model.factors = 1:3;
% is equivalent to
model.factors{1} = {{1}};
model.factors{2} = {{2}};
model.factors{3} = {{3}};

model.factors.A = rand(2,2);
% is equivalent to
model.factors.A = {{rand(2,2)}};

model.factors = {1, @struct_nonneg};
% is equivalent to
model.factors{1} = {{1, @struct_nonneg}};

model.factors{1} = {1, 1; 'a', {'b', @struct_nonneg}};
% is equivalent to
model.factors{1} = {{1}, {1}; {'a'}, {'b', @struct_nonneg}};

model.factorizations{1}.cpd = 1:3;
% is equivalent to
model.factorizations{1}.cpd = {1,2,3};
% but the following is invalid
model.factorizations{1}.cpd = {1:3};

model.factorizations{1}.btd = 1:4;
% is equivalent to
model.factorizations{1}.btd = {1:4};
% is equivalent to
model.factorizations{1}.btd = {1,2,3,4};
% is equivalent to
model.factorizations{1}.btd = {{1,2,3,4}};
% but the following is invalid
model.factorizations{1}.btd = {{1:4}};
% mixing is allowed though:
model.factorizations{1}.btd = {{1,2,3,4}, 5:8};

model.transform = {1:3, 3:5, @struct_nonneg};
% is equivalent to
model.transform = {{1:3, 3:5, @struct_nonneg}};
% is equivalent to
model.transform{1} = {1:3, 3:5, @struct_nonneg};
% is equivalent to
model.transform{1} = {{1,2,3}, {3,4,5}, @struct_nonneg};

In the examples above, the last lines (excluding the invalid cases) are the internal representations used. All other formulations facilitate the definition of the models. If in doubt, sdf_check(model, 'print') can be used to see if the desired model has been constructed.

The model struct

Each SDF model is defined as a struct (in Matlab’s definition). Five different fields can be defined, some of which are always obligatory, others are only in some cases obligatory, and some are optional. The five fields are:

  • model.variables: always obligatory
  • model.factors: obligatory unless the model.transform field is present or implicit factors are used
  • model.factorizations: always obligatory
  • model.transform: optional
  • model.options: optional

In the remaining part of this section, the different fields are discussed.

The variables field

The model.variables field is a struct or cell defining named or indexed variables, respectively. Here, we only consider indexed variables as named variables are converted to indexed variables internally. (The names are kept to be able to resolve named references.) Each variable can be either an array of numerical values, or a cell of arrays of numerical values.

If named variables are used (and hence model.variables is a struct) the name (constant) cannot be used.

The factors field

The model.factors field is a struct or cell defining named or indexed factors, respectively. Here, we only consider indexed factors as named factors are converted to indexed factors internally (while keeping track of the names for references). Each entry in the cell defines a single factor, and has one of the following forms (after auto-wrapping):

% one subfactor with variable v1
model.factors{1} = {{v1}};
% two subfactors concatenated column-wise, no transformation
model.factors{1} = {{v1}, {v2}};
% two subfactors concatenated row-wise, with transformation t1
model.factors{1} = {{v1,t1}; {v2}};
% two subfactors concatenated in mode 3, with transformations t1, t2, t3
model.factors{1} = reshape({{v1, t1}, {v2,t1,t2,t3}}, 1,1,2);
% two subfactors concatenated in mode 3, with transformed constant c1
model.factors{1} = {{v1}, {c1,t1}};

Each factor can be a concatenation of an arbitrary number of subfactors, as long as the dimensions of the subfactors are compatible, i.e., if the dimensions of the expanded subfactors are compatible for concatenation using cell2mat (see sdf_check(model, 'print')). Each subfactor is of the form {v1,t1,t2,...} or {c1,t1,t2,...} in which v1 is a valid variable reference and c1 is a constant. Each variable reference or constant can be followed by an arbitrary number of transformations t1, t2, etc. Each transformation is a function handle that accepts two arguments: a variable z and a task struct. If extra parameters are to be defined, the following can be used:

t1 = @(z,task) struct_name(z,task,param1,param2,...);

All transformations are applied from left to right. For each subfactor, the result of the final transformation should be a scalar, vector, matrix or tensor.

Constants follow the same rules as variables, i.e., each constant is a scalar, vector, matrix, or tensor, or is a cell of scalars, vectors, matrices and/or tensors. All transformations defined for constants are applied before the actual computations start. An alternative syntax for constant subfactors uses the (constant) keyword:

model.factors{1} = {{c1, '(constant)', t1, t2, ..}};

The (constant) keyword is optional if c1 is not a scalar. If c1 is a scalar, it is recognized as a variable reference if the (constant) keyword is not given.

If implicit factors are allowed and if model.factors is a struct (and hence named factors are used), names of the form IFx in which x is a number are prohibited.

The factorizations field

The model.factorizations field defines the different parts of the objective function. These parts can be factorization or regularization terms. model.factorizations is a struct or a cell, each entry being a single factorization or regularization term. Each such term is a struct with fields depending on the factorization or regularization type. In general, a factorization looks like

model.factorizations.fac1.data = T;
model.factorizations.fac1.cpd  = {f1,f2,f3};

in which f1, f2 and f3 should be valid named or indexed factor references.

As a special case, model.factorizations can be a struct array as well, e.g.,

T = {rand(10,11,12), randn(12,13,14)};
couplingind = {1:3, 3:5};
model.factorizations = struct('data', T, 'cpd', couplingind);

% is equivalent to
model.factorizations{1}.data = rand(10,11,12);
model.factorizations{1}.cpd  = 1:3;
model.factorizations{2}.data = rand(12,13,14);
model.factorizations{2}.cpd  = 3:5;

This only works if numel(model.factorizations) > 1.

Factorization types

Seven different factorization types are currently defined: four factorizations (cpd (and a cpdi variant), btd, ll1 and lmlra) and three regularizations (regL0, regL1 and regL2). Each factorization defines a field with the factorization or regularization type as name, and a (possibly nested) cell of factor references as values. Apart from the factorization type, a second field data is required for the factorizations, and optional for the regularizations. The data has as value the tensor to be decomposed or the value to be regularized to.

The different factorization and regularization types are now discussed. The shorthand notation \(\ten{M}_{\text{type}}(\mat{A},\ldots)\) is used to define a factorization of type type involving factors A, etc.

  • cpd: a term \(\frac{1}{2}\norm{\ten{T} - \ten{M}_\text{CPD}(\mat{A},\mat{B},\ldots)}^2_{\text{F}}\) is added to the objective function. The references in the cpd field indicate which factors are involved in this term. The data field defines the tensor \(\ten{T}\). For example:

    model.factorizations{1}.cpd = {'A','B',...};
    

    In the case \(\ten{T}\) is an incomplete tensor, a more accurate algorithm can be selected if an NLS based solver is used. For SDF models this algorithm is chosen by using the cpdi factorization instead of cpd, e.g.,

    model.factorizations{1}.cpdi = {'A','B',...};
    
  • btd: a term \(\frac{1}{2}\norm{\ten{T} - \ten{M}_\text{BTD}(\mat{A}_1, \mat{B}_1, \mat{C}_1, \ten{S}_1, \mat{A}_2, \mat{B}_2, \mat{C}_2,\ten{S}_2,\ldots)}^2_{\text{F}}\) is added to the objective function. The references in the btd field indicate which factors are involved in this term. The btd field is a nested cell of factor references (after auto-wrapping), e.g.,

    model.factorizations{1}.btd = {{'A1','B1','C2','S1'},{'A2','B2','C2','S2'}};
    

    The data field defines the tensor \(\ten{T}\).

  • ll1: a term \(\frac{1}{2}\norm{\ten{T} - \ten{M}_\text{LL1}(\mat{A},\mat{B},\mat{C})}^2_{\text{F}}\) is added to the objective function. The references in the ll1 field indicate which factors are involved in this term. The LL1 term only accepts factors in the CPD format, e.g.,

    model.factorizations{1}.ll1 = {'A', 'B', 'C'};
    model.factorizations{1}.L = L;
    

    The extra field L = [L1,L2,...,LR] is obligatory. A and B have sum(L) columns, and C has length(L) columns. The data field defines the tensor \(\ten{T}\).

  • lmlra: a term \(\frac{1}{2}\norm{\ten{T} - \ten{M}_\text{LMLRA}(\mat{A}, \mat{B}, \mat{C}, \ldots,\ten{S})}^2_{\text{F}}\) is added to the objective function. The references in the lmlra field indicate which factors are involved in this term. The lmlra field is a cell, or a cell of cells (after auto-wrapping), e.g.,

    model.factorizations{1}.lmlra = {'A','B','C','S'}; % or
    model.factorizations{1}.lmlra = {{'A','B','C','S'}};
    

    Note that in contrast to other LMLRA routines such as lmlra_rnd and lmlra and in contrast to the structured tensor format, the correct format here is {A,B,C,S} and not {{A,B,C},S}. The data field defines the tensor \(\ten{T}\).

In the case of regularization types, define \(\ten{M}(\mat{A}) = \norm{\vectorize(\mat{A})}_0\) for type regL0, \(\ten{M}(\mat{A}) = \frac{1}{2}\norm{\vectorize(\mat{A})}_1\) for type regL1 and \(\frac{1}{2}\ten{M}(\mat{A}) = \norm{\mat{A}}_{\text{F}}^2\) for type regL2, in which \(\mat{A}\) can be a scalar, vector, matrix or tensor. We then have (replace reg by regL0, regL1 or regL2):

  • \(\ten{M}(\mat{A})\) can be modeled as

    model.factorizations.regterm.reg = {'A'};
    
  • \((\ten{M}(\mat{A}) + \ten{M}(\mat{B}))\) can be modeled as

    model.factorizations.regterm.reg = {'A', 'B'};
    
  • \((\ten{M}(\mat{1} - \mat{A}) + \ten{M}(\mat{2} - \mat{B}))\) can be modeled as

    model.factorizations.regterm.data = {ones(size(A)), ones(size(B))*2};
    model.factorizations.regterm.reg  = {'A', 'B'};
    

In the case of L0 regularization, a smoothed approximation of the L0 norm is used for computational reasons: \(\|\vectorize(\mat{A})\|_0\) is approximated by \(\sum_{i=1}^{I} 1 - \text{exp}(-\frac{a_i^2}{\sigma^2})\) with \(I\) the number of entries in \(\mat{A}\) [39]. The parameter \(\sigma\) can be a scalar constant or a function of the iteration k and the cell of regularized factors x. The value can be set using the sigma field:

model.factorizations.myreg.regL0 = 'A';
model.factorizations.myreg.sigma = 0.01; % (the default), or
model.factorizations.myreg.sigma = @(k,x) 0.01*frob(x{1}); % x = cell of factors

For all the cases above, sdf_check(model) (which is automatically called before all computational routines) checks if the given data and factors are consistent, i.e., if the assumptions for the model or type hold, and match with the provided data. For example, for the CPD all factors should be matrices with exactly \(R\) columns, and the number of rows of each factor should match the dimension of the data in the corresponding mode. It is recommended to run sdf_check(model, 'print') as this signals potential syntax and consistency errors.

Internally, the different fields in a factorization are redefined. The type field contains a string with the factorization type, the factors field contains the factor references. The original field, e.g., cpd = 1:3, is removed. For example:

model.factorizations{1}.myfac.data = T;
model.factorizations{1}.myfac.cpd  = {1,2,3};
% becomes
model.factorizations{1}.myfac.data     = T;
model.factorizations{1}.myfac.type     = 'cpd';
model.factorizations{1}.myfac.factors  = {1,2,3};

Defining weights

When different datasets are coupled or regularization terms are added, it is often needed to give terms a weight. For example:

\[\begin{align} \min_{\mat{A},\mat{B},\mat{C}} \frac{\lambda_1}{2} \norm{\ten{T} - \ten{M}_{\text{CPD}}(\mat{A},\mat{B},\mat{C})}_{\text{F}}^2 + \frac{\lambda_2}{2} \norm{A}_{\text{F}}^2 + \frac{\lambda_3}{2} (\norm{B}_1 + \norm{C}_1) \end{align}\]

The weights \(\lambda_i\) are often called hyperparameters. Here, the term weight is used. The weights can be set as follows

model = struct;
% definition of variables and factors omitted
model.factorizations.tensor.data   = T;
model.factorizations.tensor.cpd    = {'A', 'B', 'C'};
model.factorizations.tensor.weight = lambda1;
model.factorizations.regA.regL2    = 'A';
model.factorizations.regA.weight   = lambda2;
model.factorizations.regBC.regL1   = {'B', 'C'};
model.factorizations.regBC.weight  = lambda3;

or can be set as an option of the computational routine, e.g.:

sdf_nls(model, 'Weight', [lambda1, lambda2, lambda3]);

If weights are provided to both the model and the computational routine, the latter weights take precedence. If weights are provided solely through the model, and some factorizations do not define a weight, weight is assumed to be 1.

Instead of absolute weights, it is possible to define relative weights using the relweight field of a factorization or the RelWeights option of the computational routine. Relative weights \(\rho_i\) are translated into absolute weights using the following formula:

\[\lambda_i = 2 \frac{\rho_i}{\sum_j \rho_j} \frac{1}{N_i}\]

in which \(N_i\) is the number of entries in a tensor Ti, i.e., Ni = prod(getsize(Ti)). If Ti is incomplete, then Ni = length(Ti.val). For regularization terms, Ni = sum(cellfun(@numel, f)) in which f is the cell of all expanded factors to be regularized by regularization term i.

In the case weights are provided by the model, absolute weights (the weight field) and relative weights (the relweight field) cannot be combined, i.e., if one factorization defines the field weight it cannot define relweight and no other factorization can define relweight.

Providing extra information

Some computational routines accept extra information provided by the model to speed up computations. Currently, the ccpd_nls and ccpd_minf routines exploit symmetry if symmetry is detected or if a dataset is explicitly declared symmetric using the issymmetric field. The example below exploits the symmetry between the first and second mode of T.

model = struct;
model.variables = {A, B};
model.factors{1} = {{1}};
model.factors{2} = {{2}};
model.factorizations{1}.data = T;
model.factorizations{1}.cpd = {1,1,2};
model.factorizations{1}.issymmetric = true;

Ures = ccpd_nls(model);

The cpd field defines the type of symmetry.

The transform field

The model.transform field facilitates the creation of multiple factors having a similar structure. Using a single line of code, multiple variables can be transformed using the same transformation. The transformations do not necessarily have the same parameters.

Consider the following example. Each column in factor matrix 1 is a polynomial evaluated in points t1. Similarly each column in factor matrix 2 is a polynomial evaluated in points t2, and the same holds for factor matrix 3. Without the transform field, this is written as:

model.factors{1} = {1, @(z,task) struct_poly(z, task, t1)};
model.factors{2} = {2, @(z,task) struct_poly(z, task, t3)};
model.factors{3} = {3, @(z,task) struct_poly(z, task, t3)};

The transform field allows the code block above to be rewritten as

model.transform{1} = {1:3, @struct_poly, {t1, t2, t3}};

Both code fragments result in exactly the same model, as the transform fields are converted to factors by the DSL parser sdf_check. If all points sets are the same, i.e., if t1 = t2 = t3 = t, this can be further simplified as

model.transform{1} = {1:3, @struct_poly, t};

The transform field should be a struct or a cell with different transformations. (In contrast to variables and factors, the names of the transformations do not have a meaning.) A transform should have the following structure:

% as a struct
model.transform.a = {varrefs, factrefs, func1, args1, func2, args2, ...};
% or as a cell
model.transform{1} = {varrefs, factrefs, func1, args1, func2, args2, ...};

The entry varrefs contains references to variables, either indexed or named. The entry factrefs contains the indices or names of the factors to be created as a result of the transformations func1, func2, etc. This entry is optional. If omitted, factrefs = varrefs. An arbitrary number of transformations func1, func2 can be defined (including zero transformations). Each transformation can have an arbitrary number of parameters (including zero), separated by commas. For example, if func1 does not require any parameter, and func2 requires two, the code is as follows:

model.transform{1} = {varrefs, factrefs, func1, func2, param1, param2};

In the remaining part of this subsection, different restrictions are outlined.

  • varrefs: variable references should be valid references to defined variables. If factrefs is omitted, the rules for factrefs also apply for varrefs.
  • factrefs: factor references should be valid references to undefined factors. A number of additional rules apply:
    • All entries of the cell factrefs (after auto-wrapping) should be either integers or strings. Mixing named and indexed references is not allowed.
    • If model.factors is defined, and named factors are used, then named references should be used for factrefs as well. If indexed factors are used, indexed references should be used for factrefs.
    • All references in factrefs should point to non-existing factors to prevent overwriting previously defined factors. Factors can be defined previously using model.factors (this field is always processed before model.transform), or by previous transforms. This also means that all references in factrefs should be unique.
    • The number of references in varrefs should be equal to the number of references in factrefs.
    • If named references are used in factrefs the list of factors is created as follows. First the factors defined using the factors field are added. Then the factors created by the first transform model.transform{1} are added in the order defined by factrefs. Next the factors created by the second transform model.transform{2} are added, and so on. This order is important if indexed references are used in the factorizations field.
  • func: each transformation should be a function handle to a valid transformation, e.g., struct_xxx in which xxx can be any transformation defined in Tensorlab. func should accept two + \(N\) arguments in which \(N\) is the number of extra parameters.
  • arg: arguments, or parameters, of the transformations. Each created factor can have a different parameter value for a transformation. How the parameters are distributed to the factors depends on the type of the parameter:
    • if arg is not a cell, every created factor gets the same value for the parameter.
    • if arg is a cell of length 1, every created factor gets the same value for the parameter, being arg{1}. (Additional braces may be necessary depending on the intended result. For example, to pass a cell {1,2} it has to be wrapped: {{1,2}}.)
    • if arg is a cell with length length(factrefs) (or length(varrefs) if factrefs is omitted), the \(n\)th factor gets as parameter the value arg{n}.
    • if arg is a cell of a different length, an error is thrown. If one intends to have this cell as a parameter for all factors, arg should be replaced by {arg} such that the length 1 case applies.

The options field

The model.options field can be used to set additional options to allow or disallow certain language constructs. The field model.options is a struct with case-sensitive fields defining the option names. Currently, one option can be defined:

  • model.options.ImplicitFactors can be used to allow (true) or disallow (false) the use of implicit factors. The default is false.

The options can be permanently set to a value chosen by the user. If a file tensorlabsettings.mat is on the search path, this file is loaded by sdf_check. All fields in the sdfoptions struct (if present in the file) are used automatically. Hence, if sdfoptions.ImplicitFactors = true, the default value for model.options.ImplicitFactors is set to true. If an option is defined in model.options and sdfoptions, the value provided by model.options takes precedence.

Implicit factors

If implicit factors are allowed, variables can be used directly in factorizations. For example, consider the following:

model = struct;
model.variables = {rand(4,3), rand(5,3)};
model.factors{2} = 2;
model.factorizations{1}.data = rand(4,4,5);
model.factorizations{1}.cpd  = {1,1,2};

If sdf_check(model) is called, an error is thrown as factor 1 is not defined (model.factors{1} is empty). If implicit factors are allowed, the DSL parser sdf_check attempts to create all missing factors by using variables with the same reference. Hence, in the example above, model.variables{1} can be used as a replacement for model.factors{1}. Therefore, sdf_check creates the new factor matrix 1 as variable 1, i.e., as if the following had been written:

model = struct;
model.variables = {rand(4,3), rand(5,3)};
model.factors{1} = 1;
model.factors{2} = 2;
model.factorizations{1}.data = rand(4,4,5);
model.factorizations{1}.cpd  = {1,1,2};

Note that implicit factors are only created in the last phase of the DSL parsing, i.e., after model.factors has been checked, and after all transforms in model.transform have been converted to factors. If named factors are used, the new implicitly defined factors are added after the already defined factors, in the order in which they occur in model.factorizations. If indexed variables and named factors are used, the newly created factor has as name IFx with x the index of the variable to which is referred in the factorization. Note that IFx is only created when needed, so it cannot be referenced before it is created. For example, the following model is invalid:

model = struct;
model.variables = cpd_rnd([10 11], 5);
model.factors.A = 1;
model.factorizations{1}.data = randn(10,11,11);
model.factorizations{1}.cpd  = {1, 'IF2', 'IF2'};
model.options.ImplicitFactors = true;
sdf_check(model, 'print') % throws unknown factor error

The factors IF2 cannot be resolved because no factor or variable with the name IF2 is known. The following model is valid though:

model = struct;
model.variables = cpd_rnd([10 11], 5);
model.factors.A = 1;
model.factorizations{1}.data = randn(10,11,11);
model.factorizations{1}.cpd  = {1, 2, 'IF2'};
model.options.ImplicitFactors = true;
sdf_check(model, 'print')

Now sdf_check creates a factor IF2 when the reference 2 is encountered. When the reference IF2 is processed, this factor already exists and can therefore be resolved.