NEST main@f146f75
Loading...
Searching...
No Matches
eprop_synapse_bsshslm_2020.h
Go to the documentation of this file.
1/*
2 * eprop_synapse_bsshslm_2020.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_BSSHSLM_2020_H
24#define EPROP_SYNAPSE_BSSHSLM_2020_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_bsshslm_2020`` is an implementation of a connector model to create synapses between postsynaptic
47neurons :math:`j` and presynaptic neurons :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 suffix ``_bsshslm_2020`` follows the NEST convention to indicate in the
52model name the paper that introduced it by the first letter of the authors' last
53names and the publication year.
54
55The e-prop synapse collects the presynaptic spikes needed for calculating the
56weight update. When it is time to update, it triggers the calculation of the
57gradient which is specific to the post-synaptic neuron and is thus defined there.
58
59Eventually, it optimizes the weight with the specified optimizer.
60
61E-prop synapses require archiving of continuous quantities. Therefore e-prop
62synapses can only be connected to neuron models that are capable of
63archiving. So far, compatible models are ``eprop_iaf_bsshslm_2020``,
64``eprop_iaf_adapt_bsshslm_2020``, and ``eprop_readout_bsshslm_2020``.
65
66For more information, see the following topics:
67
68* other e-prop plasticity models:
69
70 * :doc:`eprop_iaf_bsshslm_2020<../models/eprop_iaf_bsshslm_2020/>`
71 * :doc:`eprop_iaf_adapt_bsshslm_2020<../models/eprop_iaf_adapt_bsshslm_2020/>`
72 * :doc:`eprop_readout_bsshslm_2020<../models/eprop_readout_bsshslm_2020/>`
73 * :doc:`eprop_learning_signal_connection_bsshslm_2020<../models/eprop_learning_signal_connection_bsshslm_2020/>`
74
75* :doc:`weight optimizer<../models/weight_optimizer/>`
76* triggering synaptic plasticity computations to reduce memory usage via the
77 :ref:`flush event mechanism<flush_event_mechanism>`
78
79Details on the event-based NEST implementation of e-prop can be found in :footcite:p:`KorcsakGorzo2026`.
80
81.. warning::
82
83 This synaptic plasticity rule does not take
84 :ref:`precise spike timing <sim_precise_spike_times>` into
85 account. When calculating the weight update, the precise spike time part
86 of the timestamp is ignored.
87
88Parameters
89++++++++++
90
91The following parameters can be set in the status dictionary.
92
93==================== ======= =============== ========= ======================================================
94**Common e-prop synapse parameters**
95-------------------------------------------------------------------------------------------------------------
96Parameter Unit Math equivalent Default Description
97==================== ======= =============== ========= ======================================================
98``average_gradient`` Boolean ``False`` If ``True``, average the gradient over the learning
99 window
100``optimizer`` {} Dictionary of optimizer parameters
101==================== ======= =============== ========= ======================================================
102
103============= ==== ========================= ======= ================================
104**Individual synapse parameters**
105-------------------------------------------------------------------------------------
106Parameter Unit Math equivalent Default Description
107============= ==== ========================= ======= ================================
108``delay`` ms :math:`d_{ji}` 1.0 Dendritic delay
109``weight`` pA :math:`W_{ji}` 1.0 Initial value of synaptic weight
110============= ==== ========================= ======= ================================
111
112================= ==== ========================= ======= ==============================
113**Individual e-prop synapse parameters**
114---------------------------------------------------------------------------------------
115Parameter Unit Math equivalent Default Description
116================= ==== ========================= ======= ==============================
117``tau_m_readout`` ms :math:`\tau_\text{m,out}` 10.0 Time constant for low-pass
118 filtering of eligibility trace
119================= ==== ========================= ======= ==============================
120
121Recordables
122+++++++++++
123
124The following variables can be recorded.
125
126================== ==== =============== ============= ===============
127**Synapse recordables**
128---------------------------------------------------------------------
129State variable Unit Math equivalent Initial value Description
130================== ==== =============== ============= ===============
131``weight`` pA :math:`B_{jk}` 1.0 Synaptic weight
132================== ==== =============== ============= ===============
133
134Usage
135+++++
136
137This model can only be used in combination with the other e-prop models
138and the network architecture requires specific wiring, input, and output.
139The usage is demonstrated in several
140:doc:`supervised regression and classification tasks <../auto_examples/eprop_plasticity/index>`
141reproducing among others the original proof-of-concept tasks in :footcite:p:`Bellec2020`.
142
143Transmits
144+++++++++
145
146SpikeEvent, DSSpikeEvent
147
148References
149++++++++++
150
151.. footbibliography::
152
153See also
154++++++++
155
156Examples using this model
157+++++++++++++++++++++++++
158
159.. listexamples:: eprop_synapse_bsshslm_2020
160
161EndUserDocs */
162
207
209void register_eprop_synapse_bsshslm_2020( const std::string& name );
210
237template < typename targetidentifierT >
238class eprop_synapse_bsshslm_2020 : public Connection< targetidentifierT, TotalDelay >
239{
240
241public:
244
247
249 static constexpr bool supports_flush_event = true;
250
259
262
265
268
271
274
277
282
284 void get_status( Dictionary& d ) const;
285
287 void set_status( const Dictionary& d, ConnectorModel& cm );
288
290 bool send( Event& e, size_t thread, const EpropSynapseBSSHSLM2020CommonProperties& cp );
291
294 {
295 public:
297
298 size_t
300 {
301 return invalid_port;
302 }
303
304 size_t
306 {
307 return invalid_port;
308 }
309 };
310
316 void check_connection( Node& s,
317 Node& t,
318 const size_t receptor_type,
319 const synindex syn_id,
320 const CommonPropertiesType& cp );
321
323 void
324 set_weight( const double w )
325 {
326 weight_ = w;
327 }
328
330 void delete_optimizer();
331
332private:
334 double weight_;
335
337 double gradient_;
338
341
344
347
350
353
356
358 double kappa_;
359
362
364 std::vector< long > presyn_isis_;
365
372};
373
374template < typename targetidentifierT >
376
377// Explicitly declare specializations of Connector methods that need to do special things for eprop_synapse_bsshslm_2020
378template <>
380
381template <>
383
384template <>
386
387template <>
389
390template < typename targetidentifierT >
393 , weight_( 1.0 )
394 , gradient_( 0.0 )
395 , t_spike_previous_( 0 )
397 , t_previous_update_( 0 )
398 , t_next_update_( 0 )
400 , tau_m_readout_( 10.0 )
401 , kappa_( std::exp( -Time::get_resolution().get_ms() / tau_m_readout_ ) )
403 , optimizer_( nullptr )
404{
405}
406
407template < typename targetidentifierT >
411
412// This copy constructor is used to create instances from prototypes.
413// Therefore, only parameter values are copied.
414template < typename targetidentifierT >
416 : ConnectionBase( es )
417 , weight_( es.weight_ )
418 , gradient_( es.gradient_ )
419 , t_spike_previous_( 0 )
421 , t_previous_update_( 0 )
422 , t_next_update_( kernel().simulation_manager.get_eprop_update_interval().get_steps() )
425 , kappa_( std::exp( -Time::get_resolution().get_ms() / tau_m_readout_ ) )
427 , optimizer_( es.optimizer_ )
428{
429}
430
431// This copy assignment operator is used to write a connection into the connection array.
432template < typename targetidentifierT >
457
458template < typename targetidentifierT >
475
476// This move assignment operator is used to write a connection into the connection array.
477template < typename targetidentifierT >
480{
481 if ( this == &es )
482 {
483 return *this;
484 }
485
487
488 weight_ = es.weight_;
489 gradient_ = es.gradient_;
490 t_spike_previous_ = es.t_spike_previous_;
491 previous_was_flush_event_ = es.previous_was_flush_event_;
492 t_previous_update_ = es.t_previous_update_;
493 t_next_update_ = es.t_next_update_;
494 t_previous_trigger_spike_ = es.t_previous_trigger_spike_;
495 tau_m_readout_ = es.tau_m_readout_;
496 kappa_ = es.kappa_;
497 is_recurrent_to_recurrent_conn_ = es.is_recurrent_to_recurrent_conn_;
498
499 optimizer_ = es.optimizer_;
500 es.optimizer_ = nullptr;
501
502 return *this;
503}
504
505template < typename targetidentifierT >
506inline void
508 Node& t,
509 size_t receptor_type,
510 const synindex syn_id,
511 const CommonPropertiesType& cp )
512{
513 // When we get here, delay has been set so we can check it.
514 if ( get_delay_steps() != 1 )
515 {
516 throw IllegalConnection( "eprop synapses currently require a delay of one simulation step" );
517 }
518
519 ConnTestDummyNode dummy_target;
520 ConnectionBase::check_connection_( dummy_target, s, t, syn_id, receptor_type );
521
524
526}
527
528template < typename targetidentifierT >
529inline void
531{
532 delete optimizer_;
533 // do not set to nullptr to allow detection of double deletion
534}
535
536template < typename targetidentifierT >
537bool
539 size_t thread,
541{
542 Node* target = get_target( thread );
543 assert( target );
544
545 const long t_spike = e.get_stamp().get_steps();
546 const bool is_flush_event = e.is_flush_event();
547
549 {
550 if ( is_flush_event )
551 {
552 return false;
553 }
554
555 t_spike_previous_ = t_spike;
557 }
558
559 const long update_interval = kernel().simulation_manager.get_eprop_update_interval().get_steps();
560 const long shift = target->get_shift();
561
562 const long interval_step = ( t_spike - shift ) % update_interval;
563
564 if ( target->is_eprop_recurrent_node() and interval_step == 0 )
565 {
566 return false;
567 }
568
569 if ( t_previous_trigger_spike_ == 0 )
570 {
572 }
573
574 if ( t_spike_previous_ > 0 )
575 {
576 const long presyn_isi =
577 std::min( t_spike, t_next_update_ + shift ) - std::min( t_spike_previous_, t_next_update_ + shift );
578
579 if ( presyn_isi > 0 )
580 {
581 presyn_isis_.push_back( presyn_isi );
582 }
583 }
584
585 if ( t_spike > t_next_update_ + shift )
586 {
587 const long idx_current_update = ( t_spike - shift ) / update_interval;
588 const long t_current_update = idx_current_update * update_interval;
589
590 target->write_update_to_history( t_previous_update_, t_current_update, is_flush_event, previous_was_flush_event_ );
591
592 if ( not presyn_isis_.empty() and t_spike_previous_ > 0 )
593 {
594 gradient_ += target->compute_gradient(
596 }
597
598 if ( not is_flush_event )
599 {
600 weight_ = optimizer_->optimized_weight( *cp.optimizer_cp_, idx_current_update, gradient_, weight_ );
601 gradient_ = 0.0;
602 }
603
604 t_previous_update_ = t_current_update;
605 t_next_update_ = t_current_update + update_interval;
606
608 }
609 else
610 {
611 if ( not is_flush_event and previous_was_flush_event_ )
612 {
613 const long idx_current_update = ( t_spike - shift ) / update_interval;
614 const long t_current_update = idx_current_update * update_interval;
615
616 target->write_update_to_history(
617 t_previous_update_, t_current_update, is_flush_event, previous_was_flush_event_ );
618
619 weight_ = optimizer_->optimized_weight( *cp.optimizer_cp_, idx_current_update, gradient_, weight_ );
620 gradient_ = 0.0;
621 }
622 }
623
624 if ( not is_flush_event )
625 {
626 t_spike_previous_ = t_spike;
627
628 e.set_receiver( *target );
629 e.set_weight( weight_ );
630 e.set_delay_steps( get_delay_steps() );
631 e.set_rport( get_rport() );
632 e();
633 }
634
635 previous_was_flush_event_ = is_flush_event;
636
637 return true;
638}
639
640template < typename targetidentifierT >
641void
643{
645 d[ names::weight ] = weight_;
647 d[ names::size_of ] = static_cast< long >( sizeof( *this ) );
648
649 Dictionary optimizer_dict;
650
651 // The default_connection_ has no optimizer, therefore we need to protect it
652 if ( optimizer_ )
653 {
654 optimizer_->get_status( optimizer_dict );
655 d[ names::optimizer ] = optimizer_dict;
656 }
657}
658
659template < typename targetidentifierT >
660void
662{
664 if ( d.known( names::optimizer ) and optimizer_ )
665 {
666 // We must pass here if called by SetDefaults. In that case, the user will get an error
667 // message because the parameters for the synapse-specific optimizer have not been accessed.
668 optimizer_->set_status( d.get< Dictionary >( names::optimizer ) );
669 }
670
671 d.update_value( names::weight, weight_ );
672
673 if ( d.update_value( names::tau_m_readout, tau_m_readout_ ) )
674 {
675 if ( tau_m_readout_ <= 0 )
676 {
677 throw BadProperty( "Membrane time constant of readout neuron tau_m_readout > 0 required." );
678 }
679 kappa_ = std::exp( -Time::get_resolution().get_ms() / tau_m_readout_ );
680 }
681
682 const auto& gcm = dynamic_cast< const GenericConnectorModel< eprop_synapse_bsshslm_2020 >& >( cm );
683 const CommonPropertiesType& epcp = gcm.get_common_properties();
684 if ( weight_ < epcp.optimizer_cp_->get_Wmin() )
685 {
686 throw BadProperty( "Minimal weight Wmin ≤ weight required." );
687 }
688
689 if ( weight_ > epcp.optimizer_cp_->get_Wmax() )
690 {
691 throw BadProperty( "weight ≤ maximal weight Wmax required." );
692 }
693}
694
695} // namespace nest
696
697#endif // EPROP_SYNAPSE_BSSHSLM_2020_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.
Definition eprop_synapse_bsshslm_2020.h:177
void get_status(Dictionary &d) const
Get parameter dictionary.
Definition eprop_synapse_bsshslm_2020.cpp:58
~EpropSynapseBSSHSLM2020CommonProperties()
Destructor.
Definition eprop_synapse_bsshslm_2020.cpp:52
WeightOptimizerCommonProperties * optimizer_cp_
Pointer to common properties object for weight optimizer.
Definition eprop_synapse_bsshslm_2020.h:205
EpropSynapseBSSHSLM2020CommonProperties & operator=(const EpropSynapseBSSHSLM2020CommonProperties &)=delete
Assignment operator.
EpropSynapseBSSHSLM2020CommonProperties()
Definition eprop_synapse_bsshslm_2020.cpp:37
void set_status(const Dictionary &d, ConnectorModel &cm)
Update values in parameter dictionary.
Definition eprop_synapse_bsshslm_2020.cpp:68
bool average_gradient_
If True, average the gradient over the learning window.
Definition eprop_synapse_bsshslm_2020.h:198
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
virtual void initialize_update_history()
Initializes the update history.
Definition node.cpp:229
Time get_eprop_update_interval() const
Definition simulation_manager.h:343
Event for spike information.
Definition event.h:420
Definition nest_time.h:135
static Time get_resolution()
Definition nest_time.h:325
long get_steps() const
Definition nest_time.h:504
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_bsshslm_2020.h:294
size_t handles_test_event(SpikeEvent &, size_t)
Check if the node can handle a particular event and receptor type.
Definition eprop_synapse_bsshslm_2020.h:299
size_t handles_test_event(DSSpikeEvent &, size_t)
Definition eprop_synapse_bsshslm_2020.h:305
Class implementing a synapse model for e-prop plasticity.
Definition eprop_synapse_bsshslm_2020.h:239
size_t get_rport() const
Definition connection.h:316
double weight_
Synaptic weight.
Definition eprop_synapse_bsshslm_2020.h:334
eprop_synapse_bsshslm_2020 & operator=(const eprop_synapse_bsshslm_2020 &)
Assignment operator.
Definition eprop_synapse_bsshslm_2020.h:434
long t_previous_update_
The time step when the previous e-prop update was.
Definition eprop_synapse_bsshslm_2020.h:346
WeightOptimizer * optimizer_
Optimizer.
Definition eprop_synapse_bsshslm_2020.h:371
static constexpr bool supports_flush_event
Whether this connection type supports flush events.
Definition eprop_synapse_bsshslm_2020.h:249
long get_delay_steps() const
Return the delay of the connection in steps.
Definition connection.h:271
~eprop_synapse_bsshslm_2020()
Destructor.
Definition eprop_synapse_bsshslm_2020.h:408
void set_weight(const double w)
Set the synaptic weight to the provided value.
Definition eprop_synapse_bsshslm_2020.h:324
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_bsshslm_2020.h:507
long t_next_update_
The time step when the next e-prop update will be.
Definition eprop_synapse_bsshslm_2020.h:349
double kappa_
Low-pass filter of the eligibility trace.
Definition eprop_synapse_bsshslm_2020.h:358
Node * get_target(const size_t tid) const
Definition connection.h:311
bool is_recurrent_to_recurrent_conn_
If this connection is between two recurrent neurons.
Definition eprop_synapse_bsshslm_2020.h:361
void delete_optimizer()
Delete optimizer.
Definition eprop_synapse_bsshslm_2020.h:530
long t_spike_previous_
The time step when the previous spike arrived.
Definition eprop_synapse_bsshslm_2020.h:340
void get_status(Dictionary &d) const
Get parameter dictionary.
Definition eprop_synapse_bsshslm_2020.h:642
Connection< targetidentifierT, TotalDelay > ConnectionBase
Type of the connection base.
Definition eprop_synapse_bsshslm_2020.h:246
static constexpr ConnectionModelProperties properties
Properties of the connection model.
Definition eprop_synapse_bsshslm_2020.h:256
EpropSynapseBSSHSLM2020CommonProperties CommonPropertiesType
Type of the common synapse properties.
Definition eprop_synapse_bsshslm_2020.h:243
bool send(Event &e, size_t thread, const EpropSynapseBSSHSLM2020CommonProperties &cp)
Send the spike event.
Definition eprop_synapse_bsshslm_2020.h:538
eprop_synapse_bsshslm_2020()
Default constructor.
Definition eprop_synapse_bsshslm_2020.h:391
double gradient_
Gradient used for the weight update.
Definition eprop_synapse_bsshslm_2020.h:337
std::vector< long > presyn_isis_
Vector of presynaptic inter-spike-intervals.
Definition eprop_synapse_bsshslm_2020.h:364
double tau_m_readout_
Time constant for low-pass filtering the eligibility trace.
Definition eprop_synapse_bsshslm_2020.h:355
bool previous_was_flush_event_
Previous event was a flush event.
Definition eprop_synapse_bsshslm_2020.h:343
void set_status(const Dictionary &d, ConnectorModel &cm)
Update values in parameter dictionary.
Definition eprop_synapse_bsshslm_2020.h:661
long t_previous_trigger_spike_
The time step when the spike arrived that triggered the previous e-prop update.
Definition eprop_synapse_bsshslm_2020.h:352
SimulationManager simulation_manager
Definition kernel_manager.h:237
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 tau_m_readout("tau_m_readout")
const std::string size_of("sizeof")
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
KernelManager & kernel()
Definition kernel_manager.h:311
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
void register_eprop_synapse_bsshslm_2020(const std::string &name)
Register the eprop synapse model.
Definition eprop_synapse_bsshslm_2020.cpp:32
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