Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup save_best_model #137

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions trainer/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,23 @@ def save_best_model(
epoch,
out_path,
keep_all_best=False,
keep_after=10000,
keep_after=0,
save_func=None,
**kwargs,
):
use_eval_loss = current_loss["eval_loss"] is not None and best_loss["eval_loss"] is not None
if (use_eval_loss and current_loss["eval_loss"] < best_loss["eval_loss"]) or (
not use_eval_loss and current_loss["train_loss"] < best_loss["train_loss"]
):
if isinstance(current_loss, dict):
use_eval_loss = current_loss["eval_loss"] is not None and best_loss["eval_loss"] is not None
is_save_model = (use_eval_loss and current_loss["eval_loss"] < best_loss["eval_loss"]) or (
not use_eval_loss and current_loss["train_loss"] < best_loss["train_loss"]
)
else:
is_save_model = current_loss < best_loss

if isinstance(keep_after, (int, float)):
keep_after = int(keep_after)
is_save_model = is_save_model and current_step > keep_after

if is_save_model:
best_model_name = f"best_model_{current_step}.pth"
checkpoint_path = os.path.join(out_path, best_model_name)
logger.info(" > BEST MODEL : %s", checkpoint_path)
Expand Down