allennlp.training.trainer

class allennlp.training.trainer.Trainer(model: allennlp.models.model.Model, optimizer: torch.optim.optimizer.Optimizer, iterator: allennlp.data.iterators.data_iterator.DataIterator, train_dataset: typing.Iterable[allennlp.data.instance.Instance], validation_dataset: typing.Union[typing.Iterable[allennlp.data.instance.Instance], NoneType] = None, patience: typing.Union[int, NoneType] = None, validation_metric: str = '-loss', validation_iterator: allennlp.data.iterators.data_iterator.DataIterator = None, shuffle: bool = True, num_epochs: int = 20, serialization_dir: typing.Union[str, NoneType] = None, num_serialized_models_to_keep: int = 20, keep_serialized_model_every_num_seconds: int = None, model_save_interval: float = None, cuda_device: typing.Union[int, typing.List] = -1, grad_norm: typing.Union[float, NoneType] = None, grad_clipping: typing.Union[float, NoneType] = None, learning_rate_scheduler: typing.Union[allennlp.training.learning_rate_schedulers.learning_rate_scheduler.LearningRateScheduler, NoneType] = None, momentum_scheduler: typing.Union[allennlp.training.momentum_schedulers.momentum_scheduler.MomentumScheduler, NoneType] = None, summary_interval: int = 100, histogram_interval: int = None, should_log_parameter_statistics: bool = True, should_log_learning_rate: bool = False, log_batch_size_period: typing.Union[int, NoneType] = None, moving_average: typing.Union[allennlp.training.moving_average.MovingAverage, NoneType] = None) → None[source]

Bases: allennlp.training.trainer_base.TrainerBase

batch_loss(batch_group: typing.List[typing.Dict[str, typing.Union[torch.Tensor, typing.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.

classmethod from_params(model: allennlp.models.model.Model, serialization_dir: str, iterator: allennlp.data.iterators.data_iterator.DataIterator, train_data: typing.Iterable[allennlp.data.instance.Instance], validation_data: typing.Union[typing.Iterable[allennlp.data.instance.Instance], NoneType], params: allennlp.common.params.Params, validation_iterator: allennlp.data.iterators.data_iterator.DataIterator = None) → allennlp.training.trainer.Trainer[source]
rescale_gradients() → typing.Union[float, NoneType][source]
train() → typing.Dict[str, typing.Any][source]

Trains the supplied model with the supplied parameters.

class allennlp.training.trainer.TrainerPieces[source]

Bases: tuple

We would like to avoid having complex instantiation logic taking place in Trainer.from_params. This helper class has a from_params that instantiates a model, loads train (and possibly validation and test) datasets, constructs a Vocabulary, creates data iterators, and handles a little bit of bookkeeping. If you’re creating your own alternative training regime you might be able to use this.

static from_params(params: allennlp.common.params.Params, serialization_dir: str, recover: bool = False) → allennlp.training.trainer.TrainerPieces[source]
iterator

Alias for field number 1

model

Alias for field number 0

params

Alias for field number 6

test_dataset

Alias for field number 4

train_dataset

Alias for field number 2

validation_dataset

Alias for field number 3

validation_iterator

Alias for field number 5