未验证 提交 4708b081 编写于 作者: F Feng Ni 提交者: GitHub

fix iters less than batchsize in warmup (#6724)

上级 e55e4194
...@@ -7,8 +7,9 @@ LearningRate: ...@@ -7,8 +7,9 @@ LearningRate:
gamma: 0.1 gamma: 0.1
milestones: [15, 22] milestones: [15, 22]
use_warmup: True use_warmup: True
- !BurninWarmup - !ExpWarmup
steps: 1000 steps: 1000
power: 4
OptimizerBuilder: OptimizerBuilder:
optimizer: optimizer:
......
...@@ -7,8 +7,9 @@ LearningRate: ...@@ -7,8 +7,9 @@ LearningRate:
gamma: 0.1 gamma: 0.1
milestones: [15, 22] milestones: [15, 22]
use_warmup: True use_warmup: True
- !BurninWarmup - !ExpWarmup
steps: 1000 steps: 1000
power: 4
OptimizerBuilder: OptimizerBuilder:
optimizer: optimizer:
......
...@@ -7,8 +7,9 @@ LearningRate: ...@@ -7,8 +7,9 @@ LearningRate:
gamma: 0.1 gamma: 0.1
milestones: [30, 44] milestones: [30, 44]
use_warmup: True use_warmup: True
- !BurninWarmup - !ExpWarmup
steps: 1000 steps: 1000
power: 4
OptimizerBuilder: OptimizerBuilder:
optimizer: optimizer:
......
...@@ -63,8 +63,9 @@ LearningRate: ...@@ -63,8 +63,9 @@ LearningRate:
gamma: 0.1 gamma: 0.1
milestones: [15, 22] milestones: [15, 22]
use_warmup: True use_warmup: True
- !BurninWarmup - !ExpWarmup
steps: 1000 steps: 1000
power: 4
OptimizerBuilder: OptimizerBuilder:
optimizer: optimizer:
......
...@@ -150,6 +150,10 @@ class Trainer(object): ...@@ -150,6 +150,10 @@ class Trainer(object):
# build optimizer in train mode # build optimizer in train mode
if self.mode == 'train': if self.mode == 'train':
steps_per_epoch = len(self.loader) steps_per_epoch = len(self.loader)
if steps_per_epoch < 1:
logger.warning(
"Samples in dataset are less than batch_size, please set smaller batch_size in TrainReader."
)
self.lr = create('LearningRate')(steps_per_epoch) self.lr = create('LearningRate')(steps_per_epoch)
self.optimizer = create('OptimizerBuilder')(self.lr, self.model) self.optimizer = create('OptimizerBuilder')(self.lr, self.model)
......
...@@ -176,6 +176,7 @@ class LinearWarmup(object): ...@@ -176,6 +176,7 @@ class LinearWarmup(object):
value = [] value = []
warmup_steps = self.epochs * step_per_epoch \ warmup_steps = self.epochs * step_per_epoch \
if self.epochs is not None else self.steps if self.epochs is not None else self.steps
warmup_steps = max(warmup_steps, 1)
for i in range(warmup_steps + 1): for i in range(warmup_steps + 1):
if warmup_steps > 0: if warmup_steps > 0:
alpha = i / warmup_steps alpha = i / warmup_steps
...@@ -187,31 +188,6 @@ class LinearWarmup(object): ...@@ -187,31 +188,6 @@ class LinearWarmup(object):
return boundary, value return boundary, value
@serializable
class BurninWarmup(object):
"""
Warm up learning rate in burnin mode
Args:
steps (int): warm up steps
"""
def __init__(self, steps=1000):
super(BurninWarmup, self).__init__()
self.steps = steps
def __call__(self, base_lr, step_per_epoch):
boundary = []
value = []
burnin = min(self.steps, step_per_epoch)
for i in range(burnin + 1):
factor = (i * 1.0 / burnin)**4
lr = base_lr * factor
value.append(lr)
if i > 0:
boundary.append(i)
return boundary, value
@serializable @serializable
class ExpWarmup(object): class ExpWarmup(object):
""" """
...@@ -220,19 +196,22 @@ class ExpWarmup(object): ...@@ -220,19 +196,22 @@ class ExpWarmup(object):
steps (int): warm up steps. steps (int): warm up steps.
epochs (int|None): use epochs as warm up steps, the priority epochs (int|None): use epochs as warm up steps, the priority
of `epochs` is higher than `steps`. Default: None. of `epochs` is higher than `steps`. Default: None.
power (int): Exponential coefficient. Default: 2.
""" """
def __init__(self, steps=5, epochs=None): def __init__(self, steps=1000, epochs=None, power=2):
super(ExpWarmup, self).__init__() super(ExpWarmup, self).__init__()
self.steps = steps self.steps = steps
self.epochs = epochs self.epochs = epochs
self.power = power
def __call__(self, base_lr, step_per_epoch): def __call__(self, base_lr, step_per_epoch):
boundary = [] boundary = []
value = [] value = []
warmup_steps = self.epochs * step_per_epoch if self.epochs is not None else self.steps warmup_steps = self.epochs * step_per_epoch if self.epochs is not None else self.steps
warmup_steps = max(warmup_steps, 1)
for i in range(warmup_steps + 1): for i in range(warmup_steps + 1):
factor = (i / float(warmup_steps))**2 factor = (i / float(warmup_steps))**self.power
value.append(base_lr * factor) value.append(base_lr * factor)
if i > 0: if i > 0:
boundary.append(i) boundary.append(i)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册