Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

使用多卡/多线程训练时无法支持传入learning_rate等超参数。

Open TomorrowIsAnOtherDay opened this issue 6 years ago • 4 comments

报错如下:

Error: The number(1) of samples[lr] of current batch is less than the count(5) of devices(CPU), currently, it is not allowed. You should set the environment variable CPU_NUM in the system to determine the number of devices you need. at (/paddle/paddle/fluid/framework/parallel_executor.cc:788)

相关使用代码如下:

        feed = {
            'obs': obs.astype('float32'),
            'act': act.astype('int32'),
            'reward': reward,
            'next_obs': next_obs.astype('float32'),
            'terminal': terminal,
            'lr': lr         <----------------------这里
        }
        cost = self.fluid_executor.run(
            self.learn_program, feed=feed, fetch_list=[self.cost])[0]

TomorrowIsAnOtherDay avatar Apr 21 '20 06:04 TomorrowIsAnOtherDay

你这里的学习率是如何设置的呢?

yongqiangma avatar Apr 22 '20 03:04 yongqiangma

def main():
    model = FlappyBirdModel(2)
    train_program = fluid.Program()
    with fluid.program_guard(train_program):
        obs = fluid.layers.data(name='obs', shape=[4, 80, 80], dtype='float32')
        action = fluid.layers.data(name='action', shape=[2], dtype='int32')
        target = fluid.layers.data(name='target', shape=[], dtype='float32')
##这里声明
        learning_rate = fluid.layers.data(name='lr', shape=[1], dtype='float32', append_batch_size=False)

        Q = model.value(obs)
        action = fluid.layers.cast(action, dtype='float32')
        all_Q = fluid.layers.elementwise_mul(action, Q)
        pred_Q = fluid.layers.reduce_sum(all_Q, dim=1)

        cost = fluid.layers.square_error_cost(pred_Q, target)
        cost = fluid.layers.reduce_mean(cost)
##用在这里
        optimizer = fluid.optimizer.Adam(learning_rate)
        optimizer.minimize(cost)

TomorrowIsAnOtherDay avatar Apr 22 '20 03:04 TomorrowIsAnOtherDay

再多贴一线lr函数设置的代码吧。 另外这个报错只在多线程、多卡才有。单卡情况下是没有的吗?

yongqiangma avatar Apr 22 '20 03:04 yongqiangma

以下是完整的main函数代码,单卡是没问题的。

from forward_model import FlappyBirdModel
from paddle import fluid
from tqdm import tqdm
import numpy as np

def main():
    model = FlappyBirdModel(2)
    train_program = fluid.Program()
    with fluid.program_guard(train_program):
        obs = fluid.layers.data(name='obs', shape=[4, 80, 80], dtype='float32')
        action = fluid.layers.data(name='action', shape=[2], dtype='int32')
        target = fluid.layers.data(name='target', shape=[], dtype='float32')
        learning_rate = fluid.layers.data(name='lr', shape=[1], dtype='float32', append_batch_size=False)

        Q = model.value(obs)
        action = fluid.layers.cast(action, dtype='float32')
        all_Q = fluid.layers.elementwise_mul(action, Q)
        pred_Q = fluid.layers.reduce_sum(all_Q, dim=1)

        cost = fluid.layers.square_error_cost(pred_Q, target)
        cost = fluid.layers.reduce_mean(cost)

        optimizer = fluid.optimizer.Adam(learning_rate)
        optimizer.minimize(cost)

    exec_strategy = fluid.ExecutionStrategy()
    exec_strategy.num_threads = 48

    build_strategy = fluid.BuildStrategy()
    build_strategy.reduce_strategy = fluid.BuildStrategy.ReduceStrategy.Reduce

    exe = fluid.ParallelExecutor(
        use_cuda=False,
        loss_name=cost.name,
        main_program=train_program,
        build_strategy=build_strategy,
        exec_strategy=exec_strategy)
    normal_exe = fluid.Executor(fluid.CPUPlace())
    normal_exe.run(fluid.default_startup_program())

    # test speed
    BATCH_SIZE = 32
    ACTIONS = 2
    data = [np.random.normal(size=[BATCH_SIZE, 4, 80, 80]).astype('float32'),
            np.random.randint(size=[BATCH_SIZE, ACTIONS], low=0, high=2).astype('int32'),
            np.random.random(size= [BATCH_SIZE]).astype('float32')]
    for i in tqdm(range(1000)):
        exe.run(fetch_list=[],
                feed={'obs': data[0],
                     'action':data[1],
                     'target':data[2],
                      'lr': 1e-6})
if __name__ == '__main__':
    main()

TomorrowIsAnOtherDay avatar Apr 22 '20 05:04 TomorrowIsAnOtherDay