LCOV - code coverage report
Current view: top level - ves - Opt_RobbinsMonroSGD.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 26 27 96.3 %
Date: 2019-08-13 10:15:31 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-2017 The VES code team
       3             :    (see the PEOPLE-VES file at the root of this folder for a list of names)
       4             : 
       5             :    See http://www.ves-code.org for more information.
       6             : 
       7             :    This file is part of VES code module.
       8             : 
       9             :    The VES code module is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    The VES code module is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with the VES code module.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : 
      23             : #include "Optimizer.h"
      24             : #include "CoeffsVector.h"
      25             : 
      26             : #include "core/ActionRegister.h"
      27             : 
      28             : 
      29             : namespace PLMD {
      30             : namespace ves {
      31             : 
      32             : //+PLUMEDOC VES_OPTIMIZER OPT_ROBBINS_MONRO_SGD
      33             : /*
      34             : Robbins-Monro stochastic gradient decent.
      35             : 
      36             : \par Examples
      37             : 
      38             : */
      39             : //+ENDPLUMEDOC
      40             : 
      41           6 : class Opt_RobbinsMonroSGD : public Optimizer {
      42             : private:
      43             :   double decay_constant_;
      44             : public:
      45             :   static void registerKeywords(Keywords&);
      46             :   explicit Opt_RobbinsMonroSGD(const ActionOptions&);
      47             :   void coeffsUpdate(const unsigned int c_id = 0);
      48             : };
      49             : 
      50             : 
      51        7838 : PLUMED_REGISTER_ACTION(Opt_RobbinsMonroSGD,"OPT_ROBBINS_MONRO_SGD")
      52             : 
      53             : 
      54           4 : void Opt_RobbinsMonroSGD::registerKeywords(Keywords& keys) {
      55           4 :   Optimizer::registerKeywords(keys);
      56           4 :   Optimizer::useDynamicStepSizeKeywords(keys);
      57           4 :   Optimizer::useMultipleWalkersKeywords(keys);
      58           4 :   Optimizer::useMaskKeywords(keys);
      59           4 :   Optimizer::useRestartKeywords(keys);
      60             :   // Optimizer::useMonitorAveragesKeywords(keys);
      61           4 :   Optimizer::useDynamicTargetDistributionKeywords(keys);
      62          16 :   keys.add("optional","DECAY_CONSTANT","the decay constant used for the step size.");
      63           4 : }
      64             : 
      65             : 
      66           3 : Opt_RobbinsMonroSGD::Opt_RobbinsMonroSGD(const ActionOptions&ao):
      67             :   PLUMED_VES_OPTIMIZER_INIT(ao),
      68           3 :   decay_constant_(1.0)
      69             : {
      70           6 :   parse("DECAY_CONSTANT",decay_constant_);
      71           3 :   if(decay_constant_<1.0) {
      72           0 :     plumed_merror("the value given in DECAY_CONSTANT doesn't make sense, it should be larger than 1.0");
      73             :   }
      74           3 :   if(decay_constant_>1.0) {
      75           1 :     log.printf("  using a decay constant of %f\n",decay_constant_);
      76             :   }
      77           3 :   checkRead();
      78           3 : }
      79             : 
      80             : 
      81          30 : void Opt_RobbinsMonroSGD::coeffsUpdate(const unsigned int c_id) {
      82             :   // getIterationCounterDbl() gives n-1 as it is updated afterwards.
      83          30 :   double current_stepsize =  StepSize(c_id) /(1.0 + getIterationCounterDbl()/decay_constant_);
      84             :   setCurrentStepSize(current_stepsize,c_id);
      85          60 :   Coeffs(c_id) += - current_stepsize * CoeffsMask(c_id) * Gradient(c_id);
      86             :   //
      87          30 :   double aver_decay = 1.0 / ( getIterationCounterDbl() + 1.0 );
      88          60 :   AuxCoeffs(c_id) += aver_decay * ( Coeffs(c_id)-AuxCoeffs(c_id) );
      89          30 : }
      90             : 
      91             : 
      92             : }
      93        5874 : }

Generated by: LCOV version 1.14