nengo / nengo-loihi

Run Nengo models on Intel's Loihi chip
https://www.nengo.ai/nengo-loihi/
Other
35 stars 12 forks source link

Force Loihi solvers to use `weights=True` #230

Open arvoelke opened 5 years ago

arvoelke commented 5 years ago

Continuation of #74.

Setting the solver to weights=True can improve accuracy by avoiding the use of DecodeNeurons, especially in the context of recurrent connections. I can add some examples in #224 to demonstrate.

One reason this is necessary is because weights=True can't be set on a passthrough connection. e.g., if you have x -> passthrough -> x as in the docs/examples/integrator_multi_d.ipynb example. Even though the passthrough ends up getting removed, there's no way to signal to the backend that I'd like to have the new connection use weights=True.

This cannot be done at the config level because if the network contains a node then you get the error:

nengo.exceptions.ValidationError: Connection.solver: weight solvers only work for connections from ensembles (got 'Node')
arvoelke commented 5 years ago

Here's a quick work-around that seems to do the trick (can add this to the top of your model):

# jupyter-specific: guard against this cell being re-run
if 'orig_build_chip_connection' not in locals():
    orig_build_chip_connection = (
        nengo_loihi.builder.connection.build_chip_connection)

    def monkeypatched_build_chip_connection(model, conn):
        # https://github.com/nengo/nengo-loihi/issues/230
        if (isinstance(conn.pre, nengo.Ensemble)
                and isinstance(conn.post, nengo.Ensemble)):
            print("Forcing neuron-to-neuron: %s" % conn)
            conn.solver = nengo.solvers.LstsqL2(weights=True)
        return orig_build_chip_connection(model, conn)

    nengo_loihi.builder.connection.build_chip_connection = (
        monkeypatched_build_chip_connection)