allennlp.training.trainer

A Trainer is responsible for training a Model.

Typically you might create a configuration file specifying the model and training parameters and then use train rather than instantiating a Trainer yourself.

class allennlp.training.trainer.TensorboardWriter(train_log: tensorboardX.writer.SummaryWriter = None, validation_log: tensorboardX.writer.SummaryWriter = None) → None[source]

Bases: object

Wraps a pair of SummaryWriter instances but is a no-op if they’re None. Allows Tensorboard logging without always checking for Nones first.

add_train_histogram(name: str, values: torch.FloatTensor, global_step: int) → None[source]
add_train_scalar(name: str, value: float, global_step: int) → None[source]
add_validation_scalar(name: str, value: float, global_step: int) → None[source]
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: int = 2, validation_metric: str = '-loss', num_epochs: int = 20, serialization_dir: typing.Union[str, NoneType] = None, num_serialized_models_to_keep: int = None, 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[torch.optim.lr_scheduler._LRScheduler, NoneType] = None, summary_interval: int = 100, histogram_interval: int = None) → None[source]

Bases: object

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) → allennlp.training.trainer.Trainer[source]
train() → typing.Dict[str, typing.Any][source]

Trains the supplied model with the supplied parameters.

allennlp.training.trainer.is_sparse(tensor)[source]
allennlp.training.trainer.sparse_clip_norm(parameters, max_norm, norm_type=2) → float[source]

Clips gradient norm of an iterable of parameters.

The norm is computed over all gradients together, as if they were concatenated into a single vector. Gradients are modified in-place. Supports sparse gradients.

Parameters:

parameters : (Iterable[Variable])

An iterable of Variables that will have gradients normalized.

max_norm : float

The max norm of the gradients.

norm_type : float

The type of the used p-norm. Can be 'inf' for infinity norm.

Returns:

Total norm of the parameters (viewed as a single vector).

allennlp.training.trainer.str_to_time(time_str: str) → datetime.datetime[source]

Convert human readable string to datetime.datetime.

allennlp.training.trainer.time_to_str(timestamp: int) → str[source]

Convert seconds past Epoch to human readable string.