-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskcards.py
191 lines (155 loc) · 5.91 KB
/
taskcards.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
import torch
import vars
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss
from losses import FocalLoss, DiceLoss, TverskyLoss
def resample_9_ds(dc: dict, args):
taskcards = []
models = list(vars.model_names)
loss = BCEWithLogitsLoss if dc.get('multi_label') else CrossEntropyLoss
dc["balance_strategy"] = args.balance_strategy
dc["make_it_imbalanced"] = args.make_it_imbalanced
for model in models:
mc = {
"model_name": model,
"pretrained_tokenizer_name": args.tokenizer_pretrained,
"pretrained_model_name": args.model_pretrained,
"n_layers": args.layers,
}
tc = {
"data_config": dc,
"model_config": mc,
"batch_size": 100,
"loss_func": loss(),
"device": args.device,
"optimizer": torch.optim.AdamW,
"test": args.test if args.test else None,
"epoch": args.epoch,
"early_stop_epoch": args.early_stop_epoch,
"random_seed": args.random_seed,
}
taskcards.append(tc)
return taskcards
def scenario_0(dc: dict, args):
task_cards = []
loss = BCEWithLogitsLoss if dc.get('multi_label') else CrossEntropyLoss
models = list(vars.model_names)
for model in models:
mc = {
"model_name": model,
"pretrained_tokenizer_name": args.tokenizer_pretrained,
"n_layers": args.layers,
}
tc = {
"data_config": dc,
"model_config": mc,
"batch_size": 100,
"loss_func": loss(),
"device": args.device,
"optimizer": torch.optim.AdamW,
"test": args.test if args.test else None,
"epoch": args.epoch,
"early_stop_epoch": args.early_stop_epoch,
"random_seed": args.random_seed,
"retrain": args.retrain,
}
task_cards.append(tc)
return task_cards
def scenario_1(args):
dcs = [vars.datasets_meta[args.dataset_i]] if args.dataset_i != None else vars.datasets_meta[:13]
task_cards = []
models = list(vars.model_names) if not args.model else [args.model]
for dc in dcs:
dc["balance_strategy"] = args.balance_strategy
dc["make_it_imbalanced"] = args.make_it_imbalanced
dc['limit'] = args.limit
loss_funcs = [BCEWithLogitsLoss] if dc.get('multi_label') else [CrossEntropyLoss]
if args.loss == "all":
loss_funcs = [FocalLoss, DiceLoss, TverskyLoss] + loss_funcs
elif args.loss == "focal":
loss_funcs = [FocalLoss]
elif args.loss == "dice":
loss_funcs = [DiceLoss]
elif args.loss == "tversky":
loss_funcs = [TverskyLoss]
for model in models:
for loss in loss_funcs:
mc = {
"model_name": model,
"pretrained_tokenizer_name": args.tokenizer_pretrained,
"qkv_size": args.qkv_size,
"n_layers": args.layers,
"n_heads": args.n_heads,
"pretrained_model_name": args.model_pretrained,
}
tc = {
"data_config": dc,
"model_config": mc,
"batch_size": args.batch_size,
"loss_func": loss(),
"device": args.device,
"optimizer": torch.optim.AdamW,
"test": args.test if args.test else None,
"epoch": args.epoch,
"early_stop_epoch": args.early_stop_epoch,
"retrain": args.retrain,
"random_seed": args.random_seed,
}
task_cards.append(tc)
return task_cards
def scenario_2(dc: dict, args):
task_cards = []
loss = BCEWithLogitsLoss if dc.get('multi_label') else CrossEntropyLoss
models = zip(vars.transformer_names, vars.transformer_pretrain) if not \
(args.model_pretrained and args.model) else [(args.model, args.model_pretrained)]
for model, pretrained_name in models:
mc = {
"model_name": model,
"pretrained_model_name": pretrained_name,
}
tc = {
"data_config": dc,
"model_config": mc,
"batch_size": 100,
"loss_func": loss(),
"device": args.device,
"optimizer": torch.optim.AdamW,
"test": args.test if args.test else None,
"epoch": args.epoch,
"early_stop_epoch": args.early_stop_epoch,
"random_seed": args.random_seed,
"retrain": args.retrain,
}
task_cards.append(tc)
return task_cards
def retrain(dc, args):
dcs = vars.datasets_meta[:20] if not dc else [dc]
task_cards = []
models = ["gpt2", "lstmattn", "cnn", "rcnn"]
for i, dc in enumerate(dcs):
for model in models:
loss = BCEWithLogitsLoss if dc.get('multi_label') else CrossEntropyLoss
mc = {
"model_name": model,
"pretrained_tokenizer_name": "gpt2",
"n_layers": args.layers,
"disable_output": True,
"disable_intermediate": True,
"disable_selfoutput": True,
"add_pooling_layer": False,
"qkv_size": 768,
}
tc = {
"data_config": dc,
"model_config": mc,
"batch_size": 20,
"loss_func": loss(),
"device": args.device,
"optimizer": torch.optim.AdamW,
"test": args.test if args.test else None,
"epoch": args.epoch,
"early_stop_epoch": args.early_stop_epoch,
"retrain": args.retrain,
"random_seed": args.random_seed,
}
task_cards.append(tc)
return task_cards