该类将代价函数返回的雅各比与使用有限差分估算的导数进行比较。它是用于单元测试的工具,比求解器选项中的 check_gradients 选项提供更精细的控制。强制执行的条件是
// my_cost_function takes two parameter blocks. The first has a
// manifold associated with it.
CostFunction* my_cost_function = ...
Manifold* my_manifold = ...
NumericDiffOptions numeric_diff_options;
std::vector<Manifold*> manifolds;
manifolds.push_back(my_manifold);
manifolds.push_back(nullptr);
std::vector parameter1;
std::vector parameter2;
// Fill parameter 1 & 2 with test data...
std::vector<double*> parameter_blocks;
parameter_blocks.push_back(parameter1.data());
parameter_blocks.push_back(parameter2.data());
GradientChecker gradient_checker(my_cost_function,
manifolds,
numeric_diff_options);
GradientCheckResults results;
if (!gradient_checker.Probe(parameter_blocks.data(), 1e-9, &results) {
LOG(ERROR) << "An error has occurred:\n" << results.error_log;
}