Stoppers

Early stoppers.

The following code will create a scenario in which training will stop (quite) early when training pykeen.models.TransE on the pykeen.datasets.Nations dataset.

>>> from pykeen.pipeline import pipeline
>>> pipeline_result = pipeline(
...     dataset='nations',
...     model='transe',
...     model_kwargs=dict(embedding_dim=20, scoring_fct_norm=1),
...     optimizer='SGD',
...     optimizer_kwargs=dict(lr=0.01),
...     loss='marginranking',
...     loss_kwargs=dict(margin=1),
...     training_loop='slcwa',
...     training_kwargs=dict(num_epochs=100, batch_size=128),
...     negative_sampler='basic',
...     negative_sampler_kwargs=dict(num_negs_per_pos=1),
...     evaluator_kwargs=dict(filtered=True),
...     evaluation_kwargs=dict(batch_size=128),
...     stopper='early',
...     stopper_kwargs=dict(frequency=5, patience=2, delta=0.002),
... )
class pykeen.stoppers.Stopper(*args, **kwargs)[source]

A harness for stopping training.

should_evaluate(epoch)[source]

Check if the stopper should be evaluated on the given epoch.

Return type

bool

abstract should_stop()[source]

Validate on validation set and check for termination condition.

Return type

bool

class pykeen.stoppers.NopStopper(*args, **kwargs)[source]

A stopper that does nothing.

should_evaluate(epoch)[source]

Return false; should never evaluate.

Return type

bool

should_stop()[source]

Return false; should never stop.

Return type

bool

class pykeen.stoppers.EarlyStopper(model, evaluator, evaluation_triples_factory, evaluation_batch_size=None, evaluation_slice_size=None, frequency=10, patience=2, metric='hits_at_k', delta=0.005, results=<factory>, number_evaluations=0, larger_is_better=True, improvement_criterion=None, result_tracker=None, continue_callbacks=<factory>, stopped_callbacks=<factory>, stopped=False)[source]

A harness for early stopping.

buffer: numpy.ndarray

A ring buffer to store the recent results

continue_callbacks: List[Callable[[pykeen.stoppers.stopper.Stopper, Union[int, float]], None]]

Callbacks when training gets continued

delta: float = 0.005

The minimum improvement between two iterations

evaluation_batch_size: Optional[int] = None

Size of the evaluation batches

evaluation_slice_size: Optional[int] = None

Slice size of the evaluation batches

evaluation_triples_factory: Optional[pykeen.triples.triples_factory.TriplesFactory]

The triples to use for evaluation

evaluator: pykeen.evaluation.evaluator.Evaluator

The evaluator

frequency: int = 10

The number of epochs after which the model is evaluated on validation set

get_summary_dict()[source]

Get a summary dict.

Return type

Mapping[str, Any]

improvement_criterion: Callable[[numpy.ndarray, float, float], bool] = None

The criterion. Set in the constructor based on larger_is_better

larger_is_better: bool = True

Whether a larger value is better, or a smaller

metric: str = 'hits_at_k'

The name of the metric to use

model: pykeen.models.base.Model

The model

number_evaluations: int = 0

A counter for the ring buffer

property number_results

Count the number of results stored in the early stopper.

Return type

int

patience: int = 2

The number of iterations (one iteration can correspond to various epochs) with no improvement after which training will be stopped.

result_tracker: Optional[pykeen.utils.ResultTracker] = None

The result tracker

results: List[float]

The metric results from all evaluations

should_evaluate(epoch)[source]

Decide if evaluation should be done based on the current epoch and the internal frequency.

Return type

bool

should_stop()[source]

Evaluate on a metric and compare to past evaluations to decide if training should stop.

Return type

bool

stopped: bool = False

Did the stopper ever decide to stop?

stopped_callbacks: List[Callable[[pykeen.stoppers.stopper.Stopper, Union[int, float]], None]]

Callbacks when training is stopped early