-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
1015 lines (861 loc) · 33.8 KB
/
utils.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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import os
import csv
import random
from collections import Counter
from typing import List
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
from datasets import Dataset, DatasetDict
from model import EventBertForMaskedLM
from modeling_retnet import (
RetNetModelWithLMHead,
)
from sklearn.decomposition import PCA
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.inspection import partial_dependence, permutation_importance
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (
accuracy_score,
confusion_matrix,
precision_recall_fscore_support,
roc_auc_score,
)
from sklearn.model_selection import RandomizedSearchCV
from sklearn.neural_network import MLPClassifier
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import LabelEncoder, StandardScaler
from torch.utils.data import DataLoader
from tqdm import tqdm
from transformers import (
AutoTokenizer,
BigBirdForMaskedLM,
DebertaV2ForMaskedLM,
GPT2LMHeadModel,
)
logger = logging.getLogger()
POOLING_MODES = ["mean", "max", "weighted_mean", "weighted_max"]
TIME_AWARE_METHODS = ["bert"]
LANGUAGE_MODELS = ["bert", "bigbird", "deberta-v2", "gpt2", "rwkv", "retnet"]
pooling_modes = {
"tf": ["na"],
"tf-l2": ["na"],
"tf-idf": ["na"],
"n-grams": ["na"],
"sgns": ["mean"],
"random": ["mean"],
"bert": ["mean"],
"gpt2": ["mean"],
"retnet": ["mean"],
"rwkv": ["mean"],
}
def make_path(path: str):
"""This function makes a path if it doesn't already exist."""
if not os.path.exists(path):
os.makedirs(path)
def read_sequences(
data_dir: str = "./data/train.txt",
):
if data_dir.endswith(".txt"):
with open(data_dir, "r") as f:
sequences = f.read().splitlines()
ids = [str(i) for i in range(len(sequences))]
elif data_dir.endswith(".csv"):
with open(data_dir, "r") as f:
data = list(csv.reader(f))
sequences = [row[0] for row in data]
ids = [row[1] for row in data]
else:
raise ValueError("Invalid file format. Please provide a .txt or .csv file.")
time_sequences = ["_" for _ in sequences] # a dummy time sequence
return sequences, time_sequences, ids
def extract_embeddings(
event_sequences: List[str],
time_sequences: List[str],
method: str,
modes: List[str],
max_model_input_size: int,
model_path: str,
tokenizer_path: str,
batch_size: int,
# use in recurrent embedding generation. Set as a positive number to activate recurent embedding.
recurrent_input_size: int = -1,
split_mode: str = None, # select from [None, rnn, split]
last_segment_only: bool = False,
):
"""
This function takes in a list of user event sequences and
converts them to sentence embeddings based on a specified
trained transformer-based model.
"""
if recurrent_input_size > 0:
assert max_model_input_size % recurrent_input_size == 0
model = import_trained_model(
method=method,
model_path=model_path,
)
tokenizer = import_tokenizer(
tokenizer_path=tokenizer_path,
max_model_input_size=max_model_input_size,
)
n_gpu = torch.cuda.device_count()
device = "cuda" if n_gpu > 0 else "cpu"
if n_gpu > 0:
model.to(device)
if n_gpu > 1:
model = torch.nn.DataParallel(model)
# build data loader to accelerate parallel computation
use_time = time_sequences and time_sequences[0]
if use_time:
data = Dataset.from_dict({"text": event_sequences, "time": time_sequences})
else:
data = Dataset.from_dict({"text": event_sequences})
dataset = DatasetDict()
dataset["eval"] = data
def _preprocess_for_masked_language_modeling(raw_sample: dict) -> dict:
tokenizer_output = tokenizer(
raw_sample["text"],
padding="max_length",
truncation=True,
return_tensors="pt",
max_length=max_model_input_size,
)
sample = {
"input_ids": tokenizer_output["input_ids"],
"attention_mask": tokenizer_output["attention_mask"],
}
if use_time:
sample["time"] = raw_sample["time"]
return sample
num_samples = len(event_sequences)
# Generate a variable "batches" as a list of batch,
# Each batch is a list of samples, where each sample is a dictionary:
# e.g., {"event": "E1 E2 E3", "time": "T1 T2 T3"}
batches = [
[
{"event": event_sequences[j], "time": time_sequences[j]}
if time_sequences
else {"event": event_sequences[j]}
for j in range(i, min(i + batch_size, num_samples))
]
for i in range(0, num_samples, batch_size) # drop the last batch
]
dataset = dataset.map(
_preprocess_for_masked_language_modeling,
batched=True,
remove_columns=["text"],
num_proc=16,
)
dataset.set_format("torch")
data_loader = DataLoader(dataset["eval"], batch_size=batch_size * n_gpu)
aggregated_embeddings = {}
for mode in modes:
aggregated_embeddings[mode] = torch.empty(0)
with torch.no_grad():
for batch_inputs in tqdm(data_loader, desc="Processing batches"):
if use_time and method in TIME_AWARE_METHODS:
batch_inputs["time_ids"] = inputs["time_ids"].to(
batch_inputs["input_ids"].device
)
if split_mode is None:
last_layer_embeddings = (
model(**batch_inputs).hidden_states[-1].cpu().detach()
)
elif split_mode == "state":
outputs = model(**batch_inputs, forward_impl="chunkwise")
prev_states = outputs.prev_states[-1]
last_layer_embeddings = prev_states.mean(dim=1)
bs = last_layer_embeddings.shape[0]
last_layer_embeddings = last_layer_embeddings.reshape([bs, 1, -1])
elif split_mode == "rnn":
prev_states = []
num_segment = max_model_input_size // recurrent_input_size
all_hidden_states = []
for i in range(num_segment):
segment_input_ids = batch_inputs["input_ids"][
:, recurrent_input_size * i : recurrent_input_size * (i + 1)
]
segment_attention_mask = batch_inputs["attention_mask"][
:, recurrent_input_size * i : recurrent_input_size * (i + 1)
]
rnn_out = model(
input_ids=segment_input_ids,
attention_mask=segment_attention_mask,
forward_impl="chunkwise",
prev_states=prev_states,
use_cache=True,
sequence_offset=i * recurrent_input_size,
output_hidden_states=True,
)
all_hidden_states.append(rnn_out.hidden_states[-1].cpu().detach())
prev_states = rnn_out.prev_states
all_hidden_states = (
all_hidden_states[-1] if last_segment_only else all_hidden_states
)
last_layer_embeddings = torch.concat(all_hidden_states, dim=1)
elif split_mode == "split":
num_segment = max_model_input_size // recurrent_input_size
all_hidden_states = []
for i in range(num_segment):
if last_segment_only and (i < num_segment - 1):
continue
segment_input_ids = batch_inputs["input_ids"][
:, recurrent_input_size * i : recurrent_input_size * (i + 1)
]
segment_attention_mask = batch_inputs["attention_mask"][
:, recurrent_input_size * i : recurrent_input_size * (i + 1)
]
split_outputs = (
model(
input_ids=segment_input_ids,
attention_mask=segment_attention_mask,
)
.hidden_states[-1]
.cpu()
.detach()
)
all_hidden_states.append(split_outputs)
last_layer_embeddings = torch.concat(all_hidden_states, dim=1)
else:
raise ValueError("Only support split_node of [None, rnn, split]")
for mode in modes:
if mode == "mean":
embedding = torch.mean(last_layer_embeddings, dim=1)
elif mode == "max":
embedding = torch.max(last_layer_embeddings, dim=1)[0]
elif mode == "cls":
embedding = last_layer_embeddings[:, 0, :]
elif mode == "no_pooling":
embedding = last_layer_embeddings
elif mode == "last_seg_mean":
embedding = torch.mean(
last_layer_embeddings[:, -recurrent_input_size:, :], dim=1
)
elif mode == "last":
# find the last non-padding token and take its embedding as sequence embedding
sequence_lengths = (
torch.eq(batch_inputs["input_ids"], tokenizer.pad_token_id)
.long()
.argmax(-1)
- 2
).to(last_layer_embeddings.device)
embedding = last_layer_embeddings[
torch.arange(
batch_inputs["input_ids"].shape[0],
device=last_layer_embeddings.device,
),
sequence_lengths,
]
else:
weights_tensor = torch.tensor(
get_weights(last_layer_embeddings.shape[1])
).to(last_layer_embeddings.device)
weighted_last_layer_embeddings = (
last_layer_embeddings * weights_tensor.view(1, -1, 1)
)
if mode == "weighted_mean":
embedding = torch.mean(weighted_last_layer_embeddings, dim=1)
elif mode == "weighted_max":
embedding = torch.max(weighted_last_layer_embeddings, dim=1)[0]
aggregated_embeddings[mode] = torch.cat(
(aggregated_embeddings[mode], embedding.cpu()), dim=0
)
return aggregated_embeddings
def get_data_split_indices(
labels: list, test_size: float = 0.2, random_state: int = 42
):
"""
This function splits a list into train and test partitions,
and returns their respective indices.
"""
numbers = list(range(len(labels)))
random.seed(random_state)
test_ind = random.sample(numbers, k=len(labels) * test_size)
train_ind = list(set(numbers) - set(test_ind))
return train_ind, test_ind
def get_weights(num_tokens):
"""
This function computes weights for the tokens in an event sequence.
The weights increase linearly from left to right.
"""
weights = np.arange(1, num_tokens + 1)
weights = weights / np.sum(weights)
return weights
def aggregate_token_embeddings_by_mode(embeddings, mode):
"""
This function takes in a list of token embeddings, and
aggregates these embeddings into sentence embeddings,
based on mean, max, weighted_mean, and weighted_max.
This is used for the baseline models: sgns and random.
"""
if len(embeddings) > 0:
if mode == "mean":
return np.mean(embeddings, axis=0)
elif mode == "max":
return np.max(embeddings, axis=0)
else:
weights = get_weights(len(embeddings[0]))
weighted_embeddings = [
weight * embedding for weight, embedding in zip(weights, embeddings)
]
if mode == "weighted_mean":
return np.mean(weighted_embeddings, axis=0)
elif mode == "weighted_max":
return np.max(weighted_embeddings, axis=0)
else:
return None
def aggregate_sgns_embedding(sequence, sgns_model, mode):
"""
This function takes in an event sequence, a trained sgns model,
a specified aggregation mode, and return the corresponding
aggregated sentence embedding.
"""
if len(sequence.split(" ")) == 0:
embeddings = np.zeros(sgns_model.vector_size)
return embeddings
else:
embeddings = []
for token in sequence.split(" "):
if token in sgns_model.wv.index_to_key:
embeddings.append(sgns_model.wv[token])
else:
embeddings.append(np.zeros(sgns_model.vector_size))
return aggregate_token_embeddings_by_mode(embeddings, mode)
def create_random_embeddings_dict(sequences, embedding_size=768):
"""
This function creates a dictionary of random embeddings, based on
an input list of event sequences.
"""
unique_tokens = list(
set(token for sequence in sequences for token in sequence.split(" "))
)
random_embeddings_dict = {}
np.random.seed(42)
for token in unique_tokens:
random_embeddings_dict[token] = np.random.uniform(
low=-0.05, high=0.05, size=embedding_size
)
return random_embeddings_dict
def aggregate_random_embedding(sequence, dictionary, mode, embedding_size):
"""
This function aggregates token-level random embeddings into
a sentence-level embedding.
"""
embedding_size = len(dictionary[next(iter(dictionary))])
if len(sequence.split(" ")) == 0:
embeddings = np.zeros(embedding_size)
return embeddings
else:
embeddings = []
for token in sequence.split(" "):
if token in dictionary:
embeddings.append(dictionary[token])
else:
embeddings.append(np.zeros(embedding_size))
return aggregate_token_embeddings_by_mode(embeddings, mode)
def trim_event_sequences(
event_sequences: list, time_sequences: list, max_model_input_size: int
):
"""
This function trims event sequences into a specified length.
"""
event_sequences_split_and_trimmed = [
" ".join(sequence.split(" ")[max_model_input_size - 1 :: -1])
for sequence in event_sequences
]
time_sequence_split_and_trimmed = [
sequence[max_model_input_size - 1 :: -1] for sequence in time_sequences
]
return event_sequences_split_and_trimmed, time_sequence_split_and_trimmed
def feature_engineering(
train_event_sequences: list,
test_event_sequences: list,
train_time_sequences: list,
test_time_sequences: list,
modes: List[str],
method: str = "tf",
max_model_input_size: int = 512,
model_path: str = None,
tokenizer_path: str = None,
n_pca_dims: int = -1,
batch_size: int = 32,
# use in recurrent embedding generation. Set as a positive number to activate recurrent embedding.
recurrent_input_size: int = -1,
split_mode: str = None,
last_segment_only: bool = False,
**kwargs,
):
"""
This function turns input event sequences into numerical representations.
"""
assert method in [
"tf",
"tf-l2",
"tf-idf",
"n-grams",
"sgns",
"random",
"bert",
"bigbird",
"deberta-v2",
"retnet",
"rwkv",
"gpt2",
]
# truncate sequences with max_model_input_size
train_event_sequences = [
" ".join(seq.split()[-max_model_input_size:]) for seq in train_event_sequences
]
test_event_sequences = [
" ".join(seq.split()[-max_model_input_size:]) for seq in test_event_sequences
]
train_time_sequences = [seq[-max_model_input_size:] for seq in train_time_sequences]
test_time_sequences = [seq[-max_model_input_size:] for seq in test_time_sequences]
train_features = {}
test_features = {}
if method == "tf":
vectorizer = CountVectorizer()
train_features["na"] = vectorizer.fit_transform(train_event_sequences).toarray()
if test_event_sequences:
test_features["na"] = vectorizer.transform(test_event_sequences).toarray()
elif method == "n-grams":
vectorizer = CountVectorizer(ngram_range=(1, 4))
train_features["na"] = vectorizer.fit_transform(train_event_sequences).toarray()
if test_event_sequences:
test_features["na"] = vectorizer.transform(test_event_sequences).toarray()
elif method == "tf-l2":
vectorizer = TfidfVectorizer(use_idf=False, norm="l2")
train_features["na"] = vectorizer.fit_transform(train_event_sequences).toarray()
if test_event_sequences:
test_features["na"] = vectorizer.transform(test_event_sequences).toarray()
elif method == "tf-idf":
vectorizer = TfidfVectorizer()
train_features["na"] = vectorizer.fit_transform(train_event_sequences).toarray()
if test_event_sequences:
test_features["na"] = vectorizer.transform(test_event_sequences).toarray()
elif method == "random":
assert all([mode in POOLING_MODES for mode in modes])
random_embeddings_dict = create_random_embeddings_dict(train_event_sequences)
embedding_size = len(random_embeddings_dict[next(iter(random_embeddings_dict))])
for mode in modes:
train_features[mode] = [
aggregate_random_embedding(
sequence, random_embeddings_dict, mode, embedding_size
)
for sequence in tqdm(train_event_sequences)
]
if test_event_sequences:
test_features[mode] = [
aggregate_random_embedding(
sequence, random_embeddings_dict, mode, embedding_size
)
for sequence in tqdm(test_event_sequences)
]
elif method in LANGUAGE_MODELS:
train_features = extract_embeddings(
event_sequences=train_event_sequences,
time_sequences=train_time_sequences,
method=method,
modes=modes,
max_model_input_size=max_model_input_size,
model_path=model_path,
tokenizer_path=tokenizer_path,
batch_size=batch_size,
recurrent_input_size=recurrent_input_size,
split_mode=split_mode,
)
if test_event_sequences:
test_features = extract_embeddings(
event_sequences=test_event_sequences,
time_sequences=test_time_sequences,
method=method,
modes=modes,
max_model_input_size=max_model_input_size,
model_path=model_path,
tokenizer_path=tokenizer_path,
batch_size=batch_size,
recurrent_input_size=recurrent_input_size,
split_mode=split_mode,
)
if n_pca_dims > 0:
# only perform PCA-based dimension reduction when the target dimension is smaller than
for mode in train_features.keys():
if n_pca_dims < len(train_features[mode][0]):
pca = PCA(n_components=n_pca_dims)
train_features[mode] = pca.fit_transform(train_features[mode])
test_features[mode] = pca.transform(test_features[mode])
if method in ["tf", "tf-l2", "tf-idf"]:
feature_names = vectorizer.get_feature_names_out().tolist()
else:
feature_names = []
return train_features, test_features, feature_names
def classifier_tune_and_fit(
classifier: str,
train_features: list,
test_features: list,
train_labels: list,
test_labels: list,
tune_n_iter: int,
n_class: int,
negative_label: str,
class_list: list,
task: str,
feature_names: list,
save_partial_dependence_results: bool,
save_feature_importance_results: bool,
save_predictions: bool,
):
"""
This function does hyperparameter tuning for a specified classifier
using random search and k-fold cross validation, fits the best
hyperparameter values to the complete data, and returns classification
results (accuracy, f1, precision, recall, auc). In addition,
for multiclass prediction, a confusion matrix is returned.
User can also specify the model to save partial dependence results,
feature importance scores, and individual predictions.
"""
assert classifier in ["lr", "mlp"]
label_encoder = LabelEncoder()
label_encoder.fit(train_labels + test_labels)
if n_class > 2:
average_choice = "macro"
scoring = "f1_macro"
train_labels = label_encoder.transform(train_labels)
test_labels = label_encoder.transform(test_labels)
else:
average_choice = "binary"
scoring = "f1"
train_labels = [0 if label == negative_label else 1 for label in train_labels]
test_labels_original = test_labels
test_labels = [0 if label == negative_label else 1 for label in test_labels]
class_list = [
class_name for class_name in class_list if class_name != negative_label
]
if classifier == "lr":
pipe = Pipeline(
[("scaler", StandardScaler()), ("clf", LogisticRegression())]
) #
param_distributions = {
"clf__penalty": ["l1", "l2", "elasticnet"],
"clf__C": np.logspace(-4, 4, 20),
"clf__l1_ratio": np.arange(0.1, 1, 0.1),
"clf__solver": ["saga"],
"clf__max_iter": [2000],
"clf__random_state": [42],
"clf__class_weight": ["balanced"],
"clf__n_jobs": [-1],
}
else:
pipe = Pipeline([("scaler", StandardScaler()), ("clf", MLPClassifier())]) #
param_distributions = {
"clf__hidden_layer_sizes": [(100,), (50, 50), (20, 20, 20)],
"clf__activation": ["tanh", "relu"],
"clf__solver": ["adam", "sgd"],
"clf__alpha": np.logspace(-5, 3, 9),
"clf__learning_rate": ["constant", "invscaling", "adaptive"],
"clf__max_iter": [2000],
"clf__random_state": [42],
"clf__early_stopping": [True, False],
}
# oversampler = RandomOverSampler(
# sampling_strategy="not majority", random_state=42
# )
# train_features, train_labels = oversampler.fit_resample(
# train_features, train_labels
# )
random_search = RandomizedSearchCV(
pipe,
param_distributions=param_distributions,
n_iter=tune_n_iter,
cv=5,
random_state=42,
n_jobs=-1,
scoring=scoring,
)
random_search.fit(train_features, train_labels)
best_estimator = random_search.best_estimator_
best_estimator.fit(train_features, train_labels)
if save_partial_dependence_results:
clf = best_estimator.named_steps.clf
n_features = len(feature_names)
partial_dependence_df = pd.DataFrame()
for feature_index in range(n_features):
partial_dependence_results = partial_dependence(
clf,
features=[feature_index],
X=train_features,
percentiles=(0, 1),
grid_resolution=10,
)
df_subset = pd.DataFrame()
df_subset["y"] = partial_dependence_results["average"][0]
df_subset["x"] = partial_dependence_results["values"][0]
df_subset["feature"] = feature_names[feature_index]
partial_dependence_df = pd.concat(
[partial_dependence_df, df_subset], axis=0, ignore_index=True
)
else:
partial_dependence_df = pd.DataFrame()
if save_feature_importance_results:
clf = best_estimator.named_steps.clf
feature_importances_df = pd.DataFrame()
for metric in ["roc_auc", "f1", "precision", "recall", "accuracy"]:
feature_importances_results = permutation_importance(
clf,
test_features,
test_labels,
n_repeats=10,
random_state=42,
scoring=metric,
n_jobs=20,
)
feature_importances_results = pd.DataFrame(
{
"importances_mean": feature_importances_results["importances_mean"],
"importances_std": feature_importances_results["importances_std"],
"metric": metric,
"feature": feature_names,
}
)
feature_importances_df = pd.concat(
[feature_importances_df, feature_importances_results],
axis=0,
ignore_index=True,
)
else:
feature_importances_df = pd.DataFrame()
predictions = best_estimator.predict(test_features)
pred_probs = best_estimator.predict_proba(test_features)
if save_predictions and n_class == 2:
predictions_df = pd.DataFrame(
{
"prediction": predictions,
"pred_prob": pred_probs[:, 1],
"label": test_labels,
}
)
else:
predictions_df = pd.DataFrame()
accuracy = accuracy_score(test_labels, predictions)
precision, recall, f1, _ = precision_recall_fscore_support(
test_labels, predictions, average=average_choice
)
results_per_class = {}
# compute auc and class- or label_name-level performance (if relevant)
if n_class > 2:
class_counts = Counter(label_encoder.inverse_transform(test_labels))
auc = roc_auc_score(
test_labels, pred_probs, average=average_choice, multi_class="ovo"
)
precisions, recalls, f1s, _ = precision_recall_fscore_support(
test_labels, predictions, average=None
)
aucs = roc_auc_score(test_labels, pred_probs, multi_class="ovr", average=None)
class_list = label_encoder.classes_.tolist()
for class_idx, class_name in enumerate(class_list):
results_per_class[f"precision_{class_name}"] = precisions[class_idx]
results_per_class[f"recall_{class_name}"] = recalls[class_idx]
results_per_class[f"f1_{class_name}"] = f1s[class_idx]
results_per_class[f"auc_{class_name}"] = aucs[class_idx]
results_per_class[f"n_{class_name}_for_metrics"] = class_counts[class_name]
cm = confusion_matrix(test_labels, predictions)
df_cm = pd.DataFrame(cm, index=class_list, columns=class_list)
else:
auc = roc_auc_score(test_labels, pred_probs[:, 1])
if len(class_list) >= 2:
# if it's a binary prediction task and there are at least two unique label_names
# that are not the negative label; allows us to look at label_name level performance
for class_name in class_list:
indices_positive_class = [
index
for index, label in enumerate(test_labels_original)
if label == class_name
]
indices_negative_class = [
index
for index, label in enumerate(test_labels_original)
if label == negative_label
]
random.seed(42)
indices_negative_class = random.sample(
indices_negative_class, k=len(indices_positive_class)
)
class_pred_prob = [
pred_probs[:, 1][index] for index in indices_positive_class
] + [pred_probs[:, 1][index] for index in indices_negative_class]
class_predictions = [
predictions[index] for index in indices_positive_class
] + [predictions[index] for index in indices_negative_class]
class_test_labels = [1] * len(indices_positive_class) + [0] * len(
indices_negative_class
)
(
class_precision,
class_recall,
class_f1,
_,
) = precision_recall_fscore_support(
class_test_labels, class_predictions, average=average_choice
)
class_auc = roc_auc_score(class_test_labels, class_pred_prob)
results_per_class[f"precision_{class_name}"] = class_precision
results_per_class[f"recall_{class_name}"] = class_recall
results_per_class[f"f1_{class_name}"] = class_f1
results_per_class[f"auc_{class_name}"] = class_auc
results_per_class[f"n_{class_name}_for_metrics"] = len(
indices_positive_class
)
df_cm = pd.DataFrame()
results = {
"accuracy": accuracy,
"precision": precision,
"recall": recall,
"f1": f1,
"auc": auc,
}
results.update(results_per_class)
return results, df_cm, partial_dependence_df, feature_importances_df, predictions_df
def split_sequence(event_sequence, time_sequence, max_len=510):
"""
This function splits an event sequence into multiple chunks,
if the length of the sequence is at least twice the max_len.
"""
n = len(event_sequence)
assert n >= max_len * 2
event_seqs = []
time_seqs = []
i = 0
while i < n // max_len:
if i == 0:
start = 0
end = max_len
else:
start = end
end = end + max_len
event = event_sequence[start:end]
time = time_sequence[start:end]
event_seqs.append(" ".join(event))
time_seqs.append(time)
i = i + 1
return event_seqs, time_seqs
def get_split_event_sequences_sample(
event_sequences, time_sequences, max_len=510, sample_size=100
):
"""
This function takes in a list of event sequences, reads up to sample_size,
splits these sequences into two or multiple ones, and returns both the split
sequences and their respective ghost_user_ids.
"""
split_event_sequences_sample = []
split_time_sequences_sample = []
user_id_ls = []
for i in range(sample_size):
try:
split_event_sequences, split_time_sequences = split_sequence(
event_sequences[i].split(), time_sequences[i], max_len=max_len
)
split_event_sequences_sample += split_event_sequences
split_time_sequences_sample += split_time_sequences
user_id_ls += [i] * len(split_event_sequences)
except:
pass
return split_event_sequences_sample, split_time_sequences_sample, user_id_ls
def extract_model_name(string):
"""
This function extract model name based on a string (e.g., model paths).
"""
if "deberta-v2" in string:
return "deberta-v2"
elif "bigbird" in string:
return "bigbird"
elif "gpt2" in string:
return "gpt2"
elif "rwkv" in string:
return "rwkv"
elif "retnet" in string:
return "retnet"
else:
return "bert"
def get_available_tasks(scope: str = "all"):
"""
This function returns all the downstream tasks we have atm.
Note that the same function exists in utils_preproccess.py.
Remember to synchronize the two functions.
"""
assert scope in ["all", "test", "downstream"]
all_tasks = [
"train",
"test_reported_user_prediction",
"test_locked_user_prediction",
"test_account_self_deletion_prediction",
"test_ad_click_binary_prediction",
"test_prop_of_ad_clicks_prediction",
"test_ad_view_time_prediction",
]
if scope == "test":
all_tasks.remove("train")
elif scope == "downstream":
all_tasks.remove("train")
all_tasks.remove("test_prop_of_ad_clicks_prediction")
return all_tasks
def import_tokenizer(
tokenizer_path: str,
max_model_input_size: int,
):
"""
This function imports a trained tokenizer from gcs.
"""
return AutoTokenizer.from_pretrained(
tokenizer_path,
model_max_length=max_model_input_size,
trust_remote_code=True,
)
def import_trained_model(
method: str,
model_path: str,
):
"""
This function imports a trained transformer-based model from gcs.
"""
# download the model from GCP if not exists locally
CLASS_MAP = {
"bert": EventBertForMaskedLM,
"bigbird": BigBirdForMaskedLM,
"deberta-v2": DebertaV2ForMaskedLM,
"retnet": RetNetModelWithLMHead,
"gpt2": GPT2LMHeadModel,
}
if method in CLASS_MAP:
model = CLASS_MAP[method].from_pretrained(
model_path, output_hidden_states=True
)
else:
raise Exception(
"Currently, only bert, bigbird and deberta-v2 are supported."
)
return model
def strip_and_split_by_space(string):
"""
This function strips a string and splits it into a list.
"""
return string.strip().split(" ")
def truncate_event_and_time_sequence(
event_sequences,
time_sequences,
get_random_segment: bool = False,
length_to_keep: int = 512,
):