Giter Club home page Giter Club logo

Comments (4)

akapet00 avatar akapet00 commented on June 23, 2024

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.

mstimberg avatar mstimberg commented on June 23, 2024

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.

akapet00 avatar akapet00 commented on June 23, 2024

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.

mstimberg avatar mstimberg commented on June 23, 2024

Closed via #52

from brian2modelfitting.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.