Early stops the training if validation loss doesn't improve after a given patience and save best model
| 98 | return train_loader, val_loader |
| 99 | |
| 100 | class MonitorBestModelEarlyStopping: |
| 101 | """Early stops the training if validation loss doesn't improve after a given patience and save best model """ |
| 102 | def __init__(self, patience=15, min_epochs=20, saving_checkpoint=True): |
| 103 | """ |
| 104 | Args: |
| 105 | patience (int): How long to wait after last time validation loss improved. |
| 106 | Default: 20 |
| 107 | min_epochs (int): Earliest epoch possible for stopping |
| 108 | verbose (bool): If True, prints a message for each validation loss improvement. |
| 109 | Default: False |
| 110 | """ |
| 111 | #self.warmup = warmup |
| 112 | self.patience = patience |
| 113 | self.min_epochs = min_epochs |
| 114 | self.counter = 0 |
| 115 | self.early_stop = False |
| 116 | |
| 117 | self.eval_loss_min = np.Inf |
| 118 | self.best_loss_score = None |
| 119 | self.best_epoch_loss = None |
| 120 | |
| 121 | self.best_CI_score = 0.0 |
| 122 | self.best_metrics_score = None |
| 123 | self.best_epoch_CI = None |
| 124 | |
| 125 | self.saving_checkpoint = saving_checkpoint |
| 126 | |
| 127 | def __call__(self, epoch, eval_loss, eval_cindex, eval_other_metrics, model, log_dir): |
| 128 | |
| 129 | loss_score = -eval_loss |
| 130 | CI_score = eval_cindex |
| 131 | metrics_score = eval_other_metrics |
| 132 | |
| 133 | # Save model at epoch 0 and starts monitoring. |
| 134 | if self.best_loss_score is None: |
| 135 | self._update_loss_scores(loss_score, eval_loss, epoch) |
| 136 | self._update_metrics_scores(CI_score, metrics_score, epoch) |
| 137 | #self.save_checkpoint(model, log_dir, epoch) |
| 138 | |
| 139 | # Eval loss starts increasing. Recommend running early stopping on the loss. |
| 140 | elif loss_score < self.best_loss_score: |
| 141 | self.counter += 1 |
| 142 | print(f'Evaluation loss does not decrease : Starting Early stopping counter {self.counter} out of {self.patience}') |
| 143 | if self.counter >= self.patience and epoch > self.min_epochs: |
| 144 | self.early_stop = True |
| 145 | # Eval loss keeps decreasing. |
| 146 | else: |
| 147 | print(f'Epoch {epoch} validation loss decreased ({self.eval_loss_min:.6f} --> {eval_loss:.6f})') |
| 148 | self._update_loss_scores(loss_score, eval_loss, epoch) |
| 149 | #self.save_checkpoint(model, log_dir, epoch) |
| 150 | self.counter = 0 |
| 151 | |
| 152 | # We may have a tiny lag between min loss and the best C-index. With the patience and early stop, it is fine but better to save based on highest C-index too. |
| 153 | if CI_score > self.best_CI_score: |
| 154 | self._update_metrics_scores(CI_score, metrics_score, epoch) |
| 155 | #self.save_checkpoint(model, log_dir, epoch) |
| 156 | |
| 157 | def save_checkpoint(self, model, log_dir, epoch): |
no outgoing calls
no test coverage detected