本文整理汇总了C++中VectorX::squaredNorm方法的典型用法代码示例。如果您正苦于以下问题:C++ VectorX::squaredNorm方法的具体用法?C++ VectorX::squaredNorm怎么用?C++ VectorX::squaredNorm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类VectorX
的用法示例。
在下文中一共展示了VectorX::squaredNorm方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: cpu_lbfgs
lbfgs::status lbfgs::cpu_lbfgs(float *h_x)
{
const size_t NX = m_costFunction.getNumberOfUnknowns();
floatdouble *d_x = new floatdouble[NX];
for (size_t idx = 0; idx < NX; ++idx)
d_x[idx] = h_x[idx];
VectorX xk = VectorX::Map(d_x, NX); // x_k, current solution
VectorX gk(NX); // g_k, gradient at x_k
VectorX gkm1(NX); // g_{k-1}, gradient at x_{k-1}
VectorX z(NX); // z, search direction
floatdouble fk; // f_k, value at x_k
floatdouble fkm1; // f_{k-1}, value at x_{k-1}
floatdouble H0 = 1.0f; // H_0, initial inverse Hessian (diagonal, same value for all elements)
// treat arrays as ring buffers!
VectorX s[HISTORY_SIZE]; // s, history of solution updates
VectorX y[HISTORY_SIZE]; // y, history of gradient updates
floatdouble alpha[HISTORY_SIZE]; // alpha, history of alphas (needed for z updates)
floatdouble rho [HISTORY_SIZE]; // rho, history of rhos (needed for z updates)
for (size_t i = 0; i < HISTORY_SIZE; ++i)
{
s[i] = VectorX(NX);
y[i] = VectorX(NX);
}
cpu_cost_function *cpucf = (cpu_cost_function*)&m_costFunction;
cpucf->cpu_f_gradf(xk.data(), &fk, gk.data());
size_t evals = 1;
status stat = LBFGS_REACHED_MAX_ITER;
#ifdef LBFGS_VERBOSE
std::cout << "lbfgs::cpu_lbfgs()" << std::endl;
#endif
size_t it;
for (it = 0; it < m_maxIter; ++it)
{
#ifdef LBFGS_VERBOSE
printf("f(x) = % 12e, ||grad||_2 = % 12e\n", fk, gk.norm());
#endif
// Check for convergence
// ---------------------
floatdouble xSquaredNorm = std::max<floatdouble>(1.0f, xk.squaredNorm());
if (gk.squaredNorm() < (m_gradientEps * m_gradientEps) * xSquaredNorm)
{
stat = LBFGS_BELOW_GRADIENT_EPS;
break;
}
// Find search direction
// ---------------------
z = -gk;
const size_t MAX_IDX = std::min<size_t>(it, HISTORY_SIZE);
for (size_t i = 1; i <= MAX_IDX; ++i)
{
const size_t idx = index(it - i);
alpha[idx] = s[idx].dot(z) * rho[idx];
z -= alpha[idx] * y[idx];
}
z *= H0;
for (size_t i = MAX_IDX; i > 0; --i)
{
const size_t idx = index(it - i);
const floatdouble beta = rho[idx] * y[idx].dot(z);
z += s[idx] * (alpha[idx] - beta);
}
// Perform backtracking line search
// --------------------------------
gkm1 = gk;
fkm1 = fk;
floatdouble step;
if (!cpu_linesearch(xk, z, cpucf, fk, gk, evals, gkm1, fkm1, stat, step, m_maxEvals))
{
break;
}
// Update s, y, rho and H_0
// ------------------------
//.........这里部分代码省略.........