How to use solvers to generate weights from ensemble?

How do I use a nengo.solvers instance, such as nengo.solvers.LstqL2(weights=True) to generate weights from a nengo.Ensemble instance?

Assuming I’m using basic nengo.LIF neurons. Otherwise, I would have to use @psipeter’s research or look into Solving for decoders by simulating the neurons over time.

I think the code of the nengo.builder.connection.solve_for_decoders function might answer your question?

(edit: forgot link)

I get it now. I knew how to call a nengo.solvers instance, but I was confused about the arguments. I now understand you have to:

  1. Choose some evaluation points
  2. Get activities from neurons using those evaluation points
  3. Get the target points for those evaluation points
  4. Use the activities, the target and the encoders from the post population as arguments to the nengo.solvers

This is demonstrated in the code below:

import nengo
import numpy as np

n_neurons = 100
seed = 0

with nengo.Network(seed=seed) as tmp_model:
    pre = nengo.Ensemble(n_neurons, 1, seed=seed)
    post = nengo.Ensemble(n_neurons, 1, seed=seed)

with nengo.Simulator(tmp_model) as tmp_sim:
    pass

pre_built = tmp_sim.data[pre]
x = np.dot(pre_built.eval_points, pre_built.encoders.T / pre.radius)
activities = pre.neuron_type.rates(x, pre_built.gain, pre_built.bias)

solver = nengo.solvers.LstsqL2(weights=True)
weights, _ = solver(activities, pre_built.eval_points, E=tmp_sim.data[post].scaled_encoders.T)