Blame internal/ceres/low_rank_inverse_hessian.cc

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#include <list>
Packit ea1746
Packit ea1746
#include "ceres/internal/eigen.h"
Packit ea1746
#include "ceres/low_rank_inverse_hessian.h"
Packit ea1746
#include "glog/logging.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
using std::list;
Packit ea1746
Packit ea1746
// The (L)BFGS algorithm explicitly requires that the secant equation:
Packit ea1746
//
Packit ea1746
//   B_{k+1} * s_k = y_k
Packit ea1746
//
Packit ea1746
// Is satisfied at each iteration, where B_{k+1} is the approximated
Packit ea1746
// Hessian at the k+1-th iteration, s_k = (x_{k+1} - x_{k}) and
Packit ea1746
// y_k = (grad_{k+1} - grad_{k}). As the approximated Hessian must be
Packit ea1746
// positive definite, this is equivalent to the condition:
Packit ea1746
//
Packit ea1746
//   s_k^T * y_k > 0     [s_k^T * B_{k+1} * s_k = s_k^T * y_k > 0]
Packit ea1746
//
Packit ea1746
// This condition would always be satisfied if the function was strictly
Packit ea1746
// convex, alternatively, it is always satisfied provided that a Wolfe line
Packit ea1746
// search is used (even if the function is not strictly convex).  See [1]
Packit ea1746
// (p138) for a proof.
Packit ea1746
//
Packit ea1746
// Although Ceres will always use a Wolfe line search when using (L)BFGS,
Packit ea1746
// practical implementation considerations mean that the line search
Packit ea1746
// may return a point that satisfies only the Armijo condition, and thus
Packit ea1746
// could violate the Secant equation.  As such, we will only use a step
Packit ea1746
// to update the Hessian approximation if:
Packit ea1746
//
Packit ea1746
//   s_k^T * y_k > tolerance
Packit ea1746
//
Packit ea1746
// It is important that tolerance is very small (and >=0), as otherwise we
Packit ea1746
// might skip the update too often and fail to capture important curvature
Packit ea1746
// information in the Hessian.  For example going from 1e-10 -> 1e-14 improves
Packit ea1746
// the NIST benchmark score from 43/54 to 53/54.
Packit ea1746
//
Packit ea1746
// [1] Nocedal J., Wright S., Numerical Optimization, 2nd Ed. Springer, 1999.
Packit ea1746
//
Packit ea1746
// TODO(alexs.mac): Consider using Damped BFGS update instead of
Packit ea1746
// skipping update.
Packit ea1746
const double kLBFGSSecantConditionHessianUpdateTolerance = 1e-14;
Packit ea1746
Packit ea1746
LowRankInverseHessian::LowRankInverseHessian(
Packit ea1746
    int num_parameters,
Packit ea1746
    int max_num_corrections,
Packit ea1746
    bool use_approximate_eigenvalue_scaling)
Packit ea1746
    : num_parameters_(num_parameters),
Packit ea1746
      max_num_corrections_(max_num_corrections),
Packit ea1746
      use_approximate_eigenvalue_scaling_(use_approximate_eigenvalue_scaling),
Packit ea1746
      approximate_eigenvalue_scale_(1.0),
Packit ea1746
      delta_x_history_(num_parameters, max_num_corrections),
Packit ea1746
      delta_gradient_history_(num_parameters, max_num_corrections),
Packit ea1746
      delta_x_dot_delta_gradient_(max_num_corrections) {
Packit ea1746
}
Packit ea1746
Packit ea1746
bool LowRankInverseHessian::Update(const Vector& delta_x,
Packit ea1746
                                   const Vector& delta_gradient) {
Packit ea1746
  const double delta_x_dot_delta_gradient = delta_x.dot(delta_gradient);
Packit ea1746
  if (delta_x_dot_delta_gradient <=
Packit ea1746
      kLBFGSSecantConditionHessianUpdateTolerance) {
Packit ea1746
    VLOG(2) << "Skipping L-BFGS Update, delta_x_dot_delta_gradient too "
Packit ea1746
            << "small: " << delta_x_dot_delta_gradient << ", tolerance: "
Packit ea1746
            << kLBFGSSecantConditionHessianUpdateTolerance
Packit ea1746
            << " (Secant condition).";
Packit ea1746
    return false;
Packit ea1746
  }
Packit ea1746
Packit ea1746
Packit ea1746
  int next = indices_.size();
Packit ea1746
  // Once the size of the list reaches max_num_corrections_, simulate
Packit ea1746
  // a circular buffer by removing the first element of the list and
Packit ea1746
  // making it the next position where the LBFGS history is stored.
Packit ea1746
  if (next == max_num_corrections_) {
Packit ea1746
    next = indices_.front();
Packit ea1746
    indices_.pop_front();
Packit ea1746
  }
Packit ea1746
Packit ea1746
  indices_.push_back(next);
Packit ea1746
  delta_x_history_.col(next) = delta_x;
Packit ea1746
  delta_gradient_history_.col(next) = delta_gradient;
Packit ea1746
  delta_x_dot_delta_gradient_(next) = delta_x_dot_delta_gradient;
Packit ea1746
  approximate_eigenvalue_scale_ =
Packit ea1746
      delta_x_dot_delta_gradient / delta_gradient.squaredNorm();
Packit ea1746
  return true;
Packit ea1746
}
Packit ea1746
Packit ea1746
void LowRankInverseHessian::RightMultiply(const double* x_ptr,
Packit ea1746
                                          double* y_ptr) const {
Packit ea1746
  ConstVectorRef gradient(x_ptr, num_parameters_);
Packit ea1746
  VectorRef search_direction(y_ptr, num_parameters_);
Packit ea1746
Packit ea1746
  search_direction = gradient;
Packit ea1746
Packit ea1746
  const int num_corrections = indices_.size();
Packit ea1746
  Vector alpha(num_corrections);
Packit ea1746
Packit ea1746
  for (list<int>::const_reverse_iterator it = indices_.rbegin();
Packit ea1746
       it != indices_.rend();
Packit ea1746
       ++it) {
Packit ea1746
    const double alpha_i = delta_x_history_.col(*it).dot(search_direction) /
Packit ea1746
        delta_x_dot_delta_gradient_(*it);
Packit ea1746
    search_direction -= alpha_i * delta_gradient_history_.col(*it);
Packit ea1746
    alpha(*it) = alpha_i;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  if (use_approximate_eigenvalue_scaling_) {
Packit ea1746
    // Rescale the initial inverse Hessian approximation (H_0) to be iteratively
Packit ea1746
    // updated so that it is of similar 'size' to the true inverse Hessian along
Packit ea1746
    // the most recent search direction.  As shown in [1]:
Packit ea1746
    //
Packit ea1746
    //   \gamma_k = (delta_gradient_{k-1}' * delta_x_{k-1}) /
Packit ea1746
    //              (delta_gradient_{k-1}' * delta_gradient_{k-1})
Packit ea1746
    //
Packit ea1746
    // Satisfies:
Packit ea1746
    //
Packit ea1746
    //   (1 / \lambda_m) <= \gamma_k <= (1 / \lambda_1)
Packit ea1746
    //
Packit ea1746
    // Where \lambda_1 & \lambda_m are the smallest and largest eigenvalues of
Packit ea1746
    // the true Hessian (not the inverse) along the most recent search direction
Packit ea1746
    // respectively.  Thus \gamma is an approximate eigenvalue of the true
Packit ea1746
    // inverse Hessian, and choosing: H_0 = I * \gamma will yield a starting
Packit ea1746
    // point that has a similar scale to the true inverse Hessian.  This
Packit ea1746
    // technique is widely reported to often improve convergence, however this
Packit ea1746
    // is not universally true, particularly if there are errors in the initial
Packit ea1746
    // jacobians, or if there are significant differences in the sensitivity
Packit ea1746
    // of the problem to the parameters (i.e. the range of the magnitudes of
Packit ea1746
    // the components of the gradient is large).
Packit ea1746
    //
Packit ea1746
    // The original origin of this rescaling trick is somewhat unclear, the
Packit ea1746
    // earliest reference appears to be Oren [1], however it is widely discussed
Packit ea1746
    // without specific attributation in various texts including [2] (p143/178).
Packit ea1746
    //
Packit ea1746
    // [1] Oren S.S., Self-scaling variable metric (SSVM) algorithms Part II:
Packit ea1746
    //     Implementation and experiments, Management Science,
Packit ea1746
    //     20(5), 863-874, 1974.
Packit ea1746
    // [2] Nocedal J., Wright S., Numerical Optimization, Springer, 1999.
Packit ea1746
    search_direction *= approximate_eigenvalue_scale_;
Packit ea1746
Packit ea1746
    VLOG(4) << "Applying approximate_eigenvalue_scale: "
Packit ea1746
            << approximate_eigenvalue_scale_ << " to initial inverse Hessian "
Packit ea1746
            << "approximation.";
Packit ea1746
  }
Packit ea1746
Packit ea1746
  for (list<int>::const_iterator it = indices_.begin();
Packit ea1746
       it != indices_.end();
Packit ea1746
       ++it) {
Packit ea1746
    const double beta = delta_gradient_history_.col(*it).dot(search_direction) /
Packit ea1746
        delta_x_dot_delta_gradient_(*it);
Packit ea1746
    search_direction += delta_x_history_.col(*it) * (alpha(*it) - beta);
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres