Reusing LossFunction instances for AddResidualBlock()

26 views
Skip to first unread message

raf...@dotproduct3d.com

unread,
Jul 27, 2024, 5:47:46 AM7/27/24
to Ceres Solver
Quick question:
Is it safe to instantiate one LossFunction and reuse it, where needed, across all my calls to AddResidualBlock()  --  as opposed to instantiating a new (identical) LossFunction object for every call to AddResidualBlock() as done in the examples?

Sergey Sharybin

unread,
Jul 27, 2024, 6:32:04 AM7/27/24
to Ceres Solver
Hi,

By default the Problem object takes ownership of the cost and loss functions, and uses user-counter approach to allow multiple residual blocks to re-use the same cost/loss function. So if you plan to reuse loss functions within the same Problem you should be able to just do it.

If you plan to use loss function across multiple Problem objects, then you'd need to set Problem::Options::loss_function_ownership to DO_NOT_TAKE_OWNERSHIP, and take care of destructing the loss function when no Problem objects refer to them.

raf...@dotproduct3d.com

unread,
Jul 27, 2024, 7:14:54 AM7/27/24
to Ceres Solver
Thanks!

Sameer Agarwal

unread,
Jul 27, 2024, 11:54:36 AM7/27/24
to ceres-...@googlegroups.com
Yes.

On Sat, Jul 27, 2024, 2:47 AM raf...@dotproduct3d.com <raf...@dotproduct3d.com> wrote:
Quick question:
Is it safe to instantiate one LossFunction and reuse it, where needed, across all my calls to AddResidualBlock()  --  as opposed to instantiating a new (identical) LossFunction object for every call to AddResidualBlock() as done in the examples?

--
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/297bf82f-1e3f-46b8-82ab-f22669903d54n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages