-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightgbm_boost.py
156 lines (122 loc) · 4.72 KB
/
lightgbm_boost.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# pylint: disable=invalid-name
from typing import Any, Dict
import lightgbm as lgb
from sklearn.preprocessing import StandardScaler
from opsml import (
CardInfo,
CardRegistries,
DataCard,
DataSplit,
LightGBMModel,
ModelCard,
PandasData,
)
from opsml.helpers.data import create_fake_data
class OpsmlLightGBMBoosterWorkflow:
def __init__(self, info: CardInfo, params: Dict[str, Any]):
"""Instantiates workflow class. Instantiation will also set up the registries that
will be used to store cards and artifacts
Args:
info:
CardInfo data structure that contains required info for cards.
You could also provide "name", "repository" and "email" to a card; however, this
simplifies the process.
"""
self.info = info
self.registries = CardRegistries()
self.params = params
def _create_datacard(self):
"""Shows how to create a data interface and datacard
You can think of cards as outputs to each step in your workflow.
In your data getting step, you will get your data, create a data interface,
and then create/register a datacard, which will be stored in the registry.
This example highlights the uses of the PandasData interface
"""
# create fake data
X, y = create_fake_data(n_samples=1000, task_type="regression")
X["target"] = y
# Create data interface
data_interface = PandasData(
data=X,
data_splits=[
DataSplit(label="train", column_name="col_1", column_value=0.5, inequality=">="),
DataSplit(label="test", column_name="col_1", column_value=0.5, inequality="<"),
],
dependent_vars=["target"],
)
# Create datacard
datacard = DataCard(interface=data_interface, info=self.info)
self.registries.data.register_card(card=datacard)
def _create_modelcard(self):
"""Shows how to create a model interface and modelcard
This example highlights the uses of the LightGBMModel.
"""
datacard: DataCard = self.registries.data.load_card(name=self.info.name)
# load data from server
datacard.load_data()
# split data
data = datacard.split_data()
# create standard scaler
scaler = StandardScaler()
scaler.fit(data["train"].X)
X_train = scaler.transform(data["train"].X)
X_test = scaler.transform(data["test"].X)
lgb_train = lgb.Dataset(X_train, data["train"].y)
lgb_eval = lgb.Dataset(X_test, data["test"].y, reference=lgb_train)
# fit model
gbm = lgb.train(
params,
lgb_train,
num_boost_round=20,
valid_sets=lgb_eval,
callbacks=[lgb.early_stopping(stopping_rounds=5)],
)
# fit model
interface = LightGBMModel(
model=gbm,
sample_data=X_train[:100],
preprocessor=scaler,
)
# create modelcard
modelcard = ModelCard(
interface=interface,
info=self.info,
to_onnx=True, # lets convert onnx
datacard_uid=datacard.uid, # modelcards must be associated with a datacard
)
self.registries.model.register_card(card=modelcard)
def _test_onnx_model(self):
"""This shows how to load a modelcard and the associated model and onnx model (if converted to onnx)"""
datacard: DataCard = self.registries.data.load_card(name=self.info.name)
modelcard: ModelCard = self.registries.model.load_card(name=self.info.name)
# load data for testing
datacard.load_data()
# split data
data = datacard.split_data()
# load model
modelcard.load_model()
# load onnx model
modelcard.load_onnx_model()
prediction = modelcard.onnx_model.sess.run(None, {"predict": data["test"].X.to_numpy()[:5]})
print(prediction)
def run_workflow(self):
"""Helper method for executing workflow"""
self._create_datacard()
self._create_modelcard()
self._test_onnx_model()
if __name__ == "__main__":
# set info (easier than specifying in each card)
info = CardInfo(name="lightgbm", repository="opsml", contact="[email protected]")
params = {
"boosting_type": "gbdt",
"objective": "regression",
"metric": {"l2", "l1"},
"num_leaves": 31,
"learning_rate": 0.05,
"feature_fraction": 0.9,
"bagging_fraction": 0.8,
"bagging_freq": 5,
"verbose": 0,
}
workflow = OpsmlLightGBMBoosterWorkflow(info=info, params=params)
workflow.run_workflow()