MCPcopy
hub / github.com/shenweichen/DeepCTR-Torch / test_early_stopping_paths

Function test_early_stopping_paths

tests/callbacks_test.py:76–116  ·  view source on GitHub ↗
(capsys)

Source from the content-addressed store, hash-verified

74
75
76def test_early_stopping_paths(capsys):
77 with pytest.raises(ValueError):
78 EarlyStopping(mode="unsupported")
79
80 # Cover baseline/min branch + _is_improvement(min)
81 es_min = EarlyStopping(monitor="val_loss", mode="min", baseline=0.5)
82 es_min.on_train_begin()
83 assert es_min.best == 0.5
84 assert es_min._is_improvement(0.4, es_min.best)
85
86 # Cover auto/max branch + restore-best-weights path
87 model = TinyModel()
88 with torch.no_grad():
89 model.linear.weight.fill_(1.0)
90
91 es = EarlyStopping(
92 monitor="val_auc",
93 mode="auto",
94 patience=1,
95 verbose=1,
96 restore_best_weights=True,
97 )
98 es.set_model(model)
99 es.on_train_begin()
100
101 # Missing metric should be ignored.
102 es.on_epoch_end(0, {})
103
104 # Improvement stores best weights.
105 es.on_epoch_end(0, {"val_auc": 0.9})
106 with torch.no_grad():
107 model.linear.weight.fill_(2.0)
108
109 # No improvement triggers early stop and restores best weights.
110 es.on_epoch_end(1, {"val_auc": 0.8})
111 es.on_train_end()
112 out = capsys.readouterr().out
113
114 assert model.stop_training is True
115 assert torch.allclose(model.linear.weight, torch.tensor([[1.0]]))
116 assert "early stopping" in out
117
118
119def test_model_checkpoint_paths(tmp_path, capsys):

Callers

nothing calls this directly

Calls 7

on_train_beginMethod · 0.95
_is_improvementMethod · 0.95
on_epoch_endMethod · 0.95
on_train_endMethod · 0.95
EarlyStoppingClass · 0.90
TinyModelClass · 0.85
set_modelMethod · 0.45

Tested by

no test coverage detected