-
Notifications
You must be signed in to change notification settings - Fork 93
/
continuous_TextTransformer.py
397 lines (337 loc) · 15.1 KB
/
continuous_TextTransformer.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""Creates a TF-IDF based text transformation that can be continuously updated with new data and vocabulary."""
import importlib
from h2oaicore.transformer_utils import CustomTransformer
from h2oaicore.transformers import TextTransformer, CPUTruncatedSVD
import datatable as dt
import numpy as np
from h2oaicore.systemutils import config, remove, user_dir
import joblib
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.pipeline import Pipeline
import ast
import copy
import scipy as sc
import pandas as pd
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
# """
# {
# 'Custom_TextTransformer_load':/home/dmitry/Desktop/tmp/save_000.pkl',
# 'Custom_TextTransformer_save':'/home/dmitry/Desktop/tmp/save_001.pkl'
# }
# """
# "{'Custom_TextTransformer_load':'/home/dmitry/Desktop/tmp/save_000.pkl','Custom_TextTransformer_save':'/home/dmitry/Desktop/tmp/save_001.pkl'}"
class Cached_TextTransformer(CustomTransformer):
_regression = True
_binary = True
_multiclass = True
_testing_can_skip_failure = False # ensure tested as if shouldn't fail
_display_name = "Cached_TextTransformer"
load_key = "Custom_TextTransformer_load"
save_key = "Custom_TextTransformer_save"
_can_use_gpu = False
_can_use_multi_gpu = False
@staticmethod
def do_acceptance_test():
return False
@staticmethod
def get_parameter_choices():
return {
"max_features": [None],
"tf_idf": [True, False],
"max_ngram": [1, 2, 3],
"dim_reduction": [50],
}
@staticmethod
def get_default_properties():
return dict(col_type="text", min_cols=1, max_cols=1, relative_importance=1)
def __init__(
self, max_features=None, tf_idf=True, max_ngram=1, dim_reduction=50, **kwargs
):
super().__init__(**kwargs)
self.loaded = False
self.load_path = get_value(config, self.load_key)
self.save_path = get_value(config, self.save_key)
if not self.load_path:
self.TextTransformer = TextTransformer(
max_features=max_features,
tf_idf=tf_idf,
max_ngram=max_ngram,
dim_reduction=dim_reduction,
**kwargs
)
self.TextTransformer._can_use_gpu = self._can_use_gpu
self.TextTransformer._can_use_multi_gpu = self._can_use_multi_gpu
else:
data = joblib.load(self.load_path)
if isinstance(data, dict):
self.TextTransformer = data["txtTransformer"]
self.tf_idf = data["tf_idf"]
self.target = data["target"]
else:
self.TextTransformer = data
self.tf_idf = {}
self.target = None
self.loaded = True
self.TextTransformer._can_use_gpu = self._can_use_gpu
self.TextTransformer._can_use_multi_gpu = self._can_use_multi_gpu
def fit_transform(self, X: dt.Frame, y: np.array = None):
self.TextTransformer.N_ = X.shape[0]
result = self.TextTransformer.fit_transform(X.to_pandas())
if self.save_path:
joblib.dump(self.TextTransformer, self.save_path)
return result
def transform(self, X: dt.Frame):
return self.TextTransformer.transform(X.to_pandas())
_mojo = True
from h2oaicore.mojo import MojoWriter, MojoFrame
def to_mojo(
self, mojo: MojoWriter, iframe: MojoFrame, group_uuid=None, group_name=None
):
return self.TextTransformer.write_to_mojo(mojo, iframe, group_uuid, group_name)
# class Updatable_TextTransformer_TFIDFOnly(Cached_TextTransformer):
# """
# Only updates TF-IDF terms, vocabulary and stop word list remain the same
# """
# _display_name = "Updatable_TextTransformer_TFIDFOnly"
# @staticmethod
# def inverse_idf(idf_, N_):
# tmp = np.exp(idf_ - 1)
# tmp = np.round((N_+1) / tmp) - 1
# return tmp
# def fit_transform(self, X: dt.Frame, y: np.array = None):
# if self.loaded:
# X_ = X.to_pandas()
# N_ = len(X_)
# for col in self.input_feature_names:
# if self.TextTransformer.tf_idf: # update tf-idf terms for tokens in new data
# cv = TfidfVectorizer()
# pre_trained = self.TextTransformer.pipes[col][0]["model"]
# cv.set_params(**pre_trained.get_params())
# cv.set_params(**{
# "vocabulary": pre_trained.vocabulary_,
# "stop_words": pre_trained.stop_words_
# })
# pipe_ = copy.deepcopy(self.TextTransformer.pipes[col][0])
# new_pipe = []
# for step in pipe_.steps:
# if step[0] != 'model':
# new_pipe.append(step)
# else:
# new_pipe.append(('model', cv))
# break
# new_pipe = Pipeline(new_pipe)
# new_pipe.fit(self.TextTransformer.stringify_col(X_[col]))
# freq2 = self.inverse_idf(cv.idf_, N_)
# freq = self.inverse_idf(
# pre_trained.idf_,
# self.TextTransformer.N_
# )
# freq = freq + freq2
# self.TextTransformer.N_ = self.TextTransformer.N_ + N_
# freq = np.log((self.TextTransformer.N_+1) / (1+freq)) + 1
# pre_trained.idf_ = freq
# result = self.TextTransformer.transform(X.to_pandas())
# else:
# self.TextTransformer.N_ = X.shape[0]
# result = self.TextTransformer.fit_transform(X.to_pandas())
# if self.save_path:
# joblib.dump(self.TextTransformer, self.save_path)
# return result
class Updatable_TextTransformer(Cached_TextTransformer):
"""
Updates TF-IDF terms, vocabulary and stop word, same for CountVectorizer
Updates SVD matrix in order to incorporate new terms and adjust influence of old ones
"""
_display_name = "Updatable_TextTransformer"
_unsupervised = False # uses target
_uses_target = True # uses target
@staticmethod
def get_parameter_choices():
dict_ = Cached_TextTransformer.get_parameter_choices()
dict_["step"] = [1e-5, 1e-4, 1e-3, 1e-2, 0.1]
return dict_
def __init__(
self,
max_features=None,
tf_idf=True,
max_ngram=1,
dim_reduction=50,
step=0.1,
**kwargs
):
super().__init__(
max_features=None, tf_idf=True, max_ngram=1, dim_reduction=50, **kwargs
)
self.step = step
@staticmethod
def inverse_idf(idf_, N_):
tmp = np.exp(idf_ - 1)
tmp = np.round((N_ + 1) / tmp) - 1
return tmp
def fit_transform(self, X: dt.Frame, y: np.array = None, append=False):
y_ = y
new_data = []
if self.loaded:
X_ = X.to_pandas()
N_ = len(X_)
for col in self.input_feature_names:
if self.TextTransformer.tf_idf:
# train new TfidfVectorizer in order to expand vocabulary of the old one and adjust idf terms
cv = TfidfVectorizer()
pre_trained = self.TextTransformer.pipes[col][0]["model"]
cv.set_params(**pre_trained.get_params())
pipe_ = copy.deepcopy(self.TextTransformer.pipes[col][0])
new_pipe = []
for step in pipe_.steps:
if step[0] != "model":
new_pipe.append(step)
else:
new_pipe.append(("model", cv))
break
new_pipe = Pipeline(new_pipe)
new_pipe.fit(self.TextTransformer.stringify_col(X_[col]))
freq2 = self.inverse_idf(cv.idf_, N_)
freq = self.inverse_idf(pre_trained.idf_, self.TextTransformer.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(pre_trained.vocabulary_)
stop_list = copy.copy(pre_trained.stop_words_)
max_val = len(dict_)
for k in cv.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[cv.vocabulary_[k]])
else:
remapped_freq[val] = freq2[cv.vocabulary_[k]]
pre_trained.vocabulary_ = dict_
pre_trained.stop_words_ = stop_list
freq = freq + remapped_freq
freq = np.hstack([freq, new_freq])
self.TextTransformer.N_ = self.TextTransformer.N_ + N_
freq = np.log((self.TextTransformer.N_ + 1) / (1 + freq)) + 1
pre_trained.idf_ = freq
else:
# train new CountVectorizer in order to expand vocabulary of the old one
cv = CountVectorizer()
pre_trained = self.TextTransformer.pipes[col][0]["model"]
cv.set_params(**pre_trained.get_params())
pipe_ = copy.deepcopy(self.TextTransformer.pipes[col][0])
new_pipe = []
for step in pipe_.steps:
if step[0] != "model":
new_pipe.append(step)
else:
new_pipe.append(("model", cv))
break
new_pipe = Pipeline(new_pipe)
new_pipe.fit(self.TextTransformer.stringify_col(X_[col]))
# adjust vocabulary and stop word list based on newly data
dict_ = copy.copy(pre_trained.vocabulary_)
stop_list = copy.copy(pre_trained.stop_words_)
max_val = len(dict_)
for k in cv.vocabulary_:
val = dict_.get(k, -1)
if val == -1:
dict_[k] = max_val
existed = stop_list.discard(k)
max_val += 1
pre_trained.vocabulary_ = dict_
pre_trained.stop_words_ = stop_list
# get transformed data in order to adjust SVD matrix
svd_ = self.TextTransformer.pipes[col][1]
if isinstance(svd_, CPUTruncatedSVD):
X_transformed = self.TextTransformer.pipes[col][0].transform(
self.TextTransformer.stringify_col(X_[col])
)
if col in self.tf_idf:
# combine saved matrix with the new one
newCols = X_transformed.shape[1] - self.tf_idf[col].shape[1]
if newCols > 0:
newCols = np.zeros((self.tf_idf[col].shape[0], newCols))
new_tf_idf = sc.sparse.hstack([self.tf_idf[col], newCols])
else:
new_tf_idf = self.tf_idf[col]
new_tf_idf = sc.sparse.vstack([new_tf_idf, X_transformed])
self.tf_idf[col] = new_tf_idf
# fit SVD on combined matrix
new_svd = CPUTruncatedSVD()
new_svd.set_params(**svd_.get_params())
new_svd.fit(self.tf_idf[col])
# replace old svd matrix with new one
svd_.components_ = new_svd.components_
if append:
data_ = svd_.transform(self.tf_idf[col])
data_ = self.TextTransformer.pipes[col][2].transform(data_)
data_ = pd.DataFrame(
data_,
columns=self.TextTransformer.get_names(
col, data_.shape[1]
),
)
new_data.append(data_)
else:
self.tf_idf[col] = X_transformed
# train new SVD to get new transform matrix
new_svd = CPUTruncatedSVD()
new_svd.set_params(**svd_.get_params())
new_svd.fit(X_transformed)
# adjust old transform matrix based on new one
grad = (
svd_.components_
- new_svd.components_[:, : svd_.components_.shape[1]]
)
grad = self.step * grad
svd_.components_ = svd_.components_ - grad
svd_.components_ = np.hstack(
[
svd_.components_,
new_svd.components_[:, svd_.components_.shape[1]:],
]
)
if append:
new_data = pd.concat(new_data, axis=1)
if self.target is not None:
y_ = np.hstack([self.target, y_])
if self.save_path:
joblib.dump(
{
"txtTransformer": self.TextTransformer,
"tf_idf": self.tf_idf,
"target": y_,
},
self.save_path,
)
return new_data, y_
result = self.TextTransformer.transform(X.to_pandas())
else:
self.TextTransformer.N_ = X.shape[0]
result = self.TextTransformer.fit_transform(X.to_pandas())
X_ = X.to_pandas()
self.tf_idf = {}
for col in self.input_feature_names:
self.tf_idf[col] = self.TextTransformer.pipes[col][0].transform(
self.TextTransformer.stringify_col(X_[col])
)
if self.save_path:
joblib.dump(
{
"txtTransformer": self.TextTransformer,
"tf_idf": self.tf_idf,
"target": y_,
},
self.save_path,
)
return result