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

Add Nesterov momentum to AdaBelief optimizer. #1127

Merged
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
21 changes: 19 additions & 2 deletions optax/_src/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def adabelief(
b1: float = 0.9,
b2: float = 0.999,
eps: float = 1e-16,
eps_root: float = 1e-16) -> base.GradientTransformation:
eps_root: float = 1e-16,
*,
nesterov: bool = False,
) -> base.GradientTransformation:
r"""The AdaBelief optimizer.

AdaBelief is an adaptive learning rate optimizer that focuses on fast
Expand Down Expand Up @@ -74,6 +77,13 @@ def adabelief(
S_t &\leftarrow (m_t, s_t).
\end{align*}

With the keyword argument `nesterov=True`, the optimizer uses Nesterov
momentum, replacing the above :math:`\hat{m}_t` with

.. math::
\hat{m}_t \leftarrow
\beta_1 m_t / {(1-\beta_1^{t+1})} + (1 - \beta_1) g_t / {(1-\beta_1^t)}.

Examples:
>>> import optax
>>> import jax
Expand Down Expand Up @@ -107,12 +117,19 @@ def adabelief(
eps_root: Term added to the second moment of the prediction error to
improve numerical stability. If backpropagating gradients through the
gradient transformation (e.g. for meta-learning), this must be non-zero.
nesterov: Whether to use Nesterov momentum.

Returns:
The corresponding `GradientTransformation`.
"""
return combine.chain(
transform.scale_by_belief(b1=b1, b2=b2, eps=eps, eps_root=eps_root),
transform.scale_by_belief(
b1=b1,
b2=b2,
eps=eps,
eps_root=eps_root,
nesterov=nesterov,
),
transform.scale_by_learning_rate(learning_rate),
)

Expand Down
14 changes: 12 additions & 2 deletions optax/_src/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,9 @@ def scale_by_belief(
b1: float = 0.9,
b2: float = 0.999,
eps: float = 1e-16,
eps_root: float = 1e-16
eps_root: float = 1e-16,
*,
nesterov: bool = False,
) -> base.GradientTransformation:
"""Rescale updates according to the AdaBelief algorithm.

Expand All @@ -699,6 +701,7 @@ def scale_by_belief(
eps_root: Term added to the second moment of the prediction error to
improve numerical stability. If backpropagating gradients through the
gradient transformation (e.g. for meta-learning), this must be non-zero.
nesterov: Whether to use Nesterov momentum.

Returns:
A `GradientTransformation` object.
Expand All @@ -717,7 +720,14 @@ def update_fn(updates, state, params=None):
nu = otu.tree_update_moment_per_elem_norm(prediction_error, state.nu, b2, 2)
nu = jax.tree.map(lambda v: v + eps_root, nu)
count_inc = numerics.safe_increment(state.count)
mu_hat = otu.tree_bias_correction(mu, b1, count_inc)
if nesterov:
mu_hat = jax.tree.map(
lambda m, g: b1 * m + (1 - b1) * g,
otu.tree_bias_correction(
mu, b1, numerics.safe_increment(count_inc)),
otu.tree_bias_correction(updates, b1, count_inc))
else:
mu_hat = otu.tree_bias_correction(mu, b1, count_inc)
nu_hat = otu.tree_bias_correction(nu, b2, count_inc)
updates = jax.tree.map(
lambda m, v: None if m is None else m / (jnp.sqrt(v) + eps),
Expand Down
Loading