allennlp.training.callback_trainer

The CallbackTrainer should be considered experimental code. Its API may change at any time, and it may disappear altogether.

class allennlp.training.callback_trainer.CallbackTrainer(model: allennlp.models.model.Model, training_data: Iterable[allennlp.data.instance.Instance], iterator: allennlp.data.iterators.data_iterator.DataIterator, optimizer: torch.optim.optimizer.Optimizer, num_epochs: int = 20, shuffle: bool = True, serialization_dir: Optional[str] = None, cuda_device: Union[int, List] = -1, callbacks: List[allennlp.training.callbacks.callback.Callback] = None)[source]

Bases: allennlp.training.trainer_base.TrainerBase

batch_loss(self, batch_group:List[Dict[str, Union[torch.Tensor, Dict[str, torch.Tensor]]]], for_training:bool) → torch.Tensor[source]

Does a forward pass on the given batches and returns the loss value in the result. If for_training is True also applies regularization penalty.

This is a method on the trainer so that it can be used both in training and validation (which are handled separately).

classmethod from_params(params:allennlp.common.params.Params, serialization_dir:str, recover:bool=False, cache_directory:str=None, cache_prefix:str=None) → 'CallbackTrainer'[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.

generate_training_batches(self)[source]

Generates one epoch worth of training data. Stores it in trainer instance variables so that callbacks can access it.

train(self) → Dict[str, Any][source]

Trains the supplied model with the supplied parameters. Fires off the events TRAINING_START and TRAINING END, and repeatedly calls self.train_one_epoch().

train_one_batch_group(self, batch_group:List[Dict[str, Union[torch.Tensor, Dict[str, torch.Tensor]]]]) → str[source]

Handles the training for a single batch group. Fires off the events BATCH_START, FORWARD, BACKWARD, and BATCH_END.

train_one_epoch(self) → None[source]

Trains the model for a single epoch. Fires off the events EPOCH_START and EPOCH_END, and repeatedly calls self.train_one_batch_group().

allennlp.training.callback_trainer.handle_errors(method)[source]