NEST main@f146f75
Loading...
Searching...
No Matches
eprop_synapse.h
Go to the documentation of this file.
1/*
2 * eprop_synapse.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef EPROP_SYNAPSE_H
24#define EPROP_SYNAPSE_H
25
26// nestkernel
27#include "connection.h"
28#include "connector_base.h"
30#include "target_identifier.h"
31#include "weight_optimizer.h"
32
33namespace nest
34{
35
36/* BeginUserDocs: synapse, abstract, learning, Bellec, e-prop plasticity, 3-factor
37
38Short description
39+++++++++++++++++
40
41Synapse type for e-prop plasticity
42
43Description
44+++++++++++
45
46``eprop_synapse`` is an implementation of a connector model to create synapses between postsynaptic
47neurons :math:`j` and presynaptic neurons and :math:`i` for eligibility propagation (e-prop) plasticity.
48
49E-prop plasticity was originally introduced and implemented in TensorFlow in :footcite:p:`Bellec2020`.
50
51The e-prop synapse triggers the calculation of the gradient at each spike
52over an interval that begins at the previous spike and ends at a cutoff specified by the user or the
53current spike, depending on which of the two time points is earlier.
54The gradient calculation is specific to the post-synaptic neuron and thus defined there.
55
56Eventually, it optimizes the weight with the specified optimizer.
57
58E-prop synapses require archiving of continuous quantities. Therefore e-prop
59synapses can only be connected to neuron models that are capable of
60archiving. So far, compatible models are ``eprop_iaf``, ``eprop_iaf_psc_delta``, ``eprop_iaf_psc_delta_adapt``,
61``eprop_iaf_adapt``, and ``eprop_readout``.
62
63For more information, see the following topics:
64
65* other e-prop plasticity models:
66
67 * :doc:`eprop_iaf<../models/eprop_iaf/>`
68 * :doc:`eprop_iaf_adapt<../models/eprop_iaf_adapt/>`
69 * :doc:`eprop_readout<../models/eprop_readout/>`
70 * :doc:`eprop_learning_signal_connection<../models/eprop_learning_signal_connection/>`
71
72* :doc:`weight optimizer<../models/weight_optimizer/>`
73* triggering synaptic plasticity computations to reduce memory usage via the
74 :ref:`flush event mechanism<flush_event_mechanism>`
75
76Details on the event-based NEST implementation of e-prop can be found in :footcite:p:`KorcsakGorzo2026`.
77
78.. warning::
79
80 This synaptic plasticity rule does not take
81 :ref:`precise spike timing <sim_precise_spike_times>` into
82 account. When calculating the weight update, the precise spike time part
83 of the timestamp is ignored.
84
85Parameters
86++++++++++
87
88The following parameters can be set in the status dictionary.
89
90================ ==== =============== ======= ======================================================
91**Common e-prop synapse parameters**
92----------------------------------------------------------------------------------------------------
93Parameter Unit Math equivalent Default Description
94================ ==== =============== ======= ======================================================
95``optimizer`` {} Dictionary of optimizer parameters
96================ ==== =============== ======= ======================================================
97
98============= ==== ========================= ======= =========================================================
99**Individual synapse parameters**
100--------------------------------------------------------------------------------------------------------------
101Parameter Unit Math equivalent Default Description
102============= ==== ========================= ======= =========================================================
103``delay`` ms :math:`d_{ji}` 1.0 Dendritic delay
104``weight`` pA :math:`W_{ji}` 1.0 Initial value of synaptic weight
105============= ==== ========================= ======= =========================================================
106
107Recordables
108+++++++++++
109
110The following variables can be recorded.
111
112================== ==== =============== ============= ==========================================================
113**Synapse recordables**
114----------------------------------------------------------------------------------------------------------------
115State variable Unit Math equivalent Initial value Description
116================== ==== =============== ============= ==========================================================
117``weight`` pA :math:`B_{jk}` 1.0 Synaptic weight
118================== ==== =============== ============= ==========================================================
119
120Usage
121+++++
122
123This model can only be used in combination with the other e-prop models
124and the network architecture requires specific wiring, input, and output.
125The usage is demonstrated in several
126:doc:`supervised regression and classification tasks <../auto_examples/eprop_plasticity/index>`
127reproducing among others the original proof-of-concept tasks in :footcite:p:`Bellec2020`.
128
129Transmits
130+++++++++
131
132SpikeEvent, DSSpikeEvent
133
134References
135++++++++++
136
137.. footbibliography::
138
139See also
140++++++++
141
142Examples using this model
143+++++++++++++++++++++++++
144
145.. listexamples:: eprop_synapse
146
147EndUserDocs */
148
192
194void register_eprop_synapse( const std::string& name );
195
220template < typename targetidentifierT >
221class eprop_synapse : public Connection< targetidentifierT, TotalDelay >
222{
223
224public:
227
230
239
241 static constexpr bool supports_flush_event = true;
242
245
248
251
254
257
260
265
267 void get_status( Dictionary& d ) const;
268
270 void set_status( const Dictionary& d, ConnectorModel& cm );
271
273 bool send( Event& e, size_t thread, const EpropSynapseCommonProperties& cp );
274
277 {
278 public:
280
281 size_t
283 {
284 return invalid_port;
285 }
286
287 size_t
289 {
290 return invalid_port;
291 }
292 };
293
299 void check_connection( Node& s,
300 Node& t,
301 const size_t receptor_type,
302 const synindex syn_id,
303 const CommonPropertiesType& cp );
304
306 void
307 set_weight( const double w )
308 {
309 weight_ = w;
310 }
311
313 void delete_optimizer();
314
315private:
317 double weight_;
318
321
324
327
329 double z_bar_ = 0.0;
330
332 double e_bar_ = 0.0;
333
335 double e_bar_reg_ = 0.0;
336
338 double epsilon_ = 0.0;
339
341 double z_previous_buffer_ = 0.0;
342
344 double gradient_ = 0.0;
345
348
350 long decay_steps_ = 0;
351
358};
359
360template < typename targetidentifierT >
362
363// Explicitly declare specializations of Connector methods that need to do special things for eprop_synapse
364template <>
366
367template <>
369
370template <>
372
373template <>
375
376template < typename targetidentifierT >
386
387template < typename targetidentifierT >
391
392// This copy constructor is used to create instances from prototypes.
393// Therefore, only parameter values are copied.
394template < typename targetidentifierT >
401
402// This assignment operator is used to write a connection into the connection array.
403template < typename targetidentifierT >
430
431template < typename targetidentifierT >
433 : ConnectionBase( es )
434 , weight_( es.weight_ )
438 , z_bar_( es.z_bar_ )
439 , e_bar_( es.e_bar_ )
440 , e_bar_reg_( es.e_bar_reg_ )
441 , epsilon_( es.epsilon_ )
443 , gradient_( es.gradient_ )
446 , optimizer_( es.optimizer_ )
447{
448 // Move operator, therefore we must null the optimizer pointer in the source of the move.
449 es.optimizer_ = nullptr;
450}
451
452// This is the move assignment operator.
453template < typename targetidentifierT >
456{
457 if ( this == &es )
458 {
459 return *this;
460 }
461
463
464 weight_ = es.weight_;
465 t_spike_previous_ = es.t_spike_previous_;
466 previous_was_flush_event_ = es.previous_was_flush_event_;
467 t_previous_trigger_spike_ = es.t_previous_trigger_spike_;
468 z_bar_ = es.z_bar_;
469 e_bar_ = es.e_bar_;
470 e_bar_reg_ = es.e_bar_reg_;
471 epsilon_ = es.epsilon_;
472 z_previous_buffer_ = es.z_previous_buffer_;
473 gradient_ = es.gradient_;
474 remaining_steps_until_cutoff_ = es.remaining_steps_until_cutoff_;
475 decay_steps_ = es.decay_steps_;
476 optimizer_ = es.optimizer_;
477
478 // Move assignment, therefore we must null the optimizer pointer in the source of the move.
479 es.optimizer_ = nullptr;
480
481 return *this;
482}
483
484template < typename targetidentifierT >
485inline void
487 Node& t,
488 size_t receptor_type,
489 const synindex syn_id,
490 const CommonPropertiesType& cp )
491{
492 // When we get here, delay has been set so we can check it.
493 if ( get_delay_steps() != 1 )
494 {
495 throw IllegalConnection( "eprop synapses currently require a delay of one simulation step" );
496 }
497
498 ConnTestDummyNode dummy_target;
499 ConnectionBase::check_connection_( dummy_target, s, t, syn_id, receptor_type );
500
502
504}
505
506template < typename targetidentifierT >
507inline void
509{
510 delete optimizer_;
511 // do not set to nullptr to allow detection of double deletion
512}
513
514template < typename targetidentifierT >
515bool
517{
518 Node* target = get_target( thread );
519 assert( target );
520
521 const long t_spike = e.get_stamp().get_steps();
522 const bool is_flush_event = e.is_flush_event();
523
524 if ( t_spike_previous_ != 0 )
525 {
526 target->compute_gradient( t_spike,
529 z_bar_,
530 e_bar_,
532 epsilon_,
533 weight_,
534 cp,
536 is_flush_event,
538 gradient_,
540 decay_steps_ );
541 }
542
543 target->erase_used_eprop_history( t_spike, t_spike_previous_ );
544
545 t_spike_previous_ = t_spike;
546 previous_was_flush_event_ = is_flush_event;
547
548 if ( not is_flush_event )
549 {
550 e.set_receiver( *target );
551 e.set_weight( weight_ );
552 e.set_delay_steps( get_delay_steps() );
553 e.set_rport( get_rport() );
554 e();
555 }
556 return true;
557}
558
559template < typename targetidentifierT >
560void
562{
564 d[ names::weight ] = weight_;
565 d[ names::size_of ] = static_cast< long >( sizeof( *this ) );
566
567 Dictionary optimizer_dict;
568
569 // The default_connection_ has no optimizer, therefore we need to protect it
570 if ( optimizer_ )
571 {
572 optimizer_->get_status( optimizer_dict );
573 d[ names::optimizer ] = optimizer_dict;
574 }
575}
576
577template < typename targetidentifierT >
578void
580{
582 if ( d.known( names::optimizer ) )
583 {
584 // We must pass here if called by SetDefaults. In that case, the user will get and error
585 // message because the parameters for the synapse-specific optimizer have not been accessed.
586 if ( optimizer_ )
587 {
588 optimizer_->set_status( d.get< Dictionary >( names::optimizer ) );
589 }
590 }
591
592 d.update_value( names::weight, weight_ );
593
594 const auto& gcm = dynamic_cast< const GenericConnectorModel< eprop_synapse< targetidentifierT > >& >( cm );
595 const CommonPropertiesType& epcp = gcm.get_common_properties();
596 if ( weight_ < epcp.optimizer_cp_->get_Wmin() )
597 {
598 throw BadProperty( "Minimal weight Wmin ≤ weight required." );
599 }
600
601 if ( weight_ > epcp.optimizer_cp_->get_Wmax() )
602 {
603 throw BadProperty( "weight ≤ maximal weight Wmax required." );
604 }
605}
606
607} // namespace nest
608
609#endif // EPROP_SYNAPSE_H
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
Exception to be thrown if a status parameter is incomplete or inconsistent.
Definition exceptions.h:680
CommonSynapseProperties()
Standard constructor.
Definition common_synapse_properties.cpp:36
Base class for dummy nodes used in connection testing.
Definition connection.h:70
double get_delay_ms() const
Definition connection.h:262
size_t get_rport() const
Definition connection.h:316
long get_delay_steps() const
Definition connection.h:271
Connection & operator=(const Connection< targetidentifierT, TotalDelay > &rhs)=default
void set_status(const Dictionary &d, ConnectorModel &cm)
Node * get_target(const size_t tid) const
Definition connection.h:311
void check_connection_(Node &dummy_target, Node &source, Node &target, const synindex syn_id, const size_t receptor_type)
Definition connector_model.h:69
void disable_connection(const size_t lcid) override
Disable the transfer of events through the connection at position lcid.
Definition connector_base.h:563
Connector(const synindex syn_id)
Definition connector_base.h:253
~Connector() override
Definition connector_base.h:259
"Callback request event" for use in Device.
Definition event.h:560
Base class implementing common properties for e-prop synapses with additional biological features.
Definition eprop_synapse.h:165
WeightOptimizerCommonProperties * optimizer_cp_
Pointer to common properties object for weight optimizer.
Definition eprop_synapse.h:190
~EpropSynapseCommonProperties()
Destructor.
Definition eprop_synapse.cpp:49
EpropSynapseCommonProperties & operator=(const EpropSynapseCommonProperties &)=delete
Assignment operator.
EpropSynapseCommonProperties()
Definition eprop_synapse.cpp:37
void get_status(Dictionary &d) const
Get parameter dictionary.
Definition eprop_synapse.cpp:55
void set_status(const Dictionary &d, ConnectorModel &cm)
Update values in parameter dictionary.
Definition eprop_synapse.cpp:65
Encapsulate information sent between nodes.
Definition event.h:103
Definition connector_model.h:156
To be thrown if a connection is not possible.
Definition exceptions.h:490
Base class for all NEST network objects.
Definition node.h:99
virtual void register_eprop_connection()
Registers an eprop connection.
Definition node.cpp:223
Event for spike information.
Definition event.h:420
Base class implementing common properties of a weight optimizer model.
Definition weight_optimizer.h:154
virtual WeightOptimizer * get_optimizer() const =0
Get optimizer.
Base class implementing a weight optimizer model.
Definition weight_optimizer.h:238
Dummy node for testing the connection.
Definition eprop_synapse.h:277
size_t handles_test_event(SpikeEvent &, size_t)
Check if the node can handle a particular event and receptor type.
Definition eprop_synapse.h:282
size_t handles_test_event(DSSpikeEvent &, size_t)
Definition eprop_synapse.h:288
Class implementing a synapse model for e-prop plasticity with additional biological features.
Definition eprop_synapse.h:222
size_t get_rport() const
Definition connection.h:316
double z_bar_
Low-pass filtered spiking variable.
Definition eprop_synapse.h:329
double weight_
Synaptic weight.
Definition eprop_synapse.h:317
static constexpr bool supports_flush_event
Whether this connection type supports flush events.
Definition eprop_synapse.h:241
EpropSynapseCommonProperties CommonPropertiesType
Type of the common synapse properties.
Definition eprop_synapse.h:226
double epsilon_
Adaptive threshold component of the eligibility vector.
Definition eprop_synapse.h:338
double gradient_
Sum of gradients.
Definition eprop_synapse.h:344
eprop_synapse & operator=(const eprop_synapse &)
Assignment operator.
Definition eprop_synapse.h:405
long remaining_steps_until_cutoff_
Remaining computation steps.
Definition eprop_synapse.h:347
void check_connection(Node &s, Node &t, const size_t receptor_type, const synindex syn_id, const CommonPropertiesType &cp)
Check if the target accepts the event and receptor type requested by the sender.
Definition eprop_synapse.h:486
long t_previous_trigger_spike_
The time step when the spike arrived that triggered the previous e-prop update.
Definition eprop_synapse.h:326
void set_status(const Dictionary &d, ConnectorModel &cm)
Update values in parameter dictionary.
Definition eprop_synapse.h:579
long get_delay_steps() const
Return the delay of the connection in steps.
Definition connection.h:271
void set_weight(const double w)
Set the synaptic weight to the provided value.
Definition eprop_synapse.h:307
~eprop_synapse()
Destructor.
Definition eprop_synapse.h:388
Node * get_target(const size_t tid) const
Definition connection.h:311
bool previous_was_flush_event_
Previous event was a flush event.
Definition eprop_synapse.h:323
double z_previous_buffer_
Value of spiking variable one time step before t_previous_spike_.
Definition eprop_synapse.h:341
eprop_synapse()
Default constructor.
Definition eprop_synapse.h:377
WeightOptimizer * optimizer_
Optimizer.
Definition eprop_synapse.h:357
Connection< targetidentifierT, TotalDelay > ConnectionBase
Type of the connection base.
Definition eprop_synapse.h:229
void get_status(Dictionary &d) const
Get parameter dictionary.
Definition eprop_synapse.h:561
double e_bar_reg_
Low-pass filtered eligibility trace for firing rate regularization.
Definition eprop_synapse.h:335
void delete_optimizer()
Delete optimizer.
Definition eprop_synapse.h:508
static constexpr ConnectionModelProperties properties
Properties of the connection model.
Definition eprop_synapse.h:236
long decay_steps_
Decay steps for the eligibility trace.
Definition eprop_synapse.h:350
long t_spike_previous_
The time step when the previous spike arrived.
Definition eprop_synapse.h:320
double e_bar_
Low-pass filtered eligibility trace.
Definition eprop_synapse.h:332
bool send(Event &e, size_t thread, const EpropSynapseCommonProperties &cp)
Send the spike event.
Definition eprop_synapse.h:516
virtual size_t handles_test_event(SpikeEvent &, size_t receptor_type)
Check if the node can handle a particular event and receptor type.
Definition node.cpp:282
const std::string optimizer("optimizer")
const std::string weight("weight")
const std::string size_of("sizeof")
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
void register_eprop_synapse(const std::string &name)
Register the eprop synapse model.
Definition eprop_synapse.cpp:32
ConnectionModelProperties
Definition connector_model.h:49
@ HAS_DELAY
Definition connector_model.h:54
@ REQUIRES_EPROP_ARCHIVING
Definition connector_model.h:59
@ SUPPORTS_HPC
Definition connector_model.h:51
@ IS_PRIMARY
Definition connector_model.h:53
constexpr size_t invalid_port
Value for invalid connection port number.
Definition nest_types.h:147
size_t synindex
For enumerations of synapse types.
Definition nest_types.h:121