leaspy.models.stateful

Classes

StatefulModel

Stateful models have an internal State to handle parameters and variables.

Module Contents

class StatefulModel(name, **kwargs)[source]

Bases: leaspy.models.base.BaseModel

Stateful models have an internal State to handle parameters and variables.

Parameters:
namestr

The name of the model.

Attributes:
stateState

The internal state of the model, which contains the variables and their values.

tracked_variablesset [str]

Set of variable names that are tracked by the model. These variables are not necessarily part of the model’s state but are monitored for changes or updates. This can include variables that are relevant for the model’s operation but not directly stored in the state.

Parameters:

name (str)

tracked_variables: set[str]
track_variable(variable)[source]

Track a variable by its name.

Parameters

variableVariableName

The name of the variable to track. This variable will be monitored for changes or updates.

Parameters:

variable (VariableName)

Return type:

None

track_variables(variables)[source]

Track multiple variables by their names.

Parameters:
variablesIterable [VariableName]

An iterable containing the names of the variables to track. Each variable will be monitored for changes or updates.

Parameters:

variables (Iterable[VariableName])

Return type:

None

untrack_variable(variable)[source]

Untrack a variable by its name.

Parameters

variableVariableName

The name of the variable to untrack. This variable will no longer be monitored for changes or updates.

Parameters:

variable (VariableName)

Return type:

None

untrack_variables(variables)[source]

Untrack multiple variables by their names.

Parameters:
variablesIterable [VariableName]

An iterable containing the names of the variables to untrack. Each variable will no longer be monitored for changes or updates.

Parameters:

variables (Iterable[VariableName])

Return type:

None

property state: State

Get the internal state of the model.

Returns:
StateState

The internal state of the model, which contains the variables and their values.

Raises:
LeaspyModelInputError

If the model’s state is not initialized yet.

Return type:

State

property dag: VariablesDAG

Get the underlying DAG of the model’s state.

Returns:
: VariablesDAG

The directed acyclic graph (DAG) representing the model’s variables and their relationships

Return type:

VariablesDAG

property hyperparameters_names: tuple[VariableName, Ellipsis]

Get the names of the model’s hyperparameters.

Returns:
: tuple [VariableName, others…]

A tuple containing the names of the model’s hyperparameters.

Return type:

tuple[VariableName, Ellipsis]

property parameters_names: tuple[VariableName, Ellipsis]

Get the names of the model’s parameters.

Returns:
: tuple [VariableName, others…]

A tuple containing the names of the model’s parameters.

Return type:

tuple[VariableName, Ellipsis]

property population_variables_names: tuple[VariableName, Ellipsis]

Get the names of the population latent variables.

Returns:
: tuple [VariableName, …]

A tuple containing the names of the population latent variables.

Return type:

tuple[VariableName, Ellipsis]

property individual_variables_names: tuple[VariableName, Ellipsis]

Get the names of the individual latent variables.

Returns:
: tuple [VariableName, …]

A tuple containing the names of the individual latent variables.

Return type:

tuple[VariableName, Ellipsis]

property parameters: DictParamsTorch

Dictionary of values for model parameters.

Returns:
: DictParamsTorch

A dictionary mapping parameter names to their values (as tensors).

Return type:

DictParamsTorch

property hyperparameters: DictParamsTorch

Dictionary of values for model hyperparameters.

Returns:
: DictParamsTorch

A dictionary mapping hyperparameter names to their values (as tensors).

Return type:

DictParamsTorch

initialize(dataset=None)[source]

Overloads base model initialization (in particular to handle internal model State).

<!> We do not put data variables in internal model state at this stage (done in algorithm)

Parameters:
datasetDataset, optional

Input dataset from which to initialize the model.

Parameters:

dataset (Optional[Dataset])

Return type:

None

load_parameters(parameters)[source]

Instantiate or update the model’s parameters.

It assumes that all model hyperparameters are defined.

Parameters:
parametersKwargsType]

Contains the model’s parameters.

Parameters:

parameters (KwargsType)

Return type:

None

abstractmethod get_variables_specs()[source]

Return the specifications of the variables (latent variables, derived variables, model ‘parameters’) that are part of the model.

Returns:
NamedVariablesNamedVariables

The specifications of the model’s variables.

Return type:

NamedVariables

move_to_device(device)[source]

Move a model and its relevant attributes to the specified torch.device.

Parameters:
devicetorch.device

The device to which the model and its attributes should be moved.

Parameters:

device (device)

Return type:

None