From 4228159d3f5e85769b6e06298a39c9cefa480d9d Mon Sep 17 00:00:00 2001 From: liyulingyue <852433440@qq.com> Date: Thu, 19 Jan 2023 21:48:00 +0800 Subject: [PATCH 1/3] fix div 0 error of NoamDecay --- python/paddle/optimizer/lr.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/paddle/optimizer/lr.py b/python/paddle/optimizer/lr.py index 258e69978a2ec..6e09f708698ac 100644 --- a/python/paddle/optimizer/lr.py +++ b/python/paddle/optimizer/lr.py @@ -296,6 +296,9 @@ def __init__( last_epoch=-1, verbose=False, ): + if d_model == 0: + raise ValueError("d_model should be grater than 0") + self.d_model = d_model self.warmup_steps = warmup_steps super().__init__(learning_rate, last_epoch, verbose) From 25a35560aa9f208023879b70c3db2ca1b8606edb Mon Sep 17 00:00:00 2001 From: liyulingyue <852433440@qq.com> Date: Sat, 21 Jan 2023 14:21:40 +0800 Subject: [PATCH 2/3] add unittest --- .../tests/unittests/test_noamdecay_op.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 python/paddle/fluid/tests/unittests/test_noamdecay_op.py diff --git a/python/paddle/fluid/tests/unittests/test_noamdecay_op.py b/python/paddle/fluid/tests/unittests/test_noamdecay_op.py new file mode 100644 index 0000000000000..62312c7a8b9f0 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/test_noamdecay_op.py @@ -0,0 +1,34 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import paddle + + +class TestSparseEmbeddingAPIError(unittest.TestCase): + def test_errors(self): + with paddle.fluid.dygraph.guard(): + # The size of input in sparse_embedding should not be 0. + def test_0_d_model(): + schedular = paddle.optimizer.lr.NoamDecay( + d_model=0, warmup_steps=0 + ) + + self.assertRaises(ValueError, test_0_d_model) + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() From bd6b191135d379d9eed649bb1fcf5e8cbe5a1204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Mon, 30 Jan 2023 18:02:57 +0800 Subject: [PATCH 3/3] Update lr.py --- python/paddle/optimizer/lr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/optimizer/lr.py b/python/paddle/optimizer/lr.py index 6e09f708698ac..35b765d83069c 100644 --- a/python/paddle/optimizer/lr.py +++ b/python/paddle/optimizer/lr.py @@ -296,7 +296,7 @@ def __init__( last_epoch=-1, verbose=False, ): - if d_model == 0: + if d_model <= 0: raise ValueError("d_model should be grater than 0") self.d_model = d_model