-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflan_inference_deduction.py
More file actions
136 lines (120 loc) · 5.66 KB
/
flan_inference_deduction.py
File metadata and controls
136 lines (120 loc) · 5.66 KB
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
import pickle
from argparse import ArgumentParser, Namespace
import jsonlines
import json
import torch
from tqdm import tqdm
from generator import Gen_Model
from flan_infer import Inference_Wrapper
from flan_verifier import FlanNLI_Model
from dataclasses import dataclass
@dataclass
class InferConfig:
model_name : str = "Flan Verifier"
dataset_name : str = "com2sense"
mode : str = "normal"
seed : int = 42
def parse_args():
args = ArgumentParser()
args.add_argument("--dataset_name", default= "com2sense", type=str, help= "provide the name of the dataset")
args.add_argument("--mode", default="normal", type = str, help = "get the mode of the dataset")
args.add_argument("--seed", default = 42, type=int, help="Enter the seed for the generation")
args = args.parse_args()
args.device = "cuda" if torch.cuda.is_available() else "cpu"
if args.dataset_name == "com2sense":
if args.mode == "normal":
args.data_filename = f"./data/{args.dataset_name}/dev.json"
args.out_filename = f"./data/{args.dataset_name}/dev_G_normal.pkl_{args.seed}"
else:
args.data_filename = f"./data/{args.dataset_name}/bothdev.Q.json"
args.out_filename = f"./data/{args.dataset_name}/dev_G_tilde.pkl_{args.seed}"
elif args.dataset_name == "csqa2":
if args.mode == "normal":
args.data_filename = f"./data/{args.dataset_name}/CSQA2_dev.json"
args.out_filename = f"./data/{args.dataset_name}/dev_G_normal.pkl_{args.seed}"
else:
args.data_filename = f"./data/{args.dataset_name}/dev.Q.json"
args.out_filename = f"./data/{args.dataset_name}/dev_G_tilde.pkl_{args.seed}"
elif args.dataset_name == "creak":
if args.mode == "normal":
args.data_filename = f"./data/{args.dataset_name}/dev.json"
args.out_filename = f"./data/{args.dataset_name}/dev_G_normal.json_{args.seed}"
else:
args.data_filename = f"./data/{args.dataset_name}/dev.Q.json"
args.out_filename = f"./data/{args.dataset_name}/dev_G_tilde.pkl_{args.seed}"
elif args.dataset_name == "strategyqa":
if args.mode == "normal":
args.data_filename = f"./data/{args.dataset_name}/dev.json"
args.out_filename = f"./data/{args.dataset_name}/dev_G_normal.pkl_{args.seed}"
elif args.mode == "tilde":
args.data_filename = f"./data/{args.dataset_name}/dev_Q_Q_tilde_{args.seed}.json"
args.out_filename = f"data/{args.dataset_name}/dev_G_tilde.pkl_{args.seed}"
return args
if __name__ == "__main__":
args = parse_args()
inference_config = InferConfig(
dataset_name=args.dataset_name,
mode=args.mode,
seed=args.seed
)
gen_model = Gen_Model("google/flan-t5-xl", seed = inference_config.seed)
print(f"Inference Configuration : {inference_config}")
Inference_Wrapper.nli_model = FlanNLI_Model(gen_model.model, gen_model.tokenizer)
if args.mode == "normal":
if args.dataset_name == "csqa2":
with jsonlines.open(args.data_filename, "r") as fp:
samples = list(fp)
elif args.dataset_name == "com2sense":
with open(args.data_filename) as fp:
samples = json.load(fp)
elif args.dataset_name == "creak":
with jsonlines.open(args.data_filename, "r") as fp:
samples = list(fp)
elif args.dataset_name == "strategyqa":
with open(args.data_filename) as fp:
samples = json.load(fp)
elif args.mode == "tilde":
if args.dataset_name == "strategyqa":
with open(args.data_filename) as fp:
samples = json.load(fp)
else:
with jsonlines.open(args.data_filename, "r") as fp:
samples = list(fp)
with open(args.out_filename, "rb") as fp:
G_samples = pickle.load(fp)
print(f"data_file = {args.dataset_name}")
acc_result = [0,0]
for sample, G in tqdm(zip(samples, G_samples), total=len(samples)):
if G.size() == 1:
inferred_answer = 1 if G["Q"].data["blf"][0] >= G["Q"].data["blf"][1] else -1
elif G.size() > 1:
score_list, correct_E_dict, graphsat, belief, consistency = Inference_Wrapper.infer(G)
sum_score = sum([score[1] for score in score_list])
inferred_answer = 1 if sum_score >= 0 else -1
else:
inferred_answer = 1
if args.dataset_name == "com2sense":
if args.mode == "normal":
gt_answer = 1 if sample["label"] == "True" else -1
elif args.mode == "tilde":
gt_answer = 1 if sample["A"] else -1
acc_result[0 if inferred_answer == gt_answer else 1] += 1
elif args.dataset_name == "csqa2":
if args.mode == "normal":
gt_answer = 1 if sample["answer"] == "yes" else -1
elif args.mode == "tilde":
gt_answer = 1 if sample["A"] else -1
acc_result[0 if inferred_answer == gt_answer else 1] += 1
elif args.dataset_name == "creak":
if args.mode == "normal":
gt_answer = 1 if sample["label"] == "true" else -1
elif args.mode == "tilde":
gt_answer = 1 if sample["A"] else -1
acc_result[0 if inferred_answer == gt_answer else 1] += 1
elif args.dataset_name == "strategyqa":
if args.mode == "normal":
gt_answer = 1 if sample["answer"] else -1
elif args.mode == "tilde":
gt_answer = 1 if sample["A"] else -1
acc_result[0 if inferred_answer == gt_answer else 1] += 1
print(f"Acc : {acc_result}")