gpt4 book ai didi

c++ - 修改平方和 [ceres-solver]

转载 作者:行者123 更新时间:2023-11-30 04:01:47 24 4
gpt4 key购买 nike

我正在尝试修改 ceres 的默认行为,即计算残差的平方和作为成本函数。我希望它只计算总和(残差已经以只能为正的方式计算)

根据文档,我应该使用 ConditionedCostFunction

这是我所做的:我定义了带有 1 个残差和 1 个参数的调节器

struct Conditioners : ceres::CostFunction
{
public:
Conditioners()
{
set_num_residuals(1);
mutable_parameter_block_sizes()->push_back(1);
}

~Conditioners()
{}

template<typename T>
T operator() (T x)
{
return T(x * x);
}

bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const
{
return true;
}
};

我把调节器放在一个 vector 中

std::vector<ceres::CostFunction*> conditioners;

for(int i = 0; i < 1; i++)
conditioners.push_back(new Conditioners());

ceres::ConditionedCostFunction* ccf =
new ceres::ConditionedCostFunction(cost_function, conditioners, ceres::TAKE_OWNERSHIP);

problem.AddResidualBlock(ccf, NULL, &x);

它编译和一切。但这并不能解决问题。它甚至没有开始。它说:

Ceres Solver Report: Iterations: 0, Initial cost: 4.512500e+01, Final cost: 4.512500e+01, Termination: CONVERGENCE
x : 0.5 -> 0.5

代替:

iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
0 4.512500e+01 0.00e+00 9.50e+00 0.00e+00 0.00e+00 1.00e+04 0 2.99e-04 1.04e-03
1 4.511598e-07 4.51e+01 9.50e-04 9.50e+00 1.00e+00 3.00e+04 1 3.84e-04 9.72e-03
2 5.012552e-16 4.51e-07 3.17e-08 9.50e-04 1.00e+00 9.00e+04 1 2.98e-05 9.92e-03
Ceres Solver Report: Iterations: 2, Initial cost: 4.512500e+01, Final cost: 5.012552e-16, Termination: CONVERGENCE
x : 0.5 -> 10

(如果你想自己试试,这个例子修改了helloword example)您对出了什么问题有任何指示吗? (ceres 报告没有更具体)

最佳答案

我找到了解决方案,即:

struct Conditioners : ceres::CostFunction
{
public:
Conditioners()
{
set_num_residuals(1);
mutable_parameter_block_sizes()->push_back(1);
}

~Conditioners()
{}

template<typename T>
T operator() (T x)
{
return T(x * x);
}

bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const
{
residuals[0] = parameters[0][0] * parameters[0][0]

if (jacobians)
jacobians[0][0] = 2.0 * parameters[0][0]

return true;
}
};

我的错误是认为我只需要重新实现 () 运算符,ceres 就会自动找到 jacobian。它没有。

关于c++ - 修改平方和 [ceres-solver],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25574010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com