-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: An Thai Le <[email protected]>
- Loading branch information
Showing
2 changed files
with
76 additions
and
120 deletions.
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import abc | ||
import functools | ||
import math | ||
from typing import Any, Callable, Dict, Optional, Tuple, Union | ||
|
||
import jax | ||
import jax.numpy as jnp | ||
import jaxopt | ||
import numpy as np | ||
|
||
from ott.math import fixed_point_loop, matrix_square_root | ||
from ott.math import utils as mu | ||
|
||
|
||
|
||
@jax.tree_util.register_pytree_node_class | ||
class ObjectiveFn(abc.ABC): | ||
"""Base class for all costs. | ||
""" | ||
|
||
@abc.abstractmethod | ||
def evaluate(self, x: jnp.ndarray) -> jnp.ndarray: | ||
"""Compute cost between :math:`x` and :math:`y`. | ||
Args: | ||
x: Array. | ||
Returns: | ||
The cost array. | ||
""" | ||
|
||
def __call__(self, x: jnp.ndarray, y: jnp.ndarray) -> float: | ||
"""Compute cost between :math:`x` and :math:`y`. | ||
Args: | ||
x: Array. | ||
y: Array. | ||
Returns: | ||
The cost, optionally including the :attr:`norms <norm>` of | ||
:math:`x`/:math:`y`. | ||
""" | ||
cost = self.evaluate(x) | ||
return cost | ||
|
||
def tree_flatten(self): # noqa: D102 | ||
return (), None | ||
|
||
@classmethod | ||
def tree_unflatten(cls, aux_data, children): # noqa: D102 | ||
del aux_data | ||
return cls(*children) |
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