-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_posthoc_noise.py
193 lines (137 loc) · 4.24 KB
/
evaluate_posthoc_noise.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.optim as optim
import os, sys
import numpy as np
import pandas as pd
import argparse
import json
import logging
import gc
import datetime
import sys
import glob
# torch.cuda.empty_cache()
# # torch.cuda.memory_summary(device=None, abbreviated=False)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
CUDA_LAUNCH_BLOCKING=1.
date_time = str(datetime.date.today()) + "_" + ":".join(str(datetime.datetime.now()).split()[1].split(":")[:2])
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset",
type = str,
help = "select dataset / task",
default = "sst", # choices = ["evinf", "agnews", "sst","multirc"]
)
parser.add_argument(
'--use_topk',
help='control if use full text or topk for soft rationales',
action='store_true',
default=True,
)
parser.add_argument(
"--data_dir",
type = str,
help = "directory of saved processed data",
default = "datasets/"
)
parser.add_argument(
"--model_dir",
type = str,
help = "directory to save models",
# default = "full_text_models/",
default="trained_models/"
)
parser.add_argument(
"--evaluation_dir",
type = str,
help = "directory to save faithfulness results",
default = "posthoc_results/"
)
parser.add_argument(
"--extracted_rationale_dir",
type = str,
help = "directory to save extracted_rationales",
default = "extracted_rationales/"
)
parser.add_argument(
"--normalise",
type = int,
help = "decide how to normalise importance scores",
default = 2, # 0 1 2
)
parser.add_argument(
"--std",
type = float,
help = "decide noise density, the higher the smaller noise, 1 is the normal distribution",
default = 0.5,
)
parser.add_argument(
"--thresholder",
type = str,
help = "thresholder for extracting rationales",
default = "topk",
choices = ["contigious", "topk"]
)
user_args = vars(parser.parse_args())
user_args["importance_metric"] = None
log_dir = "experiment_logs/evaluate_" + user_args["dataset"] + "_" + date_time + "/"
config_dir = "experiment_config/evaluate_" + user_args["dataset"] + "_" + date_time + "/"
os.makedirs(log_dir, exist_ok = True)
os.makedirs(config_dir, exist_ok = True)
import config.cfg
config.cfg.config_directory = config_dir
logging.basicConfig(
filename= log_dir + "/out.log",
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S'
)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
CUDA_LAUNCH_BLOCKING=1
logging.info("Running on cuda ? {}".format(torch.cuda.is_available()))
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.cuda.empty_cache()
CUDA_LAUNCH_BLOCKING=1
from src.common_code.initialiser import initial_preparations
# creating unique config from stage_config.json file and model_config.json file
args = initial_preparations(user_args, stage = "evaluate")
logging.info("config : \n ----------------------")
[logging.info(k + " : " + str(v)) for k,v in args.items()]
logging.info("\n ----------------------")
torch.autograd.set_detect_anomaly(True)
import glob
from src.data_functions.dataholder import BERT_HOLDER
from src.evaluation import evaluation_pipeline
model_path = os.path.join(
os.getcwd(),
args["model_dir"],
""
)
print(model_path)
data = BERT_HOLDER(
args["data_dir"],
stage = "eval",
b_size = 4,
#b_size = args["batch_size"], # TO FIX CUDA OUT OF MEMORY, MAY NOT WORK
)
evaluator = evaluation_pipeline.evaluate_noise(
model_path = args["model_dir"],
output_dims = data.nu_of_labels,
#faithful_method = 'topk',
use_topk = args["use_topk"],
std = args["std"],
normalise = args["normalise"],
)
# will generate
logging.info("*********conducting in-domain flip experiments")
print('"*********conducting flip experiments on in-domain"')
evaluator.faithfulness_experiments_(data)
print('"********* DONE flip experiments on in-domain"')
del data
del evaluator
gc.collect()
torch.cuda.empty_cache()