I’m new to Nengo, and I’ve just read the below example code:
network = nengo.Network()
v1 = nengo.Ensemble(n_neurons=100, dimensions=2)
sma = nengo.Ensemble(n_neurons=100, dimensions=2)
But I don’t understand how does the network collects the Ensemble objects created in the “with” block.
I can’t find the specific function do this work. Can anyone tell me how does these ensembles been added to the networks?
Hi @hongchf, welcome to the forum!
with syntax uses Python’s context manager protocol, which allows us to do some additional steps in the
nengo.Ensemble initialization depending on the context that it’s in. The exact way in which it works is a bit complicated and distributed across a few different Nengo classes, so I’ll do my best to explain the sequence of events.
- When you do
Network.__enter__ is called, which adds that network to the top of the
- When you call
nengo.Ensemble, a slightly different sequence of events happens than happens with a normal
object due to the use of a metaclass,
NetworkMember.__call__ is what actually ends up happening, which reads the value of
add_to_container and calls
Network.add adds the passed in object to the network that is on top of the
Hopefully that helps!
Thank you @tbekolay, I understand it now.