-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathtspp_xgboost.py
111 lines (99 loc) · 4.75 KB
/
tspp_xgboost.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
# Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import cudf
import pandas as pd
import pynvml
import numpy as np
import xgboost as xgb
import os
import glob
import dask_cudf
from distributed_utils import create_client
#Deal with the pateince and log_interval. Also objective, cluster
class TSPPXGBoost():
def __init__(self, config):
self.config = config
self.models = []
def fit(self, train, label, valid, valid_label, **kwargs):
train = train.drop(['_id_', '_timestamp_'], axis=1, errors='ignore')
valid = valid.drop(['_id_', '_timestamp_'], axis=1, errors='ignore')
X = xgb.DeviceQuantileDMatrix(cudf.from_pandas(train), label=cudf.from_pandas(label))
V = xgb.DMatrix(cudf.from_pandas(valid), label=cudf.from_pandas(valid_label))
model = xgb.train(params=self.config,
dtrain=X,
num_boost_round=self.config.n_rounds,
evals=[(X, 'train'), (V, 'valid')],
early_stopping_rounds=kwargs.get('patience', 5),
verbose_eval=kwargs.get("log_interval", 25),
)
self.models.append(model)
def predict(self, test, i):
test = test.drop(['_id_', '_timestamp_'], axis=1, errors='ignore')
model = self.models[i]
X = xgb.DMatrix(cudf.from_pandas(test))
return model.predict(X)
def save(self, path):
os.makedirs(os.path.join(path, 'checkpoints'), exist_ok=True)
for i in range(len(self.models)):
model = self.models[i]
model.save_model(os.path.join(path, f'checkpoints/xgb_{i+1}.model'))
def load(self, path):
self.models = []
for i in range(self.config.example_length - self.config.encoder_length):
p = os.path.join(path, f'checkpoints/xgb_{i+1}.model')
model = xgb.Booster()
model.load_model(p)
self.models.append(model)
class TSPPDaskXGBoost():
def __init__(self, config):
self.config = config
self.models = []
self.client = create_client(config)
self.npartitions = self.config.cluster.npartitions
def fit(self, train, label, valid, valid_label, **kwargs):
X = xgb.dask.DaskDeviceQuantileDMatrix(self.client,
dask_cudf.from_cudf(cudf.from_pandas(train), npartitions=self.npartitions),
label=dask_cudf.from_cudf(cudf.from_pandas(label), npartitions=self.npartitions))
V = xgb.dask.DaskDMatrix(self.client,
dask_cudf.from_cudf(cudf.from_pandas(valid), npartitions=self.npartitions),
label=dask_cudf.from_cudf(cudf.from_pandas(valid_label), npartitions=self.npartitions))
model = xgb.dask.train(client=self.client,
params=self.config,
dtrain=X,
num_boost_round=self.config.n_rounds,
evals=[(X, 'train'), (V, 'valid')],
early_stopping_rounds=kwargs.get('patience', 5),
verbose_eval=kwargs.get("log_interval", 25),
)
self.models.append(model)
self.client.restart()
def predict(self, test, i):
test = test.reset_index(drop=True)
model = self.models[i]
test = dask_cudf.from_cudf(cudf.from_pandas(test), npartitions=self.npartitions)
test = xgb.dask.DaskDMatrix(self.client, test)
out = xgb.dask.predict(self.client, model, test)
return out.compute()
def save(self, path):
os.makedirs(os.path.join(path, 'checkpoints'), exist_ok=True)
for i in range(len(self.models)):
model = self.models[i]
model['booster'].save_model(os.path.join(path, f'checkpoints/xgb_{i+1}.model'))
def load(self, path):
self.models = []
for i in range(self.config.example_length - self.config.encoder_length):
p = os.path.join(path, f'checkpoints/xgb_{i+1}.model')
model = {'booster': xgb.dask.Booster()}
model['booster'].load_model(p)
self.models.append(model)