fix: initialize best_param before training loop to prevent UnboundLocalError #2121
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