aspire.samplers.base#
Attributes#
Classes#
Base class for all samplers. |
Module Contents#
- class aspire.samplers.base.Sampler(log_likelihood, log_prior, dims, prior_flow, xp, dtype=None, parameters=None, preconditioning_transform=None)[source]#
Base class for all samplers.
- Parameters:
log_likelihood (
Callable) – The log likelihood function.log_prior (
Callable) – The log prior function.dims (
int) – The number of dimensions.flow (
Flow) – The flow object.xp (
Callable) – The array backend to use.parameters (
list[str] | None) – The list of parameter names. If None, any samples objects will not have the parameters names specified.prior_flow (aspire.flows.base.Flow)
dtype (Any | str | None)
preconditioning_transform (Callable | None)
- log_likelihood(samples)[source]#
Computes the log likelihood of the samples.
Also tracks the number of likelihood evaluations.
- Parameters:
samples (aspire.samples.Samples)
- Return type:
- log_prior(samples)[source]#
Computes the log prior of the samples.
- Parameters:
samples (aspire.samples.Samples)
- Return type:
- config_dict(include_sample_calls='last')[source]#
Returns a dictionary with the configuration of the sampler.
- Parameters:
include_sample_calls (
bool | str, optional) – Whether to include the sample calls in the configuration. Default is True. If True, and if the sampler has a sample method with a calls attribute, the calls will be included in the config under the key “sample_calls”. If this fails for any reason, a warning will be logged and the sample calls will be omitted.- Return type:
dict
- build_checkpoint_state(samples, iteration=None, meta=None, include_sample_calls='last')[source]#
Prepare a serializable checkpoint payload for the sampler state.
- Parameters:
samples (aspire.samples.Samples)
iteration (int | None)
meta (dict | None)
include_sample_calls (str | bool)
- Return type:
dict
- serialize_checkpoint(state, protocol=None)[source]#
Serialize a checkpoint state to bytes with pickle.
- Parameters:
state (dict)
protocol (int | None)
- Return type:
bytes
- default_checkpoint_callback(state)[source]#
Store the latest checkpoint (state + pickled bytes) on the sampler.
- Parameters:
state (dict)
- Return type:
None
- default_file_checkpoint_callback(file_path)[source]#
Return a simple default callback that overwrites an HDF5 file.
- Parameters:
file_path (str | pathlib.Path | None)
- Return type:
Callable[[dict], None]
- save_checkpoint_to_hdf(state, h5_file, path='sampler_checkpoints', dsetname=None, protocol=None)[source]#
Save a checkpoint state into an HDF5 file as a pickled blob.
- Parameters:
state (dict)
path (str)
dsetname (str | None)
protocol (int | None)
- Return type:
None
- load_checkpoint_from_file(file_path, h5_path='checkpoint', dsetname='state')[source]#
Load a checkpoint dictionary from .pkl or .hdf5 file.
- Parameters:
file_path (str | pathlib.Path)
h5_path (str)
dsetname (str)
- Return type:
dict
- restore_from_checkpoint(source)[source]#
Restore sampler state from a checkpoint source.
- Parameters:
source (str | bytes | dict)
- Return type:
tuple[aspire.samples.Samples, dict]