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