gradient_checker.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. // Copyright 2023 Google Inc. All Rights Reserved.
  29. //
  30. // Authors: wjr@google.com (William Rucklidge),
  31. // keir@google.com (Keir Mierle),
  32. // dgossow@google.com (David Gossow)
  33. #ifndef CERES_PUBLIC_GRADIENT_CHECKER_H_
  34. #define CERES_PUBLIC_GRADIENT_CHECKER_H_
  35. #include <memory>
  36. #include <string>
  37. #include <vector>
  38. #include "ceres/cost_function.h"
  39. #include "ceres/dynamic_numeric_diff_cost_function.h"
  40. #include "ceres/internal/disable_warnings.h"
  41. #include "ceres/internal/eigen.h"
  42. #include "ceres/internal/export.h"
  43. #include "ceres/internal/fixed_array.h"
  44. #include "ceres/manifold.h"
  45. #include "glog/logging.h"
  46. namespace ceres {
  47. // GradientChecker compares the Jacobians returned by a cost function against
  48. // derivatives estimated using finite differencing.
  49. //
  50. // The condition enforced is that
  51. //
  52. // (J_actual(i, j) - J_numeric(i, j))
  53. // ------------------------------------ < relative_precision
  54. // max(J_actual(i, j), J_numeric(i, j))
  55. //
  56. // where J_actual(i, j) is the Jacobian as computed by the supplied cost
  57. // function (by the user) multiplied by the manifold Jacobian and J_numeric is
  58. // the Jacobian as computed by finite differences, multiplied by the manifold
  59. // Jacobian as well.
  60. //
  61. // How to use: Fill in an array of pointers to parameter blocks for your
  62. // CostFunction, and then call Probe(). Check that the return value is 'true'.
  63. class CERES_EXPORT GradientChecker {
  64. public:
  65. // This will not take ownership of the cost function or manifolds.
  66. //
  67. // function: The cost function to probe.
  68. //
  69. // manifolds: A vector of manifolds for each parameter. May be nullptr or
  70. // contain nullptrs to indicate that the respective parameter blocks are
  71. // Euclidean.
  72. //
  73. // options: Options to use for numerical differentiation.
  74. GradientChecker(const CostFunction* function,
  75. const std::vector<const Manifold*>* manifolds,
  76. const NumericDiffOptions& options);
  77. // Contains results from a call to Probe for later inspection.
  78. struct CERES_EXPORT ProbeResults {
  79. // The return value of the cost function.
  80. bool return_value;
  81. // Computed residual vector.
  82. Vector residuals;
  83. // The sizes of the Jacobians below are dictated by the cost function's
  84. // parameter block size and residual block sizes. If a parameter block has a
  85. // manifold associated with it, the size of the "local" Jacobian will be
  86. // determined by the dimension of the manifold (which is the same as the
  87. // dimension of the tangent space) and residual block size, otherwise it
  88. // will be identical to the regular Jacobian.
  89. // Derivatives as computed by the cost function.
  90. std::vector<Matrix> jacobians;
  91. // Derivatives as computed by the cost function in local space.
  92. std::vector<Matrix> local_jacobians;
  93. // Derivatives as computed by numerical differentiation in local space.
  94. std::vector<Matrix> numeric_jacobians;
  95. // Derivatives as computed by numerical differentiation in local space.
  96. std::vector<Matrix> local_numeric_jacobians;
  97. // Contains the maximum relative error found in the local Jacobians.
  98. double maximum_relative_error;
  99. // If an error was detected, this will contain a detailed description of
  100. // that error.
  101. std::string error_log;
  102. };
  103. // Call the cost function, compute alternative Jacobians using finite
  104. // differencing and compare results. If manifolds are given, the Jacobians
  105. // will be multiplied by the manifold Jacobians before performing the check,
  106. // which effectively means that all errors along the null space of the
  107. // manifold will be ignored. Returns false if the Jacobians don't match, the
  108. // cost function return false, or if a cost function returns a different
  109. // residual when called with a Jacobian output argument vs. calling it
  110. // without. Otherwise returns true.
  111. //
  112. // parameters: The parameter values at which to probe.
  113. // relative_precision: A threshold for the relative difference between the
  114. // Jacobians. If the Jacobians differ by more than this amount, then the
  115. // probe fails.
  116. // results: On return, the Jacobians (and other information) will be stored
  117. // here. May be nullptr.
  118. //
  119. // Returns true if no problems are detected and the difference between the
  120. // Jacobians is less than error_tolerance.
  121. bool Probe(double const* const* parameters,
  122. double relative_precision,
  123. ProbeResults* results) const;
  124. private:
  125. GradientChecker() = delete;
  126. GradientChecker(const GradientChecker&) = delete;
  127. void operator=(const GradientChecker&) = delete;
  128. std::vector<const Manifold*> manifolds_;
  129. const CostFunction* function_;
  130. std::unique_ptr<CostFunction> finite_diff_cost_function_;
  131. };
  132. } // namespace ceres
  133. #include "ceres/internal/reenable_warnings.h"
  134. #endif // CERES_PUBLIC_GRADIENT_CHECKER_H_