undark-lab / swyft

A system for scientific simulation-based inference at scale.
Other
159 stars 13 forks source link

composite priors do not work in stores #96

Closed elidub closed 2 years ago

elidub commented 2 years ago

The composite priors defined as in the tutorial notebook don't work in stores. Changing priors in tutorial notebooks with

from swyft.utils import tensor_to_array, array_to_tensor
from toolz import compose

uniform = torch.distributions.Uniform(array_to_tensor(low), array_to_tensor(high))
prior = swyft.Prior(
        cdf = compose(tensor_to_array, lambda x: uniform.cdf(x), array_to_tensor),
        icdf = compose(tensor_to_array, lambda x: uniform.icdf(x), array_to_tensor),
        log_prob = compose(tensor_to_array, lambda x: uniform.log_prob(x), array_to_tensor),
        n_parameters = n_parameters,
    )

Gives the following error: PicklingError: Can't pickle <function <lambda> at 0x1530b34e7e50>: attribute lookup <lambda> on __main__ failed

bkmi commented 2 years ago

Thank you for reporting the issue. I'll fix it before the 0.3.0 release.

The reason for this is due to how pickle works. (It cannot pickle lambda functions)

bkmi commented 2 years ago

I created a solution to this issue and committed it directly to master c24ce4c5f50e8ebe56eb90e5527c7819fcf4dbc0.

elidub commented 2 years ago

Works great!