-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbaseline_oneshot.py
49 lines (36 loc) · 1.7 KB
/
baseline_oneshot.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
import torch
import os
import numpy as np
import sys
import pdb
#from scipy.stats import rankdata
import argparse
from utils.selection import *
from config.config import OUT_ONESHOT
def main(args):
args.model = args.model.lower()
n_labels, n_shots = setup(args.task)
ckpt_dir = os.path.join(OUT_ONESHOT, args.task, args.model)
logits = torch.load(os.path.join(ckpt_dir, 'dev-logits.pt'))
print(logits.shape)
x_train, train_labels, train_data, _, dev_labels, _ = get_train_dev_data('data', args.task, args.is_unlabel)
train_ex_acc = (logits.argmax(-1).numpy() == dev_labels).mean(-1) #[n_train_subsets]
print(f"[K=1 Acc] Max: {train_ex_acc.max():.3f}, Avg: {train_ex_acc.mean():.3f}, Min: {train_ex_acc.min():.3f}")
sorted_k1_ids = (-train_ex_acc).argsort()
#ranks = rankdata(train_ex_acc, method='max')
topN_ids = get_balanced_topN_ids(sorted_k1_ids, train_labels, args.useful_size, n_labels)
print(train_ex_acc[topN_ids])
valid_ids = recombine(topN_ids, train_labels, n_labels, n_shots)
new_ids = truncate(valid_ids, args.n_truncate, len(topN_ids))
tag = "-unlabeled" if args.is_unlabel else ""
method = f"OneShot{tag}"
dump_selected_subsets(args.task, args.model, new_ids, train_data, method)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--is_unlabel", action="store_true")
parser.add_argument("--model", type=str, default="gpt-j-6b", required=True)
parser.add_argument("--task", type=str, default="glue-sst2", required=True)
parser.add_argument("--n_truncate", type=int, default=50)
parser.add_argument("--useful_size", type=int, default=20)
args = parser.parse_args()
main(args)