From 96a0ce60d215b8525f63a87594ea1080bc27f174 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: Tue, 31 Jan 2023 15:42:18 +0800 Subject: [PATCH] fix div 0 error of NoamDecay (#49953) * fix div 0 error of NoamDecay * add unittest * Update lr.py --- .../tests/unittests/test_noamdecay_op.py | 34 +++++++++++++++++++ python/paddle/optimizer/lr.py | 3 ++ 2 files changed, 37 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 0000000000..62312c7a8b --- /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() diff --git a/python/paddle/optimizer/lr.py b/python/paddle/optimizer/lr.py index 07420be891..bc5f9020b7 100644 --- a/python/paddle/optimizer/lr.py +++ b/python/paddle/optimizer/lr.py @@ -296,6 +296,9 @@ class NoamDecay(LRScheduler): 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) -- GitLab