当前位置:网站首页>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 .
边栏推荐
- Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法
- Nc78 reverse linked list
- There was an error while marking a file for deletion
- Taishan Office Technology Lecture: conversion relations of inch, centimeter, pound, pika, Ti, line, word line and pixel
- C盘空间不够 mklink解决VScode扩展迁移到其他盘
- Tensor to img & imge to tensor (tensor conversion of pytorch)
- [web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
- Stm32f105rbt6 internal flash debugging
- Yes, UDP protocol can also be used to request DNS server
- Analysis of regression problem, modeling and prediction
猜你喜欢

Kendryte K210 在freertos上的lcd屏幕的使用

Circulaindicator component, which makes the indicator style more diversified

Interview shock: why does TCP need three handshakes?

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现

Stm8s003f3 internal flash debugging

Construction of Huffman tree

泛域名配置方法

Could not stop Cortex-M device! Please check the JTAG cable solution

工程师必看的示波器探头安全使用说明书

【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
随机推荐
Chapter 5 Basic Scripting: Shell Variables
SQL optimizer parsing | youth training camp notes
408第二章线性表
You can change this value on the server by setting the 'Max_ allowed_ Packet 'variable error
[HAOI2015]树上操作
7. Dependency injection
[QNX Hypervisor 2.2用户手册]9.4 dryrun
Is it true that CITIC Securities' low commission account opening is free of 5? Is it safe
JZ32 从上往下打印二叉树
【华为机试真题】字符串匹配
c语言---25 扫雷游戏
408 Chapter 2 linear table
Uniapp scroll bar topping effect, customized page scroll bar position (sorting)
浅析回归问题、建模、预测
OV7725 yuv 640* [email protected] Profile
What are the methods of traversing arrays? What is the difference between the performance of the for loop foreach for/in for/of map and how to choose?
php内存管理机制与垃圾回收机制
Cve-2022-33891 Apache spark shell command injection vulnerability recurrence
Tkinter GUI address book management system
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%