首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在chainer中,如何使用chainer.training.Trainer提前停止迭代?

在chainer中,如何使用chainer.training.Trainer提前停止迭代?
EN

Stack Overflow用户
提问于 2017-08-26 04:26:28
回答 2查看 549关注 0票数 2

我正在使用chainer框架(深度学习),假设两个迭代的目标函数值的差距很小时,我必须停止迭代:f - old_f < eps。火车的stop_trigger是(args.epoch,‘划时代’)元组。如何触发早停?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-03 05:25:01

基于您的情况,我根据@Seiya的答案实现了EarlyStoppingTrigger示例。

代码语言:javascript
复制
from chainer import reporter
from chainer.training import util

class EarlyStoppingTrigger(object):

"""Early stopping trigger

It observes the value specified by `key`, and invoke a trigger only when 
observing value satisfies the `stop_condition`.
The trigger may be used to `stop_trigger` option of Trainer module for
early stopping the training.
Args:
    max_epoch (int or float): Max epoch for the training, even if the value 
        is not reached to the condition specified by `stop_condition`,
        finish the training if it reaches to `max_epoch` epoch.
    key (str): Key of value to be observe for `stop_condition`.
    stop_condition (callable): To check the previous value and current value
        to decide early stop timing. Default value is `None`, in that case
        internal `_stop_condition` method is used.
    eps (float): It is used by the internal `_stop_condition`.
    trigger: Trigger that decides the comparison interval between previous
        best value and current value. This must be a tuple in the form of
        ``<int>, 'epoch'`` or ``<int>, 'iteration'`` which is passed to
        :class:`~chainer.training.triggers.IntervalTrigger`.
"""

def __init__(self, max_epoch, key, stop_condition=None, eps=0.01,
             trigger=(1, 'epoch')):
    self.max_epoch = max_epoch
    self.eps = eps
    self._key = key
    self._current_value = None
    self._interval_trigger = util.get_trigger(trigger)
    self._init_summary()
    self.stop_condition = stop_condition or self._stop_condition

def __call__(self, trainer):
    """Decides whether the extension should be called on this iteration.
    Args:
        trainer (~chainer.training.Trainer): Trainer object that this
            trigger is associated with. The ``observation`` of this trainer
            is used to determine if the trigger should fire.
    Returns:
        bool: ``True`` if the corresponding extension should be invoked in
            this iteration.
    """

    epoch_detail = trainer.updater.epoch_detail
    if self.max_epoch <= epoch_detail:
        print('Reached to max_epoch.')
        return True

    observation = trainer.observation
    summary = self._summary
    key = self._key
    if key in observation:
        summary.add({key: observation[key]})

    if not self._interval_trigger(trainer):
        return False

    stats = summary.compute_mean()
    value = float(stats[key])  # copy to CPU
    self._init_summary()

    if self._current_value is None:
        self._current_value = value
        return False
    else:
        if self.stop_condition(self._current_value, value):
            # print('Previous value {}, Current value {}'
            #       .format(self._current_value, value))
            print('Invoke EarlyStoppingTrigger...')
            self._current_value = value
            return True
        else:
            self._current_value = value
            return False

def _init_summary(self):
    self._summary = reporter.DictSummary()

def _stop_condition(self, current_value, new_value):
    return current_value - new_value < self.eps

用法:您可以将它传递给stop_trigger选项trainer

代码语言:javascript
复制
early_stop = EarlyStoppingTrigger(args.epoch, key='validation/main/loss', eps=0.01)
trainer = training.Trainer(updater, stop_trigger=early_stop, out=args.out)

有关整个工作示例代码,请参见这个要旨

注意,如果使用自定义的ProgressBar,我们还需要修复training_length扩展以显式传递stop_trigger

票数 1
EN

Stack Overflow用户

发布于 2017-08-28 00:37:06

可以将可调用对象传递给stop_trigger选项。可调用对象通过传递Trainer对象在每次迭代时调用。它应该返回一个布尔值。当返回的值为True时,将停止培训。为了实现早期停止,您可以编写自己的触发器函数并将其传递给TrainerTrainer选项。

接受触发器对象的其他API也接受可调用的API;有关详细信息,请参阅get_trigger

注意:stop_trigger的元组值是使用chainer.training.triggers.IntervalTrigger作为可调用的简短的手动符号。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45891924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档