NEST main@caf0ae8
 
Loading...
Searching...
No Matches
weight_optimizer.h
Go to the documentation of this file.
1/*
2 * weight_optimizer.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 WEIGHT_OPTIMIZER_H
24#define WEIGHT_OPTIMIZER_H
25
26// nestkernel
27#include "dictionary.h"
28
29namespace nest
30{
31
32/* BeginUserDocs: e-prop plasticity, synapse
33
34Short description
35+++++++++++++++++
36
37Selection of weight optimizers
38
39Description
40+++++++++++
41A weight optimizer is an algorithm that adjusts the synaptic weights in a
42network during training to minimize the loss function and thus improve the
43network's performance on a given task.
44
45This method is an essential part of plasticity rules like e-prop plasticity.
46
47Currently two weight optimizers are implemented: gradient descent and the Adam optimizer.
48
49In gradient descent :footcite:p:`Huh2018` the weights are optimized via:
50
51.. math::
52 W_t = W_{t-1} - \eta g_t \,, \\
53
54where :math:`\eta` denotes the learning rate and :math:`g_t` the gradient of the current
55time step :math:`t`.
56
57In the Adam scheme :footcite:p:`Kingma2015` the weights are optimized via:
58
59.. math::
60 m_0 &= 0, v_0 = 0, t = 1 \,, \\
61 m_t &= \beta_1 m_{t-1} + \left( 1- \beta_1 \right) g_t \,, \\
62 v_t &= \beta_2 v_{t-1} + \left( 1 - \beta_2 \right) g_t^2 \,, \\
63 \alpha_t &= \eta \frac{ \sqrt{ 1- \beta_2^t } }{ 1 - \beta_1^t } \,, \\
64 W_t &= W_{t-1} - \alpha_t \frac{ m_t }{ \sqrt{v_t} + \hat{\epsilon} } \,. \\
65
66Note that the implementation follows the implementation in TensorFlow :footcite:p:`KerasTeam2024` for comparability.
67The TensorFlow implementation deviates from :footcite:p:`Huh2018` in that it assumes
68:math:`\hat{\epsilon} = \epsilon \sqrt{ 1 - \beta_2^t }` to be constant, whereas :footcite:p:`Huh2018`
69assumes :math:`\epsilon = \hat{\epsilon} \sqrt{ 1 - \beta_2^t }` to be constant.
70
71When `optimize_each_step` is set to `True`, the weights are optimized at every
72time step. If set to `False`, optimization occurs once per spike, resulting in a
73significant speed-up. For gradient descent, both settings yield the same
74results under exact arithmetic; however, small numerical differences may be
75observed due to floating point precision. For the Adam optimizer, only setting
76`optimize_each_step` to `True` precisely implements the algorithm as described
77in :footcite:p:`Kingma2015`. The impact of this setting on learning performance may vary depending
78on the task.
79
80Parameters
81++++++++++
82
83The following parameters can be set in the status dictionary.
84
85====================== ==== ========================= ========= ============================================
86**Common optimizer parameters**
87------------------------------------------------------------------------------------------------------------
88Parameter Unit Math equivalent Default Description
89====================== ==== ========================= ========= ============================================
90``batch_size`` 1 Size of batch
91``eta`` :math:`\eta` 1e-4 Learning rate
92``optimize_each_step`` ``True`` If ``True``, optimize each step, if ``False``
93 once per spike
94``Wmax`` pA :math:`W_{ji}^\text{max}` 100.0 Maximal value for synaptic weight
95``Wmin`` pA :math:`W_{ji}^\text{min}` -100.0 Minimal value for synaptic weight
96====================== ==== ========================= ========= ============================================
97
98========= ==== =============== ================== ==============
99**Gradient descent parameters (default optimizer)**
100----------------------------------------------------------------
101Parameter Unit Math equivalent Default Description
102========= ==== =============== ================== ==============
103``type`` "gradient_descent" Optimizer type
104========= ==== =============== ================== ==============
105
106=========== ==== ================ ======= =================================================
107**Adam optimizer parameters**
108-------------------------------------------------------------------------------------------
109Parameter Unit Math equivalent Default Description
110=========== ==== ================ ======= =================================================
111``type`` "adam" Optimizer type
112``beta_1`` :math:`\beta_1` 0.9 Exponential decay rate for first moment estimate
113``beta_2`` :math:`\beta_2` 0.999 Exponential decay rate for second moment estimate
114``epsilon`` :math:`\epsilon` 1e-7 Small constant for numerical stability
115=========== ==== ================ ======= =================================================
116
117The following state variables evolve during simulation.
118
119============== ==== =============== ============= ==========================
120**Adam optimizer state variables for individual synapses**
121----------------------------------------------------------------------------
122State variable Unit Math equivalent Initial value Description
123============== ==== =============== ============= ==========================
124``m`` :math:`m` 0.0 First moment estimate
125``v`` :math:`v` 0.0 Second moment raw estimate
126============== ==== =============== ============= ==========================
127
128
129References
130++++++++++
131
132.. footbibliography::
133
134See also
135++++++++
136
137Examples using this model
138+++++++++++++++++++++++++
139
140.. listexamples:: eprop_synapse_bsshslm_2020
141
142EndUserDocs */
143
144class WeightOptimizer;
145
154{
155public:
158
161 {
162 }
163
166
169
171 virtual void get_status( Dictionary& d ) const;
172
174 virtual void set_status( const Dictionary& d );
175
178
180 virtual WeightOptimizer* get_optimizer() const = 0;
181
183 double
184 get_Wmin() const
185 {
186 return Wmin_;
187 }
188
190 double
191 get_Wmax() const
192 {
193 return Wmax_;
194 }
195
197 virtual std::string get_name() const = 0;
198
199public:
202
204 double eta_;
205
215
218
220 double Wmin_;
221
223 double Wmax_;
224
227};
228
238{
239public:
242
245 {
246 }
247
249 WeightOptimizer( const WeightOptimizer& ) = default;
250
253
255 virtual void get_status( Dictionary& d ) const;
256
258 virtual void set_status( const Dictionary& d );
259
262 const size_t idx_current_update,
263 const double gradient,
264 double weight );
265
266protected:
268 virtual double optimize_( const WeightOptimizerCommonProperties& cp, double weight, size_t current_opt_step ) = 0;
269
272
275
283
286};
287
292{
293public:
296
299
302
303private:
304 double optimize_( const WeightOptimizerCommonProperties& cp, double weight, size_t current_opt_step ) override;
305};
306
335
340{
341public:
344
347
350
351 void get_status( Dictionary& d ) const override;
352 void set_status( const Dictionary& d ) override;
353
354private:
355 double optimize_( const WeightOptimizerCommonProperties& cp, double weight, size_t current_opt_step ) override;
356
358 double m_;
359
361 double v_;
362
365
368};
369
374{
377
378public:
381
384
387
388 WeightOptimizerCommonProperties* clone() const override;
389 WeightOptimizer* get_optimizer() const override;
390
391 void get_status( Dictionary& d ) const override;
392 void set_status( const Dictionary& d ) override;
393
394 std::string
395 get_name() const override
396 {
397 return "adam";
398 }
399
400private:
402 double beta_1_;
403
405 double beta_2_;
406
408 double epsilon_;
409};
410
411} // namespace nest
412
413#endif // WEIGHT_OPTIMIZER_H
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
Base class implementing an Adam weight optimizer model.
Definition weight_optimizer.h:340
double beta_2_power_
Power of beta_2 factor.
Definition weight_optimizer.h:367
double optimize_(const WeightOptimizerCommonProperties &cp, double weight, size_t current_opt_step) override
Perform specific optimization.
Definition weight_optimizer.cpp:269
double m_
First moment estimate variable.
Definition weight_optimizer.h:358
WeightOptimizerAdam()
Default constructor.
Definition weight_optimizer.cpp:242
void get_status(Dictionary &d) const override
Get parameter dictionary.
Definition weight_optimizer.cpp:252
void set_status(const Dictionary &d) override
Update values in parameter dictionary.
Definition weight_optimizer.cpp:260
double beta_1_power_
Power of beta_1 factor.
Definition weight_optimizer.h:364
WeightOptimizerAdam & operator=(const WeightOptimizerAdam &)=delete
Assignment operator.
double v_
Second moment estimate variable.
Definition weight_optimizer.h:361
WeightOptimizerAdam(const WeightOptimizerAdam &)=default
Copy constructor.
Class implementing common properties of an Adam weight optimizer model.
Definition weight_optimizer.h:374
double epsilon_
Small constant for numerical stability.
Definition weight_optimizer.h:408
double beta_1_
Exponential decay rate for first moment estimate.
Definition weight_optimizer.h:402
double beta_2_
Exponential decay rate for second moment estimate.
Definition weight_optimizer.h:405
std::string get_name() const override
Get optimizer name.
Definition weight_optimizer.h:395
void set_status(const Dictionary &d) override
Update parameters in parameter dictionary.
Definition weight_optimizer.cpp:218
WeightOptimizer * get_optimizer() const override
Get optimizer.
Definition weight_optimizer.cpp:202
void get_status(Dictionary &d) const override
Get parameter dictionary.
Definition weight_optimizer.cpp:208
WeightOptimizerCommonPropertiesAdam()
Default constructor.
Definition weight_optimizer.cpp:186
WeightOptimizerCommonPropertiesAdam(const WeightOptimizerCommonPropertiesAdam &)=default
Copy constructor.
WeightOptimizerCommonProperties * clone() const override
Clone constructor.
Definition weight_optimizer.cpp:196
WeightOptimizerCommonPropertiesAdam & operator=(const WeightOptimizerCommonPropertiesAdam &)=delete
Assignment operator.
Class implementing common properties of a gradient descent weight optimizer model.
Definition weight_optimizer.h:311
WeightOptimizerCommonProperties * clone() const override
Clone constructor.
Definition weight_optimizer.cpp:162
WeightOptimizer * get_optimizer() const override
Get optimizer.
Definition weight_optimizer.cpp:168
WeightOptimizerCommonPropertiesGradientDescent()
Default constructor.
Definition weight_optimizer.cpp:156
std::string get_name() const override
Get optimizer name.
Definition weight_optimizer.h:330
WeightOptimizerCommonPropertiesGradientDescent & operator=(const WeightOptimizerCommonPropertiesGradientDescent &)=delete
Assignment operator.
WeightOptimizerCommonPropertiesGradientDescent(const WeightOptimizerCommonPropertiesGradientDescent &)=default
Copy constructor.
Base class implementing common properties of a weight optimizer model.
Definition weight_optimizer.h:154
double eta_
Common learning rate for all synapses.
Definition weight_optimizer.h:204
double Wmin_
Minimal value for synaptic weight.
Definition weight_optimizer.h:220
WeightOptimizerCommonProperties()
Default constructor.
Definition weight_optimizer.cpp:34
virtual WeightOptimizer * get_optimizer() const =0
Get optimizer.
double Wmax_
Maximal value for synaptic weight.
Definition weight_optimizer.h:223
double get_Wmin() const
Get minimal value for synaptic weight.
Definition weight_optimizer.h:184
virtual ~WeightOptimizerCommonProperties()
Destructor.
Definition weight_optimizer.h:160
long eta_change_count_
Count of learning rate changes so far in the simulation to identify the first change.
Definition weight_optimizer.h:217
virtual std::string get_name() const =0
Get optimizer name.
size_t batch_size_
Size of an optimization batch.
Definition weight_optimizer.h:201
WeightOptimizer & operator=(const WeightOptimizer &)=delete
Assignment operator.
virtual WeightOptimizerCommonProperties * clone() const =0
Clone constructor.
virtual void set_status(const Dictionary &d)
Update parameters in parameter dictionary.
Definition weight_optimizer.cpp:68
double eta_first_change_
First non-default learning rate.
Definition weight_optimizer.h:214
double get_Wmax() const
Get maximal value for synaptic weight.
Definition weight_optimizer.h:191
virtual void get_status(Dictionary &d) const
Get parameter dictionary.
Definition weight_optimizer.cpp:57
bool optimize_each_step_
If true, optimize each step, else once per spike.
Definition weight_optimizer.h:226
Base class implementing a gradient descent weight optimizer model.
Definition weight_optimizer.h:292
WeightOptimizerGradientDescent(const WeightOptimizerGradientDescent &)=default
Copy constructor.
WeightOptimizerGradientDescent()
Default constructor.
Definition weight_optimizer.cpp:173
WeightOptimizerGradientDescent & operator=(const WeightOptimizerGradientDescent &)=delete
Assignment operator.
double optimize_(const WeightOptimizerCommonProperties &cp, double weight, size_t current_opt_step) override
Perform specific optimization.
Definition weight_optimizer.cpp:179
Base class implementing a weight optimizer model.
Definition weight_optimizer.h:238
virtual void set_status(const Dictionary &d)
Update values in parameter dictionary.
Definition weight_optimizer.cpp:123
double eta_current_
Synapse-specific learning rate when the history for its upcoming weight update was archived.
Definition weight_optimizer.h:282
double optimized_weight(const WeightOptimizerCommonProperties &cp, const size_t idx_current_update, const double gradient, double weight)
Return optimized weight based on current weight.
Definition weight_optimizer.cpp:128
WeightOptimizer(const WeightOptimizer &)=default
Copy constructor.
virtual void get_status(Dictionary &d) const
Get parameter dictionary.
Definition weight_optimizer.cpp:118
WeightOptimizer & operator=(const WeightOptimizer &)=delete
Assignment operator.
double cumulative_gradient_
Cumulative gradient over the current batch.
Definition weight_optimizer.h:271
virtual double optimize_(const WeightOptimizerCommonProperties &cp, double weight, size_t current_opt_step)=0
Perform specific optimization.
long n_optimize_
Number of optimizations.
Definition weight_optimizer.h:285
size_t optimization_step_
Current optimization step, whereby optimization happens every batch_size_ steps.
Definition weight_optimizer.h:274
WeightOptimizer()
Default constructor.
Definition weight_optimizer.cpp:109
virtual ~WeightOptimizer()
Destructor.
Definition weight_optimizer.h:244
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33