class_docstr: Test early stopping on a real-world use case of training TransE with Adam.
class_docstr_tok: ["Test","early","stopping","on","a","real-world","use","case","of","training","TransE","with","Adam","."]
docstr: Tests early stopping.
docstr_tok: ["Tests","early","stopping","."]
code: def test_early_stopping(self):
      """Tests early stopping."""
      # Set automatic_memory_optimization to false during testing
      nations = Nations()
      model: Model = TransE(triples_factory=nations.training)
      evaluator = RankBasedEvaluator(
          automatic_memory_optimization=False
      )
      stopper = EarlyStopper(
          model=model,
          evaluator=evaluator,
          training_triples_factory=nations.training,
          evaluation_triples_factory=nations.validation,
          patience=self.patience,
          relative_delta=self.relative_delta,
          metric="mean_rank",
      )
      training_loop = SLCWATrainingLoop(
          model=model,
          triples_factory=nations.training,
          optimizer=Adam(params=model.get_grad_params()),
          automatic_memory_optimization=False,
      )
      losses = training_loop.train(
          triples_factory=nations.training,
          num_epochs=self.max_num_epochs,
          batch_size=self.batch_size,
          stopper=stopper,
          use_tqdm=False,
      )
      self.assertEqual(
          stopper.number_results, len(losses) // stopper.frequency
      )
      self.assertEqual(
          stopper.best_epoch,
          self.stop_epoch - self.patience * stopper.frequency,
      )
      self.assertEqual(
          self.stop_epoch,
          len(losses),
          msg="Did not stop early like it should have",
      )
  
imports: import unittest
  from typing import List
  import numpy
  import pytest
  import torch
  import unittest_templates
  from torch.optim import Adam
  from pykeen.datasets import Nations
  from pykeen.evaluation import RankBasedEvaluator
  from pykeen.models import Model, TransE
  from pykeen.stoppers.early_stopping import EarlyStopper, EarlyStoppingLogic, is_improvement
  from pykeen.training import SLCWATrainingLoop
  from tests import cases
label_desc: Use image node_id 2 for calling the TestEarlyStopperRealWorld obj's underlying member method code with example usage: obj.test_early_stopping() without return types