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

Added Adam Optimizer #28

Merged
merged 4 commits into from
Apr 14, 2023

Conversation

jrego43
Copy link
Collaborator

@jrego43 jrego43 commented Apr 10, 2023

Added Adam struct that implements the Optimizer trait, closely following its PyTorch implementation. It essentially combines the two types of momentums found in SGD and RMSProp (with respective decay rates β1 and β2) by keeping track of several gradient-based averages.

Source: https://pytorch.org/docs/stable/generated/torch.optim.Adam.html#torch.optim.Adam

Copy link
Owner

@LazaroHurtado LazaroHurtado left a comment

Choose a reason for hiding this comment

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

LGTM, just a small question about an import

grad += param.value() * self.weight_decay;

exp_avgs[i] = (beta1 * exp_avgs[i]) + ((1. - beta1) * grad);
exp_avg_sqs[i] = (beta2 * exp_avg_sqs[i]) + ((1. - beta2) * grad.pow(2));
Copy link
Owner

Choose a reason for hiding this comment

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

I believe you had to use num_traits::Pow because of the grad.pow(2) but you can do without the import by doing grad.powf(2.0)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're absolutely right, but the reason I did it is because RMSProp does use num_traits::Pow as well. I can go ahead and make the change to both, though!

* change pow to powf

* Update adam.rs
Copy link
Owner

@LazaroHurtado LazaroHurtado left a comment

Choose a reason for hiding this comment

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

Awesome job!

@LazaroHurtado LazaroHurtado merged commit 6a648d9 into LazaroHurtado:main Apr 14, 2023
@jrego43 jrego43 deleted the feature/adam_optimizer branch April 19, 2023 23:27
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.

3 participants