-
Notifications
You must be signed in to change notification settings - Fork 245
/
processor.py
860 lines (757 loc) · 30 KB
/
processor.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
import os
import abc
from abc import ABC
import random
import logging
import json
import inspect
from inspect import signature
import numpy as np
from sklearn.preprocessing import StandardScaler
from farm.data_handler.dataset import convert_features_to_dataset
from farm.data_handler.input_features import (
samples_to_features_ner,
samples_to_features_bert_lm,
sample_to_features_text,
sample_to_features_squad,
)
from farm.data_handler.samples import (
Sample,
SampleBasket,
create_samples_squad,
)
from farm.data_handler.utils import (
read_tsv,
read_docs_from_txt,
read_ner_file,
read_squad_file,
is_json,
)
from farm.modeling.tokenization import BertTokenizer, tokenize_with_metadata
from farm.utils import MLFlowLogger as MlLogger
from farm.data_handler.samples import get_sentence_pair
logger = logging.getLogger(__name__)
TOKENIZER_MAP = {"BertTokenizer": BertTokenizer}
class Processor(ABC):
"""
Is used to generate PyTorch Datasets from input data. An implementation of this abstract class should be created
for each new data source.
Implement the abstract methods: _file_to_dicts(), _dict_to_samples(), _sample_to_features()
to be compatible with your data format
"""
subclasses = {}
def __init__(
self,
tokenizer,
max_seq_len,
train_filename,
dev_filename,
test_filename,
dev_split,
data_dir,
tasks={}
):
"""
:param tokenizer: Used to split a sentence (str) into tokens.
:param max_seq_len: Samples are truncated after this many tokens.
:type max_seq_len: int
:param train_filename: The name of the file containing training data.
:type train_filename: str
:param dev_filename: The name of the file containing the dev data. If None and 0.0 < dev_split < 1.0 the dev set
will be a slice of the train set.
:type dev_filename: str or None
:param test_filename: The name of the file containing test data.
:type test_filename: str
:param dev_split: The proportion of the train set that will sliced. Only works if dev_filename is set to None
:type dev_split: float
:param data_dir: The directory in which the train, test and perhaps dev files can be found.
:type data_dir: str
"""
self.tokenizer = tokenizer
self.max_seq_len = max_seq_len
self.tasks = tasks
# data sets
self.train_filename = train_filename
self.dev_filename = dev_filename
self.test_filename = test_filename
self.dev_split = dev_split
self.data_dir = data_dir
self.baskets = []
self._log_params()
def __init_subclass__(cls, **kwargs):
""" This automatically keeps track of all available subclasses.
Enables generic load() and load_from_dir() for all specific Processor implementation.
"""
super().__init_subclass__(**kwargs)
cls.subclasses[cls.__name__] = cls
@classmethod
def load(
cls,
processor_name,
data_dir,
tokenizer,
max_seq_len,
train_filename,
dev_filename,
test_filename,
dev_split,
**kwargs,
):
"""
Loads the class of processor specified by processor name.
:param processor_name: The class of processor to be loaded.
:type processor_name: str
:param data_dir: Directory where data files are located.
:type data_dir: str
:param tokenizer: A tokenizer object
:param max_seq_len: Sequences longer than this will be truncated.
:type max_seq_len: int
:param train_filename: The name of the file containing training data.
:type train_filename: str
:param dev_filename: The name of the file containing the dev data. If None and 0.0 < dev_split < 1.0 the dev set
will be a slice of the train set.
:type dev_filename: str or None
:param test_filename: The name of the file containing test data.
:type test_filename: str
:param dev_split: The proportion of the train set that will sliced. Only works if dev_filename is set to None
:type dev_split: float
:param kwargs: placeholder for passing generic parameters
:type kwargs: object
:return: An instance of the specified processor.
"""
sig = signature(cls.subclasses[processor_name])
unused_args = {k: v for k, v in kwargs.items() if k not in sig.parameters}
logger.debug(
f"Got more parameters than needed for loading {processor_name}: {unused_args}. "
f"Those won't be used!"
)
processor = cls.subclasses[processor_name](
data_dir=data_dir,
tokenizer=tokenizer,
max_seq_len=max_seq_len,
train_filename=train_filename,
dev_filename=dev_filename,
test_filename=test_filename,
dev_split=dev_split,
**kwargs,
)
return processor
@classmethod
def load_from_dir(cls, load_dir):
"""
Infers the specific type of Processor from a config file (e.g. GNADProcessor) and loads an instance of it.
:param load_dir: str, directory that contains a 'processor_config.json'
:return: An instance of a Processor Subclass (e.g. GNADProcessor)
"""
# read config
processor_config_file = os.path.join(load_dir, "processor_config.json")
config = json.load(open(processor_config_file))
# init tokenizer
tokenizer = TOKENIZER_MAP[config["tokenizer"]].from_pretrained(
load_dir,
do_lower_case=config["lower_case"],
never_split_chars=config.get("never_split_chars", None),
)
# add custom vocab to tokenizer if available
if os.path.exists(os.path.join(load_dir, "custom_vocab.txt")):
tokenizer.add_custom_vocab(os.path.join(load_dir, "custom_vocab.txt"))
# we have to delete the tokenizer string from config, because we pass it as Object
del config["tokenizer"]
processor = cls.load(tokenizer=tokenizer, processor_name=config["processor"], **config)
for task_name, task in config["tasks"].items():
processor.add_task(name=task_name, metric=task["metric"], label_list=task["label_list"])
if processor is None:
raise Exception
return processor
def save(self, save_dir):
"""
Saves the vocabulary to file and also creates a json file containing all the
information needed to load the same processor.
:param save_dir: Directory where the files are to be saved
:type save_dir: str
"""
os.makedirs(save_dir, exist_ok=True)
config = self.generate_config()
# save tokenizer incl. attributes
config["tokenizer"] = self.tokenizer.__class__.__name__
self.tokenizer.save_vocabulary(save_dir)
# TODO make this generic to other tokenizers. We will probably want an own abstract Tokenizer
config["lower_case"] = self.tokenizer.basic_tokenizer.do_lower_case
config["never_split_chars"] = self.tokenizer.basic_tokenizer.never_split_chars
# save processor
config["processor"] = self.__class__.__name__
output_config_file = os.path.join(save_dir, "processor_config.json")
with open(output_config_file, "w") as file:
json.dump(config, file)
def generate_config(self):
"""
Generates config file from Class and instance attributes (only for sensible config parameters).
"""
config = {}
# self.__dict__ doesn't give parent class attributes
for key, value in inspect.getmembers(self):
if is_json(value) and key[0] != "_":
config[key] = value
return config
def add_task(self, name, metric, label_list, label_column_name=None, label_name=None, task_type=None):
if type(label_list) is not list:
raise ValueError(f"Argument `label_list` must be of type list. Got: f{type(label_list)}")
if label_name is None:
label_name = f"{name}_label"
label_tensor_name = label_name + "_ids"
self.tasks[name] = {
"label_list": label_list,
"metric": metric,
"label_tensor_name": label_tensor_name,
"label_name": label_name,
"label_column_name": label_column_name,
"task_type": task_type
}
@abc.abstractmethod
def _file_to_dicts(self, file: str) -> [dict]:
raise NotImplementedError()
@abc.abstractmethod
def _dict_to_samples(cls, dict: dict, all_dicts=None) -> [Sample]:
raise NotImplementedError()
@abc.abstractmethod
def _sample_to_features(cls, sample: Sample) -> dict:
raise NotImplementedError()
def _init_baskets_from_file(self, file):
dicts = self._file_to_dicts(file)
dataset_name = os.path.splitext(os.path.basename(file))[0]
baskets = [
SampleBasket(raw=tr, id=f"{dataset_name}-{i}") for i, tr in enumerate(dicts)
]
return baskets
def _init_samples_in_baskets(self):
for basket in self.baskets:
all_dicts = [b.raw for b in self.baskets]
basket.samples = self._dict_to_samples(dict=basket.raw, all_dicts=all_dicts)
for num, sample in enumerate(basket.samples):
sample.id = f"{basket.id}-{num}"
def _featurize_samples(self):
for basket in self.baskets:
for sample in basket.samples:
sample.features = self._sample_to_features(sample=sample)
def _create_dataset(self, keep_baskets=False):
features_flat = []
for basket in self.baskets:
for sample in basket.samples:
features_flat.extend(sample.features)
if not keep_baskets:
# free up some RAM, we don't need baskets from here on
self.baskets = None
dataset, tensor_names = convert_features_to_dataset(features=features_flat)
return dataset, tensor_names
# def dataset_from_file(self, file, log_time=True):
# """
# Contains all the functionality to turn a data file into a PyTorch Dataset and a
# list of tensor names. This is used for training and evaluation.
#
# :param file: Name of the file containing the data.
# :type file: str
# :return: a Pytorch dataset and a list of tensor names.
# """
# if log_time:
# a = time.time()
# self._init_baskets_from_file(file)
# b = time.time()
# MlLogger.log_metrics(metrics={"t_from_file": (b - a) / 60}, step=0)
# self._init_samples_in_baskets()
# c = time.time()
# MlLogger.log_metrics(metrics={"t_init_samples": (c - b) / 60}, step=0)
# self._featurize_samples()
# d = time.time()
# MlLogger.log_metrics(metrics={"t_featurize_samples": (d - c) / 60}, step=0)
# self._log_samples(3)
# else:
# self._init_baskets_from_file(file)
# self._init_samples_in_baskets()
# self._featurize_samples()
# self._log_samples(3)
# dataset, tensor_names = self._create_dataset()
# return dataset, tensor_names
#TODO remove useless from_inference flag after refactoring squad processing
def dataset_from_dicts(self, dicts, index=None, from_inference=False):
"""
Contains all the functionality to turn a list of dict objects into a PyTorch Dataset and a
list of tensor names. This can be used for inference mode.
:param dicts: List of dictionaries where each contains the data of one input sample.
:type dicts: list of dicts
:return: a Pytorch dataset and a list of tensor names.
"""
self.baskets = [
SampleBasket(raw=tr, id="infer - {}".format(i))
for i, tr in enumerate(dicts)
]
self._init_samples_in_baskets()
self._featurize_samples()
if index == 0:
self._log_samples(3)
dataset, tensor_names = self._create_dataset()
return dataset, tensor_names
def _log_samples(self, n_samples):
logger.info("*** Show {} random examples ***".format(n_samples))
for i in range(n_samples):
random_basket = random.choice(self.baskets)
random_sample = random.choice(random_basket.samples)
logger.info(random_sample)
def _log_params(self):
params = {
"processor": self.__class__.__name__,
"tokenizer": self.tokenizer.__class__.__name__,
}
names = ["max_seq_len", "dev_split"]
for name in names:
value = getattr(self, name)
params.update({name: str(value)})
try:
MlLogger.log_params(params)
except Exception as e:
logger.warning(f"ML logging didn't work: {e}")
#########################################
# Processors for Text Classification ####
#########################################
class TextClassificationProcessor(Processor):
"""
Used to handle the text classification datasets that come in tabular format (CSV, TSV, etc.)
"""
def __init__(
self,
tokenizer,
max_seq_len,
data_dir,
label_list=None,
metric=None,
train_filename="train.tsv",
dev_filename=None,
test_filename="test.tsv",
dev_split=0.1,
delimiter="\t",
quote_char="'",
skiprows=None,
label_column_name="label",
multilabel=False,
header=0,
**kwargs,
):
#TODO If an arg is misspelt, e.g. metrics, it will be swallowed silently by kwargs
# Custom processor attributes
self.delimiter = delimiter
self.quote_char = quote_char
self.skiprows = skiprows
self.header = header
super(TextClassificationProcessor, self).__init__(
tokenizer=tokenizer,
max_seq_len=max_seq_len,
train_filename=train_filename,
dev_filename=dev_filename,
test_filename=test_filename,
dev_split=dev_split,
data_dir=data_dir,
tasks={},
)
#TODO raise info when no task is added due to missing "metric" or "labels" arg
if metric and label_list:
if multilabel:
task_type = "multilabel_classification"
else:
task_type = "classification"
self.add_task(name="text_classification",
metric=metric,
label_list=label_list,
label_column_name=label_column_name,
task_type=task_type)
def _file_to_dicts(self, file: str) -> [dict]:
column_mapping = {task["label_column_name"]: task["label_name"] for task in self.tasks.values()}
dicts = read_tsv(
filename=file,
delimiter=self.delimiter,
skiprows=self.skiprows,
quotechar=self.quote_char,
rename_columns=column_mapping,
header=self.header
)
return dicts
def _dict_to_samples(self, dict: dict, **kwargs) -> [Sample]:
# this tokenization also stores offsets
tokenized = tokenize_with_metadata(dict["text"], self.tokenizer, self.max_seq_len)
return [Sample(id=None, clear_text=dict, tokenized=tokenized)]
def _sample_to_features(self, sample) -> dict:
features = sample_to_features_text(
sample=sample,
tasks=self.tasks,
max_seq_len=self.max_seq_len,
tokenizer=self.tokenizer,
)
return features
#########################################
# Processors for Basic Inference ####
#########################################
class InferenceProcessor(Processor):
"""
Generic processor used at inference time:
- fast
- no labels
- pure encoding of text into pytorch dataset
- Doesn't read from file, but only consumes dictionaries (e.g. coming from API requests)
"""
def __init__(
self,
tokenizer,
max_seq_len,
**kwargs,
):
super(InferenceProcessor, self).__init__(
tokenizer=tokenizer,
max_seq_len=max_seq_len,
train_filename=None,
dev_filename=None,
test_filename=None,
dev_split=None,
data_dir=None,
tasks={}
)
@classmethod
def load_from_dir(cls, load_dir):
"""
Overwriting method from parent class to **always** load the InferenceProcessor instead of the specific class stored in the config.
:param load_dir: str, directory that contains a 'processor_config.json'
:return: An instance of an InferenceProcessor
"""
# read config
processor_config_file = os.path.join(load_dir, "processor_config.json")
config = json.load(open(processor_config_file))
# init tokenizer
tokenizer = TOKENIZER_MAP[config["tokenizer"]].from_pretrained(
load_dir,
do_lower_case=config["lower_case"],
never_split_chars=config.get("never_split_chars", None),
)
# add custom vocab to tokenizer if available
if os.path.exists(os.path.join(load_dir, "custom_vocab.txt")):
tokenizer.add_custom_vocab(os.path.join(load_dir, "custom_vocab.txt"))
# we have to delete the tokenizer string from config, because we pass it as Object
del config["tokenizer"]
processor = cls.load(tokenizer=tokenizer, processor_name="InferenceProcessor", **config)
for task_name, task in config["tasks"].items():
processor.add_task(name=task_name, metric=task["metric"], label_list=task["label_list"])
if processor is None:
raise Exception
return processor
def _file_to_dicts(self, file: str) -> [dict]:
raise NotImplementedError
def _dict_to_samples(self, dict: dict, **kwargs) -> [Sample]:
# this tokenization also stores offsets
tokenized = tokenize_with_metadata(dict["text"], self.tokenizer, self.max_seq_len)
return [Sample(id=None, clear_text=dict, tokenized=tokenized)]
def _sample_to_features(self, sample) -> dict:
features = sample_to_features_text(
sample=sample,
tasks=self.tasks,
max_seq_len=self.max_seq_len,
tokenizer=self.tokenizer,
)
return features
#########################################
# Processors for NER data ####
#########################################
class NERProcessor(Processor):
"""
Used to handle most NER datasets, like CoNLL or GermEval 2014
"""
def __init__(
self,
tokenizer,
max_seq_len,
data_dir,
label_list=None,
metric=None,
train_filename="train.txt",
dev_filename="dev.txt",
test_filename="test.txt",
dev_split=0.0,
delimiter="\t",
**kwargs,
):
# Custom processor attributes
self.delimiter = delimiter
super(NERProcessor, self).__init__(
tokenizer=tokenizer,
max_seq_len=max_seq_len,
train_filename=train_filename,
dev_filename=dev_filename,
test_filename=test_filename,
dev_split=dev_split,
data_dir=data_dir,
tasks={}
)
if metric and label_list:
self.add_task("ner", metric, label_list)
def _file_to_dicts(self, file: str) -> [dict]:
dicts = read_ner_file(filename=file, sep=self.delimiter)
return dicts
def _dict_to_samples(self, dict: dict, **kwargs) -> [Sample]:
# this tokenization also stores offsets, which helps to map our entity tags back to original positions
tokenized = tokenize_with_metadata(dict["text"], self.tokenizer, self.max_seq_len)
return [Sample(id=None, clear_text=dict, tokenized=tokenized)]
def _sample_to_features(self, sample) -> dict:
features = samples_to_features_ner(
sample=sample,
tasks=self.tasks,
max_seq_len=self.max_seq_len,
tokenizer=self.tokenizer,
)
return features
#####################
# LM Processors ####
#####################
class BertStyleLMProcessor(Processor):
"""
Prepares data for masked language model training and next sentence prediction in the style of BERT
"""
def __init__(
self,
tokenizer,
max_seq_len,
data_dir,
train_filename="train.txt",
dev_filename="dev.txt",
test_filename="test.txt",
dev_split=0.0,
next_sent_pred=True,
max_docs=None,
**kwargs,
):
self.delimiter = ""
self.max_docs = max_docs
super(BertStyleLMProcessor, self).__init__(
tokenizer=tokenizer,
max_seq_len=max_seq_len,
train_filename=train_filename,
dev_filename=dev_filename,
test_filename=test_filename,
dev_split=dev_split,
data_dir=data_dir,
tasks={}
)
self.next_sent_pred = next_sent_pred
self.add_task("lm", "acc", list(self.tokenizer.vocab))
if self.next_sent_pred:
self.add_task("nextsentence", "acc", ["False", "True"])
def _file_to_dicts(self, file: str) -> list:
dicts = read_docs_from_txt(filename=file, delimiter=self.delimiter, max_docs=self.max_docs)
return dicts
def _dict_to_samples(self, dict, all_dicts=None):
doc = dict["doc"]
samples = []
for idx in range(len(doc) - 1):
text_a, text_b, is_next_label = get_sentence_pair(doc, all_dicts, idx)
sample_in_clear_text = {
"text_a": text_a,
"text_b": text_b,
"nextsentence_label": is_next_label,
}
tokenized = {}
tokenized["text_a"] = tokenize_with_metadata(
text_a, self.tokenizer, self.max_seq_len
)
tokenized["text_b"] = tokenize_with_metadata(
text_b, self.tokenizer, self.max_seq_len
)
samples.append(
Sample(id=None, clear_text=sample_in_clear_text, tokenized=tokenized)
)
return samples
def _sample_to_features(self, sample) -> dict:
features = samples_to_features_bert_lm(
sample=sample, max_seq_len=self.max_seq_len, tokenizer=self.tokenizer,
next_sent_pred=self.next_sent_pred
)
return features
#########################################
# SQUAD 2.0 Processor ####
#########################################
class SquadProcessor(Processor):
""" Used to handle the SQuAD dataset"""
def __init__(
self,
tokenizer,
max_seq_len,
data_dir,
labels=None,
metric=None,
train_filename="train-v2.0.json",
dev_filename="dev-v2.0.json",
test_filename=None,
dev_split=0,
doc_stride=128,
max_query_length=64,
**kwargs,
):
"""
:param tokenizer: Used to split a sentence (str) into tokens.
:param max_seq_len: Samples are truncated after this many tokens.
:type max_seq_len: int
:param data_dir: The directory in which the train and dev files can be found. Squad has a private test file
:type data_dir: str
:param train_filename: The name of the file containing training data.
:type train_filename: str
:param dev_filename: The name of the file containing the dev data. If None and 0.0 < dev_split < 1.0 the dev set
will be a slice of the train set.
:type dev_filename: str or None
:param test_filename: None
:type test_filename: str
:param dev_split: The proportion of the train set that will sliced. Only works if dev_filename is set to None
:type dev_split: float
:param data_dir: The directory in which the train, test and perhaps dev files can be found.
:type data_dir: str
:param doc_stride: When the document containing the answer is too long it gets split into part, strided by doc_stride
:type doc_stride: int
:param max_query_length: Maximum length of the question (in number of subword tokens)
:type max_query_length: int
:param kwargs: placeholder for passing generic parameters
:type kwargs: object
"""
self.target = "classification"
self.ph_output_type = "per_token_squad"
self.doc_stride = doc_stride
self.max_query_length = max_query_length
super(SquadProcessor, self).__init__(
tokenizer=tokenizer,
max_seq_len=max_seq_len,
train_filename=train_filename,
dev_filename=dev_filename,
test_filename=test_filename,
dev_split=dev_split,
data_dir=data_dir,
tasks={},
)
if metric and labels:
self.add_task("question_answering", metric, labels)
def dataset_from_dicts(self, dicts, index=None, from_inference=False):
if(from_inference):
dicts = [self._convert_inference(x) for x in dicts]
self.baskets = [
SampleBasket(raw=tr, id="infer - {}".format(i))
for i, tr in enumerate(dicts)
]
self._init_samples_in_baskets()
self._featurize_samples()
if index == 0:
self._log_samples(3)
dataset, tensor_names = self._create_dataset()
return dataset, tensor_names
def _convert_inference(self, infer_dict):
# convert input coming from inferencer to SQuAD format
converted = {}
converted["paragraphs"] = [
{
"qas": [
{
"question": infer_dict.get("questions", ["Missing?"])[0],
"id": "unusedID",
}
],
"context": infer_dict.get("text", "Missing!"),
}
]
return converted
def _file_to_dicts(self, file: str) -> [dict]:
dict = read_squad_file(filename=file)
return dict
def _dict_to_samples(self, dict: dict, **kwargs) -> [Sample]:
# TODO split samples that are too long in this function, related to todo in self._sample_to_features
if "paragraphs" not in dict: # TODO change this inference mode hack
dict = self._convert_inference(infer_dict=dict)
samples = create_samples_squad(entry=dict)
for sample in samples:
tokenized = tokenize_with_metadata(
text=" ".join(sample.clear_text["doc_tokens"]),
tokenizer=self.tokenizer,
max_seq_len=self.max_seq_len,
)
sample.tokenized = tokenized
return samples
def _sample_to_features(self, sample) -> dict:
# TODO, make this function return one set of features per sample
features = sample_to_features_squad(
sample=sample,
tokenizer=self.tokenizer,
max_seq_len=self.max_seq_len,
doc_stride=self.doc_stride,
max_query_length=self.max_query_length,
tasks=self.tasks
)
return features
class RegressionProcessor(Processor):
"""
Used to handle a regression dataset in tab separated text + label
"""
def __init__(
self,
tokenizer,
max_seq_len,
data_dir,
train_filename="train.tsv",
dev_filename=None,
test_filename="test.tsv",
dev_split=0.1,
delimiter="\t",
quote_char="'",
skiprows=None,
label_column_name="label",
label_name="regression_label",
scaler_mean=None,
scaler_scale=None,
**kwargs,
):
# Custom processor attributes
self.delimiter = delimiter
self.quote_char = quote_char
self.skiprows = skiprows
super(RegressionProcessor, self).__init__(
tokenizer=tokenizer,
max_seq_len=max_seq_len,
train_filename=train_filename,
dev_filename=dev_filename,
test_filename=test_filename,
dev_split=dev_split,
data_dir=data_dir,
)
self.add_task(name="regression", metric="mse", label_list= [scaler_mean, scaler_scale], label_column_name=label_column_name, task_type="regression", label_name=label_name)
def _file_to_dicts(self, file: str) -> [dict]:
column_mapping = {task["label_column_name"]: task["label_name"] for task in self.tasks.values()}
dicts = read_tsv(
rename_columns=column_mapping,
filename=file,
delimiter=self.delimiter,
skiprows=self.skiprows,
quotechar=self.quote_char,
)
# collect all labels and compute scaling stats
train_labels = []
for d in dicts:
train_labels.append(float(d[self.tasks["regression"]["label_name"]]))
scaler = StandardScaler()
scaler.fit(np.reshape(train_labels, (-1, 1)))
# add to label list in regression task
self.tasks["regression"]["label_list"] = [scaler.mean_.item(), scaler.scale_.item()]
return dicts
def _dict_to_samples(self, dict: dict, **kwargs) -> [Sample]:
# this tokenization also stores offsets
tokenized = tokenize_with_metadata(dict["text"], self.tokenizer, self.max_seq_len)
# Samples don't have labels during Inference mode
if "label" in dict:
label = float(dict["label"])
scaled_label = (label - self.tasks["regression"]["label_list"][0]) / self.tasks["regression"]["label_list"][1]
dict["label"] = scaled_label
return [Sample(id=None, clear_text=dict, tokenized=tokenized)]
def _sample_to_features(self, sample) -> dict:
features = sample_to_features_text(
sample=sample,
tasks=self.tasks,
max_seq_len=self.max_seq_len,
tokenizer=self.tokenizer
)
return features