本文整理匯總了Python中chainer.training.triggers.IntervalTrigger方法的典型用法代碼示例。如果您正苦於以下問題:Python triggers.IntervalTrigger方法的具體用法?Python triggers.IntervalTrigger怎麽用?Python triggers.IntervalTrigger使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類chainer.training.triggers
的用法示例。
在下文中一共展示了triggers.IntervalTrigger方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_chainer_pruning_extension_trigger
# 需要導入模塊: from chainer.training import triggers [as 別名]
# 或者: from chainer.training.triggers import IntervalTrigger [as 別名]
def test_chainer_pruning_extension_trigger() -> None:
study = optuna.create_study()
trial = create_running_trial(study, 1.0)
extension = ChainerPruningExtension(trial, "main/loss", (1, "epoch"))
assert isinstance(extension._pruner_trigger, triggers.IntervalTrigger)
extension = ChainerPruningExtension(trial, "main/loss", triggers.IntervalTrigger(1, "epoch"))
assert isinstance(extension._pruner_trigger, triggers.IntervalTrigger)
extension = ChainerPruningExtension(
trial, "main/loss", triggers.ManualScheduleTrigger(1, "epoch")
)
assert isinstance(extension._pruner_trigger, triggers.ManualScheduleTrigger)
with pytest.raises(TypeError):
ChainerPruningExtension(trial, "main/loss", triggers.TimeTrigger(1.0))
示例2: test_observation_exists
# 需要導入模塊: from chainer.training import triggers [as 別名]
# 或者: from chainer.training.triggers import IntervalTrigger [as 別名]
def test_observation_exists() -> None:
study = optuna.create_study()
trial = create_running_trial(study, 1.0)
MockTrainer = namedtuple("_MockTrainer", ("observation",))
trainer = MockTrainer(observation={"OK": 0})
# Trigger is deactivated. Return False whether trainer has observation or not.
with patch.object(triggers.IntervalTrigger, "__call__", Mock(return_value=False)) as mock:
extension = ChainerPruningExtension(trial, "NG", (1, "epoch"))
assert extension._observation_exists(trainer) is False
extension = ChainerPruningExtension(trial, "OK", (1, "epoch"))
assert extension._observation_exists(trainer) is False
assert mock.call_count == 2
# Trigger is activated. Return True if trainer has observation.
with patch.object(triggers.IntervalTrigger, "__call__", Mock(return_value=True)) as mock:
extension = ChainerPruningExtension(trial, "NG", (1, "epoch"))
assert extension._observation_exists(trainer) is False
extension = ChainerPruningExtension(trial, "OK", (1, "epoch"))
assert extension._observation_exists(trainer) is True
assert mock.call_count == 2
示例3: __init__
# 需要導入模塊: from chainer.training import triggers [as 別名]
# 或者: from chainer.training.triggers import IntervalTrigger [as 別名]
def __init__(self, trial, observation_key, pruner_trigger):
# type: (optuna.trial.Trial, str, TriggerType) -> None
_imports.check()
self._trial = trial
self._observation_key = observation_key
self._pruner_trigger = chainer.training.get_trigger(pruner_trigger)
if not (
isinstance(self._pruner_trigger, triggers.IntervalTrigger)
or isinstance(self._pruner_trigger, triggers.ManualScheduleTrigger)
):
pruner_type = type(self._pruner_trigger)
raise TypeError(
"Invalid trigger class: " + str(pruner_type) + "\n"
"Pruner trigger is supposed to be an instance of "
"IntervalTrigger or ManualScheduleTrigger."
)
示例4: initialize
# 需要導入模塊: from chainer.training import triggers [as 別名]
# 或者: from chainer.training.triggers import IntervalTrigger [as 別名]
def initialize(self, trainer):
self._out = trainer.out
CommandItem.remove_commands_file(trainer.out)
CommandsState.run(trainer.out)
if isinstance(trainer.stop_trigger, IntervalTrigger):
trainer.stop_trigger = _CommandIntervalTrigger(
trainer.stop_trigger)
else:
trainer.stop_trigger = _CommandTrigger(trainer.stop_trigger)
示例5: _get_mock_trainer
# 需要導入模塊: from chainer.training import triggers [as 別名]
# 或者: from chainer.training.triggers import IntervalTrigger [as 別名]
def _get_mock_trainer(self, out_path, trigger=None, updater=None):
class _MockTrainer(Trainer):
def __init__(
self, out_path, stop_trigger=IntervalTrigger(100, 'epoch'),
updater=None):
self.out = out_path
self.stop_trigger = stop_trigger
hyperparam = Hyperparameter()
hyperparam.lr = 0.005
optimizer = MagicMock()
optimizer.__class__.__name__ = 'MomentumSGD'
optimizer.hyperparam = hyperparam
if updater is None:
updater = MagicMock()
updater.epoch = 0
updater.iteration = 0
updater.get_optimizer.return_value = optimizer
self.updater = updater
@property
def elapsed_time(self):
return 0
def serialize(self, serializer):
pass
return _MockTrainer(out_path, trigger, updater)