leaspy.samplers.base module
- class AbstractIndividualSampler(name: str, shape: Tuple[int, ...], *, n_patients: int, acceptation_history_length: int = 25)
Bases:
AbstractSamplerAbstract class for samplers of individual random variables.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- n_patients
int Number of patients.
- acceptation_history_length
int> 0 (default 25) Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (It is the same for population or individual variables.)
- name
- Attributes:
- name
str Name of variable
- shape
tupleofint Shape of variable
- n_patients
int Number of patients.
- acceptation_history_length
int Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (It is the same for population or individual variables.)
- acceptation_history
torch.Tensor History of binary acceptations to compute mean acceptation rate for the sampler in MCMC-SAEM algorithm. It keeps the history of the last acceptation_history_length steps.
- name
Methods
sample(dataset, model, realizations, ...)Sample new realization (either population or individual) for a given
CollectionRealizationstate,Dataset,AbstractModel, and temperature.- abstract sample(dataset: Dataset, model: AbstractModel, realizations: CollectionRealization, temperature_inv: float, **attachment_computation_kws) Tuple[Tensor, Tensor]
Sample new realization (either population or individual) for a given
CollectionRealizationstate,Dataset,AbstractModel, and temperature.<!> Modifies in-place the realizations object, <!> as well as the model through its update_MCMC_toolbox for population variables.
- Parameters:
- dataset
Dataset Dataset class object build with leaspy class object Data, model & algo
- model
AbstractModel Model for loss computations and updates
- realizations
CollectionRealization Contain the current state & information of all the variables of interest
- temperature_inv
float> 0 Inverse of the temperature used in tempered MCMC-SAEM
- **attachment_computation_kws
Optional keyword arguments for attachment computations. As of now, we only use it for individual variables, and only attribute_type. It is used to know whether to compute attachments from the MCMC toolbox (esp. during fit) or to compute it from regular model parameters (esp. during personalization in mean/mode realization)
- dataset
- Returns:
- attachment, regularity_var
torch.Tensor The attachment and regularity tensors (only for the current variable) at the end of this sampling step (globally or per individual, depending on variable type). The tensors are 0D for population variables, or 1D for individual variables (with length n_individuals).
- attachment, regularity_var
- class AbstractPopulationSampler(name: str, shape: Tuple[int, ...], *, acceptation_history_length: int = 25, mask: Tensor | None = None)
Bases:
AbstractSamplerAbstract class for samplers of population random variables.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- acceptation_history_length
int> 0 (default 25) Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (It is the same for population or individual variables.)
- mask
torch.Tensor, optional If not None, mask should be 0/1 tensor indicating the sampling variable to adapt variance from 1 indices are kept for sampling while 0 are excluded.
- name
- Attributes:
- name
str Name of variable
- shape
tupleofint Shape of variable
- acceptation_history_length
int Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (It is the same for population or individual variables.)
- acceptation_history
torch.Tensor History of binary acceptations to compute mean acceptation rate for the sampler in MCMC-SAEM algorithm. It keeps the history of the last acceptation_history_length steps.
- mask
torch.Tensorof obj:bool, optional If not None, mask should be 0/1 tensor indicating the sampling variable to adapt variance from 1 (True) indices are kept for sampling while 0 (False) are excluded.
- name
Methods
sample(dataset, model, realizations, ...)Sample new realization (either population or individual) for a given
CollectionRealizationstate,Dataset,AbstractModel, and temperature.- abstract sample(dataset: Dataset, model: AbstractModel, realizations: CollectionRealization, temperature_inv: float, **attachment_computation_kws) Tuple[Tensor, Tensor]
Sample new realization (either population or individual) for a given
CollectionRealizationstate,Dataset,AbstractModel, and temperature.<!> Modifies in-place the realizations object, <!> as well as the model through its update_MCMC_toolbox for population variables.
- Parameters:
- dataset
Dataset Dataset class object build with leaspy class object Data, model & algo
- model
AbstractModel Model for loss computations and updates
- realizations
CollectionRealization Contain the current state & information of all the variables of interest
- temperature_inv
float> 0 Inverse of the temperature used in tempered MCMC-SAEM
- **attachment_computation_kws
Optional keyword arguments for attachment computations. As of now, we only use it for individual variables, and only attribute_type. It is used to know whether to compute attachments from the MCMC toolbox (esp. during fit) or to compute it from regular model parameters (esp. during personalization in mean/mode realization)
- dataset
- Returns:
- attachment, regularity_var
torch.Tensor The attachment and regularity tensors (only for the current variable) at the end of this sampling step (globally or per individual, depending on variable type). The tensors are 0D for population variables, or 1D for individual variables (with length n_individuals).
- attachment, regularity_var
- class AbstractSampler(name: str, shape: Tuple[int, ...], *, acceptation_history_length: int = 25)
Bases:
ABCAbstract sampler class.
- Parameters:
- name
str The name of the random variable to sample.
- shape
tupleofint The shape of the random variable to sample.
- acceptation_history_length
int> 0 (default 25) Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (It is the same for population or individual variables.)
- name
- Raises:
- Attributes:
- name
str Name of variable
- shape
tupleofint Shape of variable
- acceptation_history_length
int Deepness (= number of iterations) of the history kept for computing the mean acceptation rate. (Same for population or individual variables by default.)
- acceptation_history
torch.Tensor History of binary acceptations to compute mean acceptation rate for the sampler in MCMC-SAEM algorithm. It keeps the history of the last acceptation_history_length steps.
- name
Methods
sample(dataset, model, realizations, ...)Sample new realization (either population or individual) for a given
CollectionRealizationstate,Dataset,AbstractModel, and temperature.- abstract sample(dataset: Dataset, model: AbstractModel, realizations: CollectionRealization, temperature_inv: float, **attachment_computation_kws) Tuple[Tensor, Tensor]
Sample new realization (either population or individual) for a given
CollectionRealizationstate,Dataset,AbstractModel, and temperature.<!> Modifies in-place the realizations object, <!> as well as the model through its update_MCMC_toolbox for population variables.
- Parameters:
- dataset
Dataset Dataset class object build with leaspy class object Data, model & algo
- model
AbstractModel Model for loss computations and updates
- realizations
CollectionRealization Contain the current state & information of all the variables of interest
- temperature_inv
float> 0 Inverse of the temperature used in tempered MCMC-SAEM
- **attachment_computation_kws
Optional keyword arguments for attachment computations. As of now, we only use it for individual variables, and only attribute_type. It is used to know whether to compute attachments from the MCMC toolbox (esp. during fit) or to compute it from regular model parameters (esp. during personalization in mean/mode realization)
- dataset
- Returns:
- attachment, regularity_var
torch.Tensor The attachment and regularity tensors (only for the current variable) at the end of this sampling step (globally or per individual, depending on variable type). The tensors are 0D for population variables, or 1D for individual variables (with length n_individuals).
- attachment, regularity_var