Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

梯度全部为0(v2 api)

Open liuyuuan opened this issue 9 years ago • 7 comments

我使用了如下的训练过程:

    def train(args):
           paddle.init(use_gpu=args.use_gpu, trainer_count=args.trainer_count)
           
           optimizer = paddle.optimizer.Momentum(
                               momentum=0.9,
                               learning_rate=1e-3,
                               regularization=paddle.optimizer.L2Regularization(rate=1e-3))
           feeding = {...}
           reader = some_dataset.create_reader()
           train_batch_reader = paddle.batch(reader=reader, batch_size=args.batch_size)
           cost = my_network(args)
           parameters = paddle.parameters.create(cost)
           def event_handler(event):
                  """print logs"""
                  ...
           trainer.train(reader=train_batch_reader,
                                event_handler=event_handler,
                                num_passes=10,
                                feeding=feeding)

cost是用的是 sum_cost, 简化如下:

    label_probs = paddle.layer.scaling(input=neg_log_probs, weight=labels)
    cost = paddle.layer.sum_cost(input=label_probs)

其中 neg_log_probs 和 labels 是两个dense_vector(1) 的sequence。

但是训练得到的梯度全部是0,而参数的值看起来正常,cost的值也正常。看起来就像只执行了forward一样,请问这有可能是什么问题导致的错误?

liuyuuan avatar Sep 26 '17 03:09 liuyuuan

感觉这里的paddle.layer.scaling可能不是你期望的,ScalingLayer所做计算参见 https://github.com/PaddlePaddle/Paddle/blob/ed808f5ef65be490ee1950058b42328d304fd269/paddle/gserver/layers/ScalingLayer.cpp#L26 和 https://github.com/PaddlePaddle/Paddle/blob/14a7399d22d26de242196cf0085081780a21a918/python/paddle/trainer_config_helpers/layers.py#L2107 ,如果需要element-wise multiplication的话可能dotmul_operator https://github.com/PaddlePaddle/Paddle/blob/14a7399d22d26de242196cf0085081780a21a918/python/paddle/trainer_config_helpers/layers.py#L681 是你需要的操作

guoshengCS avatar Sep 26 '17 03:09 guoshengCS

@guoshengCS 多谢,不过我试过了dotmul_operator, 梯度依然是全0。

liuyuuan avatar Sep 26 '17 04:09 liuyuuan

换成CPU试试。

lcy-seso avatar Sep 26 '17 05:09 lcy-seso

@lcy-seso 多谢,cpu就ok了,是sum_cost的backward在gpu上没有实现?最终还是希望能在gpu上正常训练,cpu太慢了。

liuyuuan avatar Sep 26 '17 05:09 liuyuuan

a related issue https://github.com/PaddlePaddle/Paddle/issues/3714

lcy-seso avatar Sep 26 '17 05:09 lcy-seso

sum_cost 可以在GPU下使用,问题可以参考 https://github.com/PaddlePaddle/Paddle/issues/3714 ,我建议可以调参稳定之后,切换到GPU下训练。

lcy-seso avatar Sep 26 '17 05:09 lcy-seso

恩,好的,多谢,这个issue我close掉吧

liuyuuan avatar Sep 26 '17 06:09 liuyuuan