leaspy.variables.specs
Attributes
Classes
Interface for variable specifications. |
|
Base class for variable that is not dependent on any other variable. |
|
Hyperparameters that can not be reset. |
|
A convenient class to produce a function to collect sufficient stats that are existing or dedicated variables (to be automatically created). |
|
Variable for model parameters with a maximization rule. This variable shouldn't be sampled and it shouldn't be data, a hyperparameter or a linked variable. |
|
Variables for input data, that may be reset. |
|
Type of initialization for latent variables. |
|
Unobserved variable that will be sampled, with symbolic prior distribution [e.g. Normal('xi_mean', 'xi_std')]. |
|
Population latent variable. |
|
Individual latent variable. |
|
Variable which is a deterministic expression of other variables (we directly use variables names instead of mappings: kws <-> vars). |
|
Convenient dictionary for named variables specifications. |
Module Contents
- VariableName
- VariableValue
- VariableNameToValueMapping
- VariablesToFrozenSet
- VariablesLazyValuesRO
- VariablesLazyValuesRW
- SuffStatsRO
- SuffStatsRW
- class VariableInterface
Interface for variable specifications.
- is_settable: ClassVar[bool]
Is True if and only if state of variables is intended to be manually modified by user.
- fixed_shape: ClassVar[bool]
Is True as soon as we guarantee that shape of variable is only dependent on model hyperparameters, not data.
- abstractmethod compute(state)
Compute variable value from a state exposing a dict-like interface: var_name -> values.
If not relevant for variable type return None.
- Parameters:
- state
VariableNameToValueMapping The state to use in order to perform computations.
- state
- Returns:
VariableValueThe variable value computed from the state.
- Parameters:
state (VariableNameToValueMapping)
- Return type:
Optional[VariableValue]
- abstractmethod get_ancestors_names()
Get the names of the variables that the current variable directly depends on.
- Returns:
frozenset[VariableName]The set of ancestors variable names.
- Return type:
frozenset[VariableName]
- class IndepVariable
Bases:
VariableInterfaceBase class for variable that is not dependent on any other variable.
- get_ancestors_names()
Get the names of the variables that the current variable directly depends on.
- Returns:
frozenset[VariableName]The set of ancestors variable names.
- Return type:
frozenset[VariableName]
- compute(state)
Compute variable value from a state exposing a dict-like interface: var_name -> values.
If not relevant for variable type return None.
- Parameters:
- state
VariableNameToValueMapping The state to use in order to perform computations.
- state
- Returns:
VariableValueor None:The variable value computed from the state.
- Parameters:
state (VariableNameToValueMapping)
- Return type:
Optional[VariableValue]
- class Hyperparameter
Bases:
IndepVariableHyperparameters that can not be reset.
- value: VariableValue
The hyperparameter value.
- fixed_shape: ClassVar = True
Whether the variable has a fixed shape or not. For hyperparameters this is True.
- is_settable: ClassVar = False
Whether the variable is mutable or not. For hyperparameters this is False.
- to_device(device)
Move the value to specified device (other variables never hold values so need for this method).
- Parameters:
- device
torch.device The device on which to move the variable value.
- device
- Parameters:
device (device)
- Return type:
None
- class Collect(*existing_variables, **dedicated_variables)
A convenient class to produce a function to collect sufficient stats that are existing or dedicated variables (to be automatically created).
- Parameters:
existing_variables (VariableName)
dedicated_variables (LinkedVariable)
- dedicated_variables: Mapping[VariableName, LinkedVariable] | None = None
- class ModelParameter
Bases:
IndepVariableVariable for model parameters with a maximization rule. This variable shouldn’t be sampled and it shouldn’t be data, a hyperparameter or a linked variable.
- suff_stats: Collect
The symbolic update functions will take variadic suff_stats values, in order to re-use NamedInputFunction logic: e.g. update_rule=Std(‘xi’)
<!> ISSUE: for tau_std and xi_std we also need state values in addition to suff_stats values (only after burn-in) since we can NOT use the variadic form readily for both state and suff_stats (names would be conflicting!), we sent state as a special kw variable (a bit lazy but valid) (and we prevent using this name for a variable as a safety)
- update_rule: Callable[Ellipsis, VariableValue]
Update rule for normal phase, and memory-less (burn-in) phase unless update_rule_burn_in is not None.
- update_rule_burn_in: Callable[Ellipsis, VariableValue] | None = None
Specific rule for burn-in (currently implemented for some variables -> e.g. xi_std)
- fixed_shape: ClassVar = True
Is True as soon as we guarantee that shape of variable is only dependent on model hyperparameters, not data.
- is_settable: ClassVar = True
Is True if and only if state of variables is intended to be manually modified by user.
- compute_update(*, state, suff_stats, burn_in)
Compute the updated value for the model parameter using a maximization step.
- Parameters:
- state
VariableNameToValueMapping The state to use for computations.
- suff_stats
SuffStatsRO The sufficient statistics to use.
- burn_in
bool If True, use the update rule in burning phase.
- state
- Returns:
VariableValueThe computed variable value.
- Parameters:
state (VariableNameToValueMapping)
suff_stats (SuffStatsRO)
burn_in (bool)
- Return type:
VariableValue
- classmethod for_pop_mean(population_variable_name, shape)
Smart automatic definition of ModelParameter when it is the mean of Gaussian prior of a population latent variable.
- classmethod for_ind_mean(individual_variable_name, shape)
Smart automatic definition of ModelParameter when it is the mean of Gaussian prior of an individual latent variable.
- class DataVariable
Bases:
IndepVariableVariables for input data, that may be reset.
- fixed_shape: ClassVar = False
Is True as soon as we guarantee that shape of variable is only dependent on model hyperparameters, not data.
- is_settable: ClassVar = True
Is True if and only if state of variables is intended to be manually modified by user.
- class LatentVariableInitType
-
Type of initialization for latent variables.
- PRIOR_MODE = 'mode'
- PRIOR_MEAN = 'mean'
- PRIOR_SAMPLES = 'samples'
- class LatentVariable
Bases:
IndepVariableUnobserved variable that will be sampled, with symbolic prior distribution [e.g. Normal(‘xi_mean’, ‘xi_std’)].
- prior: SymbolicDistribution
- is_settable: ClassVar = True
Is True if and only if state of variables is intended to be manually modified by user.
- get_prior_shape(named_vars)
Get shape of prior distribution (i.e. without any expansion for IndividualLatentVariable).
- Parameters:
named_vars (Mapping[VariableName, VariableInterface])
- Return type:
- abstractmethod get_regularity_variables(value_name)
Get extra linked variables to compute regularity term for this latent variable.
- Parameters:
value_name (VariableName)
- Return type:
dict[VariableName, LinkedVariable]
- class PopulationLatentVariable
Bases:
LatentVariablePopulation latent variable.
- fixed_shape: ClassVar = True
Is True as soon as we guarantee that shape of variable is only dependent on model hyperparameters, not data.
- get_init_func(method)
Return a NamedInputFunction: State -> Tensor, that may be used for initialization.
- Parameters:
- method
LatentVariableInitTypeorstr The method to be used.
- method
- Returns:
NamedInputFunctionThe initialization function.
- Parameters:
method (Union[str, LatentVariableInitType])
- Return type:
- get_regularity_variables(variable_name)
Return the negative log likelihood regularity for the provided variable name.
- Parameters:
- variable_name
VariableName The name of the variable for which to retrieve regularity.
- variable_name
- Returns:
dict[VariableName,LinkedVariable]The dictionary holding the
LinkedVariablefor the regularity.
- Parameters:
variable_name (VariableName)
- Return type:
dict[VariableName, LinkedVariable]
- class IndividualLatentVariable
Bases:
LatentVariableIndividual latent variable.
- fixed_shape: ClassVar = False
Is True as soon as we guarantee that shape of variable is only dependent on model hyperparameters, not data.
- get_init_func(method, *, n_individuals)
Return a NamedInputFunction: State -> Tensor, that may be used for initialization.
- Parameters:
- method
LatentVariableInitTypeorstr The method to be used.
- n_individuals
int The number of individuals, used to define the shape.
- method
- Returns:
NamedInputFunctionThe initialization function.
- Parameters:
method (Union[str, LatentVariableInitType])
n_individuals (int)
- Return type:
- get_regularity_variables(variable_name)
Return the negative log likelihood regularity for the provided variable name.
- Parameters:
- variable_name
VariableName The name of the variable for which to retrieve regularity.
- variable_name
- Returns:
dict[VariableName,LinkedVariable]The dictionary holding the
LinkedVariablefor the regularity.
- Parameters:
variable_name (VariableName)
- Return type:
dict[VariableName, LinkedVariable]
- class LinkedVariable
Bases:
VariableInterfaceVariable which is a deterministic expression of other variables (we directly use variables names instead of mappings: kws <-> vars).
- f: Callable[Ellipsis, VariableValue]
- is_settable: ClassVar = False
Is True if and only if state of variables is intended to be manually modified by user.
- fixed_shape: ClassVar = False
Is True as soon as we guarantee that shape of variable is only dependent on model hyperparameters, not data.
- get_ancestors_names()
Get the names of the variables that the current variable directly depends on.
- Returns:
frozenset[VariableName]The set of ancestors variable names.
- Return type:
frozenset[VariableName]
- compute(state)
Compute the variable value from a given State.
- Parameters:
- state
VariableNameToValueMapping The state to use for computations.
- state
- Returns:
VariableValueThe value of the variable.
- Parameters:
state (VariableNameToValueMapping)
- Return type:
VariableValue
- class NamedVariables(*args, **kws)
Bases:
collections.UserDictConvenient dictionary for named variables specifications.
- In particular, it:
forbids the collisions in variable names when assigning/updating the collection
forbids the usage of some reserved names like ‘state’ or ‘suff_stats’
automatically adds implicit variables when variables of certain kind are added (e.g. dedicated vars for sufficient stats of ModelParameter)
automatically adds summary variables depending on all contained variables (e.g. nll_regul_ind_sum that depends on all individual latent variables contained)
<!> For now, you should NOT update a NamedVariables with another one, only update with a regular mapping.
- FORBIDDEN_NAMES: ClassVar
- AUTOMATIC_VARS: ClassVar = ('nll_regul_ind_sum_ind', 'nll_regul_ind_sum')