Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Saving & loading optimizer state #2

Closed
OverLordGoldDragon opened this issue Sep 13, 2019 · 8 comments
Closed

Saving & loading optimizer state #2

OverLordGoldDragon opened this issue Sep 13, 2019 · 8 comments
Assignees
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@OverLordGoldDragon
Copy link

OverLordGoldDragon commented Sep 13, 2019

Optimizer weights are now stored in model.optimizer.optimizer, and model.optimizer.weights==[] - hence, default saving and loading methods will not work. Existing code can account for this as follows:

if "Lookahead" in str(model.optimizer):
    optimizer = model.optimizer.optimizer
else:
    optimizer = model.optimizer

optimizer.get_weights(...)
optimizer.set_weights(...)

NOTE: unsure if above accounts for all differences. Packing weights into model.optimizer directly will render this redundant.

@OverLordGoldDragon OverLordGoldDragon added the bug Something isn't working label Sep 13, 2019
CyberZHG added a commit that referenced this issue Sep 13, 2019
@CyberZHG
Copy link
Owner

Fixed.

@OverLordGoldDragon
Copy link
Author

OverLordGoldDragon commented Sep 13, 2019

@CyberZHG That was fast - thanks; I'll test it shortly. To clarify, is below

self.weights = self.optimizer.weights + slow_params

applying weight updates to self.weights, while Keras applies updates to self.optimizer.weights via self.updates? That is, if this is the only weight update, then before this fix, the 'slow' parameters weren't accounted for at all.

@OverLordGoldDragon
Copy link
Author

OverLordGoldDragon commented Sep 13, 2019

Tested - model.optimizer.weights now holds weights, but also,

len(model.optimizer.updates) == len(model.optimizer.optimizer.updates) # 171 == 171
len(model.optimizer.weights) >  len(model.optimizer.optimizer.weights) # 137 > 103
# up to matching len, the two sets of weights are all equal

Is this intended? If so, was this accounted for before? It may explain my poor model performance. A snippet of the point where the weight tensors differ below:

image


(Also, optimizer save size has increased - unless the exact partial duplicate is necessary, better without it)

@OverLordGoldDragon
Copy link
Author

OverLordGoldDragon commented Sep 13, 2019

Something's faulty - model performance plummets when loading states/weights and re-compiling model for a different batch_shape (greater timesteps); it behaves as if nothing was loaded, not even layer weights; I check that optimizer weights are loaded as saved

image
(val spikes should coincide with train spikes - plot error)

@OverLordGoldDragon
Copy link
Author

Plummeting problem fixed; Lookahead appears strongly bound to its internal optimizer's state - the problem was solved by loading the iterations attribute, which before was thrown out as Nadam itself worked better that way. The iterations attr mediates Nadam's momentum - lower = lower, down to half of max. Same holds for abruptly changing .lr. Not rigorous evidence though, as still unsure whether Lookahead's properly implemented.

@stale
Copy link

stale bot commented Sep 19, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Sep 19, 2019
@OverLordGoldDragon
Copy link
Author

To clarify, is the weight len discrepancy mentioned in my third comment intentional or a bug?

@stale stale bot removed the wontfix This will not be worked on label Sep 19, 2019
@stale
Copy link

stale bot commented Sep 24, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Sep 24, 2019
@stale stale bot closed this as completed Sep 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants