Loading src/TNL/Solvers/Optimization/AdaGrad.h +7 −8 Original line number Diff line number Diff line Loading @@ -9,14 +9,15 @@ #include <TNL/Solvers/IterativeSolver.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { /*** * https://arxiv.org/pdf/1609.04747.pdf * */ template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > class AdaGrad : public IterativeSolver< typename Vector::RealType, typename Vector::IndexType, SolverMonitor > { public: Loading Loading @@ -45,15 +46,13 @@ public: solve( VectorView& w, GradientGetter&& getGradient ); protected: RealType relaxation = 1.0, epsilon = 1.0e-8; VectorType gradient, a; }; } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL #include <TNL/Solvers/Optimization/AdaGrad.hpp> src/TNL/Solvers/Optimization/AdaGrad.hpp +18 −22 Original line number Diff line number Diff line Loading @@ -9,14 +9,12 @@ #include <TNL/Solvers/Optimization/AdaGrad.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { template< typename Vector, typename SolverMonitor > void AdaGrad< Vector, SolverMonitor >:: configSetup( Config::ConfigDescription& config, const String& prefix ) AdaGrad< Vector, SolverMonitor >::configSetup( Config::ConfigDescription& config, const String& prefix ) { IterativeSolver< RealType, IndexType, SolverMonitor >::configSetup( config, prefix ); config.addEntry< double >( prefix + "relaxation", "Relaxation parameter for the gradient descent.", 1.0 ); Loading @@ -24,8 +22,7 @@ configSetup( Config::ConfigDescription& config, const String& prefix ) template< typename Vector, typename SolverMonitor > bool AdaGrad< Vector, SolverMonitor >:: setup( const Config::ParameterContainer& parameters, const String& prefix ) AdaGrad< Vector, SolverMonitor >::setup( const Config::ParameterContainer& parameters, const String& prefix ) { this->setRelaxation( parameters.getParameter< double >( prefix + "relaxation" ) ); return IterativeSolver< RealType, IndexType, SolverMonitor >::setup( parameters, prefix ); Loading @@ -33,25 +30,22 @@ setup( const Config::ParameterContainer& parameters, const String& prefix ) template< typename Vector, typename SolverMonitor > void AdaGrad< Vector, SolverMonitor >:: setRelaxation( const RealType& lambda ) AdaGrad< Vector, SolverMonitor >::setRelaxation( const RealType& lambda ) { this->relaxation = lambda; } template< typename Vector, typename SolverMonitor > auto AdaGrad< Vector, SolverMonitor >:: getRelaxation() const -> const RealType& AdaGrad< Vector, SolverMonitor >::getRelaxation() const -> const RealType& { return this->relaxation; } template< typename Vector, typename SolverMonitor > template< typename GradientGetter > template< typename GradientGetter > bool AdaGrad< Vector, SolverMonitor >:: solve( VectorView& w, GradientGetter&& getGradient ) AdaGrad< Vector, SolverMonitor >::solve( VectorView& w, GradientGetter&& getGradient ) { this->gradient.setLike( w ); this->a.setLike( w ); Loading @@ -67,27 +61,29 @@ solve( VectorView& w, GradientGetter&& getGradient ) ///// // Start the main loop while( 1 ) { while( 1 ) { ///// // Compute the gradient getGradient( w_view, gradient_view ); RealType lastResidue = this->getResidue(); // a_i += grad_i^2 a += gradient_view * gradient_view; this->setResidue( addAndReduceAbs( w_view, -this->relaxation / sqrt( this->a + this->epsilon ) * gradient_view, TNL::Plus(), ( RealType ) 0.0 ) / ( this->relaxation * ( RealType ) w.getSize() ) ); this->setResidue( addAndReduceAbs( w_view, -this->relaxation / sqrt( this->a + this->epsilon ) * gradient_view, TNL::Plus(), (RealType) 0.0 ) / ( this->relaxation * (RealType) w.getSize() ) ); if( ! this->nextIteration() ) return this->checkConvergence(); ///// // Check the stop condition if( this->getConvergenceResidue() != 0.0 && this->getResidue() < this -> getConvergenceResidue() ) if( this->getConvergenceResidue() != 0.0 && this->getResidue() < this->getConvergenceResidue() ) return true; } return false; // just to avoid warnings return false; // just to avoid warnings } } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL src/TNL/Solvers/Optimization/GradientDescent.h +7 −8 Original line number Diff line number Diff line Loading @@ -9,10 +9,11 @@ #include <TNL/Solvers/IterativeSolver.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > class GradientDescent : public IterativeSolver< typename Vector::RealType, typename Vector::IndexType, SolverMonitor > { public: Loading Loading @@ -41,15 +42,13 @@ public: solve( VectorView& w, GradientGetter&& getGradient ); protected: RealType relaxation = 1.0; VectorType gradient; }; } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL #include <TNL/Solvers/Optimization/GradientDescent.hpp> src/TNL/Solvers/Optimization/GradientDescent.hpp +16 −22 Original line number Diff line number Diff line Loading @@ -9,14 +9,12 @@ #include <TNL/Solvers/Optimization/GradientDescent.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { template< typename Vector, typename SolverMonitor > void GradientDescent< Vector, SolverMonitor >:: configSetup( Config::ConfigDescription& config, const String& prefix ) GradientDescent< Vector, SolverMonitor >::configSetup( Config::ConfigDescription& config, const String& prefix ) { IterativeSolver< RealType, IndexType, SolverMonitor >::configSetup( config, prefix ); config.addEntry< double >( prefix + "relaxation", "Relaxation parameter for the gradient descent.", 1.0 ); Loading @@ -24,8 +22,7 @@ configSetup( Config::ConfigDescription& config, const String& prefix ) template< typename Vector, typename SolverMonitor > bool GradientDescent< Vector, SolverMonitor >:: setup( const Config::ParameterContainer& parameters, const String& prefix ) GradientDescent< Vector, SolverMonitor >::setup( const Config::ParameterContainer& parameters, const String& prefix ) { this->setRelaxation( parameters.getParameter< double >( prefix + "relaxation" ) ); return IterativeSolver< RealType, IndexType, SolverMonitor >::setup( parameters, prefix ); Loading @@ -33,25 +30,22 @@ setup( const Config::ParameterContainer& parameters, const String& prefix ) template< typename Vector, typename SolverMonitor > void GradientDescent< Vector, SolverMonitor >:: setRelaxation( const RealType& lambda ) GradientDescent< Vector, SolverMonitor >::setRelaxation( const RealType& lambda ) { this->relaxation = lambda; } template< typename Vector, typename SolverMonitor > auto GradientDescent< Vector, SolverMonitor >:: getRelaxation() const -> const RealType& GradientDescent< Vector, SolverMonitor >::getRelaxation() const -> const RealType& { return this->relaxation; } template< typename Vector, typename SolverMonitor > template< typename GradientGetter > template< typename GradientGetter > bool GradientDescent< Vector, SolverMonitor >:: solve( VectorView& w, GradientGetter&& getGradient ) GradientDescent< Vector, SolverMonitor >::solve( VectorView& w, GradientGetter&& getGradient ) { this->gradient.setLike( w ); auto gradient_view = gradient.getView(); Loading @@ -65,25 +59,25 @@ solve( VectorView& w, GradientGetter&& getGradient ) ///// // Start the main loop while( 1 ) { while( 1 ) { ///// // Compute the gradient getGradient( w_view, gradient_view ); RealType lastResidue = this->getResidue(); this->setResidue( addAndReduceAbs( w_view, -this->relaxation * gradient_view, TNL::Plus(), ( RealType ) 0.0 ) / ( this->relaxation * ( RealType ) w.getSize() ) ); this->setResidue( addAndReduceAbs( w_view, -this->relaxation * gradient_view, TNL::Plus(), (RealType) 0.0 ) / ( this->relaxation * (RealType) w.getSize() ) ); if( ! this->nextIteration() ) return this->checkConvergence(); ///// // Check the stop condition if( this->getConvergenceResidue() != 0.0 && this->getResidue() < this -> getConvergenceResidue() ) if( this->getConvergenceResidue() != 0.0 && this->getResidue() < this->getConvergenceResidue() ) return true; } return false; // just to avoid warnings return false; // just to avoid warnings } } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL src/TNL/Solvers/Optimization/Momentum.h +7 −8 Original line number Diff line number Diff line Loading @@ -9,10 +9,11 @@ #include <TNL/Solvers/IterativeSolver.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > class Momentum : public IterativeSolver< typename Vector::RealType, typename Vector::IndexType, SolverMonitor > { public: Loading Loading @@ -47,15 +48,13 @@ public: solve( VectorView& w, GradientGetter&& getGradient ); protected: RealType relaxation = 1.0, momentum = 0.9; VectorType gradient, v; }; } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL #include <TNL/Solvers/Optimization/Momentum.hpp> Loading
src/TNL/Solvers/Optimization/AdaGrad.h +7 −8 Original line number Diff line number Diff line Loading @@ -9,14 +9,15 @@ #include <TNL/Solvers/IterativeSolver.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { /*** * https://arxiv.org/pdf/1609.04747.pdf * */ template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > class AdaGrad : public IterativeSolver< typename Vector::RealType, typename Vector::IndexType, SolverMonitor > { public: Loading Loading @@ -45,15 +46,13 @@ public: solve( VectorView& w, GradientGetter&& getGradient ); protected: RealType relaxation = 1.0, epsilon = 1.0e-8; VectorType gradient, a; }; } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL #include <TNL/Solvers/Optimization/AdaGrad.hpp>
src/TNL/Solvers/Optimization/AdaGrad.hpp +18 −22 Original line number Diff line number Diff line Loading @@ -9,14 +9,12 @@ #include <TNL/Solvers/Optimization/AdaGrad.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { template< typename Vector, typename SolverMonitor > void AdaGrad< Vector, SolverMonitor >:: configSetup( Config::ConfigDescription& config, const String& prefix ) AdaGrad< Vector, SolverMonitor >::configSetup( Config::ConfigDescription& config, const String& prefix ) { IterativeSolver< RealType, IndexType, SolverMonitor >::configSetup( config, prefix ); config.addEntry< double >( prefix + "relaxation", "Relaxation parameter for the gradient descent.", 1.0 ); Loading @@ -24,8 +22,7 @@ configSetup( Config::ConfigDescription& config, const String& prefix ) template< typename Vector, typename SolverMonitor > bool AdaGrad< Vector, SolverMonitor >:: setup( const Config::ParameterContainer& parameters, const String& prefix ) AdaGrad< Vector, SolverMonitor >::setup( const Config::ParameterContainer& parameters, const String& prefix ) { this->setRelaxation( parameters.getParameter< double >( prefix + "relaxation" ) ); return IterativeSolver< RealType, IndexType, SolverMonitor >::setup( parameters, prefix ); Loading @@ -33,25 +30,22 @@ setup( const Config::ParameterContainer& parameters, const String& prefix ) template< typename Vector, typename SolverMonitor > void AdaGrad< Vector, SolverMonitor >:: setRelaxation( const RealType& lambda ) AdaGrad< Vector, SolverMonitor >::setRelaxation( const RealType& lambda ) { this->relaxation = lambda; } template< typename Vector, typename SolverMonitor > auto AdaGrad< Vector, SolverMonitor >:: getRelaxation() const -> const RealType& AdaGrad< Vector, SolverMonitor >::getRelaxation() const -> const RealType& { return this->relaxation; } template< typename Vector, typename SolverMonitor > template< typename GradientGetter > template< typename GradientGetter > bool AdaGrad< Vector, SolverMonitor >:: solve( VectorView& w, GradientGetter&& getGradient ) AdaGrad< Vector, SolverMonitor >::solve( VectorView& w, GradientGetter&& getGradient ) { this->gradient.setLike( w ); this->a.setLike( w ); Loading @@ -67,27 +61,29 @@ solve( VectorView& w, GradientGetter&& getGradient ) ///// // Start the main loop while( 1 ) { while( 1 ) { ///// // Compute the gradient getGradient( w_view, gradient_view ); RealType lastResidue = this->getResidue(); // a_i += grad_i^2 a += gradient_view * gradient_view; this->setResidue( addAndReduceAbs( w_view, -this->relaxation / sqrt( this->a + this->epsilon ) * gradient_view, TNL::Plus(), ( RealType ) 0.0 ) / ( this->relaxation * ( RealType ) w.getSize() ) ); this->setResidue( addAndReduceAbs( w_view, -this->relaxation / sqrt( this->a + this->epsilon ) * gradient_view, TNL::Plus(), (RealType) 0.0 ) / ( this->relaxation * (RealType) w.getSize() ) ); if( ! this->nextIteration() ) return this->checkConvergence(); ///// // Check the stop condition if( this->getConvergenceResidue() != 0.0 && this->getResidue() < this -> getConvergenceResidue() ) if( this->getConvergenceResidue() != 0.0 && this->getResidue() < this->getConvergenceResidue() ) return true; } return false; // just to avoid warnings return false; // just to avoid warnings } } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL
src/TNL/Solvers/Optimization/GradientDescent.h +7 −8 Original line number Diff line number Diff line Loading @@ -9,10 +9,11 @@ #include <TNL/Solvers/IterativeSolver.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > class GradientDescent : public IterativeSolver< typename Vector::RealType, typename Vector::IndexType, SolverMonitor > { public: Loading Loading @@ -41,15 +42,13 @@ public: solve( VectorView& w, GradientGetter&& getGradient ); protected: RealType relaxation = 1.0; VectorType gradient; }; } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL #include <TNL/Solvers/Optimization/GradientDescent.hpp>
src/TNL/Solvers/Optimization/GradientDescent.hpp +16 −22 Original line number Diff line number Diff line Loading @@ -9,14 +9,12 @@ #include <TNL/Solvers/Optimization/GradientDescent.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { template< typename Vector, typename SolverMonitor > void GradientDescent< Vector, SolverMonitor >:: configSetup( Config::ConfigDescription& config, const String& prefix ) GradientDescent< Vector, SolverMonitor >::configSetup( Config::ConfigDescription& config, const String& prefix ) { IterativeSolver< RealType, IndexType, SolverMonitor >::configSetup( config, prefix ); config.addEntry< double >( prefix + "relaxation", "Relaxation parameter for the gradient descent.", 1.0 ); Loading @@ -24,8 +22,7 @@ configSetup( Config::ConfigDescription& config, const String& prefix ) template< typename Vector, typename SolverMonitor > bool GradientDescent< Vector, SolverMonitor >:: setup( const Config::ParameterContainer& parameters, const String& prefix ) GradientDescent< Vector, SolverMonitor >::setup( const Config::ParameterContainer& parameters, const String& prefix ) { this->setRelaxation( parameters.getParameter< double >( prefix + "relaxation" ) ); return IterativeSolver< RealType, IndexType, SolverMonitor >::setup( parameters, prefix ); Loading @@ -33,25 +30,22 @@ setup( const Config::ParameterContainer& parameters, const String& prefix ) template< typename Vector, typename SolverMonitor > void GradientDescent< Vector, SolverMonitor >:: setRelaxation( const RealType& lambda ) GradientDescent< Vector, SolverMonitor >::setRelaxation( const RealType& lambda ) { this->relaxation = lambda; } template< typename Vector, typename SolverMonitor > auto GradientDescent< Vector, SolverMonitor >:: getRelaxation() const -> const RealType& GradientDescent< Vector, SolverMonitor >::getRelaxation() const -> const RealType& { return this->relaxation; } template< typename Vector, typename SolverMonitor > template< typename GradientGetter > template< typename GradientGetter > bool GradientDescent< Vector, SolverMonitor >:: solve( VectorView& w, GradientGetter&& getGradient ) GradientDescent< Vector, SolverMonitor >::solve( VectorView& w, GradientGetter&& getGradient ) { this->gradient.setLike( w ); auto gradient_view = gradient.getView(); Loading @@ -65,25 +59,25 @@ solve( VectorView& w, GradientGetter&& getGradient ) ///// // Start the main loop while( 1 ) { while( 1 ) { ///// // Compute the gradient getGradient( w_view, gradient_view ); RealType lastResidue = this->getResidue(); this->setResidue( addAndReduceAbs( w_view, -this->relaxation * gradient_view, TNL::Plus(), ( RealType ) 0.0 ) / ( this->relaxation * ( RealType ) w.getSize() ) ); this->setResidue( addAndReduceAbs( w_view, -this->relaxation * gradient_view, TNL::Plus(), (RealType) 0.0 ) / ( this->relaxation * (RealType) w.getSize() ) ); if( ! this->nextIteration() ) return this->checkConvergence(); ///// // Check the stop condition if( this->getConvergenceResidue() != 0.0 && this->getResidue() < this -> getConvergenceResidue() ) if( this->getConvergenceResidue() != 0.0 && this->getResidue() < this->getConvergenceResidue() ) return true; } return false; // just to avoid warnings return false; // just to avoid warnings } } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL
src/TNL/Solvers/Optimization/Momentum.h +7 −8 Original line number Diff line number Diff line Loading @@ -9,10 +9,11 @@ #include <TNL/Solvers/IterativeSolver.h> namespace TNL { namespace Solvers { namespace Optimization { namespace Solvers { namespace Optimization { template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > template< typename Vector, typename SolverMonitor = IterativeSolverMonitor< typename Vector::RealType, typename Vector::IndexType > > class Momentum : public IterativeSolver< typename Vector::RealType, typename Vector::IndexType, SolverMonitor > { public: Loading Loading @@ -47,15 +48,13 @@ public: solve( VectorView& w, GradientGetter&& getGradient ); protected: RealType relaxation = 1.0, momentum = 0.9; VectorType gradient, v; }; } //namespace Optimization } //namespace Solvers } //namespace TNL } // namespace Optimization } // namespace Solvers } // namespace TNL #include <TNL/Solvers/Optimization/Momentum.hpp>