Using prebuilt nengo models

Hi,

Is there a way to save models (as prebuilt) and then simulate them without the need to rebuild them?

Thanks :slight_smile:

Hi Elishai,
For the standard Nengo backend, the easiest way to do this is to create a Simulator object from your model, and then use pickle to save it to file. Here’s an example with a simple demo network:

import pickle
import nengo
import numpy as np

with nengo.Network() as model:
    inp = nengo.Node(np.cos)
    ens = nengo.Ensemble(100, dimensions=1)
    nengo.Connection(inp, ens)
    probe = nengo.Probe(ens)


built = nengo.Simulator(model)


with open('built_model.pkl', 'wb') as pfile:
    pickle.dump(built, pfile)


with open('built_model.pkl', 'rb') as pfile:
    sim = pickle.load(pfile)


with sim:
    sim.run(1)

This isn’t guaranteed to work with all simulator backends, so you may run into issues if you are using e.g. NengoFPGA to run your model. In that case, there is the fallback option of building your model, manually saving each of the parameters you care about (e.g. decoders), and then passing those directly as arguments to ensembles, connections, etc. when creating a new model. I’m not sure if that approach would be helpful for what you’re trying to accomplish though.

Anyway, if this doesn’t solve your problem or you have follow-up questions, please let us know.

GREAT. Thank you Peter!