# Early stopping class class EarlyStopping: def __init__(self, patience=5, delta=0): self.patience = patience self.delta = delta self.counter = 0 self.best_score = None self.early_stop = False def __call__(self, val_loss): score = -val_loss if self.best_score is None: self.best_score = score elif score < self.best_score + self.delta: self.counter += 1 if self.counter >= self.patience: self.early_stop = True else: self.best_score = score self.counter = 0