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

the init parameter "optimizer" of Trainer() should be a function #11157

Closed
jacquesqiao opened this issue Jun 4, 2018 · 3 comments · Fixed by #11168
Closed

the init parameter "optimizer" of Trainer() should be a function #11157

jacquesqiao opened this issue Jun 4, 2018 · 3 comments · Fixed by #11168
Assignees

Comments

@jacquesqiao
Copy link
Member

jacquesqiao commented Jun 4, 2018

Background

@seiriosPlus find a bug in the new Trainer high-level API when writing a model with learning rate decay. The optimizer is created in a different program than the trainer use.

Reason and solution

class Trainer(object):
"""
Args:
train_func(callable): A function which will return loss. The loss must be a scalar.
optimizer(optimizer.Optimizer): The optimizer should be an instance of Optimizer
place: The device place of this trainer.
"""
def __init__(self,
train_func,
optimizer,
param_path=None,
place=None,
parallel=False):
self.__stop = False

In the design of Trainer API, train_program is a function call, it will be called inside Trainer under a with scope

with framework.program_guard(self.train_program, self.startup_program):
program_func_outs = train_func()

But the init parameter optimizer is an object, it will be created outside the with scope, this is not right, so we should make optimizer also a function call that return an optimizer boject

the interface should be

class Trainer(object):
    def __init__(self,
                 train_func,
                 optimizer_func,
                 param_path=None,
                 place=None,
                 parallel=False):
@jacquesqiao jacquesqiao changed the title the init parameter optimizer of Trainer() should be a function the init parameter "optimizer" of Trainer() should be a function Jun 4, 2018
@seiriosPlus
Copy link
Collaborator

when we init Optimizer with the code of Fluid, the variables will be created outside the scope.
For example:

optimizer = fluid.optimizer.Momentum(
                learning_rate=fluid.layers.piecewise_decay(
                    boundaries=bd, values=lr),
                momentum=0.9,
                regularization=fluid.regularizer.L2Decay(1e-4))

@jetfuel
Copy link
Contributor

jetfuel commented Jun 4, 2018

@jacquesqiao @seiriosPlus Does the bug only happen with the learning rate decay? I wonder if there is a way to create a copy of the optimizer with the proper.

@sidgoyal78
Copy link
Contributor

sidgoyal78 commented Jun 4, 2018

@jacquesqiao : I think your suggestion sounds pretty good. I will make a PR implementing your approach.

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 a pull request may close this issue.

6 participants