My Project
newton.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../common.hpp"
5 #include <Eigen/Eigen>
6 #include <iostream>
7 
8 namespace cpu_mlp {
9 
13 template <typename V, typename M> class Newton : public FullBatchMinimizer<V, M> {
15  using Base::_iters;
16  using Base::_max_iters;
17  using Base::_tol;
18  using Base::line_search;
19 
20 public:
25  void setHessian(const HessFun<V, M> &hessFun) noexcept { _hessFun = hessFun; }
26 
34  V solve(V x, VecFun<V, double> &f, GradFun<V> &Gradient) override {
35  Eigen::LDLT<M> ldlt;
36 
37  check(static_cast<bool>(_hessFun), "Hessian function must be set for Newton solver");
38 
39  for (_iters = 0; _iters < _max_iters; ++_iters) {
40  V g = Gradient(x);
41  double gnorm = g.norm();
42  if (gnorm <= _tol) break;
43 
44  M H = _hessFun(x);
45  check(H.rows() == H.cols(), "Hessian must be square");
46  check(H.rows() == g.size(), "Hessian/gradient size mismatch");
47 
48  V p;
49  bool found = false;
50  double mu = reg_init;
51  const double max_mu = reg_max;
52 
53  while (mu <= max_mu) {
54  M Hreg = H;
55  Hreg += mu * M::Identity(H.rows(), H.cols());
56 
57  ldlt.compute(Hreg);
58  if (ldlt.info() == Eigen::Success) {
59  p = ldlt.solve(-g);
60  if (ldlt.info() == Eigen::Success && p.dot(g) < 0.0) {
61  found = true;
62  break;
63  }
64  }
65  mu *= reg_growth;
66  }
67 
68  if (!found) {
69  p = -g;
70  }
71 
72  double alpha = line_search(x, p, f, Gradient);
73  x = x + alpha * p;
74  }
75 
76  return x;
77  }
78 
79 private:
80  HessFun<V, M> _hessFun;
81  double reg_init = 1e-6; // Initial diagonal damping.
82  double reg_max = 1e6; // Maximum damping.
83  double reg_growth = 10.0; // Growth factor for damping.
84 };
85 
86 } // namespace cpu_mlp
Base class for Full Batch Minimizers.
Definition: full_batch_minimizer.hpp:23
double _tol
Definition: full_batch_minimizer.hpp:109
double line_search(V x, V p, VecFun< V, double > &f, GradFun< V > &Gradient)
Backtracking Line Search satisfying Wolfe Conditions.
Definition: full_batch_minimizer.hpp:126
unsigned int _iters
Definition: full_batch_minimizer.hpp:108
unsigned int _max_iters
Definition: full_batch_minimizer.hpp:107
Newton minimizer (full Newton) for unconstrained optimization.
Definition: newton.hpp:13
V solve(V x, VecFun< V, double > &f, GradFun< V > &Gradient) override
Solves the unconstrained optimization problem using Newton's Method.
Definition: newton.hpp:34
void setHessian(const HessFun< V, M > &hessFun) noexcept
Sets the Hessian function for computing the second derivative matrix.
Definition: newton.hpp:25
std::function< T(T)> GradFun
Gradient function type alias (T -> T).
Definition: common.hpp:32
std::function< W(T)> VecFun
Objective function type alias (T -> W).
Definition: common.hpp:35
#define check(condition, message)
Debug assertion with message and source location.
Definition: common.hpp:14
std::function< M(V)> HessFun
Hessian function type alias (V -> M).
Definition: common.hpp:38
Definition: layer.hpp:13