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

Update second_order package internal structure. #625

Merged
merged 1 commit into from
Nov 10, 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
6 changes: 3 additions & 3 deletions optax/second_order/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
# ==============================================================================
"""The second order optimisation sub-package."""

from optax.second_order.fisher import fisher_diag
from optax.second_order.hessian import hessian_diag
from optax.second_order.hessian import hvp
from optax.second_order._fisher import fisher_diag
from optax.second_order._hessian import hessian_diag
from optax.second_order._hessian import hvp
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
from jax import flatten_util
import jax.numpy as jnp

from optax.second_order import base
from optax.second_order import _base


def _ravel(p: Any) -> jax.Array:
return flatten_util.ravel_pytree(p)[0]


def fisher_diag(
negative_log_likelihood: base.LossFn,
negative_log_likelihood: _base.LossFn,
params: Any,
inputs: jax.Array,
targets: jax.Array,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
from jax import flatten_util
import jax.numpy as jnp

from optax.second_order import base
from optax.second_order import _base


def _ravel(p: Any) -> jax.Array:
return flatten_util.ravel_pytree(p)[0]


def hvp(
loss: base.LossFn,
loss: _base.LossFn,
v: jax.Array,
params: Any,
inputs: jax.Array,
Expand All @@ -58,7 +58,7 @@ def hvp(


def hessian_diag(
loss: base.LossFn,
loss: _base.LossFn,
params: Any,
inputs: jax.Array,
targets: jax.Array,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import jax.numpy as jnp
import numpy as np

from optax import second_order
from optax.second_order import _hessian


NUM_CLASSES = 2
Expand Down Expand Up @@ -77,7 +77,7 @@ def jax_hessian_diag(loss_fun, params, inputs, targets):
@chex.all_variants
def test_hessian_diag(self):
hessian_diag_fn = self.variant(
functools.partial(second_order.hessian_diag, self.loss_fn))
functools.partial(_hessian.hessian_diag, self.loss_fn))
actual = hessian_diag_fn(self.parameters, self.data, self.labels)
np.testing.assert_array_almost_equal(self.hessian, actual, 5)

Expand Down
Loading