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

Raise warning for schedulers following chainable shedulers #31125

Closed
wants to merge 1 commit into from

Conversation

vincentqb
Copy link
Contributor

@vincentqb vincentqb commented Dec 11, 2019

Fixes #29697.

Raise warning for schedulers following chainable schedulers in #26423. See explanation for

@gchanan -- This should go in the upcoming release following #26423.

Example

Version 1.3.1:

import torch
from torch.nn import Parameter
from torch.optim import SGD
from torch.optim.lr_scheduler import StepLR

model = [Parameter(torch.randn(2, 2, requires_grad=True))]

print('Initial training')

optimizer = SGD(model, 0.1)
scheduler = StepLR(optimizer, 2)

for epoch in range(5):
    print(epoch, optimizer.param_groups[0]['lr'])
    optimizer.step()
    scheduler.step()

state_dict_lr = scheduler.state_dict()

print('Loading from checkpoint')

optimizer = SGD(model, 0.1)
scheduler = StepLR(optimizer, 2)

scheduler.load_state_dict(state_dict_lr)

for epoch in range(5, 10):
    print(epoch, optimizer.param_groups[0]['lr'])
    optimizer.step()
    scheduler.step()
Initial training
0 0.1
1 0.1
2 0.010000000000000002
3 0.010000000000000002
4 0.0010000000000000002
Loading from checkpoint
5 0.1
6 0.00010000000000000003
7 0.00010000000000000003
8 1.0000000000000003e-05
9 1.0000000000000003e-05

This is not correct, but no warnings are raised.

Version 1.3.1 and 1.4.0:

import torch
from torch.nn import Parameter
from torch.optim import SGD
from torch.optim.lr_scheduler import StepLR

model = [Parameter(torch.randn(2, 2, requires_grad=True))]

print('Initial training')

optimizer = SGD(model, 0.1)
scheduler = StepLR(optimizer, 2)

for epoch in range(5):
    print(epoch, optimizer.param_groups[0]['lr'])
    optimizer.step()
    scheduler.step()

state_dict_lr = scheduler.state_dict()
state_dict_opt = optimizer.state_dict()

print('Loading from checkpoint')

optimizer = SGD(model, 0.1)
scheduler = StepLR(optimizer, 2)

scheduler.load_state_dict(state_dict_lr)
optimizer.load_state_dict(state_dict_opt)

for epoch in range(5, 10):
    print(epoch, optimizer.param_groups[0]['lr'])
    optimizer.step()
    scheduler.step()
Initial training
0 lr [0.1]
1 lr [0.1]
2 lr [0.010000000000000002]
3 lr [0.010000000000000002]
4 lr [0.0010000000000000002]
Loading from checkpoint
5 lr [0.0010000000000000002]
6 lr [0.00010000000000000003]
7 lr [0.00010000000000000003]
8 lr [1.0000000000000004e-05]
9 lr [1.0000000000000004e-05]

@vincentqb vincentqb changed the title Raise warning for schedulers from 26423 Raise warning for schedulers following #26423 Dec 11, 2019
@vincentqb vincentqb changed the title Raise warning for schedulers following #26423 Raise warning for schedulers following chainable shedulers Dec 11, 2019
@vincentqb vincentqb changed the title Raise warning for schedulers following chainable shedulers [WIP] Raise warning for schedulers following chainable shedulers Dec 11, 2019
@vincentqb vincentqb changed the title [WIP] Raise warning for schedulers following chainable shedulers Raise warning for schedulers following chainable shedulers Dec 17, 2019
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@vincentqb has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@vincentqb merged this pull request in 9459db8.

wuhuikx pushed a commit to wuhuikx/pytorch that referenced this pull request Jan 30, 2020
…1125)

Summary:
Fixes pytorch#29697.

Raise warning for schedulers following chainable schedulers in pytorch#26423. See explanation for
* [new warning when load/save](pytorch#29697 (comment))
* [change from deprecation to user warning](pytorch#29697 (comment)).

gchanan -- This should go in the upcoming release following pytorch#26423.
Pull Request resolved: pytorch#31125

Differential Revision: D19143740

Pulled By: vincentqb

fbshipit-source-id: 35b55fe6c5b39ca5a68b1a6e19f14eb95b9a784e
@jrmcornish
Copy link

This change seems to raise a warning under all circumstances that state_dict() is called for LambdaLR schedulers, i.e. regardless of whether or not the state of the optimizer is being saved or not. Is this correct? Are we supposed to simply manually suppress this warning if we are using state_dict() correctly? Or are we not supposed to be using state_dict() at all (at least for LambdaLR schedulers)?

@vincentqb
Copy link
Contributor Author

vincentqb commented Aug 20, 2020

This change seems to raise a warning under all circumstances that state_dict() is called for LambdaLR schedulers, i.e. regardless of whether or not the state of the optimizer is being saved or not. Is this correct? Are we supposed to simply manually suppress this warning if we are using state_dict() correctly? Or are we not supposed to be using state_dict() at all (at least for LambdaLR schedulers)?

Thanks for reporting this! Now that the warning has been part of 1.4, 1.5, and 1.6, we can remove it and replace it by a message in the documentation. Do you want to open a pull request to do this @jrmcornish ?

Created #43352 to keep track of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MultiStepLR does not return good lr after load_state_dict
5 participants