NotImplementedError: Solvers must implement '__call__'

I am getting the following errors on the code below:

import nengo_dl
import nengo
from nengo import solvers
import math
import numpy as np
model = nengo.Network()

case 1:

with model:
    inp = nengo.Node(lambda t: np.cos(2 * np.pi * t), label="cosine wave"
    A = nengo.Ensemble(30, 1)
    nengo.Connection(inp, A)
    B = nengo.Ensemble(30, 1)
 
    conn1 = nengo.Connection(A, B,
                             transform=nengo_dl.dists.Glorot(),
                             function=math.sin,
                             learning_rule_type=nengo.BCM(),
                             label="conn1",
                             solver=nengo.solvers.Solver(weights=True)
                             )

with nengo.Simulator(model, dt=1e-3) as sim:
    sim.run(1)

conn1_weights_sig = sim.signals[sim.model.sig[conn1]["weights"]]

print("conn1_weights_sig", conn1_weights_sig)

Error: NotImplementedError: Solvers must implement ‘__ call __’

Case 2:

conn1 = nengo.Connection(pre=A.neurons, post=B.neurons,
                         transform=nengo_dl.dists.Glorot(),
                         function=math.sin,
                         learning_rule_type=nengo.BCM(),
                         label="conn1",
                         solver=solvers.Solver(weights=True)
                         )

Error: nengo.exceptions.ValidationError: Connection.function: function ‘’ must accept a single np.array argument

case 3:

conn1 = nengo.Connection(pre=A, post=B,
                         transform=nengo_dl.dists.Glorot(),
                         function=math.sin,
                         learning_rule_type=nengo.BCM(),
                         label="conn1",
                         solver=solvers.LstsqL2(weights=True)
                         )

conn1_weights_sig = sim.signals[sim.model.sig[conn1]["weights"]]

Error: TypeError: ‘NoneType’ object is not subscriptable

I believe it is because I am using function. But, I don’t know why I am getting these errors and even if it runs, why are there no weights (case 3)? With nengo.Simulator is same in all three cases.

Thank you for your time :slight_smile:

Solver is an abstract base class that the actual solvers are derived from. So here Case 3 is correct; you want to be using a specific solver like LstsqL2.

For the function, you want to use a function that can handle np.array inputs. So if you use np.sin instead of math.sin, that should fix that problem.

In the last case, it’s not quite clear why you’re getting that error. The first step would be to see where the error is coming from. Do you get a stack trace for that error, and if so, can you provide it?

Hi @Eric, thank you for your reply.

  1. You mentioned

I have used solvers.Solver(weights=True) in some other code and it was executed without any error. What could be the reason?

The only difference between this code and the other code where I used solvers.Solver(weights=True) was there was no function in other code. But, I don’t think function should be the reason why solvers.Solver(weights=True) was executed in other code without any error.

  1. I tried doing conn1_weights_sig = sim.signals[sim.model.sig[conn1]["weights"]]) outside nengo.Simulator, hence the error

Error: TypeError: ‘NoneType’ object is not subscriptable

with nengo.Simulator(model, dt=1e-3) as sim:
    sim.run(1)
conn1_weights_sig = sim.signals[sim.model.sig[conn1]["weights"]]

When it was inside nengo.Simulator, I could see the weights of the model. To access the weights outside nengo.Simulator, I must use sim.data[conn1].weights.

But, my main problem is still the solver.