-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfasttext_wrapper.py
310 lines (272 loc) · 11.9 KB
/
fasttext_wrapper.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
# Copyright (C) 2017-2023 Cleanlab Inc.
# This file is part of cleanlab.
#
# cleanlab is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cleanlab is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with cleanlab. If not, see <https://www.gnu.org/licenses/>.
"""
Text classification with fastText models that are compatible with cleanlab.
This module allows you to easily find label issues in your text datasets.
You must have fastText installed: ``pip install "fasttext==0.9.2"`` or lower.
Version 0.9.3 has a regression bug and the official package has been archived on GitHub.
Tips:
* Check out our example using this class: `fasttext_amazon_reviews <https://github.com/cleanlab/examples/blob/master/fasttext_amazon_reviews/fasttext_amazon_reviews.ipynb>`_
* Our `unit tests <https://github.com/cleanlab/cleanlab/blob/master/tests/test_frameworks.py>`_ also provide basic usage examples.
"""
import time
import os
import copy
import numpy as np
from sklearn.base import BaseEstimator
from fasttext import train_supervised, load_model
LABEL = "__label__"
NEWLINE = " __newline__ "
def data_loader(
fn=None,
indices=None,
label=LABEL,
batch_size=1000,
):
"""Returns a generator, yielding two lists containing
[labels], [text]. Items are always returned in the
order in the file, regardless if indices are provided."""
def _split_labels_and_text(batch):
l, t = [list(t) for t in zip(*(z.split(" ", 1) for z in batch))]
return l, t
# Prepare a stack of indices
if indices is not None:
stack_indices = sorted(indices, reverse=True)
stack_idx = stack_indices.pop()
with open(fn, "r") as f:
len_label = len(label)
idx = 0
batch_counter = 0
prev = f.readline()
batch = []
while True:
try:
line = f.readline()
line = line
if line[:len_label] == label or line == "":
if indices is None or stack_idx == idx:
# Write out prev line and reset prev
batch.append(prev.strip().replace("\n", NEWLINE))
batch_counter += 1
if indices is not None:
if len(stack_indices):
stack_idx = stack_indices.pop()
else: # No more data in indices, quit loading data.
yield _split_labels_and_text(batch)
break
prev = ""
idx += 1
if batch_counter == batch_size:
yield _split_labels_and_text(batch)
# Reset batch
batch_counter = 0
batch = []
prev += line
if line == "":
if len(batch) > 0:
yield _split_labels_and_text(batch)
break
except EOFError:
if indices is None or stack_idx == idx:
# Write out prev line and reset prev
batch.append(prev.strip().replace("\n", NEWLINE))
batch_counter += 1
yield _split_labels_and_text(batch)
break
class FastTextClassifier(BaseEstimator): # Inherits sklearn base classifier
"""Instantiate a fastText classifier that is compatible with :py:class:`CleanLearning <cleanlab.classification.CleanLearning>`.
Parameters
----------
train_data_fn: str
File name of the training data in the format compatible with fastText.
test_data_fn: str, optional
File name of the test data in the format compatible with fastText.
"""
def __init__(
self,
train_data_fn,
test_data_fn=None,
labels=None,
tmp_dir="",
label=LABEL,
del_intermediate_data=True,
kwargs_train_supervised={},
p_at_k=1,
batch_size=1000,
):
self.train_data_fn = train_data_fn
self.test_data_fn = test_data_fn
self.tmp_dir = tmp_dir
self.label = label
self.del_intermediate_data = del_intermediate_data
self.kwargs_train_supervised = kwargs_train_supervised
self.p_at_k = p_at_k
self.batch_size = batch_size
self.clf = None
self.labels = labels
if labels is None:
# Find all class labels across the train and test set (if provided)
unique_labels = set([])
for labels, _ in data_loader(fn=train_data_fn, batch_size=batch_size):
unique_labels = unique_labels.union(set(labels))
if test_data_fn is not None:
for labels, _ in data_loader(fn=test_data_fn, batch_size=batch_size):
unique_labels = unique_labels.union(set(labels))
else:
# Prepend labels with self.label token (e.g. '__label__').
unique_labels = [label + str(l) for l in labels]
# Create maps: label strings <-> integers when label strings are used
unique_labels = sorted(list(unique_labels))
self.label2num = dict(zip(unique_labels, range(len(unique_labels))))
self.num2label = dict((y, x) for x, y in self.label2num.items())
def _create_train_data(self, data_indices):
"""Returns filename of the masked fasttext data file.
Items are written in the order they are in the file,
regardless if indices are provided."""
# If X indexes all training data, no need to rewrite the file.
if data_indices is None:
self.masked_data_was_created = False
return self.train_data_fn
# Mask training data by data_indices
else:
len_label = len(LABEL)
data_indices = sorted(data_indices, reverse=True)
masked_fn = "fastTextClf_" + str(int(time.time())) + ".txt"
open(masked_fn, "w").close()
# Read in training data one line at a time
with open(self.train_data_fn, "r") as rf:
idx = 0
data_idx = data_indices.pop()
for line in rf:
# Mask by data_indices
if idx == data_idx:
with open(masked_fn, "a") as wf:
wf.write(line.strip().replace("\n", NEWLINE) + "\n")
if line[:len_label] == LABEL:
if len(data_indices):
data_idx = data_indices.pop()
else:
break
# Increment data index if starts with __label__
# This enables support for text data containing '\n'.
if line[:len_label] == LABEL:
idx += 1
self.masked_data_was_created = True
return masked_fn
def _remove_masked_data(self, fn):
"""Deletes intermediate data files."""
if self.del_intermediate_data and self.masked_data_was_created:
os.remove(fn)
def __deepcopy__(self, memo):
if self.clf is None:
self_clf_copy = None
else:
fn = "tmp_{}.fasttext.model".format(int(time.time()))
self.clf.save_model(fn)
self_clf_copy = load_model(fn)
os.remove(fn)
# Store self.clf
params = self.__dict__
clf = params.pop("clf")
# Copy params without self.clf (it can't be copied)
params_copy = copy.deepcopy(params)
# Add clf back to self.clf
self.clf = clf
# Create copy to return
clf_copy = FastTextClassifier(self.train_data_fn)
params_copy["clf"] = self_clf_copy
clf_copy.__dict__ = params_copy
return clf_copy
def fit(self, X=None, y=None, sample_weight=None):
"""Trains the fast text classifier.
Typical usage requires NO parameters,
just clf.fit() # No params.
Parameters
----------
X : iterable, e.g. list, numpy array (default None)
The list of indices of the data to use.
When in doubt, set as None. None defaults to range(len(data)).
y : None
Leave this as None. It's a filler to suit sklearns reqs.
sample_weight : None
Leave this as None. It's a filler to suit sklearns reqs."""
train_fn = self._create_train_data(data_indices=X)
self.clf = train_supervised(train_fn, **self.kwargs_train_supervised)
self._remove_masked_data(train_fn)
def predict_proba(self, X=None, train_data=True, return_labels=False):
"""Produces a probability matrix with examples on rows and
classes on columns, where each row sums to 1 and captures the
probability of the example belonging to each class."""
fn = self.train_data_fn if train_data else self.test_data_fn
pred_probs_list = []
if return_labels:
labels_list = []
for labels, text in data_loader(fn=fn, indices=X, batch_size=self.batch_size):
pred = self.clf.predict(text=text, k=len(self.clf.get_labels()))
# Get p(label = k | x) matrix of shape (N x K) of pred probs for each x
pred_probs = [
[p for _, p in sorted(list(zip(*l)), key=lambda x: x[0])] for l in list(zip(*pred))
]
pred_probs_list.append(np.array(pred_probs))
if return_labels:
labels_list.append(labels)
pred_probs = np.concatenate(pred_probs_list, axis=0)
if return_labels:
gold_labels = [self.label2num[z] for l in labels_list for z in l]
return (pred_probs, np.array(gold_labels))
else:
return pred_probs
def predict(self, X=None, train_data=True, return_labels=False):
"""Predict labels of X"""
fn = self.train_data_fn if train_data else self.test_data_fn
pred_list = []
if return_labels:
labels_list = []
for labels, text in data_loader(fn=fn, indices=X, batch_size=self.batch_size):
pred = [self.label2num[z[0]] for z in self.clf.predict(text)[0]]
pred_list.append(pred)
if return_labels:
labels_list.append(labels)
pred = np.array([z for l in pred_list for z in l])
if return_labels:
gold_labels = [self.label2num[z] for l in labels_list for z in l]
return (pred, np.array(gold_labels))
else:
return pred
def score(self, X=None, y=None, sample_weight=None, k=None):
"""Compute the average precision @ k (single label) of the
labels predicted from X and the true labels given by y.
score expects a `y` variable. In this case, `y` is the noisy labels."""
# Set the k for precision@k.
# For single label: 1 if label is in top k, else 0
if k is None:
k = self.p_at_k
fn = self.test_data_fn
pred_list = []
if y is None:
labels_list = []
for labels, text in data_loader(fn=fn, indices=X, batch_size=self.batch_size):
pred = self.clf.predict(text, k=k)[0]
pred_list.append(pred)
if y is None:
labels_list.append(labels)
pred = np.array([z for l in pred_list for z in l])
if y is None:
y = [z for l in labels_list for z in l]
else:
y = [self.num2label[z] for z in y]
apk = np.mean([y[i] in l for i, l in enumerate(pred)])
return apk