Demo for using the checkpoint callback. Custom logic for handling output is usually required and users are encouraged to define their own callback for checkpointing operations. The builtin one can be used as a starting point.
()
| 87 | |
| 88 | |
| 89 | def check_point_callback() -> None: |
| 90 | """Demo for using the checkpoint callback. Custom logic for handling output is |
| 91 | usually required and users are encouraged to define their own callback for |
| 92 | checkpointing operations. The builtin one can be used as a starting point. |
| 93 | |
| 94 | """ |
| 95 | # Only for demo, set a larger value (like 100) in practice as checkpointing is quite |
| 96 | # slow. |
| 97 | rounds = 2 |
| 98 | |
| 99 | def check(as_pickle: bool) -> None: |
| 100 | for i in range(0, 10, rounds): |
| 101 | if i == 0: |
| 102 | continue |
| 103 | if as_pickle: |
| 104 | path = os.path.join(tmpdir, "model_" + str(i) + ".pkl") |
| 105 | else: |
| 106 | path = os.path.join( |
| 107 | tmpdir, |
| 108 | f"model_{i}.{xgb.callback.TrainingCheckPoint.default_format}", |
| 109 | ) |
| 110 | assert os.path.exists(path) |
| 111 | |
| 112 | X, y = load_breast_cancer(return_X_y=True) |
| 113 | m = xgb.DMatrix(X, y) |
| 114 | # Check point to a temporary directory for demo |
| 115 | with tempfile.TemporaryDirectory() as tmpdir: |
| 116 | # Use callback class from xgboost.callback |
| 117 | # Feel free to subclass/customize it to suit your need. |
| 118 | check_point = xgb.callback.TrainingCheckPoint( |
| 119 | directory=tmpdir, interval=rounds, name="model" |
| 120 | ) |
| 121 | xgb.train( |
| 122 | {"objective": "binary:logistic"}, |
| 123 | m, |
| 124 | num_boost_round=10, |
| 125 | verbose_eval=False, |
| 126 | callbacks=[check_point], |
| 127 | ) |
| 128 | check(False) |
| 129 | |
| 130 | # This version of checkpoint saves everything including parameters and |
| 131 | # model. See: doc/tutorials/saving_model.rst |
| 132 | check_point = xgb.callback.TrainingCheckPoint( |
| 133 | directory=tmpdir, interval=rounds, as_pickle=True, name="model" |
| 134 | ) |
| 135 | xgb.train( |
| 136 | {"objective": "binary:logistic"}, |
| 137 | m, |
| 138 | num_boost_round=10, |
| 139 | verbose_eval=False, |
| 140 | callbacks=[check_point], |
| 141 | ) |
| 142 | check(True) |
| 143 | |
| 144 | |
| 145 | if __name__ == "__main__": |
no test coverage detected