How to retroconnect two neurons

Description of problem

I am trying to create a system like so:
input layer → excitatory layer <-> inhibitory layer

Such that excitatory and inhibitory layers are interconnected.

Minimal code to reproduce problem

I tried to do it like so:

synapse_from_input_to_exci = Synapses(neurons_input, neurons_exci, model = synapse_model_eqs, on_pre=synapse_1_on_pre_eqs)
synapse_from_input_to_exci.connect(p=p_synapse_1)
synapse_from_input_to_exci.w = initial_synapse_weight

synapse_from_exci_to_inhi  = Synapses(neurons_exci,  neurons_inhi, model = synapse_model_eqs, on_pre=synapse_2_on_pre_eqs)
synapse_from_exci_to_inhi.connect(p=p_synapse_2)
synapse_from_exci_to_inhi.w = initial_synapse_weight

synapse_from_inhi_to_exci  = Synapses(neurons_inhi,  neurons_exci, model = synapse_model_eqs, on_pre=synapse_3_on_pre_eqs)
synapse_from_inhi_to_exci.connect(p=p_synapse_3)
synapse_from_inhi_to_exci.w = initial_synapse_weight

.
.
.

defaultclock.dt = 10*ms 
#restore() # required for multiple run
simulation_duration = total_duration*second # adjust simulation duration here
run(simulation_duration, report='text')

I however get a long error saying that a neuron cannot be part of another network,

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\brian2\core\network.py in before_run(self, run_namespace)
    891                 try:
--> 892                     obj.before_run(run_namespace)
    893                 except Exception as ex:

~\anaconda3\lib\site-packages\brian2\core\base.py in device_override_decorated_function(*args, **kwds)
    292             else:
--> 293                 return func(*args, **kwds)
    294 

~\anaconda3\lib\site-packages\brian2\synapses\synapses.py in before_run(self, run_namespace)
    319     def before_run(self, run_namespace):
--> 320         super(SynapticPathway, self).before_run(run_namespace)
    321 

~\anaconda3\lib\site-packages\brian2\groups\group.py in before_run(self, run_namespace)
   1134     def before_run(self, run_namespace):
-> 1135         self.create_code_objects(run_namespace)
   1136         super(CodeRunner, self).before_run(run_namespace)

~\anaconda3\lib\site-packages\brian2\synapses\synapses.py in create_code_objects(self, run_namespace)
    342         self.code_objects[:] = [weakref.proxy(self._pushspikes_codeobj),
--> 343                                 weakref.proxy(self.create_default_code_object(run_namespace))]
    344 

~\anaconda3\lib\site-packages\brian2\groups\group.py in create_default_code_object(self, run_namespace)
   1109         else:
-> 1110             self.codeobj = create_runner_codeobj(group=self.group,
   1111                                                  code=self.abstract_code,

~\anaconda3\lib\site-packages\brian2\codegen\codeobject.py in create_runner_codeobj(group, code, template_name, run_namespace, user_code, variable_indices, name, check_units, needed_variables, additional_variables, template_kwds, override_conditional_write, codeobj_class)
    340     for v, u_v in zip(code.values(), user_code.values()):
--> 341         _, uk, u = analyse_identifiers(v, all_variables, recursive=True)
    342         identifiers |= uk | u

~\anaconda3\lib\site-packages\brian2\codegen\translation.py in analyse_identifiers(code, variables, recursive)
     94     known |= STANDARD_IDENTIFIERS
---> 95     scalar_stmts, vector_stmts = make_statements(code, variables, np.float64, optimise=False)
     96     stmts = scalar_stmts + vector_stmts

~\anaconda3\lib\site-packages\brian2\utils\caching.py in cached_func(*args, **kwds)
    100             func._cache_statistics.misses += 1
--> 101             func._cache[cache_key] = func(*args, **kwds)
    102         return func._cache[cache_key]

~\anaconda3\lib\site-packages\brian2\codegen\translation.py in make_statements(code, variables, dtype, optimise, blockname)
    275             statement = Statement(var, op, expr, comment,
--> 276                                   dtype=variables[var].dtype,
    277                                   scalar=variables[var].scalar)

KeyError: 'g_e'

The above exception was the direct cause of the following exception:

BrianObjectException                      Traceback (most recent call last)
<ipython-input-20-ead1fbf13667> in <module>
      2 # restore() # required for multiple run
      3 simulation_duration = total_duration*second # adjust simulation duration here
----> 4 run(simulation_duration, report='text')

~\anaconda3\lib\site-packages\brian2\units\fundamentalunits.py in new_f(*args, **kwds)
   2419                                                      get_dimensions(newkeyset[k]))
   2420 
-> 2421             result = f(*args, **kwds)
   2422             if 'result' in au:
   2423                 if isinstance(au['result'], Callable) and au['result'] != bool:

~\anaconda3\lib\site-packages\brian2\core\magic.py in run(duration, report, report_period, namespace, profile, level)
    371         intended use. See `MagicNetwork` for more details.
    372     """
--> 373     return magic_network.run(duration, report=report, report_period=report_period,
    374                              namespace=namespace, profile=profile, level=2+level)
    375 run.__module__ = __name__

~\anaconda3\lib\site-packages\brian2\core\magic.py in run(self, duration, report, report_period, namespace, profile, level)
    228             namespace=None, profile=None, level=0):
    229         self._update_magic_objects(level=level+1)
--> 230         Network.run(self, duration, report=report, report_period=report_period,
    231                     namespace=namespace, profile=profile, level=level+1)
    232 

~\anaconda3\lib\site-packages\brian2\core\base.py in device_override_decorated_function(*args, **kwds)
    291                 return getattr(curdev, name)(*args, **kwds)
    292             else:
--> 293                 return func(*args, **kwds)
    294 
    295         device_override_decorated_function.original_function = func

~\anaconda3\lib\site-packages\brian2\units\fundamentalunits.py in new_f(*args, **kwds)
   2419                                                      get_dimensions(newkeyset[k]))
   2420 
-> 2421             result = f(*args, **kwds)
   2422             if 'result' in au:
   2423                 if isinstance(au['result'], Callable) and au['result'] != bool:

~\anaconda3\lib\site-packages\brian2\core\network.py in run(self, duration, report, report_period, namespace, profile, level)
   1004             namespace = get_local_namespace(level=level+3)
   1005 
-> 1006         self.before_run(namespace)
   1007 
   1008         if len(all_objects) == 0:

~\anaconda3\lib\site-packages\brian2\core\base.py in device_override_decorated_function(*args, **kwds)
    291                 return getattr(curdev, name)(*args, **kwds)
    292             else:
--> 293                 return func(*args, **kwds)
    294 
    295         device_override_decorated_function.original_function = func

~\anaconda3\lib\site-packages\brian2\core\network.py in before_run(self, run_namespace)
    892                     obj.before_run(run_namespace)
    893                 except Exception as ex:
--> 894                     raise BrianObjectException("An error occurred when preparing an object.", obj) from ex
    895 
    896         # Check that no object has been run as part of another network before

BrianObjectException: Error encountered with object named 'synapses_6_pre'.
Object was created here (most recent call only, full details in debug log):
  File '<ipython-input-19-83292ced657b>', line 25, in <module>
    synapse_from_exci_to_inhi  = Synapses(neurons_exci,  neurons_inhi, model = synapse_model_eqs, on_pre=synapse_2_on_pre_eqs)

An error occurred when preparing an object. (See above for original error message and traceback.)

This should be a fairly simple problem, apologies if it is clear in the documentation, I could not find the answer after 1 hour of searching and trial-and-error.

Hi @Borgesvpm,

that’s not really the error you are getting (I think you are referring to a comment in the code shown in the stack trace), the error is the following: KeyError: 'g_e', raised by the synapses_from_exci_to_inhi. What I suspect is that the statement in synapse_2_on_pre_eqs is referring to a post-synaptic conductance (or current) g_e, but that the equations for neurons_inhi do not define such a variable.