[docs]classCustomPeriodicEvent:"""DEPRECATED. Use filtered events instead. Handler to define a custom periodic events as a number of elapsed iterations/epochs for an engine. When custom periodic event is created and attached to an engine, the following events are fired: 1) K iterations is specified: - `Events.ITERATIONS_<K>_STARTED` - `Events.ITERATIONS_<K>_COMPLETED` 1) K epochs is specified: - `Events.EPOCHS_<K>_STARTED` - `Events.EPOCHS_<K>_COMPLETED` Examples: .. code-block:: python from ignite.engine import Engine, Events from ignite.contrib.handlers import CustomPeriodicEvent # Let's define an event every 1000 iterations cpe1 = CustomPeriodicEvent(n_iterations=1000) cpe1.attach(trainer) # Let's define an event every 10 epochs cpe2 = CustomPeriodicEvent(n_epochs=10) cpe2.attach(trainer) @trainer.on(cpe1.Events.ITERATIONS_1000_COMPLETED) def on_every_1000_iterations(engine): # run a computation after 1000 iterations # ... print(engine.state.iterations_1000) @trainer.on(cpe2.Events.EPOCHS_10_STARTED) def on_every_10_epochs(engine): # run a computation every 10 epochs # ... print(engine.state.epochs_10) Args: n_iterations (int, optional): number iterations of the custom periodic event n_epochs (int, optional): number iterations of the custom periodic event. Argument is optional, but only one, either n_iterations or n_epochs should defined. """def__init__(self,n_iterations=None,n_epochs=None):warnings.warn("CustomPeriodicEvent is deprecated since 0.4.0 and will be removed in 0.5.0. Use filtered events instead.",DeprecationWarning,)ifn_iterationsisnotNoneand(notisinstance(n_iterations,int)orn_iterations<1):raiseValueError("Argument n_iterations should be positive integer number")ifn_epochsisnotNoneand(notisinstance(n_epochs,int)orn_epochs<1):raiseValueError("Argument n_epochs should be positive integer number")if(n_iterationsisNoneandn_epochsisNone)or(n_iterationsandn_epochs):raiseValueError("Either n_iterations or n_epochs should be defined")ifn_iterations:prefix="iterations"self.state_attr="iteration"self.period=n_iterationsifn_epochs:prefix="epochs"self.state_attr="epoch"self.period=n_epochsself.custom_state_attr="{}_{}".format(prefix,self.period)event_name="{}_{}".format(prefix.upper(),self.period)setattr(self,"Events",EventEnum("Events"," ".join(["{}_STARTED".format(event_name),"{}_COMPLETED".format(event_name)])),)# Update State.event_to_attrforeinself.Events:State.event_to_attr[e]=self.custom_state_attr# Create aliasesself._periodic_event_started=getattr(self.Events,"{}_STARTED".format(event_name))self._periodic_event_completed=getattr(self.Events,"{}_COMPLETED".format(event_name))def_on_started(self,engine):setattr(engine.state,self.custom_state_attr,0)def_on_periodic_event_started(self,engine):ifgetattr(engine.state,self.state_attr)%self.period==1:setattr(engine.state,self.custom_state_attr,getattr(engine.state,self.custom_state_attr)+1)engine.fire_event(self._periodic_event_started)def_on_periodic_event_completed(self,engine):ifgetattr(engine.state,self.state_attr)%self.period==0:engine.fire_event(self._periodic_event_completed)defattach(self,engine):engine.register_events(*self.Events)engine.add_event_handler(Events.STARTED,self._on_started)engine.add_event_handler(getattr(Events,"{}_STARTED".format(self.state_attr.upper())),self._on_periodic_event_started)engine.add_event_handler(getattr(Events,"{}_COMPLETED".format(self.state_attr.upper())),self._on_periodic_event_completed)