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

use accumulation for adam #1757

Merged
merged 14 commits into from
Oct 18, 2021
Merged

Conversation

MoritzWillmann
Copy link
Contributor

@MoritzWillmann MoritzWillmann commented Oct 15, 2021

This PR will introduce the usage of accumulation to the Adam optimizer.

Context: All other optimisers use a single class variable called accumulation to keep track of running quantities while Adam uses three: fm, sm and t. This should be changed to standardize accumulation across all optimizers.

Description of the Change: Use named tuple accumulation including the previous variables fm, sm and t.

Benefits: Standardization

Possible Drawbacks: Not Sure

Related GitHub Issues: closes #1580

@MoritzWillmann
Copy link
Contributor Author

MoritzWillmann commented Oct 15, 2021

Hey @albi3ro,

I opened this WIP PR so you can maybe have a look whether the changes are along the lines of what you suggested.
I'll start working on the property interface now

Cheers Moritz

@MoritzWillmann MoritzWillmann changed the title [WIP] use accumulation for adam use accumulation for adam Oct 15, 2021
@codecov
Copy link

codecov bot commented Oct 15, 2021

Codecov Report

Merging #1757 (9feed32) into master (8de4f0c) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1757   +/-   ##
=======================================
  Coverage   99.22%   99.22%           
=======================================
  Files         209      209           
  Lines       15617    15626    +9     
=======================================
+ Hits        15496    15505    +9     
  Misses        121      121           
Impacted Files Coverage Δ
pennylane/optimize/adam.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8de4f0c...9feed32. Read the comment docs.

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Thanks so much @MoritzWillmann 🎉 😃

I have a couple minor requests for changes, but the essence is all there.

self.t += 1

if self.accumulation is None:
accumulation = namedtuple("accumulation", "fm sm t")
Copy link
Contributor

Choose a reason for hiding this comment

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

We could probably move this definition outside the class, around Line 22 or so. That way we won't be redefining the type a bunch of times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll change that


if self.accumulation is None:
accumulation = namedtuple("accumulation", "fm sm t")
self.accumulation = accumulation([None] * len(args), [None] * len(args), [0])
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.accumulation = accumulation([None] * len(args), [None] * len(args), [0])
self.accumulation = accumulation([None] * len(args), [None] * len(args), 0)

We only need the latest time, so we don't need to store it in an array.

This change would need to get propagated throughout the file, changing self.accumulation.t[0] to self.accumulation.t.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I think t needs to be defined mutable. Subclasses of namedtuples can't be changed naturally. Alternatively I could use a class definition or a dictionary.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point. I forgot about that...

Maybe we should then just use a dictionary. Even though we technically could just keep using [0] wrapped in a list, it's probably better to use an intrinsically mutable type instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll change it to a dictionary 🙂

pennylane/optimize/adam.py Outdated Show resolved Hide resolved
pennylane/optimize/adam.py Outdated Show resolved Hide resolved
pennylane/optimize/adam.py Outdated Show resolved Hide resolved
MoritzWillmann and others added 3 commits October 18, 2021 14:45
Co-authored-by: Christina Lee <chrissie.c.l@gmail.com>
Co-authored-by: Christina Lee <chrissie.c.l@gmail.com>
"""Test the newly added property interfaces"""
bunch.adam_opt.reset()
# check if errors get raised when accumulation is empty
with pytest.raises(ValueError):
Copy link
Contributor

Choose a reason for hiding this comment

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

Remember to update the tests with the new return behaviour

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the reminder!

Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

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

Thanks @MoritzWillmann 💯 🎉

@albi3ro
Copy link
Contributor

albi3ro commented Oct 18, 2021

Once the branch is up to date with master I'll merge it in.

@MoritzWillmann
Copy link
Contributor Author

@albi3ro Cool, thanks! 🥳

@albi3ro albi3ro merged commit 3c5863a into PennyLaneAI:master Oct 18, 2021
@MoritzWillmann MoritzWillmann deleted the adam-accumulation branch April 8, 2022 18:17
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.

Optimizers: standardize use of accumulation
2 participants