Comments (4)
This is covered in FAQs in the official sbi
documentation here.
NeuralInference
objects are not picklable and the proposed way of saving these objects is by using dill
.
Since NeuralPosterior
objects are picklable , storing is as simple as:
import pickle
posterior = ...
with open("/path/to/posterior.pkl", "wb") as handle:
pickle.dump(posterior, handle)
from brian2modelfitting.
Thanks for the info. It might be worth looking into the sbi
code (or contacting the developers) to see whether there are alternatives to pickle/dill, e.g. if we can directly store the pytorch model state dictionary to disk (see pytorch docs). The problem with pickle-based approaches is that it stores more than we want (class structure, module names, etc.) which can easily break when switching between machines/versions/etc.
from brian2modelfitting.
Once we have NeuralPosterior
, we can access its state dictionary through net
instance by calling state_dict()
:
posterior.net.state_dict()
Then we can use PyTorch to save this state dictionary, which is basically ordered dictionary and is really easy to handle and is quite lightweight compared to pickled objects.
Here is the minimal working example where the storing of the neural density estimator's state dictionary is demonstrated.
import torch
from sbi import utils as utils
from sbi import analysis as analysis
from sbi.inference import SNPE, prepare_for_sbi, simulate_for_sbi
def simulator(params):
return params + torch.randn(params.shape) * 0.1
# data
prior = utils.BoxUniform(low=-2*torch.ones(3), high=2*torch.ones(3))
observation = torch.zeros(3)
# learning the density estimator and building the posterior
simulator, prior = prepare_for_sbi(simulator, prior)
inference = SNPE(prior)
theta, x = simulate_for_sbi(simulator, proposal=prior, num_simulations=500)
density_estimator = inference.append_simulations(theta, x).train()
og_posterior = inference.build_posterior(density_estimator)
# sampling from the posterior
samples = og_posterior.sample((10000,), x=observation)
log_probability = og_posterior.log_prob(samples, x=observation)
_ = analysis.pairplot(samples)
# save the density estimator state dictionary
torch.save(og_posterior.net.state_dict(), 'psd.pth')
and to load it:
# build new "empty" posterior
new_posterior = inference.build_posterior()
# load the state dictionary
new_posterior.net.load_state_dict(torch.load('psd.pth'))
# sampling from the new posterior
samples = new_posterior.sample((10000,), x=observation)
log_probability = new_posterior.log_prob(samples, x=observation)
_ = analysis.pairplot(samples)
Everything works like a charm.
from brian2modelfitting.
Closed via #52
from brian2modelfitting.
Related Issues (20)
- Typo in the exception message at the assertion of optimizer's parent class HOT 1
- Create class to wrap first stages of sbi HOT 1
- Investigate using dimensionality-reduction methods on outputs HOT 7
- Split up simulations if necessary HOT 2
- Generalize `FeatureMetric` HOT 1
- Enable all inference methods included in `sbi` HOT 2
- Inestigate additional visualization techniques of parameter space
- Support multiple output variables for sbi
- Support spikes in sbi
- Add tests for sbi
- Add documentation for sbi
- Refactor `Inferencer`/`Fitter`
- Enable GPU support
- Compare model fitting algorithms
- Enable the user to define features for model fitting
- Implement multi-objective optimisation
- Improve the simulation-based inference model
- Make dependencies like sbi optional
- Issue with Multi-run of Hodgkin-Huxley Example HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from brian2modelfitting.