Skip to content

Bug: Sampling functions with structure_prior_from_depth/max_depth is not deterministic #29

@lfrommelt

Description

@lfrommelt

How to reproduce:

prior=structure_prior_from_depth(depth)

print(prior)

restart runtime

prior=structure_prior_from_depth(depth)

print(prior)

At this point we can see two differently ordered dictionaries. Since the position of items in the prior dictionaries is used during sampling, this will lead to undeterministic behaviour despite seeding. I assume the

UserWarning: No hashed prior found. Sample frequencies may diverge from the prior. Consider burning this prior first.

warning might be related to this issue (if the hash that is being referred to is in fact the position as int), but I am not sure and I think this should be solved instead of warning about it, anyways.

Here is my recommended fix: Instead of making sure that the outputs of structure_prior_from[_max]_depth returns deterministic and ordered results, the sampling of equations should not depend on the ordering of (usually unordered) dictionaries. Since the dictionary/prior keys are strings, we can use the sorted strings as an arbitrary but deterministic ordering, by including the following line in sample.sample:

    _prior = copy.deepcopy(prior)
    _prior = {prior_type: {key: _prior[prior_type][key] for key in sorted(_prior[prior_type])} for prior_type in _prior.keys()}
    if max_depth is not None and depth is not None:

Or more explicitly: iterate over the priors and then do the dict comprehension individiually. Also, apparently nowadays any python dict is ordered, not only OrderedDicts, so after this point we don't have to worry anymore.

I'm not sure if there are additional chunks that need to be changed, but this fix worked pretty well for my own use-case.

Cheers,
Leonard

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions