Skip to content

Commit

Permalink
Changed FAQ for saving and loading objects
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldeistler committed Jan 27, 2022
1 parent 8febbe8 commit 23f3225
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions docs/docs/faq/question_05.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ with open("/path/to/my_posterior.pkl", "wb") as handle:
pickle.dump(posterior, handle)
```

Note: if you try to load a posterior that was saved under `sbi v0.14.x` or earlier under `sbi v0.15.0` or later, you have to add:
Note: posterior objects that were saved under `sbi v0.17.2` or older can not be loaded under `sbi v0.18.0` or newer.

Note: if you try to load a posterior that was saved under `sbi v0.14.x` or earlier under `sbi v0.15.x` until `sbi v0.17.x`, you have to add:
```python
import sys
from sbi.utils import user_input_checks_utils
Expand All @@ -22,35 +24,16 @@ sys.modules["sbi.user_input.user_input_checks_utils"] = user_input_checks_utils
to your script before loading the posterior.


`NeuralInference` objects are not picklable. There are two workarounds:

- Pickle with [`dill`](https://pypi.org/project/dill/) (has to be installed with `pip install dill` first)
```python
import dill

inference = SNPE(prior)
# ... run inference

with open("path/to/my_inference.pkl", "wb") as handle:
dill.dump(inference)
```

- Delete un-picklable attributes and serialize with pickle. Using this option, you will not be able to use the `retrain_from_scratch` feature and you can only use the default `SummaryWriter`.
As of `sbi v0.18.0`, `NeuralInference` objects are also picklable.
```python
import pickle

inference = SNPE(prior)
# ... run inference

posterior = inference.build_posterior()
inference._summary_writer = None
inference._build_neural_net = None

with open("/path/to/my_inference.pkl", "wb") as handle:
pickle.dump(inference, handle)
```
Then, to load:
```python
with open("/path/to/my_inference.pkl", "rb") as handle:
inference_from_disk = pickle.load(handle)
inference_from_disk._summary_writer = inference_from_disk._default_summary_writer()
```
However, saving and loading the `inference` object will slightly modify the object (in order to make it serializable). These modifications lead to the following two changes in behaviour:
1) Retraining from scratch is not supported, i.e. `.train(..., retrain_from_scratch=True)` does not work.
2) When the loaded object calls the `.train()` method, it generates a new tensorboard summary writer (instead of appending to the current one).

0 comments on commit 23f3225

Please sign in to comment.