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

Return parameters with lowest loss from LM optimizer #54

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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
10 changes: 7 additions & 3 deletions descent/optim/_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def levenberg_marquardt(
whether the step was accepted or rejected.

Returns:
The optimized parameters.
The parameters that minimize the loss.
"""

x = x.clone().detach().requires_grad_(x.requires_grad)
Expand All @@ -526,9 +526,10 @@ def levenberg_marquardt(
trust_radius = torch.tensor(config.trust_radius).to(x.device)

loss_history = []

has_converged = False

best_x, best_loss = x.clone(), closure_prev[0]

for step in range(config.max_steps):
loss_prev, gradient_prev, hessian_prev = closure_prev

Expand Down Expand Up @@ -574,6 +575,9 @@ def levenberg_marquardt(
x.data.copy_(x_next.data)
loss_history.append(loss.detach().cpu().clone())

if loss < best_loss:
best_x, best_loss = x.clone(), loss.detach().clone()

closure_prev = (loss, gradient, hessian)

report_fn(step, x, loss, gradient, hessian, step_quality, accept_step)
Expand All @@ -588,4 +592,4 @@ def levenberg_marquardt(
if not has_converged:
_LOGGER.info(f"optimization has not converged after {config.max_steps} steps.")

return x
return best_x
10 changes: 8 additions & 2 deletions descent/tests/optim/test_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ def test_levenberg_marquardt_adaptive(mocker, caplog):
torch.tensor([19.0, 20.0]),
torch.tensor([[21.0, 22.0], [23.0, 24.0]]),
),
(
torch.tensor(-2.11),
torch.tensor([19.0, 20.0]),
torch.tensor([[21.0, 22.0], [23.0, 24.0]]),
),
]
expected_loss_traj = [
(
Expand Down Expand Up @@ -264,9 +269,10 @@ def mock_loss_fn(_x, *_):
# previous step should have been rejected
torch.tensor([0.1, 0.2]),
torch.tensor([0.15, 0.21]),
torch.tensor([0.2, 0.3]),
]
assert x_new.shape == expected_x_traj[-1].shape
assert torch.allclose(x_new, expected_x_traj[-1])
assert x_new.shape == expected_x_traj[-2].shape # -2 has lowest loss.
assert torch.allclose(x_new, expected_x_traj[-2])

trust_radius_messages = [m for m in caplog.messages if "trust radius" in m]

Expand Down