Skip to content

Conversation

@Ayush10
Copy link

@Ayush10 Ayush10 commented Feb 1, 2026

Summary

  • Initializes best_param with the model's initial state dict before the training loop in 21 PyTorch model files
  • Prevents UnboundLocalError when no epoch improves the validation score (e.g. due to NaN scores from missing data or aggressive early stopping)
  • Follows the pattern already established in pytorch_gru.py, which was the only model file that handled this correctly

Root Cause

best_param was assigned only inside the if val_score > best_score: branch but used unconditionally after the loop via model.load_state_dict(best_param). When val_score is NaN (common with missing data), the comparison val_score > best_score is always False, so best_param is 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

  • Verified each file has exactly 1 line added with the correct model variable name
  • Verified pytorch_gru.py (already fixed), pytorch_general_nn.py (uses step==0 guard), pytorch_tra.py (different pattern), and pytorch_tcts.py (loads from file) were not modified
  • CI pipeline passes

Fixes #1794

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnboundLocalError: local variable 'best_param' referenced before assignment

1 participant