I’ve had a few different situations where I’ve wanted to compute the norm of the vector stored in a
spa.State component. This is handy for doing things like detecting if a
spa.State is empty. But, I’ve never been quite happy with the different ways of doing so.
However, I was just chatting with Ivana and we came up with a nice way to do it that I haven’t seen before, and so I thought I’d post it here:
import nengo import numpy as np import nengo.spa as spa model = spa.SPA() with model: D = 256 model.a = spa.State(D, subdimensions=4) # add an output that computes the sum of squares for each sub-ensemble def sumsq(x): return np.sum(x**2) model.a.state_ensembles.add_output('sumsq', sumsq) # add the sums of squares together norm2 = nengo.Ensemble(n_neurons=50, dimensions=1) nengo.Connection(model.a.state_ensembles.sumsq, norm2, transform=np.ones((1, model.a.state_ensembles.sumsq.size_out))) # take the square root (note that this is optional, as for many uses norm2 is as good as norm) norm = nengo.Ensemble(n_neurons=50, dimensions=1) nengo.Connection(norm2, norm, function=lambda x: np.sqrt(np.abs(x)))
One sneaky thing in here is to set
subdimensions=4. I find the accuracy degrades too much as you go above that.
Anyway, I just thought I’d post it here in case anyone needs this in the future!