-
Notifications
You must be signed in to change notification settings - Fork 194
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
Fix complex support for L-BFGS #1142
Open
gautierronan
wants to merge
10
commits into
google-deepmind:main
Choose a base branch
from
gautierronan:complex-lbfgs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
413f2c6
Fix complex support for L-BFGS
gautierronan d4e1d6f
Introduce `otu.tree_real` instead of fixing `otu.tree_vdot`
gautierronan 02b5968
Additional fixes for lbfgs vdots
gautierronan b2ccb35
Add tests for tree_math
gautierronan cf9de95
Add test for complex-valued LBFGS
gautierronan 3917b63
Fix CI formatting
gautierronan c9e85b2
Fix tests
gautierronan 0d22fc0
Add test parametrization over linesearch
gautierronan efcad36
Fix linting for CI
gautierronan 5d1e6bc
Fix remaining tests in CI
gautierronan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
import numpy as np | ||
from optax._src import alias | ||
from optax._src import base | ||
from optax._src import linesearch as _linesearch | ||
from optax._src import numerics | ||
from optax._src import transform | ||
from optax._src import update | ||
|
@@ -333,6 +334,7 @@ def stopping_criterion(carry): | |
def step(carry): | ||
params, state, count, _ = carry | ||
value, grad = value_and_grad_fun(params) | ||
grad = otu.tree_conj(grad) | ||
updates, state = opt.update( | ||
grad, state, params, value=value, grad=grad, value_fn=fun | ||
) | ||
|
@@ -865,6 +867,49 @@ def fun(x): | |
sol, _ = _run_opt(opt, fun, init_params=jnp.ones(n), tol=tol) | ||
chex.assert_trees_all_close(sol, jnp.zeros(n), atol=tol, rtol=tol) | ||
|
||
@parameterized.product( | ||
linesearch=[ | ||
# _linesearch.zoom_linesearch(max_linesearch_steps=20), | ||
_linesearch.scale_by_backtracking_linesearch(max_backtracking_steps=20), | ||
_linesearch.scale_by_zoom_linesearch( | ||
max_linesearch_steps=20, initial_guess_strategy='one' | ||
) | ||
], | ||
) | ||
def test_complex(self, linesearch): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name it |
||
# Test that optimization over complex variable matches equivalent real case | ||
|
||
tol = 1e-5 | ||
mat = jnp.array( | ||
[[1, - 2], | ||
[3, 4], | ||
[-4 + 2j, 5 - 3j], | ||
[-2 - 2j, 6]] | ||
) | ||
|
||
def to_real(z): | ||
return jnp.stack((z.real, z.imag)) | ||
|
||
def to_complex(x): | ||
return x[..., 0, :] + 1j * x[..., 1, :] | ||
|
||
def f_complex(z): | ||
return jnp.sum(jnp.abs(mat @ z) ** 1.5) | ||
|
||
def f_real(x): | ||
return f_complex(to_complex(x)) | ||
|
||
z0 = jnp.array([1 - 1j, 0 + 1j]) | ||
x0 = to_real(z0) | ||
|
||
opt_complex = alias.lbfgs(linesearch=linesearch) | ||
opt_real = alias.lbfgs(linesearch=linesearch) | ||
sol_complex, _ = _run_opt(opt_complex, f_complex, init_params=z0, tol=tol) | ||
sol_real, _ = _run_opt(opt_real, f_real, init_params=x0, tol=tol) | ||
|
||
chex.assert_trees_all_close( | ||
sol_complex, to_complex(sol_real), atol=tol, rtol=tol | ||
) | ||
|
||
if __name__ == '__main__': | ||
absltest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove that commented line. There is no need to test for any possible variant of the linesearch. It's already great that you included the backtracking linesearch in addition to the default linesearch.