pysdic.SubsetParametrization#
SubsetParametrization class#
- class SubsetParametrization(mask, fixed_values=None)[source]#
Bases:
ParametrizationA parametrization \(P\) that selects a subset of the output parameters to be driven by the input parameters, the other output parameters being fixed.
Lets \(\mathcal{S} \subset \{1, \dots, n_o\}\) be the set of the selected indices defined by a boolean
maskof shape \((n_o,)\), with \(n_i = |\mathcal{S}|\) the number of selected parameters. The parametrization maps the input parameters \(\vec{p_i} \in \mathbb{R}^{n_i}\) to the output parameters \(\vec{p_o} \in \mathbb{R}^{n_o}\) according to:\[\vec{p_o}[\mathcal{S}] = \vec{p_i} \quad \text{and} \quad \vec{p_o}[\overline{\mathcal{S}}] = \vec{v}[\overline{\mathcal{S}}]\]where \(\vec{v}\) are the fixed values of the non-selected parameters.
This is typically used to optimize only some parameters of a transformation while the others are kept fixed (see
pysdic.ReparametrizedTransformation).The Jacobian of the parametrization is the constant selection matrix \(\mathbf{E} \in \mathbb{R}^{n_o \times n_i}\) whose columns are the columns of the identity matrix at the selected indices:
\[\mathbf{J}_p = \frac{\partial P}{\partial \vec{p_i}} = \mathbf{E} = \mathbf{I}_{n_o}[:, \mathcal{S}]\]The parametrization can be decoded by extracting the selected parameters:
\[\vec{p_i} = P^{-1}(\vec{p_o}) = \vec{p_o}[\mathcal{S}]\]The decoding map is a left inverse of \(P\). It is also a right inverse only if all the parameters are selected (i.e. \(P\) is the identity).
See also
Class
pysdic.Parametrizationfor the base class of parametrizations.Class
pysdic.AffineParametrizationfor general affine parametrizations.
Note
pysdic.SubsetParametrizationis equivalent to anpysdic.AffineParametrizationwithmodes\(\mathbf{E}\) andoffset\(\vec{v}\) (with zeros at the selected indices), but it avoids dense matrix products. In particular, for any matrix \(\mathbf{J}\) with \(n_o\) columns (e.g. the Jacobian of a transformation with respect to its parameters), the chain rule reduces to a column selection:\[\mathbf{J} \cdot \mathbf{E} = \mathbf{J}[:, \mathcal{S}]\]which can be computed with
jacobian[..., parametrization.mask].Note
The
maskandfixed_valuesare constants of the parametrization. They are stored as read-only copies and cannot be modified after initialization.- Parameters:
mask (ArrayLike) – A boolean array of shape \((n_o,)\) indicating the selected output parameters (True) driven by the input parameters. At least one parameter must be selected.
fixed_values (Optional[ArrayLike], optional (default:
None)) – The values \(\vec{v}\) of shape \((n_o,)\) used for the non-selected output parameters. The values at the selected indices must be0.0(aValueErroris raised otherwise, to avoid ignoring values given by mistake). If None, the non-selected parameters are fixed to zero.
Examples
Optimize only the first and third parameters of a vector of four parameters, the second and fourth parameters being fixed:
parametrization = SubsetParametrization( mask=[True, False, True, False], fixed_values=[0.0, 5.0, 0.0, 7.0], ) parametrization.encode([1.0, 2.0]).p # array([1., 5., 2., 7.]) parametrization.decode([1.0, 5.0, 2.0, 7.0]).p # array([1., 2.])
A convenient way to freeze the current values of the parameters of a transformation is to pass them as
fixed_values, with zeros at the selected indices:parametrization = SubsetParametrization( mask=mask, fixed_values=numpy.where(mask, 0.0, transformation.parameters), )
Instantiation#
The SubsetParametrization class is instantiated directly with its constructor:
With a boolean mask of shape \((n_o,)\):
SubsetParametrization(mask)(the non-selected parameters are fixed to zero).With a boolean mask and fixed values \(\vec{v}\) of shape \((n_o,)\):
SubsetParametrization(mask, fixed_values)(the fixed values must be0.0at the selected indices, e.g.numpy.where(mask, 0.0, transformation.parameters)).
Implemented methods and attributes#
The following sections provide an overview of the attributes and methods defined
in the pysdic.SubsetParametrization class.
Attributes#
The pysdic.SubsetParametrization defines the following attributes
(the constants mask, indices and fixed_values are read-only):
[Get] the values \(\vec{v}\) of the non-selected output parameters. |
|
[Get] the indices \(\mathcal{S}\) of the selected output parameters, in increasing order. |
|
[Get] the input dimension \(n_i\) of the parametrization, i.e. the number of selected output parameters. |
|
[Get] the boolean mask of the selected output parameters. |
|
[Get] the output dimension \(n_o\) of the parametrization, i.e. the size of the mask. |
Encoding and decoding#
Use the encode and decode methods from
pysdic.Parametrization to perform the parametrization:
encode: mapsp_i→p_o(p_iinserted at the selected indices, the fixed values elsewhere).decode: mapsp_o→p_i(extraction of the selected parameters, left inverse ofencode).
The Jacobian with respect to the parameters can be computed by enabling the dp flag:
dp=True: compute ∂p_o / ∂p_i (constant selection matrix \(\mathbf{E} = \mathbf{I}_{n_o}[:, \mathcal{S}]\))
|
Insert the input parameters \(\vec{p_i}\) at the selected indices of the output parameters. |
|
Extract the selected parameters from the output parameters \(\vec{p_o}\). |