src/models/utils.py
def _save_best(self, model, optimizer, metric, current_epoch, loss):
        """Save best performing checkpoint observed so far."""
        # Default value
        is_best = False

        if self.mode == 'min':
            if metric['testing'] <= self.best_metric:
                is_best = True
        else:
            if metric['testing'] >= self.best_metric:
                is_best = True

        # Update the best and save the model
        if is_best:
            self.best_metric = metric['testing']
            torch.save(
                {
                    "model_weights": self._model_state_dict(model),
                    "optimizer": optimizer.state_dict(),
                    "metric": metric,
                    "loss": loss,
                    "epoch": current_epoch
                }, self.ckpt_dirpath / "checkpoint_best.pth")
        return None
Save best performing checkpoint observed so far.
Save best performing checkpoint observed so far.
Save best performing checkpoint observed so far .
def _save_best(self, model, optimizer, metric, current_epoch, loss):
        is_best = False
        if self.mode == 'min':
            if metric['testing'] <= self.best_metric:
                is_best = True
        else:
            if metric['testing'] >= self.best_metric:
                is_best = True
        if is_best:
            self.best_metric = metric['testing']
            torch.save(
                {
                    "model_weights": self._model_state_dict(model),
                    "optimizer": optimizer.state_dict(),
                    "metric": metric,
                    "loss": loss,
                    "epoch": current_epoch
                }, self.ckpt_dirpath / "checkpoint_best.pth")
        return None
def _save_best ( self , model , optimizer , metric , current_epoch , loss ) : is_best = False if self . mode == 'min' : if metric [ 'testing' ] <= self . best_metric : is_best = True else : if metric [ 'testing' ] >= self . best_metric : is_best = True if is_best : self . best_metric = metric [ 'testing' ] torch . save ( { "model_weights" : self . _model_state_dict ( model ) , "optimizer" : optimizer . state_dict ( ) , "metric" : metric , "loss" : loss , "epoch" : current_epoch } , self . ckpt_dirpath / "checkpoint_best.pth" ) return None
Save best performing checkpoint observed so far.
Save best performing checkpoint observed so far .
"""Save best performing checkpoint observed so far.""" # Default value # Update the best and save the model
self
model
optimizer
metric
current_epoch
loss