-
Notifications
You must be signed in to change notification settings - Fork 84
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
Feature/quadratic solve #433
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6f8ad36
Add `solve` function for quadratic problems
michalk8 b94ccb8
Update docs
michalk8 0aae7ce
Merge branch 'main' into feature/quadratic-solve
michalk8 999899a
Fix typo in notebook
michalk8 eb1ba6f
Fix implicit diff passing in LR Sinkhorn
michalk8 aa228b2
Fix typo
michalk8 62e0cf2
Clean `GWLoss` docs
michalk8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,8 @@ | |
# limitations under the License. | ||
from typing import Callable, NamedTuple | ||
|
||
import jax | ||
import jax.numpy as jnp | ||
import jax.scipy as jsp | ||
|
||
__all__ = ["make_square_loss", "make_kl_loss"] | ||
|
||
|
@@ -24,23 +24,54 @@ class Loss(NamedTuple): # noqa: D101 | |
is_linear: bool | ||
|
||
|
||
class GWLoss(NamedTuple): # noqa: D101 | ||
class GWLoss(NamedTuple): | ||
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. thanks |
||
r"""Efficient decomposition of the Gromov-Wasserstein loss function. | ||
|
||
The loss function :math:`L` is assumed to match the form given in eq. 5. of | ||
:cite:`peyre:16`: | ||
|
||
.. math:: | ||
L(x, y) = f_1(x) + f_2(y) - h_1(x) h_2(y) | ||
|
||
Args: | ||
f1: First linear term. | ||
f2: Second linear term. | ||
h1: First quadratic term. | ||
h2: Second quadratic term. | ||
""" | ||
f1: Loss | ||
f2: Loss | ||
h1: Loss | ||
h2: Loss | ||
|
||
|
||
def make_square_loss() -> GWLoss: # noqa: D103 | ||
def make_square_loss() -> GWLoss: | ||
"""Squared Euclidean loss for Gromov-Wasserstein. | ||
|
||
See Prop. 1 and Remark 1 of :cite:`peyre:16` for more information. | ||
|
||
Returns: | ||
The squared Euclidean loss. | ||
""" | ||
f1 = Loss(lambda x: x ** 2, is_linear=False) | ||
f2 = Loss(lambda y: y ** 2, is_linear=False) | ||
h1 = Loss(lambda x: x, is_linear=True) | ||
h2 = Loss(lambda y: 2.0 * y, is_linear=True) | ||
return GWLoss(f1, f2, h1, h2) | ||
|
||
|
||
def make_kl_loss(clipping_value: float = 1e-8) -> GWLoss: # noqa: D103 | ||
f1 = Loss(lambda x: -jax.scipy.special.entr(x) - x, is_linear=False) | ||
def make_kl_loss(clipping_value: float = 1e-8) -> GWLoss: | ||
r"""Kullback-Leibler loss for Gromov-Wasserstein. | ||
|
||
See Prop. 1 and Remark 1 of :cite:`peyre:16` for more information. | ||
|
||
Args: | ||
clipping_value: Value used to avoid :math:`\log(0)`. | ||
|
||
Returns: | ||
The KL loss. | ||
""" | ||
f1 = Loss(lambda x: -jsp.special.entr(x) - x, is_linear=False) | ||
f2 = Loss(lambda y: y, is_linear=True) | ||
h1 = Loss(lambda x: x, is_linear=True) | ||
h2 = Loss(lambda y: jnp.log(jnp.clip(y, clipping_value)), is_linear=False) | ||
|
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.
thanks for the catch!