How to save network to file and resume from same point

Hello!

Let’s say we build a network. Then we create a simulator and run it/step it for some time, using probes to retrieve data. We also have a learning_rule which modulates some connections.
How is it possible to save the current status of the network to a file, and then reload it and run/step it from the same point onwards?

Thanks

Panos

The most efficient way to save the “status” of a network is to save the current decoder weights. The actual neuron states are saveable, but are way more annoying to preserve and aren’t really necessary. @tcstewar made some tools for accomplishing this.

1 Like

Yes, I see there has been much talk on this.
So as I get it, in order to save the weights for a connection having a learning rule :

class Mousebrain(nengo.Network) :
    def build(self):
        with self :
            a = nengo.Ensemble(n_neurons=50, dimensions=2, radius=1)
            b = nengo.Ensemble(n_neurons=200, dimensions=2)
            conn = nengo.Connection(a,b, function=some_function, learning_rule_type=nengo.PES(learning_rate=1e-4, pre_tau=0.1))
            self.ws = WeightSaver(conn, 'conn_weights')

I don’t want the weights be initialized at 0 but be optimized towards some_function.
So I change the WeightSaver class and I don’t build a LoadFrom instance as I just want to save the weights

class WeightSaver(object):
    def __init__(self, connection, filename, sample_every=None, weights=False):
        assert isinstance(connection.pre, nengo.Ensemble)
        if not filename.endswith('.npy'):
            filename = filename + '.npy'
        self.filename = filename
        #connection.solver = LoadFrom(self.filename, weights=weights)
        self.probe = nengo.Probe(connection, 'weights', sample_every=sample_every)
        self.connection = connection
    def save(self, sim):
        np.save(self.filename, sim.data[self.probe][-1].T)

I create an instance of Mousebrain and a Simulator :

mousebrain = Mousebrain()
mousebrain.build()
mousebrain_sim = nengo.Simulator(mousebrain, dt=0.001) 

I run the simulator for some steps

mousebrain_sim.step()

and at some point I call the saver

mousebrain.ws.save(mousebrain_sim)

The file it creates is not readable, containing some gibbrish.

“NUMPY F {‘descr’: ‘<f8’, ‘fortran_order’: True, ‘shape’: (50, 2), }
ÞL\Þ¡Ç?Uø… and more like this

Is that the expected behavior?

Yep, that’s the expected result. The file is being saved as in a numpy format, so it can be loaded and read with np.load.

1 Like