MCPcopy Create free account
hub / github.com/dmlc/xgboost / check_point_callback

Function check_point_callback

demo/guide-python/callbacks.py:89–142  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

87
88
89def 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
145if __name__ == "__main__":

Callers 1

callbacks.pyFile · 0.85

Calls 4

TemporaryDirectoryMethod · 0.80
checkFunction · 0.70
DMatrixMethod · 0.45
trainMethod · 0.45

Tested by

no test coverage detected