Demo for training continuation
:
"""Basic training continuation."""
X
,
y
=
(
return_X_y
=
True
)
clf
=
xgboost
.
XGBClassifier
(
n_estimators
=
128
,
eval_metric
=
"logloss"
)
clf
.
fit
(
X
,
y
,
eval_set
=
[(
X
,
y
)])
print
(
"Total boosted rounds:"
,
clf
.
get_booster
()
.
num_boosted_rounds
())
clf
=
xgboost
.
XGBClassifier
(
n_estimators
=
32
,
eval_metric
=
"logloss"
)
clf
.
fit
(
X
,
y
,
eval_set
=
[(
X
,
y
)])
assert
clf
.
get_booster
()
.
num_boosted_rounds
()
==
32
if
use_pickle
:
path
=
(
tmpdir
,
"model-first-32.pkl"
)
with
open
(
path
,
"wb"
)
as
fd
:
(
clf
,
fd
)
with
open
(
path
,
"rb"
)
as
fd
:
loaded
=
(
fd
)
else
:
path
=
(
tmpdir
,
"model-first-32.json"
)
clf
.
save_model
(
path
)
loaded
=
xgboost
.
XGBClassifier
()
loaded
.
load_model
(
path
)
clf
=
xgboost
.
XGBClassifier
(
n_estimators
=
128
-
32
,
eval_metric
=
"logloss"
)
clf
.
fit
(
X
,
y
,
eval_set
=
[(
X
,
y
)],
xgb_model
=
loaded
)
print
(
"Total boosted rounds:"
,
clf
.
get_booster
()
.
num_boosted_rounds
())
assert
clf
.
get_booster
()
.
num_boosted_rounds
()
==
128
def
training_continuation_early_stop
(
tmpdir
:
str
,
use_pickle
:
bool
)
->
None
:
"""Training continuation with early stopping."""
early_stopping_rounds
=
5
early_stop
=
xgboost
.
callback
.
EarlyStopping
(
rounds
=
early_stopping_rounds
,
save_best
=
True
)
n_estimators
=
512
X
,
y
=
(
return_X_y
=
True
)
clf
=
xgboost
.
XGBClassifier
(
n_estimators
=
n_estimators
,
eval_metric
=
"logloss"
,
callbacks
=
[
early_stop
]
)
clf
.
fit
(
X
,
y
,
eval_set
=
[(
X
,
y
)])
print
(
"Total boosted rounds:"
,
clf
.
get_booster
()
.
num_boosted_rounds
())
best
=
clf
.
best_iteration
clf
=
xgboost
.
XGBClassifier
(
n_estimators
=
128
,
eval_metric
=
"logloss"
,
callbacks
=
[
early_stop
]
)
early_stop
=
xgboost
.
callback
.
EarlyStopping
(
rounds
=
early_stopping_rounds
,
save_best
=
True
)
clf
.
set_params
(
callbacks
=
[
early_stop
])
clf
.
fit
(
X
,
y
,
eval_set
=
[(
X
,
y
)])
assert
clf
.
get_booster
()
.
num_boosted_rounds
()
==
128
if
use_pickle
:
path
=
(
tmpdir
,
"model-first-128.pkl"
)
with
open
(
path
,
"wb"
)
as
fd
:
(
clf
,
fd
)
with
open
(
path
,
"rb"
)
as
fd
:
loaded
=
(
fd
)
else
:
path
=
(
tmpdir
,
"model-first-128.json"
)
clf
.
save_model
(
path
)
loaded
=
xgboost
.
XGBClassifier
()
loaded
.
load_model
(
path
)
early_stop
=
xgboost
.
callback
.
EarlyStopping
(
rounds
=
early_stopping_rounds
,
save_best
=
True
)
clf
=
xgboost
.
XGBClassifier
(
n_estimators
=
n_estimators
-
128
,
eval_metric
=
"logloss"
,
callbacks
=
[
early_stop
]
)
clf
.
fit
(
X
,
y
,
eval_set
=
[(
X
,
y
)],
xgb_model
=
loaded
,
)
print
(
"Total boosted rounds:"
,
clf
.
get_booster
()
.
num_boosted_rounds
())
assert
clf
.
best_iteration
==
best
if
__name__
==
"__main__"
:
with
()
as
tmpdir
:
training_continuation_early_stop
(
tmpdir
,
False
)
training_continuation_early_stop
(
tmpdir
,
True
)
training_continuation
(
tmpdir
,
True
)
training_continuation
(
tmpdir
,
False
)