Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

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

Open jacquesqiao opened this issue 8 years ago • 3 comments

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

https://github.com/PaddlePaddle/Paddle/blob/9f0dcfd40c048fef6a57fbf4816a38074da1b1fc/python/paddle/fluid/trainer.py#L88-L103

In the design of Trainer API, train_program is a function call, it will be called inside Trainer under a with scope https://github.com/PaddlePaddle/Paddle/blob/9f0dcfd40c048fef6a57fbf4816a38074da1b1fc/python/paddle/fluid/trainer.py#L115-L117

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 avatar Jun 04 '18 12:06 jacquesqiao

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))

seiriosPlus avatar Jun 04 '18 12:06 seiriosPlus

@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.

jetfuel avatar Jun 04 '18 17:06 jetfuel

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

sidgoyal78 avatar Jun 04 '18 19:06 sidgoyal78