当前位置:网站首页>Ceres analytic derivation
Ceres analytic derivation
2022-07-25 18:27:00 【Zhan Miao】
1. Analytical derivative
In some cases , Don't use AutoDiffCostFunction, For example, we calculate the derivative in an approximate way , Rather than using AutoDiff The chain rule of , We need our own residuals and Jacobin Calculation . Now we define a CostFunction perhaps SizedCostFunction Subclasses of .
#include <vector>
#include "ceres/ceres.h"
#include "glog/logging.h"
using ceres::CostFunction;
using ceres::SizedCostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;
// A CostFunction implementing analytically derivatives for the
// function f(x) = 10 - x.
class QuadraticCostFunction :
public SizedCostFunction<1 /* number of residuals */,
1 /* size of first parameter */> {
public:
virtual ~QuadraticCostFunction() {}
virtual bool Evaluate(double const* const* parameters,
double* residuals,
double** jacobians) const {
double x = parameters[0][0];
// f(x) = 10 - x.
residuals[0] = 10 - x;
if (jacobians != NULL && jacobians[0] != NULL) {
jacobians[0][0] = -1;
}
return true;
}
};
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
// Initialize the variable to be optimized
double x = 0.5;
const double initial_x = x;
// Construction issues
Problem problem;
// Set residual function
CostFunction* cost_function = new QuadraticCostFunction;
problem.AddResidualBlock(cost_function, NULL, &x);
// Build solver
Solver::Options options;
// Output to cout
options.minimizer_progress_to_stdout = true;
// Optimize information
Solver::Summary summary;
// Start calculating
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x
<< " -> " << x << "\n";
return 0;
}The jacobians array is optional, Evaluate is expected to check when it is non-null, and if it is the case then fill it with the values of the derivative of the residual function.
QuadraticCostFunction::Evaluateparameters As input ,residuals Output as residual and Jacobians As the output of Jacobian matrix .jacobians It's optional ,Evaluate is expected to check when it is non-null, and if it is the case then fill it with the values of the derivative of the residual function. In this case , The residual function is linear , so Jacobian Is a constant .
Unless there is a good management Jacobian The reason of , Otherwise, it is recommended to use AutoDiffCostFunction or NumericDiffCostFunction.
2. CMakeLists.txt file
cmake_minimum_required(VERSION 2.8)
project(ceres)
find_package(Ceres REQUIRED)
include_directories(${CERES_INCLUDE_DIRS})
add_executable(use_ceres main.cpp)
target_link_libraries(use_ceres ${CERES_LIBRARIES})More information about derivatives
up to now , The derivative is calculated using Ceres The most complex part of , And according to the situation , Users may need more complex methods of calculating derivatives . This section only describes how to provide derivatives to Ceres.
When you are familiar with NumericDiffCostFunction and AutoDiffCostFunction After the use of , It is suggested to consider DynamicAutoDiffCostFunction, CostFunctionToFunctor,NumericDiffFunctor as well as ConditionedCostFunction More advanced methods to build and calculate cost functions .
边栏推荐
- Chapter 5 Basic Scripting: Shell Variables
- Linux启动mysql报错
- How developers choose the right database for react native
- MySQL 索引优化全攻略
- Software testing -- common testing tools
- Insufficient space on Disk C mklink solves vscode expansion migration to other disks
- Use of join function in MATLAB
- The use of invocationcount, threadpoolsize, timeout of concurrent tests in TestNG
- NC78 反转链表
- 408第二章线性表
猜你喜欢

How to create an effective help document?

Circulaindicator component, which makes the indicator style more diversified

Cve-2022-33891 Apache spark shell command injection vulnerability recurrence

Interview shock: why does TCP need three handshakes?

网易严选库存中心设计实践

Connect to MySQL using sqldeveloper

Flexible current probe selection guide

乐观锁解析

How to build an enterprise level OLAP data engine for massive data and high real-time requirements?

You can change this value on the server by setting the 'Max_ allowed_ Packet 'variable error
随机推荐
Detailed explanation of super full mavan label
Today's sleep quality record is 84 points
pd.melt() vs reshape2::melt()
Could not stop Cortex-M device! please check the JTAG cable的解决办法
BiSeNet v1
11.2-hj86 find the maximum number of continuous bits
List转换问题
Imx6 rtl8189ftv migration
Number two 2010 real test site
Nc68 jumping steps
testng执行顺序的3中控制方法
Sequential storage structure, chain storage structure and implementation of stack
You can change this value on the server by setting the 'Max_ allowed_ Packet 'variable error
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
Analysis of regression problem, modeling and prediction
Repair process of bad blocks of primary standby database
C language libcurl cross compilation
Thales launches solutions to help SAP customers control cloud data
Optimistic lock resolution
MySQL optimistic lock