allennlp.training.callbacks¶
-
class
allennlp.training.callbacks.callback.
Callback
[source]¶ Bases:
allennlp.common.registrable.Registrable
The base class for Callbacks that are used by the CallbackTrainer. Notice that other than serializing / deserializing training state, there is no other “API”.
In a subclass you would register methods to handle specific events using the
handle_event
decorator defined above; for example@handle_event(Events.EPOCH_END) def epoch_end_stuff(self, trainer) -> None: ... @handle_event(Events.TRAINING_END) def training_end_stuff(self, trainer) -> None: ...
In this way, each callback can respond to whatever events it wants. Notice also that the methods take only the trainer as input and return nothing, which means that any shared state needs to belong to the trainer itself. (Each callback can of course maintain its own non-shared state.)
-
get_training_state
(self) → dict[source]¶ If this callback contains state that should be checkpointed for training, return it here (with a key that’s unique to this callback). If the state lives in a pytorch object with a state_dict method, this should return the output of state_dict(), not the object itself.
This default implementation suffices when there’s no state to checkpoint.
-
-
class
allennlp.training.callbacks.callback_handler.
CallbackHandler
(callbacks: Iterable[allennlp.training.callbacks.callback.Callback], state: allennlp.training.trainer_base.TrainerBase, verbose: bool = False)[source]¶ Bases:
object
A
CallbackHandler
owns zero or moreCallback``s, each of which is associated with some "event". It then exposes a ``fire_event
method, which calls each callback associated with that event ordered by their priorities.The callbacks take no parameters; instead they read from and write to this handler’s
state
, which should be a Trainer.- Parameters
- callbacks
Iterable[Callback]
The callbacks to be handled.
- state
TrainerBase
The trainer from which the callbacks will read state and to which the callbacks will write state.
- verbosebool, optional (default = False)
If true, will log every event -> callback. Please only use this for debugging purposes.
- callbacks
-
class
allennlp.training.callbacks.callback_handler.
EventHandler
(name, callback, handler, priority)[source]¶ Bases:
tuple
-
property
callback
¶ Alias for field number 1
-
property
handler
¶ Alias for field number 2
-
property
name
¶ Alias for field number 0
-
property
priority
¶ Alias for field number 3
-
property
-
class
allennlp.training.callbacks.events.
Events
[source]¶ Bases:
object
-
BACKWARD
= 'BACKWARD'¶
-
BATCH_END
= 'BATCH_END'¶
-
BATCH_START
= 'BATCH_START'¶
-
EPOCH_END
= 'EPOCH_END'¶
-
EPOCH_START
= 'EPOCH_START'¶
-
ERROR
= 'ERROR'¶
-
FORWARD
= 'FORWARD'¶
-
TRAINING_END
= 'TRAINING_END'¶
-
TRAINING_START
= 'TRAINING_START'¶
-
VALIDATE
= 'VALIDATE'¶
-
-
class
allennlp.training.callbacks.checkpoint.
Checkpoint
(checkpointer: allennlp.training.checkpointer.Checkpointer, model_save_interval: Optional[float] = None, state_dict_attrs: List[str] = None, other_attrs: List[str] = None)[source]¶ Bases:
allennlp.training.callbacks.callback.Callback
Callback that orchestrates checkpointing of your model and training state.
- Parameters
- checkpointer
Checkpointer
The checkpoint reader and writer to use.
- model_save_interval
float
, optional (default=None) If provided, then serialize models every
model_save_interval
seconds within single epochs. In all cases, models are also saved at the end of every epoch ifserialization_dir
is provided.- state_dict_attrs
List[str]
, optional (default = [‘optimizer’]) The attributes of the Trainer state whose .state_dict() should be persisted at each checkpoint.
- other_attrs
List[str]
, optional (default = [‘batch_num_total’]) The attributes of the Trainer state that should be persisted as-is at each checkpoint.
- checkpointer
-
classmethod
from_params
(params: allennlp.common.params.Params, serialization_dir: str) → 'Checkpoint'[source]¶ This is the automatic implementation of from_params. Any class that subclasses FromParams (or Registrable, which itself subclasses FromParams) gets this implementation for free. If you want your class to be instantiated from params in the “obvious” way – pop off parameters and hand them to your constructor with the same names – this provides that functionality.
If you need more complex logic in your from from_params method, you’ll have to implement your own method that overrides this one.
-
class
allennlp.training.callbacks.gradient_norm_and_clip.
GradientNormAndClip
(grad_norm: Optional[float] = None, grad_clipping: Optional[float] = None)[source]¶ Bases:
allennlp.training.callbacks.callback.Callback
Applies gradient norm and/or clipping.
- Parameters
- grad_normfloat, optional (default = None)
If provided, we rescale the gradients before the optimization step.
- grad_clippingfloat, optional (default = None)
If provided, we use this to clip gradients in our model.
-
class
allennlp.training.callbacks.update_learning_rate.
UpdateLearningRate
(learning_rate_scheduler: allennlp.training.learning_rate_schedulers.learning_rate_scheduler.LearningRateScheduler)[source]¶ Bases:
allennlp.training.callbacks.callback.Callback
Callback that runs the learning rate scheduler.
- Parameters
- learning_rate_scheduler
LearningRateScheduler
The scheduler to handler.
- learning_rate_scheduler
-
classmethod
from_params
(params: allennlp.common.params.Params, optimizer: torch.optim.optimizer.Optimizer) → 'UpdateLearningRate'[source]¶ This is the automatic implementation of from_params. Any class that subclasses FromParams (or Registrable, which itself subclasses FromParams) gets this implementation for free. If you want your class to be instantiated from params in the “obvious” way – pop off parameters and hand them to your constructor with the same names – this provides that functionality.
If you need more complex logic in your from from_params method, you’ll have to implement your own method that overrides this one.
-
get_training_state
(self) → dict[source]¶ We need to persist the learning_rate_scheduler state as training state.
-
class
allennlp.training.callbacks.log_to_tensorboard.
LogToTensorboard
(tensorboard: allennlp.training.tensorboard_writer.TensorboardWriter, log_batch_size_period: int = None)[source]¶ Bases:
allennlp.training.callbacks.callback.Callback
Callback that handles all Tensorboard logging.
- Parameters
- tensorboard
TensorboardWriter
The TensorboardWriter instance to write to.
- log_batch_size_periodint, optional (default: None)
If provided, we’ll log the average batch sizes to Tensorboard every this-many batches.
- tensorboard
-
classmethod
from_params
(serialization_dir: str, params: allennlp.common.params.Params) → 'LogToTensorboard'[source]¶ This is the automatic implementation of from_params. Any class that subclasses FromParams (or Registrable, which itself subclasses FromParams) gets this implementation for free. If you want your class to be instantiated from params in the “obvious” way – pop off parameters and hand them to your constructor with the same names – this provides that functionality.
If you need more complex logic in your from from_params method, you’ll have to implement your own method that overrides this one.
-
class
allennlp.training.callbacks.update_momentum.
UpdateMomentum
(momentum_scheduler: allennlp.training.momentum_schedulers.momentum_scheduler.MomentumScheduler)[source]¶ Bases:
allennlp.training.callbacks.callback.Callback
Callback that runs a Momentum Scheduler.
- Parameters
- momentum_scheduler
MomentumScheduler
The momentum scheduler to run.
- momentum_scheduler
-
classmethod
from_params
(params: allennlp.common.params.Params, optimizer: torch.optim.optimizer.Optimizer) → 'UpdateMomentum'[source]¶ This is the automatic implementation of from_params. Any class that subclasses FromParams (or Registrable, which itself subclasses FromParams) gets this implementation for free. If you want your class to be instantiated from params in the “obvious” way – pop off parameters and hand them to your constructor with the same names – this provides that functionality.
If you need more complex logic in your from from_params method, you’ll have to implement your own method that overrides this one.
-
get_training_state
(self) → dict[source]¶ If this callback contains state that should be checkpointed for training, return it here (with a key that’s unique to this callback). If the state lives in a pytorch object with a state_dict method, this should return the output of state_dict(), not the object itself.
This default implementation suffices when there’s no state to checkpoint.
-
class
allennlp.training.callbacks.post_to_url.
PostToUrl
(url: str, message: str = 'Your experiment has finished running!', key: str = 'text')[source]¶ Bases:
allennlp.training.callbacks.callback.Callback
Posts to a URL when training finishes. Useful if you want to, for example, create a Slack webhook.
- Parameters
- urlstr
The URL to post to.
- messagestr, optional (default = “Your experiment has finished running!”)
The message to post.
- keystr, optional (default = “text”)
The key to use in the JSON message blob.
-
class
allennlp.training.callbacks.track_metrics.
TrackMetrics
(patience: int = None, validation_metric: str = '-loss')[source]¶ Bases:
allennlp.training.callbacks.callback.Callback
Callback that handles tracking of metrics and (potentially) early stopping.
- Parameters
- patienceint, optional (default = None)
If a positive number is provided, training will stop when the supplied validation_metric has not improved in this many epochs.
- validation_metricstr, optional (default = “-loss”)
The metric to use for early stopping. The initial +/- indicates whether we expect the metric to increase or decrease during training.
-
get_training_state
(self) → dict[source]¶ If this callback contains state that should be checkpointed for training, return it here (with a key that’s unique to this callback). If the state lives in a pytorch object with a state_dict method, this should return the output of state_dict(), not the object itself.
This default implementation suffices when there’s no state to checkpoint.
-
class
allennlp.training.callbacks.validate.
Validate
(validation_data: Iterable[allennlp.data.instance.Instance], validation_iterator: allennlp.data.iterators.data_iterator.DataIterator)[source]¶ Bases:
allennlp.training.callbacks.callback.Callback
Evaluates the trainer’s
Model
using the provided validation dataset. Uses the results to populate trainer.val_metrics.- Parameters
- validation_data
Iterable[Instance]
The instances in the validation dataset.
- validation_iterator
DataIterator
The iterator to use in the evaluation.
- validation_data
-
class
allennlp.training.callbacks.update_moving_average.
UpdateMovingAverage
(moving_average: allennlp.training.moving_average.MovingAverage)[source]¶ Bases:
allennlp.training.callbacks.callback.Callback
Callback that orchestrates checkpointing of your model and training state.
- Parameters
- moving_aveage
MovingAverage
The MovingAverage object to update.
- moving_aveage
-
classmethod
from_params
(params: allennlp.common.params.Params, model: allennlp.models.model.Model) → 'UpdateMovingAverage'[source]¶ This is the automatic implementation of from_params. Any class that subclasses FromParams (or Registrable, which itself subclasses FromParams) gets this implementation for free. If you want your class to be instantiated from params in the “obvious” way – pop off parameters and hand them to your constructor with the same names – this provides that functionality.
If you need more complex logic in your from from_params method, you’ll have to implement your own method that overrides this one.