n パラメータの関数を最適化するために Ceres を使用したいと考えています。明確に定義されたコストはありますが、この関数の勾配を見つける方法は不明です。今まで数値微分でGSLを使っていましたが、自動微分でCeresを使ってみようと思いました。
AutoDiff を使用して関数 f(x) = 0.5 (10 - x)^2 を最小化するおもちゃの例helloworld_analytic_diff.ccを見て、チュートリアルを読んだので、これを2 次元関数 f(x,y) = (10-x)^2 +(20- y)^2、x、y = 10、20 で全体的な最小値を持ちます。
#include "ceres/ceres.h"
#include "glog/logging.h"
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;
struct CostFunctor {
template <typename T> bool operator()(const T* const x, T* residual) const {
const T x1 = x[0];
const T y1 = x[1];
residual[0] = (10.0-x[0]) + (20.0-x[1]);
return true;
}
};
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
double x[2] = {0.5, -3.0};
const double initial_x[2] = {0.5, -3.0};
Problem problem;
CostFunction* cost_function = new AutoDiffCostFunction<CostFunctor, 1, 2>(new CostFunctor);
problem.AddResidualBlock(cost_function, NULL, &x[0]);
// Run the solver!
Solver::Options options;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x[0] << ", " << initial_x[0]
<< " -> " << x[0] << ", " << x[1]<< "\n";
return 0;
}
ただし、これを実行すると、最初の推測に応じて、間違ったものに収束してしまいます。
Ceres Solver Report: Iterations: 3, Initial cost: 5.281250e+02, Final cost: 3.667046e-16, Termination: CONVERGENCE
x : 0.5, 0.5 -> 16.75, 13.25
私がここで間違ったことについてのアイデアはありますか?どうもありがとう!