diff --git a/freqtrade/freqai/torch/PyTorchModelTrainer.py b/freqtrade/freqai/torch/PyTorchModelTrainer.py index 1c12f15fd..223f786e4 100644 --- a/freqtrade/freqai/torch/PyTorchModelTrainer.py +++ b/freqtrade/freqai/torch/PyTorchModelTrainer.py @@ -63,6 +63,11 @@ class PyTorchModelTrainer(PyTorchTrainerInterface): self.tb_logger = tb_logger self.test_batch_counter = 0 + # Early stopping parameters + self.early_stopping_patience: int = kwargs.get("early_stopping_patience", 0) + self.best_val_loss: float = float("inf") + self.patience_counter: int = 0 + def fit(self, data_dictionary: dict[str, pd.DataFrame], splits: list[str]): """ :param data_dictionary: the dictionary constructed by DataHandler to hold @@ -99,15 +104,40 @@ class PyTorchModelTrainer(PyTorchTrainerInterface): # evaluation if "test" in splits: - self.estimate_loss(data_loaders_dictionary, "test") + val_loss = self.estimate_loss(data_loaders_dictionary, "test") + + # Early stopping check + if self.early_stopping_patience > 0 and val_loss is not None: + if val_loss < self.best_val_loss: + self.best_val_loss = val_loss + self.patience_counter = 0 + else: + self.patience_counter += 1 + if self.patience_counter >= self.early_stopping_patience: + logger.info( + f"Early stopping triggered after {self.patience_counter} " + f"epochs without improvement. " + f"Best val_loss: {self.best_val_loss:.6f}" + ) + break @torch.no_grad() def estimate_loss( self, data_loader_dictionary: dict[str, DataLoader], split: str, - ) -> None: + ) -> float | None: + """ + Estimate loss on a data split. + + :param data_loader_dictionary: dictionary of data loaders. + :param split: split to estimate loss on (e.g. "test"). + :return: average loss over all batches, or None if no batches. + """ self.model.eval() + total_loss = 0.0 + num_batches = 0 + for _, batch_data in enumerate(data_loader_dictionary[split]): xb, yb = batch_data xb = xb.to(self.device) @@ -115,11 +145,17 @@ class PyTorchModelTrainer(PyTorchTrainerInterface): yb_pred = self.model(xb) loss = self.criterion(yb_pred, yb) + total_loss += loss.item() + num_batches += 1 self.tb_logger.log_scalar(f"{split}_loss", loss.item(), self.test_batch_counter) self.test_batch_counter += 1 self.model.train() + if num_batches > 0: + return total_loss / num_batches + return None + def create_data_loaders_dictionary( self, data_dictionary: dict[str, pd.DataFrame], splits: list[str] ) -> dict[str, DataLoader]: