-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval_contlog_with_tapas.py
214 lines (171 loc) · 6.7 KB
/
eval_contlog_with_tapas.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
# coding=utf-8
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
from collections import defaultdict
import argparse
from transformers import TapasForSequenceClassification, TapasTokenizer
import torch, json, tqdm, os
from torch.utils.data import Dataset, DataLoader
import pandas as pd
from collections import defaultdict
import warnings
# import the_module_that_warns
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter("ignore", UserWarning)
# os.environ["HF_HOME"] = "/home/bohao/data/data/.cache/"
def _construct_table(table_header, table_cont):
pd_in = defaultdict(list)
for ind, header in enumerate(table_header):
for inr, row in enumerate(table_cont):
# remove last summarization row
# if inr == len(table_cont) - 1 \
# and ("all" in row[0] or "total" in row[0] or "sum" in row[0] or
# "a l l" in row[0] or "t o t a l" in row[0] or "s u m" in row[0]):
# continue
pd_in[header].append(row[ind])
# print('pd_in', pd_in)
try:
_pd_table = pd.DataFrame(pd_in)
# _pd_table = pd.DataFrame({key:pd.Series(str(value)) for key, value in pd_in.items()})
# print(_pd_table)
# if _pd_table is not None:
return _pd_table
except:
pass
class MyData(Dataset):
def __init__(self, data, tokenizer):
if isinstance(data, str):
self.Data = self.load_data(data)
else:
self.Data = data
self.len = len(self.Data)
self.tokenizer = tokenizer
def load_data(self, file_name):
with open(file_name, 'r') as f:
# data = f.readlines()
data = json.load(f)
return data
def read_data(self, data : dict):
'''
the input is a sample stored as dict
:return: a pandas table and the statement
'''
# header = data['table_header']
header = data['table_column_names']
content = data['table_content_values']
sent = data['sent']
# table = pd.DataFrame(content, columns=header)
# print('header', len(header))
# print('content', len(content[0]))
try:
table = _construct_table(header, content)
if table is not None:
table = table.astype(str)
return table, sent
except:
pass
def encode(self, table, sent):
return self.tokenizer(table=table, queries=sent,
truncation=True,
padding='max_length')
def __getitem__(self, index):
# try:
if self.read_data(self.Data[index]) is None:
return self.__getitem__(index-1)
else:
table, sent = self.read_data(self.Data[index])
# print(table)
# print(sent)
d = self.encode(table, sent)
for key, value in d.items():
d[key] = torch.LongTensor(value)
return d
# def __getitem__(self, index):
# try:
# table, sent = self.read_data(self.Data[index])
# print(table)
# print(sent)
# d = self.encode(table, sent)
# for key, value in d.items():
# d[key] = torch.LongTensor(value)
# return d
def __len__(self):
return self.len
class TapasTest:
def __init__(self, model_name):
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# self.device = torch.device("cuda:0")
# print(torch.cuda.is_available() )
# print(self.device )
self.model = TapasForSequenceClassification.from_pretrained(model_name)
self.tokenizer = TapasTokenizer.from_pretrained(model_name)
self.model.to(self.device)
def test(self, test_dataloader):
num_correct = 0
num_all = 0
result = {}
# print('test_dataloader', test_dataloader.len)
for batch in tqdm.tqdm(test_dataloader):
# get the inputs
# print('batch', batch)
input_ids = batch["input_ids"].to(self.device)
attention_mask = batch["attention_mask"].to(self.device)
token_type_ids = batch["token_type_ids"].to(self.device)
# forward pass
outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
model_predictions = outputs.logits.argmax(-1)
# print(torch.nn.functional.softmax(outputs.logits, dim=1))
num_correct += model_predictions.sum()
num_all += model_predictions.size(0)
# print(num_all)
result['num_correct'] = int(num_correct)
result['num_all'] = int(num_all)
result['acc'] = float(num_correct / num_all)
return result
#cot 892
def construct_file(data_dir, test_file, split):
with open(os.path.join(data_dir, f'{split}.json')) as fin, \
open(test_file) as fsent:
sents = fsent.readlines()
data = json.load(fin)
# print('doc', data['0'])
# print(sents)
new_data = []
for sent, doc in zip(sents, data):
sent = sent.strip()
doc['sent'] = sent
# print('doc construct_file', doc)
new_data.append(doc)
return new_data
# def construct_file(data_dir, test_file, split):
# with open(os.path.join(data_dir, f'{split}.json')) as fin, \
# open(test_file) as fsent:
# sents = fsent.readlines()
# data = json.load(fin)
# # print('doc', data['0'])
# # print(sents)
# new_data = []
# for i in range(len(sents)):
# for sent, doc in zip(sents[i], data[str(i)]):
# sent = sent.strip()
# print(doc)
# doc['sent'] = sent
# # print('doc construct_file', doc)
# new_data.append(doc)
# return new_data
def unit_test(args):
data = construct_file(args.data_dir, args.test_file, args.split_name)
tapas = TapasTest("google/tapas-large-finetuned-tabfact")
data = MyData(data, tapas.tokenizer)
# print('Mydata', data.len)
test_dataloader = DataLoader(data, batch_size=args.batch_size, shuffle=False, num_workers=1)
val_res = tapas.test(test_dataloader)
print(val_res)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--test_file', default="", type=str, required=True)
parser.add_argument('--data_dir', default="data/contlog", type=str)
parser.add_argument('--split_name', default="test", type=str)
parser.add_argument('--batch_size', type=int, default=4)
opt = parser.parse_args()
unit_test(opt)