How to find when Jacobian becomes Nan

187 views
Skip to first unread message

Yan Cui

unread,
Apr 20, 2022, 12:49:32 AM4/20/22
to Ceres Solver
Hi,

I am solving a slam problem, where I have specified 15x1 residuals like 
res = [pos, vel, rotation_vec, accel_bias, gyro_bias], 

and I am using AutoDiffCostFunction. When I solve it, I can see the initial residuals are as expected, but the solver reports an error saying there are "-nan" in Jacobian, at iteration 0. From the output message I see all elements in 3 rotation_vec columns are "-nan" values. (please see the pic below)

Any suggestions of how to debug this? Is there any tools/util functions that I can take deeper look at how these Jacobians are derived, and where they went wrong?

Thank you,
Yan

Screen Shot 2022-04-19 at 9.48.00 PM.png

William Rucklidge

unread,
Apr 20, 2022, 10:24:47 AM4/20/22
to ceres-...@googlegroups.com
Since this looks easy to reproduce, the simplest path would be to add some logging into your cost functor: just log some intermediate values, and the logged Jets will almost certainly show you which operations are generating NaN. One common source of infinities/NaNs are square root operations, which have infinite derivatives at zero.

--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/72fd032f-d6ed-4037-80d8-3251ed2d0e94n%40googlegroups.com.

Yan Cui

unread,
Apr 20, 2022, 1:42:02 PM4/20/22
to Ceres Solver
Thank you, and yeah I will log more intermediate values in my cost functor.

Yan

Sameer Agarwal

unread,
Apr 20, 2022, 1:43:56 PM4/20/22
to ceres-...@googlegroups.com
and you do not have to run the whole code, the log already tells you what parameter values you are having problems at (for example one of the parameter block being mostly zeros),
so you can just create that costfunction and call Evaluate on it with logging to see whats going on.
Sameer


Yan Cui

unread,
Apr 20, 2022, 5:21:49 PM4/20/22
to Ceres Solver
Yeah I have figured this out. The problem is that I have an angle axis to quaternion conversion function, where I implement like this

double angle = rotvec.norm();
Vector3d vec = rotvec.normalized();
return Quaterniond(Eigen::AngleAxisd(angle, vec));

But this does not consider the case of infinite derivatives at origin, and that is why all rotation related jacobians are nan. Now I have switched to use ceres::AngleAxisToQuaternion() to do the conversion, and the solver works well. 

Thank you all for the help.

Yan
 

Reply all
Reply to author
Forward
0 new messages