fix: initialize best_param before training loop to prevent UnboundLocalError#2121
Open
Ayush10 wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: initialize best_param before training loop to prevent UnboundLocalError#2121Ayush10 wants to merge 1 commit intomicrosoft:mainfrom
Ayush10 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…alError When validation score never improves (e.g. due to NaN scores from missing data), best_param was never assigned inside the if branch, causing UnboundLocalError on model.load_state_dict(best_param) after the loop. Initialize best_param with the model's initial state before the training loop, following the pattern already used in pytorch_gru.py. This ensures the model falls back to initial weights if no epoch improves the score. Fixes microsoft#1794
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
best_paramwith the model's initial state dict before the training loop in 21 PyTorch model filesUnboundLocalErrorwhen no epoch improves the validation score (e.g. due to NaN scores from missing data or aggressive early stopping)pytorch_gru.py, which was the only model file that handled this correctlyRoot Cause
best_paramwas assigned only inside theif val_score > best_score:branch but used unconditionally after the loop viamodel.load_state_dict(best_param). Whenval_scoreis NaN (common with missing data), the comparisonval_score > best_scoreis always False, sobest_paramis never assigned.Files Changed (21 files, 1 line each)
pytorch_adarnn, pytorch_add, pytorch_alstm, pytorch_alstm_ts, pytorch_gats, pytorch_gats_ts, pytorch_gru_ts, pytorch_hist, pytorch_igmtf, pytorch_krnn, pytorch_localformer, pytorch_localformer_ts, pytorch_lstm, pytorch_lstm_ts, pytorch_sandwich, pytorch_sfm, pytorch_tabnet, pytorch_tcn, pytorch_tcn_ts, pytorch_transformer, pytorch_transformer_ts
Test plan
pytorch_gru.py(already fixed),pytorch_general_nn.py(uses step==0 guard),pytorch_tra.py(different pattern), andpytorch_tcts.py(loads from file) were not modifiedFixes #1794