Demo for using and defining callback functions
np
from
matplotlib
import
pyplot
as
plt
from
sklearn.datasets
import
from
sklearn.model_selection
import
import
xgboost
as
xgb
class
Plotting
(
xgb
.
callback
.
TrainingCallback
):
"""Plot evaluation result during training. Only for demonstration purpose as it's
quite slow to draw using matplotlib.
"""
def
__init__
(
self
,
rounds
:
int
)
->
None
:
self
.
fig
=
plt
.
figure
()
self
.
ax
=
self
.
fig
.
add_subplot
(
111
)
self
.
rounds
=
rounds
self
.
lines
:
[
str
,
plt
.
Line2D
]
=
{}
self
.
fig
.
show
()
self
.
x
=
(
0
,
self
.
rounds
,
self
.
rounds
)
plt
.
ion
()
def
_get_key
(
self
,
data
:
str
,
metric
:
str
)
->
str
:
return
f
"
{
data
}
-
{
metric
}
"
def
after_iteration
(
self
,
model
:
xgb
.
Booster
,
epoch
:
int
,
evals_log
:
[
str
,
dict
]
)
->
bool
:
"""Update the plot."""
if
not
self
.
lines
:
for
data
,
metric
in
evals_log
.
items
():
for
metric_name
,
log
in
metric
.
items
():
key
=
self
.
_get_key
(
data
,
metric_name
)
expanded
=
log
+
[
0
]
*
(
self
.
rounds
-
len
(
log
))
(
self
.
lines
[
key
],)
=
self
.
ax
.
plot
(
self
.
x
,
expanded
,
label
=
key
)
self
.
ax
.
legend
()
else
:
for
data
,
metric
in
evals_log
.
items
():
for
metric_name
,
log
in
metric
.
items
():
key
=
self
.
_get_key
(
data
,
metric_name
)
expanded
=
log
+
[
0
]
*
(
self
.
rounds
-
len
(
log
))
self
.
lines
[
key
]
.
set_ydata
(
expanded
)
self
.
fig
.
canvas
.
draw
()
return
False
def
custom_callback
()
->
None
:
"""Demo for defining a custom callback function that plots evaluation result during
training."""
X
,
y
=
(
return_X_y
=
True
)
X_train
,
X_valid
,
y_train
,
y_valid
=
(
X
,
y
,
random_state
=
0
)
D_train
=
xgb
.
DMatrix
(
X_train
,
y_train
)
D_valid
=
xgb
.
DMatrix
(
X_valid
,
y_valid
)
num_boost_round
=
100
plotting
=
Plotting
(
num_boost_round
)
xgb
.
train
(
{
"objective"
:
"binary:logistic"
,
"eval_metric"
:
[
"error"
,
"rmse"
],
"tree_method"
:
"hist"
,
"device"
:
"cuda"
,
},
D_train
,
evals
=
[(
D_train
,
"Train"
),
(
D_valid
,
"Valid"
)],
num_boost_round
=
num_boost_round
,
callbacks
=
[
plotting
],
)
def
check_point_callback
()
->
None
:
"""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.
"""
rounds
=
2
def
check
(
as_pickle
:
bool
)
->
None
:
for
i
in
range
(
0
,
10
,
rounds
):
if
i
==
0
:
continue
if
as_pickle
:
path
=
(
tmpdir
,
"model_"
+
str
(
i
)
+
".pkl"
)
else
:
path
=
(
tmpdir
,
f
"model_
{
i
}
.
{
xgb
.
callback
.
TrainingCheckPoint
.
default_format
}
"
,
)
assert
(
path
)
X
,
y
=
(
return_X_y
=
True
)
m
=
xgb
.
DMatrix
(
X
,
y
)
with
()
as
tmpdir
:
check_point
=
xgb
.
callback
.
TrainingCheckPoint
(
directory
=
tmpdir
,
interval
=
rounds
,
name
=
"model"
)
xgb
.
train
(
{
"objective"
:
"binary:logistic"
},
m
,
num_boost_round
=
10
,
verbose_eval
=
False
,
callbacks
=
[
check_point
],
)
check
(
False
)
check_point
=
xgb
.
callback
.
TrainingCheckPoint
(
directory
=
tmpdir
,
interval
=
rounds
,
as_pickle
=
True
,
name
=
"model"
)
xgb
.
train
(
{
"objective"
:
"binary:logistic"
},
m
,
num_boost_round
=
10
,
verbose_eval
=
False
,
callbacks
=
[
check_point
],
)
check
(
True
)
if
__name__
==
"__main__"
:
parser
=
()
parser
.
add_argument
(
"--plot"
,
default
=
1
,
type
=
int
)
args
=
parser
.
parse_args
()
check_point_callback
()
if
args
.
plot
:
custom_callback
()