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 Lion optimizer #610

Merged
merged 6 commits into from
Jul 27, 2023
Merged

Add Lion optimizer #610

merged 6 commits into from
Jul 27, 2023

Conversation

james77777778
Copy link
Contributor

@james77777778 james77777778 commented Jul 26, 2023

Related to keras-team/keras#18442

EDITED: update learning_rate=0.001

The golden in test_correctness_with_golden is generated by tf.keras.optimizers.Lion(learning_rate=0.001) with following script:

import numpy as np
import tensorflow as tf

optimizer = tf.keras.optimizers.Lion(learning_rate=0.001)

x = tf.Variable(np.ones([10]))
grads = tf.constant(np.arange(0.1, 1.1, 0.1))
first_grads = tf.constant(np.full((10,), 0.01))

optimizer.apply_gradients(zip([first_grads], [x]))
for _ in range(5):
    print(x.numpy())
    optimizer.apply_gradients(zip([grads], [x]))
# outputs
[0.999 0.999 0.999 0.999 0.999 0.999 0.999 0.999 0.999 0.999]
[0.998 0.998 0.998 0.998 0.998 0.998 0.998 0.998 0.998 0.998]
[0.997 0.997 0.997 0.997 0.997 0.997 0.997 0.997 0.997 0.997]
[0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996 0.996]
[0.995 0.995 0.995 0.995 0.995 0.995 0.995 0.995 0.995 0.995]

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! Looking great!

For performance reasons, we reimplement our optimizers for torch using torch C++ ops called from Python -- here's an example of such a PR: https://github.com/keras-team/keras-core/pull/534/files

Would you be able to include the torch version of the optimizer in this PR?


def __init__(
self,
learning_rate=0.0001,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this -- the LR here is 1e-4, but it should be 1e-3 like in the other optimizers. This was already an issue in tf.keras, we should fix it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

self.beta_2 = beta_2
if beta_1 <= 0 or beta_1 > 1:
raise ValueError(
"Argument `beta_1` must be between [0, 1]. Otherwise, the "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either "between 0 and 1" or "in the [0, 1] range"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@james77777778
Copy link
Contributor Author

For performance reasons, we reimplement our optimizers for torch using torch C++ ops called from Python -- here's an example of such a PR: https://github.com/keras-team/keras-core/pull/534/files

Would you be able to include the torch version of the optimizer in this PR?

I didn't notice that there is a separate folder for torch's optimizers.
torch_lion.py has been added.

As torch lacks _foreach_sign_ operator, I implemented it by c_t = [c.sign() for c in c_t]
Ref: https://github.com/pytorch/pytorch/blob/6847c965f5a05d5631357e6af4cf759231770b44/torch/optim/rprop.py#L300

Copy link
Member

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work! 👍
LGTM

@fchollet fchollet merged commit 6ebb868 into keras-team:main Jul 27, 2023
6 checks passed
@james77777778 james77777778 deleted the add-lion branch July 27, 2023 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants