未验证 提交 3e656414 编写于 作者: R RedContritio 提交者: GitHub

Fix Python IndexError of case4: paddle.optimizer.lr.PiecewiseDecay (#49987)

上级 a34ecad8
...@@ -728,6 +728,17 @@ class TestLRScheduler(unittest.TestCase): ...@@ -728,6 +728,17 @@ class TestLRScheduler(unittest.TestCase):
step_size_down=-1, step_size_down=-1,
scale_mode='test', scale_mode='test',
) )
# check empty boundaries
with self.assertRaises(ValueError):
paddle.optimizer.lr.PiecewiseDecay(boundaries=[], values=[])
# check non-empty boundaries but empty values
with self.assertRaises(ValueError):
paddle.optimizer.lr.PiecewiseDecay(boundaries=[100, 200], values=[])
# check boundaries and values has same length
with self.assertRaises(ValueError):
paddle.optimizer.lr.PiecewiseDecay(
boundaries=[100, 200], values=[0.5, 0.1]
)
func_api_kwargs = [ func_api_kwargs = [
( (
......
...@@ -391,6 +391,14 @@ class PiecewiseDecay(LRScheduler): ...@@ -391,6 +391,14 @@ class PiecewiseDecay(LRScheduler):
""" """
def __init__(self, boundaries, values, last_epoch=-1, verbose=False): def __init__(self, boundaries, values, last_epoch=-1, verbose=False):
if len(boundaries) == 0:
raise ValueError('The boundaries cannot be empty.')
if len(values) <= len(boundaries):
raise ValueError(
f'The values have one more element than boundaries, but received len(values) [{len(values)}] < len(boundaries) + 1 [{len(boundaries) + 1}].'
)
self.boundaries = boundaries self.boundaries = boundaries
self.values = values self.values = values
super().__init__(last_epoch=last_epoch, verbose=verbose) super().__init__(last_epoch=last_epoch, verbose=verbose)
......
...@@ -88,7 +88,7 @@ class TestReduceLROnPlateau(unittest.TestCase): ...@@ -88,7 +88,7 @@ class TestReduceLROnPlateau(unittest.TestCase):
optim = paddle.optimizer.Adam( optim = paddle.optimizer.Adam(
learning_rate=paddle.optimizer.lr.PiecewiseDecay( learning_rate=paddle.optimizer.lr.PiecewiseDecay(
[0.001, 0.0001], [5, 10] [0.001, 0.0001], [5, 10, 10]
), ),
parameters=net.parameters(), parameters=net.parameters(),
) )
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册