Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix for the batched LM #664

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions theseus/optimizer/nonlinear/levenberg_marquardt.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,18 @@ def _check_accept(
den = (delta * (damping * delta + linearization.Atb.squeeze(2))).sum(dim=1) / 2
rho = (previous_err - err) / den
reject_indices = rho <= damping_accept
previous_damping = self._damping.clone()
self._damping = torch.where(
reject_indices,
self._damping * up_damping_ratio,
self._damping / down_damping_ratio,
)
# If some batch have already converged, the damping factor should not be updated
self._damping = torch.where(
rho.abs() < 1e-16,
previous_damping,
self._damping,
)
self._damping = self._damping.clamp(
LevenbergMarquardt._MIN_DAMPING, LevenbergMarquardt._MAX_DAMPING
)
Expand Down
12 changes: 9 additions & 3 deletions theseus/optimizer/nonlinear/nonlinear_least_squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _optimize_loop(

# For now, step size is combined with delta. If we add more sophisticated
# line search, will probably need to pass it separately, or compute inside.
err, all_rejected = self._step(
err, all_rejected, reject_indices = self._step(
delta * steps_tensor,
info.last_err,
converged_indices,
Expand All @@ -196,6 +196,8 @@ def _optimize_loop(
f"Error: {err.mean().item()}"
)
converged_indices = self._check_convergence(err, info.last_err)
if (reject_indices is not None) and reject_indices.any():
converged_indices[reject_indices] = False
info.status[
converged_indices.cpu().numpy()
] = NonlinearOptimizerStatus.CONVERGED
Expand Down Expand Up @@ -355,14 +357,18 @@ def _step(
else:
reject_indices = self._complete_step(delta, err, previous_err, **kwargs)

# If the step is converged, it should not be considered as rejected
if reject_indices is not None:
reject_indices[converged_indices] = False

if reject_indices is not None and reject_indices.all():
return previous_err, True
return previous_err, True, reject_indices

self.objective.update(tensor_dict, batch_ignore_mask=reject_indices)
if reject_indices is not None and reject_indices.any():
# Some steps were rejected so the error computed above is not accurate
err = self.objective.error_metric()
return err, False
return err, False, reject_indices

# Resets any internal state needed by the optimizer for a new optimization
# problem. Optimizer loop will pass all optimizer kwargs to this method.
Expand Down