pyro-ppl / pyro

Deep universal probabilistic programming with Python and PyTorch
http://pyro.ai
Apache License 2.0
8.55k stars 987 forks source link

[feature request] Informative error message about potentially having forgotten to call pyro.clear_param_store #3094

Open e-pet opened 2 years ago

e-pet commented 2 years ago

Issue Description

pyro beginner here. I just spent several hours trying to decipher the reason for a bunch of quite uninformative error messages (see my forum post here), only to be taught by a friendly forum user that I just needed to call pyro.clear_param_store() in between training different models, and that that was the cause of the weird unintelligible error messages I got. To make things more beginner-friendly, it would be great if this problem could somehow be detected automatically, and an error message or a warning along the lines of "Did you maybe forget to call pyro.clear_param_store? See for details." could be shown. I am not yet knowledgeable enough about pytorch/pyro to understand how complex this would be to implement.

Environment

Win 10, python 3.9.12, pytorch 1.11.0, pyro 1.8.1+06911dc

Code Snippet

Not relevant I believe, but see my forum post for a fully reproducible example.

fritzo commented 2 years ago

Agreed, this does seem like a sharp edge.

One option would be to emit a warning if the param store is not empty during the first call of SVI.step() for each instance of SVI (and provide an option SVI(..., reuse_param_store=True) to silence the warning. Can anyone think of use cases this wouldn't cover, i.e. a common use case where the emitted warning would be inappropriate?

A further option would be to provide a context manager:

with pyro.new_param_store() as store:
    # do stuff
...
# then to reuse the store:
with store:
    # do stuff

I think we could even make the store an nn.Module to facilitate easy .cuda() etc.

martinjankowiak commented 2 years ago

one common-ish use case might be creating an SVI object with large num_particles that is only used for evaluation, e.g. after training; i guess this would trigger your warning

you already implemented the context manager ; )