-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_run_sglue.py
1237 lines (1054 loc) · 52.6 KB
/
script_run_sglue.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 os, pdb
# ______________________________________NLPDV____________________________________
# _______________________________________________________________________
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from transformers import *
import _pickle as pkl
import shutil
import numpy as np
from tqdm import trange, tqdm
# For full_training set None
# _______________________________________________________________________
# ______________________________________NLPDV____________________________________
# Model params
GLUE_DIR = '/home/rizwan/NLPDV/glue/'
run_file = './examples/run_sglue.py'
# run_file= './examples/data_valuation.py'
model_type = 'bert'
train_model_name_or_path = 'bert-base-cased' # 'bert-large-uncased-whole-word-masking'
do_lower_case = False
num_train_epochs = 1.0
num_eval_epochs = 2.0
per_gpu_eval_batch_size = 8
per_gpu_train_batch_size = 32
learning_rate = 5e-5
max_seq_length = 128
fp16 = True
overwrite_cache = False
# Task param
train_task_name = 'SNLI'
eval_task_name = 'SNLI'
# CUDA gpus
CUDA_VISIBLE_DEVICES = [6,7]
# _______________________________________________________________________
# ______________________________________NLPDV____________________________________
# Debug data size
train_data_size = 20000
eval_data_size = 2000
cluster_size = 10
cluster_num = train_data_size // cluster_size
# Seed
seed = 43
max_iter = 50
save_every = 1
# total _iter = max_iter x save_every
tolerance = 0.2
err = 0.3
# _______________________________________________________________________
# ______________________________________NLPDV____________________________________
# Shapley params
metric = 'acc'
sources = np.array([i // cluster_size for i in range(train_data_size)])
loo_run = True
tmc_run = True
g_run = False
load_removing_performance_plot = load_adding_performance_plot = True
overwrite_directory = False # True when just load shapley's when to plot only
load_shapley = False
train_output_dir = 'temp/' + train_task_name + '_output/' # +str(seed)+'/' # For full training set train_output_dir = 'temp/' + train_task_name + '_output_full_training/'
eval_output_dir = 'temp/' + eval_task_name + '_output/' # +str(seed)+'/' # For full training set eval_output_dir = 'temp/' + eval_task_name + '_output_full_training/'
directory = train_output_dir
indices_to_delete_file_path = directory + '/indices_to_delete_file_path_' + str(seed) + '.json'
n_points_file = os.path.join(train_output_dir, "training_results" + ".txt")
ALL_BINARY_TASKS = ['snli', 'qqp', 'qnli', 'mnli-fiction', 'mnli-travel', 'mnli-slate', 'mnli-government',
'mnli-telephone']
if eval_task_name == 'MNLI': ALL_BINARY_TASKS = ['snli', 'qqp', 'qnli']
DOMAIN_TRANSFER = True
# _______________________________________________________________________
# ______________________________________NLPDV____________________________________
if eval_task_name.lower() in ALL_BINARY_TASKS: ALL_BINARY_TASKS.remove(eval_task_name.lower())
if DOMAIN_TRANSFER:
sources = np.arange(len(ALL_BINARY_TASKS))
if load_shapley:
tmc_run = False
train_data_dir = GLUE_DIR
eval_data_dir = GLUE_DIR
name = train_task_name + '_' + eval_task_name
tol = None
mean_score = None
np.random.seed(seed)
run_command = "CUDA_VISIBLE_DEVICES=" + str(CUDA_VISIBLE_DEVICES[0])
for i in CUDA_VISIBLE_DEVICES[1:]:
run_command += ',' + str(i)
run_command += ' python '
if len(CUDA_VISIBLE_DEVICES) > 1: run_command += '-m torch.distributed.launch --nproc_per_node ' \
+ str(len(CUDA_VISIBLE_DEVICES))
run_command += ' ' + run_file + ' ' + ' --model_type ' + model_type + \
' --max_seq_length ' + str(max_seq_length) + ' --per_gpu_eval_batch_size=' + str(
per_gpu_eval_batch_size) + \
' --per_gpu_train_batch_size=' + str(per_gpu_train_batch_size) + ' --learning_rate ' + str(learning_rate) \
+ ' --overwrite_output_dir '
if do_lower_case: run_command += '--do_lower_case '
if fp16: run_command += ' --fp16 '
if overwrite_cache:
run_command += ' --overwrite_cache '
# For training:
train_run_command_full = run_command + ' --do_train --task_name ' + train_task_name + \
' --data_dir ' + train_data_dir + ' --output_dir ' + \
train_output_dir + ' --model_name_or_path ' + train_model_name_or_path
train_run_command = train_run_command_full + ' --data_size ' + str(train_data_size)
# For eval:
eval_run_command_full = run_command + ' --do_eval --task_name ' + eval_task_name + \
' --data_dir ' + eval_data_dir + ' --output_dir ' + eval_output_dir + \
' --model_name_or_path ' + train_output_dir
eval_run_command = eval_run_command_full + ' --data_size ' + str(eval_data_size)
small_performance_dict = {}
full_performance_dict = {}
n_points = None
if eval_task_name =='MNLI-mm' and 'full_training' in train_output_dir:
small_performance_dict = full_performance_dict = {
'snli_qqp_qnli_mnli-fiction_mnli-travel_mnli-slate_mnli-government_mnli-telephone': 0.8905,
'qqp_qnli_mnli-fiction_mnli-travel_mnli-slate_mnli-government_mnli-telephone': 0.8875,
'snli_qnli_mnli-fiction_mnli-travel_mnli-slate_mnli-government_mnli-telephone': 0.8875,
'snli_qqp_mnli-fiction_mnli-travel_mnli-slate_mnli-government_mnli-telephone': 0.8875,
'snli_qqp_qnli_mnli-travel_mnli-slate_mnli-government_mnli-telephone': 0.8875,
'snli_qqp_qnli_mnli-fiction_mnli-slate_mnli-government_mnli-telephone': 0.8875,
'snli_qqp_qnli_mnli-fiction_mnli-travel_mnli-government_mnli-telephone': 0.8875,
'snli_qqp_qnli_mnli-fiction_mnli-travel_mnli-slate_mnli-telephone': 0.8875,
'snli_qqp_qnli_mnli-fiction_mnli-travel_mnli-slate_mnli-government': 0.8875}
if eval_task_name =='QNLI' and 'full_training' in train_output_dir:
baseline_value = 0.508
mean_score = 0.5084249999999999
random_score = 0.5085
tol = 0.010168917100655303
n_points = len(sources)
# for SNLI:
# full_performance_dict = {'mnli-fiction': 0.8782767730136151, 'mnli-fiction_mnli-government': 0.8807153017679333,
# 'mnli-fiction_mnli-slate_mnli-government': 0.8877260719365982,
# 'mnli-fiction_mnli-travel_mnli-slate_mnli-government': 0.883357041251778,
# 'mnli-fiction_mnli-travel_mnli-slate_mnli-government_mnli-telephone': 0.8908758382442593,
# 'qqp_mnli-fiction_mnli-travel_mnli-slate_mnli-government_mnli-telephone': 0.8906726275147328}
#For MNLI matched:
# full_performance_dict: {'snli': 0.8231278655119715, 'snli_qqp': 0.8187468160978095, 'snli_qqp_qnli': 0.8079470198675497}
# _______________________________________________________________________
# ______________________________________NLPDV____________________________________
def _which_parallel(directory):
'''Prevent conflict with parallel runs.'''
previous_results = os.listdir(directory)
tmc_nmbrs = [int(name.split('.')[-2].split('_')[-1])
for name in previous_results if 'mem_tmc' in name]
g_nmbrs = [int(name.split('.')[-2].split('_')[-1])
for name in previous_results if 'mem_g' in name]
tmc_number = str(np.max(tmc_nmbrs) + 1) if len(tmc_nmbrs) else '0'
g_number = str(np.max(g_nmbrs) + 1) if len(g_nmbrs) else '0'
return tmc_number, g_number
def write_indices_to_delete(indices_to_delete_file_path, ids):
with open(indices_to_delete_file_path, "w") as writer:
print(f"***** Writing ids to {str(indices_to_delete_file_path)} *****", flush=True)
for id in ids:
writer.write("%s " % (id))
def _create_results_placeholder(directory, tmc_number, mem_tmc, idxs_tmc, g_number=None, mem_g=None, idxs_g=None):
tmc_dir = os.path.join(
directory,
'mem_tmc_{}.pkl'.format(tmc_number.zfill(4))
)
pkl.dump({'mem_tmc': mem_tmc, 'idxs_tmc': idxs_tmc},
open(tmc_dir, 'wb'))
if mem_g and idxs_g and g_number:
g_dir = os.path.join(
directory,
'mem_g_{}.pkl'.format(g_number.zfill(4))
)
pkl.dump({'mem_g': mem_g, 'idxs_g': idxs_g}, open(g_dir, 'wb'))
def _calculate_loo_vals(sources, baseline_value, n_points, eval_output_dir, train_run_command, eval_run_command,
n_points_file):
"""Calculated leave-one-out values for the given metric.
Args:
metric: If None, it will use the objects default metric.
sources: If values are for sources of data points rather than
individual points. In the format of an assignment array
or dict.
Returns:
Leave-one-out scores
"""
print('Starting LOO score calculations!', flush=True)
vals_loo = np.zeros(n_points)
counter = 0
for i in tqdm(sources.keys()):
''' write the ids in a file so that model codes can access it.
train model on the dataset exclusive the sources ids
parse result and calculate the result_score or removed_value'''
counter += 1
print('=' * 50, flush=True)
print(f'Calculating LOO score for {counter}/{len(sources)}!', flush=True)
print('=' * 50, flush=True)
data_combination = sorted(list(sources.keys()))
data_combination.remove(i)
data_combination_name = '_'.join([ALL_BINARY_TASKS[id] for id in data_combination])
removed_value = None
if data_combination_name in small_performance_dict:
removed_value = small_performance_dict[data_combination_name]
else:
write_indices_to_delete(indices_to_delete_file_path, sources[i])
command = train_run_command + ' --LOO --seed ' + str(
seed) + ' --indices_to_delete_file_path ' + indices_to_delete_file_path
print(command, flush=True)
os.system(command)
# parse file and set n_points
with open(n_points_file, "r") as reader:
for line in reader:
line = line.strip().split()
key = line[0]
value = line[-1]
if key == 'n_points':
n_points2 = int(value)
command = eval_run_command + ' --seed ' + str(seed) + ' '
print(command)
os.system(command)
# parse file and set n_points
output_eval_file = os.path.join(eval_output_dir, "eval_results.txt")
with open(output_eval_file, "r") as reader:
for line in reader:
line = line.strip().split()
key = line[0]
value = line[-1]
if key in ['acc']:
removed_value = float(value)
if not removed_value: pdb.set_trace()
print('baseline_value: ', baseline_value, 'removed value: ', removed_value, 'baseline_value - removed_value: ', baseline_value - removed_value, flush=True)
vals_loo[sources[i]] = (baseline_value - removed_value)
vals_loo[sources[i]] /= len(sources[i])
small_performance_dict.update({data_combination_name: removed_value})
print("After Loo: small_performance_dict: ", small_performance_dict, 'full_performance_dict:',
full_performance_dict, flush=True)
return vals_loo
def save_results(directory, vals_loo, tmc_number, mem_tmc, idxs_tmc, \
g_number=None, mem_g=None, idxs_g=None, sources=None, overwrite=False, n_points=None, tol=None, \
baseline_value=None, random_score=None, mean_score=None):
"""Saves results computed so far."""
if directory is None:
return
loo_dir = os.path.join(directory, 'loo.pkl')
if not os.path.exists(loo_dir) or overwrite:
data_dict = {'loo': vals_loo, 'n_points': n_points, 'tol': tol, 'sources': sources, \
'baseline_value': baseline_value, 'mean_score': mean_score, 'random_score': random_score}
pkl.dump(data_dict, open(loo_dir, 'wb'))
performance_dir = os.path.join(directory, 'perf.pkl')
pkl.dump({'small_performance_dict': small_performance_dict, 'full_performance_dict': full_performance_dict}, \
open(performance_dir, 'wb'))
tmc_dir = os.path.join(
directory,
'mem_tmc_{}.pkl'.format(tmc_number.zfill(4))
)
pkl.dump({'mem_tmc': mem_tmc, 'idxs_tmc': idxs_tmc},
open(tmc_dir, 'wb'))
if g_number and mem_g and idxs_g:
g_dir = os.path.join(
directory,
'mem_g_{}.pkl'.format(g_number.zfill(4))
)
pkl.dump({'mem_g': mem_g, 'idxs_g': idxs_g},
open(g_dir, 'wb'))
def error(mem, min_convergence_iter=50):
if len(mem) < min_convergence_iter:
return 1.0
# if min_convergence_iter>50: min_convergence_iter= len(mem)
all_vals = (np.cumsum(mem, 0) / np.reshape(np.arange(1, len(mem) + 1), (-1, 1)))[
-min_convergence_iter:] # (100 or min_convergence_iter last iterations, train size)
errors = np.mean(np.abs(all_vals[-min_convergence_iter:] - all_vals[-1:]) / (np.abs(all_vals[-1:]) + 1e-12),
-1) # (100 or min_convergence_iter last iterations)
return np.max(errors)
def _merge_parallel_results(key, directory, n_points, sources, max_samples=None):
"""Helper method for 'merge_results' method."""
numbers = [name.split('.')[-2].split('_')[-1]
for name in os.listdir(directory)
if 'mem_{}'.format(key) in name]
mem = np.zeros((0, n_points))
n_sources = n_points if sources is None else len(sources)
idxs = np.zeros((0, n_sources), int)
vals = np.zeros(n_points)
counter = 0.
for number in numbers:
if max_samples is not None:
if counter > max_samples:
break
samples_dir = os.path.join(
directory,
'mem_{}_{}.pkl'.format(key, number)
)
print(samples_dir, flush=True)
dic = pkl.load(open(samples_dir, 'rb'))
if not len(dic['mem_{}'.format(key)]):
continue
mem = np.concatenate([mem, dic['mem_{}'.format(key)]])
idxs = np.concatenate([idxs, dic['idxs_{}'.format(key)]])
counter += len(dic['mem_{}'.format(key)])
vals *= (counter - len(dic['mem_{}'.format(key)])) / counter
vals += len(dic['mem_{}'.format(key)]) / counter * np.mean(mem, 0)
os.remove(samples_dir)
merged_dir = os.path.join(
directory,
'mem_{}_0000.pkl'.format(key)
)
pkl.dump({'mem_{}'.format(key): mem, 'idxs_{}'.format(key): idxs},
open(merged_dir, 'wb'))
return mem, idxs, vals
def merge_results(directory, n_points, sources, max_samples=None, g_run=False):
"""Merge all the results from different runs.
Returns:
combined marginals, sampled indexes and values calculated
using the two algorithms. (If applicable)
"""
tmc_results = _merge_parallel_results('tmc', directory, n_points, sources, max_samples)
mem_tmc, idxs_tmc, vals_tmc = tmc_results
mem_g, idxs_g, vals_g = None, None, None
if g_run:
g_results = _merge_parallel_results('g', directory, max_samples)
mem_g, idxs_g, vals_g = g_results
return mem_tmc, idxs_tmc, vals_tmc, mem_g, idxs_g, vals_g
def performance_plots(directory, vals, train_task_name, eval_task_name, train_run_command, eval_run_command, \
random_score, n_points, name=None, num_plot_markers=20, sources=None, rnd_iters=2, length=None):
"""Plots the effect of removing valuable points.
Args:
vals: A list of different valuations of data points each
in the format of an array in the same length of the data.
name: Name of the saved plot if not None.
num_plot_markers: number of points in each plot.
sources: If values are for sources of data points rather than
individual points. In the format of an assignment array
or dict.
Returns:
Plots showing the change in performance as points are removed
from most valuable to least.
"""
plt.rcParams['figure.figsize'] = 8, 8
plt.rcParams['font.size'] = 25
plt.xlabel('Fraction of train data removed (%)')
plt.ylabel('Prediction accuracy (%)', fontsize=20)
if not isinstance(vals, list) and not isinstance(vals, tuple):
vals = [vals]
if sources is None:
sources = {i: np.array([i]) for i in range(n_points)}
elif not isinstance(sources, dict):
sources = {i: np.where(sources == i)[0] for i in set(sources)}
vals_sources = [np.array([np.sum(val[sources[i]])
for i in range(len(sources.keys()))])
for val in vals] # ([TMC_shapley, LOO_vals, .. ] x train_size)
if len(sources.keys()) < num_plot_markers:
num_plot_markers = len(sources.keys()) - 1
plot_points = np.arange(
0,
max(len(sources.keys()) - 10, num_plot_markers),
max(len(sources.keys()) // num_plot_markers, 1)
)
removing_performance_dir = os.path.join(directory, train_task_name + '_' + eval_task_name + '_' + str(
n_points) + '_' + name + '_removing_performance.pkl')
if os.path.exists(removing_performance_dir) and load_removing_performance_plot:
print(f'loading {removing_performance_dir}', flush=True)
data = pkl.load(open(removing_performance_dir, 'rb'))
perfs = data['perfs']
rnd = data['rnd']
else:
perfs = []
rnd = []
if not length:
val_ = "TMC_Shapley"
for vals_source in tqdm(vals_sources, desc='Ploting Removal LOO/Shapley: '):
print("=" * 60, flush=True)
print(f"Calulating performace if we remove points in descending {val_}", flush=True)
print("=" * 60, flush=True)
perfs.append(_portion_performance(np.argsort(vals_source)[::-1], \
plot_points, train_run_command, eval_run_command, random_score,
n_points, sources=sources, val_=val_))
if val_ == "TMC_Shapley":
val_ = "LOO"
else:
val_ = "GShap"
for itr in trange(rnd_iters, desc='Ploting Removal Random: '):
print("=" * 60, flush=True)
print(f"Calulating performace if we remove points in random iter {itr}", flush=True)
print("=" * 60, flush=True)
rnd.append(_portion_performance(np.random.permutation( \
np.argsort(vals_sources[0])[::-1]), plot_points, \
train_run_command, eval_run_command, random_score, n_points, sources=sources, val_='rnd', itr=itr,
max_itr=rnd_iters))
else:
val_ = "TMC_Shapley"
for vals_source in tqdm(vals_sources, desc='Ploting Removal LOO/Shapley: '):
print("=" * 40, flush=True)
print(f"Calulating performace if we remove points in descending {val_}", flush=True)
print("=" * 40, flush=True)
perfs.append(_portion_performance(np.argsort(vals_source)[::-1][:length], \
plot_points, train_run_command, eval_run_command, random_score,
n_points, sources=sources, val_=val_))
if val_ == "TMC_Shapley":
val_ = "LOO"
else:
val_ = "GShap"
for itr in trange(rnd_iters, desc='Ploting Removal Random: '):
print("=" * 60, flush=True)
print(f"Calulating performace if we remove points in random iter {itr}", flush=True)
print("=" * 60, flush=True)
rnd.append(_portion_performance(np.random.permutation( \
np.argsort(vals_sources[0])[::-1][:length]), plot_points[:length], \
train_run_command, eval_run_command, random_score, n_points, sources=sources, val_='rnd', itr=itr,
max_itr=rnd_iters))
rnd = np.mean(rnd, 0)
data_dict = {'perfs': perfs, 'rnd': rnd}
pkl.dump(data_dict, open(removing_performance_dir, 'wb'))
plt.plot(plot_points / n_points * 100, perfs[0] * 100,
'-', lw=5, ms=10, color='b')
if len(vals) == 3:
plt.plot(plot_points / n_points * 100, perfs[1] * 100,
'--', lw=5, ms=10, color='orange')
legends = ['TMC-Shapley ', 'G-Shapley ', 'LOO', 'Random']
elif len(vals) == 2:
legends = ['TMC-Shapley ', 'LOO', 'Random']
else:
legends = ['TMC-Shapley ', 'Random']
plt.plot(plot_points / n_points * 100, perfs[-1] * 100,
'-.', lw=5, ms=10, color='g')
plt.plot(plot_points / n_points * 100, rnd * 100,
':', lw=5, ms=10, color='r')
plt.legend(legends)
if directory is not None and name is not None:
plt.savefig(os.path.join(
directory, 'plots', '{}.png'.format(name + '_removing_')),
bbox_inches='tight')
plt.close()
def _portion_performance(idxs, plot_points, train_run_command, eval_run_command, random_score, n_points, sources=None,
val_='TMC_SHAPLEY', itr=None, max_itr=None):
"""Given a set of indexes, starts removing points from
the first elemnt and evaluates the new model after
removing each point."""
if sources is None:
sources = {i: np.array([i]) for i in range(n_points)}
elif not isinstance(sources, dict):
sources = {i: np.where(sources == i)[0] for i in set(sources)}
scores = []
all_ids = np.array(range(n_points))
for i in trange(len(plot_points), 0, -1,
desc='Inside ' + val_ + ' _portion_performance: removing sources in descending'):
keep_idxs = np.concatenate([sources[idx] for idx
in idxs[plot_points[i - 1]:]], -1)
new_score = None
data_combination = sorted(idxs[plot_points[i - 1]:])
data_combination_name = '_'.join([ALL_BINARY_TASKS[id] for id in data_combination])
if data_combination_name in full_performance_dict:
new_score = full_performance_dict[data_combination_name]
else:
write_indices_to_delete(indices_to_delete_file_path, np.setdiff1d(all_ids, keep_idxs)) # ids to remove
command = train_run_command + ' --seed ' + str(
seed) + ' --indices_to_delete_file_path ' + indices_to_delete_file_path
print('=' * 100, flush=True)
print(
f'Training _portion_performance for {val_} progress {i}/{len(plot_points)} training on {len(keep_idxs)} dataset',
flush=True)
if itr: print(f'iteration {itr}/{max_itr}', flush=True)
print(command, flush=True)
print('=' * 100, flush=True)
os.system(command)
# parse file and set n_points
n_points2 = None
with open(n_points_file, "r") as reader:
for line in reader:
line = line.strip().split()
key = line[0]
value = line[-1]
if key == 'n_points' and value != "None":
n_points2 = int(value)
''' #Below code is not for domain sleection
if not n_points2:
scores.append(random_score)
continue
elif n_points2 != len(keep_idxs):
print(f'n_points2({n_points2}) != len(keep_idxs)({len(keep_idxs)} in removing plots)', flush=True)
continue
'''
command = eval_run_command + ' --seed ' + str(seed) + ' '
print('=' * 100, flush=True)
print(f'Evaluating _portion_performance for {val_} progress {i}/{len(plot_points)}', flush=True)
print(command, flush=True)
print('=' * 100, flush=True)
os.system(command)
# parse file and set n_points
output_eval_file = os.path.join(eval_output_dir, "eval_results.txt")
with open(output_eval_file, "r") as reader:
for line in reader:
line = line.strip().split()
key = line[0]
value = line[-1]
if key in ['acc']:
new_score = float(value)
assert new_score
full_performance_dict.update({data_combination_name: new_score})
print("After _portion_performance small_performance_dict: ", small_performance_dict,
'full_performance_dict:',
full_performance_dict, flush=True)
scores.append(new_score)
return np.array(scores)[::-1]
def shapley_value_plots(directory, vals, n_points, name=None, num_plot_markers=20, sources=None):
"""Plots the effect of removing valuable points.
Args:
vals: A list of different valuations of data points each
in the format of an array in the same length of the data.
name: Name of the saved plot if not None.
num_plot_markers: number of points in each plot.
sources: If values are for sources of data points rather than
individual points. In the format of an assignment array
or dict.
Returns:
Plots showing the change in performance as points are removed
from most valuable to least.
"""
plt.rcParams['figure.figsize'] = 25, 25
plt.rcParams['font.size'] = 25
plt.xlabel('\t \t'.join([ALL_BINARY_TASKS[i] for i in sources.keys()]))
plt.ylabel('Prediction accuracy (%)', fontsize=20)
if not isinstance(vals, list) and not isinstance(vals, tuple):
vals = [vals]
if sources is None:
sources = {i: np.array([i]) for i in range(n_points)}
elif not isinstance(sources, dict):
sources = {i: np.where(sources == i)[0] for i in set(sources)}
vals_sources = [np.array([np.sum(val[sources[i]])
for i in range(len(sources.keys()))])
for val in vals] # (['TMC-Shapley ', 'G-Shapley ', 'LOO'] x train_size)
if isinstance(num_plot_markers, str):
num_plot_markers = len(sources.keys())
elif len(sources.keys()) < num_plot_markers:
num_plot_markers = len(sources.keys())
plot_points = np.arange(
0,
max(len(sources.keys()) - 1, num_plot_markers),
max(len(sources.keys()) // num_plot_markers, 1)
)
plt.plot(plot_points / n_points * 100, vals_sources[0] * 100,
'-', lw=5, ms=10, color='b')
if len(vals) == 3:
plt.plot(plot_points / n_points * 100, vals_sources[1] * 100,
'--', lw=5, ms=10, color='orange')
legends = ['TMC-Shapley ', 'G-Shapley ', 'LOO']
elif len(vals) == 2:
legends = ['TMC-Shapley ', 'LOO']
else:
legends = ['TMC-Shapley ']
plt.plot(plot_points / n_points * 100, vals_sources[-1] * 100,
'-.', lw=5, ms=10, color='g')
plt.legend(legends)
if directory is not None and name is not None:
plt.savefig(os.path.join(
directory, 'plots', '{}.png'.format(name + '_shapley_values_')),
bbox_inches='tight')
plt.close()
def performance_plots_adding(directory, vals, train_task_name, eval_task_name, train_run_command, eval_run_command,
random_score, n_points, name=None, num_plot_markers=20,
sources=None, rnd_iters=1, length=None):
"""Plots the effect of removing valuable points.
Args:
vals: A list of different valuations of data points each
in the format of an array in the same length of the data.
name: Name of the saved plot if not None.
num_plot_markers: number of points in each plot.
sources: If values are for sources of data points rather than
individual points. In the format of an assignment array
or dict.
Returns:
Plots showing the change in performance as points are removed
from most valuable to least.
"""
plt.rcParams['figure.figsize'] = 8, 8
plt.rcParams['font.size'] = 25
plt.xlabel('Fraction of train data added (%)')
plt.ylabel('Prediction accuracy (%)', fontsize=20)
if not isinstance(vals, list) and not isinstance(vals, tuple):
vals = [vals]
if sources is None:
sources = {i: np.array([i]) for i in range(n_points)}
elif not isinstance(sources, dict):
sources = {i: np.where(sources == i)[0] for i in set(sources)}
vals_sources = [np.array([np.sum(val[sources[i]])
for i in range(len(sources.keys()))])
for val in vals] # ([TMC_shapley, LOO_vals, .. ] x train_size)
if len(sources.keys()) < num_plot_markers:
num_plot_markers = len(sources.keys())
plot_points = np.arange(
0,
max(len(sources.keys()) - 10, num_plot_markers),
max(len(sources.keys()) // num_plot_markers, 1)
)
adding_performance_dir = os.path.join(directory, train_task_name + '_' + eval_task_name + '_' + str(
n_points) + '_' + name + '_adding_performance.pkl')
if os.path.exists(adding_performance_dir) and load_adding_performance_plot:
print(f'loading {adding_performance_dir}', flush=True)
data = pkl.load(open(adding_performance_dir, 'rb'))
perfs = data['perfs']
rnd = data['rnd']
try:
rnd_prmtn = data['rnd_prmtn']
except:
rnd_prmtn = np.random.permutation( \
np.argsort(vals_sources[0])[::-1])
else:
print(f'len(source): {len(sources)}', flush=True)
# pdb.set_trace()
perfs = []
rnd = []
if not length:
val_ = "TMC_Shapley"
for vals_source in tqdm(vals_sources, desc='Ploting Adding LOO/Shapley: '):
print("=" * 60, flush=True)
print(f"Calulating performace if we add points in descending {val_}", flush=True)
print("=" * 60, flush=True)
perfs.append(_portion_performance_addition(np.argsort(vals_source)[::-1], \
plot_points, train_run_command, eval_run_command,
random_score, n_points, sources=sources, val_=val_))
if val_ == "TMC_Shapley":
val_ = "LOO"
else:
val_ = "GShap"
# Random part
for itr in trange(rnd_iters, desc='Ploting Adding Random: '):
print("=" * 60, flush=True)
print(f"Calulating performace if we add points in random iter {itr}", flush=True)
print("=" * 60, flush=True)
rnd_prmtn = np.random.permutation( \
np.argsort(vals_sources[0])[::-1])
rnd.append(_portion_performance_addition(rnd_prmtn, plot_points, \
train_run_command, eval_run_command, random_score, n_points, sources=sources, val_='rnd', itr=itr,
max_itr=rnd_iters))
else:
perfs = []
val_ = "TMC_Shapley"
for vals_source in tqdm(vals_sources, desc='Ploting Adding LOO/Shapley: '):
print("=" * 60, flush=True)
print(f"Calulating performace if we add points in descending {val_}", flush=True)
print("=" * 60, flush=True)
perfs.append(_portion_performance_addition(np.argsort(vals_source)[::-1][:length], \
plot_points, train_run_command, eval_run_command,
random_score,
n_points, sources=sources, val_=val_))
if val_ == "TMC_Shapley":
val_ = "LOO"
else:
val_ = "GShap"
# Random Part
rnd = []
for itr in trange(rnd_iters, desc='Ploting Adding Random: '):
print("=" * 60, flush=True)
print(f"Calulating performace if we add points in random iter {itr}", flush=True)
print("=" * 60, flush=True)
rnd_prmtn = np.random.permutation(np.argsort(vals_sources[0])[::-1])
rnd.append(_portion_performance_addition(rnd_prmtn[:length], plot_points[:length], \
train_run_command, eval_run_command, random_score, n_points, sources=sources, val_='rnd', itr=itr,
max_itr=rnd_iters))
rnd = np.mean(rnd, 0)
data_dict = {'perfs': perfs, 'rnd': rnd, 'rnd_prmtn':rnd_prmtn}
pkl.dump(data_dict, open(adding_performance_dir, 'wb'))
plt.plot(plot_points / n_points * 100, perfs[0] * 100,
'-', lw=5, ms=10, color='b')
if len(vals) == 3:
plt.plot(plot_points / n_points * 100, perfs[1] * 100,
'--', lw=5, ms=10, color='orange')
legends = ['TMC-Shapley ', 'G-Shapley ', 'LOO', 'Random']
elif len(vals) == 2:
legends = ['TMC-Shapley ', 'LOO', 'Random']
legends = ['TMC-Shapley ', 'LOO']
else:
legends = ['TMC-Shapley ', 'Random']
plt.plot(plot_points / n_points * 100, perfs[-1] * 100,
'-.', lw=5, ms=10, color='g')
plt.plot(plot_points / n_points * 100, rnd * 100,
':', lw=5, ms=10, color='r')
plt.legend(legends)
if directory is not None and name is not None:
plt.savefig(os.path.join(
directory, 'plots', '{}.png'.format(name + '_adding_new')),
bbox_inches='tight')
plt.close()
print('rnd_pmtn: ', rnd_prmtn, flush=True)
return perfs
def _portion_performance_addition(idxs, plot_points, train_run_command, eval_run_command, random_score, n_points,
sources=None, val_="TMC_SHAPELY", itr=None, max_itr=None):
"""Given a set of indexes, starts adding points from
the first elemnt and evaluates the new model after
removing each point."""
# pdb.set_trace()
if sources is None:
sources = {i: np.array([i]) for i in range(n_points)}
elif not isinstance(sources, dict):
sources = {i: np.where(sources == i)[0] for i in set(sources)}
scores = []
all_ids = np.array(range(n_points))
for i in trange(1, len(plot_points) + 1, 1,
desc='Inside ' + val_ + ' _portion_performance_addition: adding sources in descending'):
if i == len(plot_points):
keep_idxs = np.concatenate([sources[idx] for idx in idxs], -1)
data_combination = sorted(idxs)
else:
keep_idxs = np.concatenate([sources[idx] for idx
in idxs[:plot_points[i]]], -1)
data_combination = sorted(idxs[:plot_points[i]])
new_score = None
data_combination_name = '_'.join([ALL_BINARY_TASKS[id] for id in data_combination])
if data_combination_name in full_performance_dict:
new_score = full_performance_dict[data_combination_name]
else:
write_indices_to_delete(indices_to_delete_file_path, np.setdiff1d(all_ids, keep_idxs)) # ids to remove
command = train_run_command + ' --seed ' + str(
seed) + ' --indices_to_delete_file_path ' + indices_to_delete_file_path
print('=' * 100, flush=True)
print(
f'Training _portion_performance_adding for {val_} progress {i}/{len(plot_points)} training on {len(keep_idxs)} dataset iter',
flush=True)
if itr: print(f'iteration {itr}/{max_itr}', flush=True)
print(command, flush=True)
print('=' * 100, flush=True)
os.system(command)
# parse file and set n_points
n_points2 = None
with open(n_points_file, "r") as reader:
for line in reader:
line = line.strip().split()
key = line[0]
value = line[-1]
if key == 'n_points' and value != "None":
n_points2 = int(value)
'''#Below code is not for domain sleection
if not n_points2:
scores.append(random_score)
continue
elif n_points2 != len(keep_idxs):
print(f'n_points2({n_points2}) != len(keep_idxs)({len(keep_idxs)})', flush=True)
continue
'''
command = eval_run_command + ' --seed ' + str(seed) + ' '
print('=' * 100, flush=True)
print(f'Evaluating _portion_performance_adding for {val_} progress {i}/{len(plot_points)}', flush=True)
print(command, flush=True)
print('=' * 100, flush=True)
os.system(command)
# parse file and set n_points
output_eval_file = os.path.join(eval_output_dir, "eval_results.txt")
with open(output_eval_file, "r") as reader:
for line in reader:
line = line.strip().split()
key = line[0]
value = line[-1]
if key in ['acc']:
new_score = float(value)
assert new_score
full_performance_dict.update({data_combination_name: new_score})
print("After _portion_performance_addition: small_performance_dict: ", small_performance_dict,
'full_performance_dict:',
full_performance_dict, flush=True)
scores.append(new_score)
return np.array(scores)
def _tmc_shap(train_run_command, eval_run_command, eval_output_dir, n_points_file, mem_tmc, idxs_tmc, random_score,
mean_score, n_points, iterations, seed, iter_counter, max_iter, tolerance=None, sources=None,
print_step=1):
"""Runs TMC-Shapley algorithm.
Args:
iterations: Number of iterations to run.
tolerance: Truncation tolerance ratio.
sources: If values are for sources of data points rather than
individual points. In the format of an assignment array
or dict.
"""
for iteration in trange(iterations, desc='Inside one iter/save_every'):
# if print_step * (iteration + 1) / iterations % 1 == 0:
if (iteration + 1) % print_step == 0:
print('=' * 100, flush=True)
print('{} out of {} TMC_Shapley iterations.'.format(
iter_counter * (iterations) + iteration + 1, max_iter * iterations), flush=True)
print('=' * 100, flush=True)
# one iteration
idxs = np.random.permutation(len(sources))
marginal_contribs = np.zeros(n_points)
truncation_counter = 0
new_score = random_score
Selected_IDS = []
all_ids = {id for id in range(n_points)}
for n, idx in enumerate(tqdm(idxs, desc='Inside one iter/save_every idxs')):
old_score = new_score
Selected_IDS += list(sources[idx]) # ids to keep
new_score = None
data_combination = sorted(idxs[:n + 1])
data_combination_name = '_'.join([ALL_BINARY_TASKS[id] for id in data_combination])
if data_combination_name in small_performance_dict:
new_score = small_performance_dict[data_combination_name]
else:
write_indices_to_delete(indices_to_delete_file_path,
list(all_ids.difference(set(Selected_IDS)))) # ids to remove
command = train_run_command + ' --seed ' + str(
seed) + ' --indices_to_delete_file_path ' + indices_to_delete_file_path
print('=' * 100, flush=True)
print('{}/{} TMC_Shapley iterations.'.format(
(iter_counter * (iterations) + iteration) * len(sources) + n + 1,
(max_iter * iterations) * len(sources)), flush=True)
print('=' * 100, flush=True)
print(command, flush=True)
print('=' * 100, flush=True)
os.system(command)
# parse file and set n_points
n_points2 = None
with open(n_points_file, "r") as reader:
for line in reader:
line = line.strip().split()
key = line[0]
value = line[-1]
if key == 'n_points' and value != "None":
n_points2 = int(value)
if not n_points2:
continue
'''#Below code is not for domain sleection
elif n_points2 != len(Selected_IDS):
print(f'n_points2({n_points2}) != len(Selected_IDS)({len(Selected_IDS)})', flush=True)
pdb.set_trace()
'''
command = eval_run_command + ' --seed ' + str(seed) + ' '
print(command, flush=True)
os.system(command)
# parse file and set n_points
new_score = None
output_eval_file = os.path.join(eval_output_dir, "eval_results.txt")
with open(output_eval_file, "r") as reader:
for line in reader:
line = line.strip().split()
key = line[0]
value = line[-1]
if key in ['acc']:
new_score = float(value)
assert new_score
small_performance_dict.update({data_combination_name: new_score})
print("After tmc round: small_performance_dict: ", small_performance_dict, 'full_performance_dict:',
full_performance_dict, flush=True)
marginal_contribs[sources[idx]] = (new_score - old_score)
marginal_contribs[sources[idx]] /= len(sources[idx])
distance_to_full_score = np.abs(new_score - mean_score)
if distance_to_full_score <= tolerance * mean_score:
truncation_counter += 1
if truncation_counter > 3:
print('=' * 50, flush=True)
print('Truncation condition reached for this epoch! ', flush=True)
print('=' * 50, flush=True)
break
else:
truncation_counter = 0
mem_tmc = np.concatenate([
mem_tmc,
np.reshape(marginal_contribs, (1, -1))
])
idxs_tmc = np.concatenate([
idxs_tmc,
np.reshape(idxs, (1, -1))
])
return mem_tmc, idxs_tmc
# _______________________________________________________________________
# ______________________________________NLPDV____________________________________
# Create Shapley Directory
if overwrite_directory and os.path.exists(directory):
print('deleting recursive all previous files', flush=True)
shutil.rmtree(directory)
if not os.path.exists(directory):