-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathtext_tfidf_model_continuous.py
382 lines (344 loc) · 13.9 KB
/
text_tfidf_model_continuous.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""Text classification model using TFIDF"""
import random
import numpy as np
import scipy as sp
import datatable as dt
from sklearn.preprocessing import LabelEncoder
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LinearRegression, LogisticRegression
from h2oaicore.models import CustomModel
from h2oaicore.systemutils_more import arch_type
from h2oaicore.systemutils import config, remove, user_dir, get_num_gpus_for_prediction, IgnoreEntirelyError
import joblib
import copy
import ast
import scipy as sc
def get_value(config, key):
if key in config.recipe_dict:
return config.recipe_dict[key]
elif "config_overrides" in config.get_overrides_dict():
data = config.get_overrides_dict()["config_overrides"]
data = ast.literal_eval(ast.literal_eval(data))
return data.get(key, None)
else:
return None
class TextTFIDFContinuousModel(CustomModel):
"""Text classification / regression model using TFIDF"""
_regression = False
_binary = True
_multiclass = True
_can_handle_non_numeric = True
_can_handle_text = True
_testing_can_skip_failure = False # ensure tested as if shouldn't fail
_included_transformers = ["TextOriginalTransformer"]
load_key = "Custom_TextTFIDF_load"
save_key = "Custom_TextTFIDF_save"
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.load_path = None
self.prev_params = None
@staticmethod
def reverse_sigmoid(x):
return np.log(x / (1 - x))
@staticmethod
def inverse_idf(idf_, N_):
tmp = np.exp(idf_ - 1)
tmp = np.round((N_ + 1) / tmp) - 1
return tmp
def return_tfidf_params(self):
params_ = {}
for k in ["max_features", "ngram_range", "norm", "max_df", "min_df"]:
params_[k] = self.params[k]
return params_
def sync_vectorizers(self, old, new):
# sync old and new versions
freq2 = self.inverse_idf(new.idf_, new.N_)
freq = self.inverse_idf(old.idf_, old.N_)
# adjust vocabulary and stop word list based on newly data
# adjust frequency terms and idf terms
new_freq = []
remapped_freq = np.zeros(len(freq))
dict_ = copy.copy(old.vocabulary_)
stop_list = copy.copy(old.stop_words_)
max_val = len(dict_)
for k in new.vocabulary_:
val = dict_.get(k, -1)
if val == -1:
dict_[k] = max_val
existed = stop_list.discard(k)
max_val += 1
new_freq.append(freq2[new.vocabulary_[k]])
else:
remapped_freq[val] = freq2[new.vocabulary_[k]]
old.vocabulary_ = dict_
old.stop_words_ = stop_list
freq = freq + remapped_freq
freq = np.hstack([freq, new_freq])
old.N_ = old.N_ + new.N_
freq = np.log((old.N_ + 1) / (1 + freq)) + 1
old.idf_ = freq
return old
def sync_tfidf(self, old, new):
newCols = new.shape[1] - old.shape[1]
if newCols > 0:
newCols = np.zeros((old.shape[0], newCols))
new_tf_idf = sc.sparse.hstack([old, newCols])
else:
new_tf_idf = old
XX = sc.sparse.vstack([new_tf_idf, new])
return XX
def return_lin_params(self):
params_ = {}
for k in ["random_state", "penalty", "C"]:
params_[k] = self.params[k]
return params_
def return_rf_params(self):
params_ = {}
for k in ["n_estimators", "num_leaves", "reg_alpha", "reg_lambda"]:
params_[k] = self.params[k]
return params_
def set_default_params(self, accuracy=None, time_tolerance=None,
interpretability=None, **kwargs):
if self.load_path and self.prev_params:
self.params = self.prev_params
else:
self.params = dict(
max_features=kwargs.get("max_features", None),
ngram_range=kwargs.get("ngram_range", (1, 1)),
norm=None,
random_state=2019,
max_df=0.9,
min_df=3,
penalty=kwargs.get("penalty", "l2"),
C=kwargs.get("C", 1.),
add_rf=kwargs.get("add_rf", False),
rf_alpha=kwargs.get("rf_alpha", .5),
n_estimators=kwargs.get("n_estimators", 100),
num_leaves=kwargs.get("num_leaves", 128),
reg_alpha=kwargs.get("reg_alpha", .1),
reg_lambda=kwargs.get("reg_lambda", .1),
)
def mutate_params(self, accuracy=None, time_tolerance=None, interpretability=None, **kwargs):
self.params["max_features"] = None # np.random.choice([50000, 100000, None])
self.params["ngram_range"] = random.choice([(1, 1), (1, 2), (1, 3)])
self.params["max_df"] = 0.9
self.params["min_df"] = 3
self.params["norm"] = None
self.params["random_state"] = 2019
self.params["penalty"] = random.choice(["l2", "l1"])
self.params["C"] = random.choice([1e-3, 1e-2, 1e-1, 1., 1e1, 1e2, 1e3])
self.params["add_rf"] = random.choice([False, True])
self.params["rf_alpha"] = random.choice([.1, .2, .3, .4, .5, .6, .7, .8, .9])
self.params["n_estimators"] = random.choice([10, 20, 50, 100])
self.params["num_leaves"] = random.choice([4, 16, 32, 64, 128, 256])
self.params["reg_alpha"] = random.choice([0, .1, .5, 1., 2., 10])
self.params["reg_lambda"] = random.choice([0, .1, .5, 1., 2., 10])
def _fit_vectorizer(self, vec, data):
try_flag = True
min_df = self.params["min_df"]
max_df = self.params["max_df"]
while try_flag:
try:
vec.set_params(
min_df=min_df,
max_df=max_df,
)
vec.fit(data)
try_flag = False
except ValueError:
if min_df == 1 and max_df == 1.0: # if min_df is 1 & max_df == 1.0 no more tries left
try_flag = False
raise
min_df -= 1
max_df += 0.05
max_df = min(max_df, 1.0) # limiting max_df to 1.0
min_df = max(min_df, 1) # limiting min_df to 1
try_flag = True
self.params["min_df"] = min_df
self.params["max_df"] = max_df
return vec
def fit(self, X, y, sample_weight=None, eval_set=None, sample_weight_eval_set=None, **kwargs):
y_ = y.copy()
orig_cols = list(X.names)
text_names = X[:, [str]].names
self.loaded = False
self.load_path = get_value(config, self.load_key)
self.save_path = get_value(config, self.save_key)
if self.load_path:
data = joblib.load(self.load_path)
self.tfidf_objs = data["tf_idf_obj"]
self.tf_idf_data = data["tf_idf_data"]
self.prev_params = data["params"]
self.target = data["target"]
self.loaded = True
if not self.loaded:
if self.num_classes >= 2:
lb = LabelEncoder()
lb.fit(self.labels)
y = lb.transform(y)
self.tfidf_objs = {}
self.tf_idf_data = {}
new_X = None
for col in text_names:
XX = X[:, col].to_pandas()
XX = XX[col].astype(str).fillna("NA").values.tolist()
tfidf_vec = TfidfVectorizer(**self.return_tfidf_params())
try:
tfidf_vec = self._fit_vectorizer(tfidf_vec, XX)
except ValueError as e:
if 'vocab' in str(e):
# skip non-text-like column
continue
else:
raise
XX = tfidf_vec.transform(XX)
tfidf_vec.N_ = XX.shape[0]
self.tfidf_objs[col] = tfidf_vec
self.tf_idf_data[col] = XX
if new_X is None:
new_X = XX
else:
new_X = sp.sparse.hstack([new_X, XX])
else:
y_ = np.hstack([self.target, y_])
y = y_.copy()
if self.num_classes >= 2:
lb = LabelEncoder()
lb.fit(self.labels)
y = lb.transform(y)
new_X = None
for col in text_names:
XX = X[:, col].to_pandas()
XX = XX[col].astype(str).fillna("NA").values.tolist()
N_ = len(XX)
tfidf_vec = TfidfVectorizer()
tfidf_vec.set_params(**self.tfidf_objs[col].get_params())
try:
tfidf_vec.fit(XX)
new_data_avail = True
except ValueError as e:
if 'vocab' in str(e):
# skip non-text-like column
continue
new_data_avail = False
if new_data_avail:
tfidf_vec.N_ = N_
pre_trained = self.tfidf_objs[col]
pre_trained = self.sync_vectorizers(pre_trained, tfidf_vec)
else:
pre_trained = self.tfidf_objs[col]
XX = pre_trained.transform(XX)
self.tfidf_objs[col] = pre_trained
XX = self.sync_tfidf(self.tf_idf_data[col], XX)
self.tf_idf_data[col] = XX
if new_X is None:
new_X = XX
else:
new_X = sp.sparse.hstack([new_X, XX])
models = [LogisticRegression(**self.return_lin_params())]
if self.params["add_rf"]:
from h2oaicore.lightgbm_dynamic import import_lightgbm
lgbm = import_lightgbm()
import lightgbm as lgbm
models.append(lgbm.LGBMClassifier(
boosting_type='rf',
colsample_bytree=.5,
subsample=.632, # Standard RF bagging fraction
min_child_weight=2.5,
min_child_samples=5,
subsample_freq=1,
min_split_gain=0,
n_jobs=-1,
**self.return_rf_params()
))
for mi, m in enumerate(models):
try:
m.fit(new_X, y)
except ValueError as e:
# general mutation as specified is not alllowed, see logistic_regression recipe.
# Could use restricted choices there, but for simplicity just ignore the error entirely
if mi == 0:
raise IgnoreEntirelyError(str(e))
raise
importances = [1] * len(orig_cols)
self.set_model_properties(
model={
"model": models,
"tf-idfs": self.tfidf_objs
},
features=orig_cols,
importances=importances,
iterations=0
)
if self.save_path:
joblib.dump({
"tf_idf_obj": self.tfidf_objs,
"tf_idf_data": self.tf_idf_data,
"params": self.params,
"target": y_,
},
self.save_path
)
# clear large objects to avoid large data in subprocess pipe
self.tfidf_objs = None
self.tf_idf_data = None
def predict(self, X, **kwargs):
X = dt.Frame(X)
new_X = None
data, _, _, _ = self.get_model_properties()
models = data["model"]
self.tfidf_objs = data["tf-idfs"]
text_names = X[:, [str]].names
for col in text_names:
if col not in self.tfidf_objs:
continue
XX = X[:, col].to_pandas()
XX = XX[col].astype(str).fillna("NA").values.tolist()
tfidf_vec = self.tfidf_objs[col]
XX = tfidf_vec.transform(XX)
if new_X is None:
new_X = XX
else:
new_X = sp.sparse.hstack([new_X, XX])
preds = []
for m in models:
if self.num_classes == 1:
preds.append(m.predict(new_X).reshape(-1, 1))
else:
preds.append(m.predict_proba(new_X))
if len(preds) > 1:
preds = np.dstack(preds[:2])
preds = np.average(preds, axis=2, weights=[(1. - self.params["rf_alpha"]), self.params["rf_alpha"]])
else:
preds = preds[0]
return preds
def pre_get_model(self, X_shape=(1, 1), **kwargs): # copy-paste from LightGBM model class
from h2oaicore.lightgbm_dynamic import import_lightgbm
lgbm = import_lightgbm()
if arch_type == 'ppc64le':
# ppc has issues with this, so force ppc to only keep same architecture
return
if self.self_model_was_not_set_for_predict:
# if no self.model, then should also not have imported lgbm/xgb yet
# well, for rulefit not true
# check_no_xgboost_or_lightgbm()
pass
# Force the C++ session to have good "defaults" by making an exemplary dummy model to set the state from which
# our model will later inherit default settings from.
try:
import lightgbm as lgb
params = dict(n_jobs=self.params_base['n_jobs'])
if get_num_gpus_for_prediction() == 0 or arch_type == 'ppc64le': # power has no GPU for lgbm yet
params['device_type'] = 'cpu'
else:
params['device_type'] = 'gpu'
params['gpu_device_id'] = self.params_base.get('gpu_id', 0)
params['gpu_platform_id'] = 0
params['gpu_use_dp'] = config.reproducible
model = lgb.LGBMClassifier(**params)
X = np.array([[1, 2, 3, 4], [1, 3, 4, 2]])
y = np.array([1, 0])
model.fit(X=X, y=y)
except:
if config.hard_asserts:
raise