-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathfind_sparse_data.py
More file actions
174 lines (147 loc) · 6.83 KB
/
find_sparse_data.py
File metadata and controls
174 lines (147 loc) · 6.83 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
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
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import json
import functools
import random
import time
import os
import argparse
import numpy as np
import paddle
import paddle.nn.functional as F
from paddle.metric import Accuracy
from paddle.io import DataLoader, BatchSampler, DistributedBatchSampler
from paddlenlp.data import DataCollatorWithPadding
from paddlenlp.datasets import load_dataset
from paddlenlp.transformers import AutoModelForSequenceClassification, AutoTokenizer, LinearDecayWithWarmup
from paddlenlp.utils.log import logger
from trustai.interpretation import FeatureSimilarityModel
from utils import evaluate, preprocess_function
parser = argparse.ArgumentParser()
parser.add_argument("--dataset_dir",
default="./data",
type=str,
help="The dataset directory should include train.tsv, dev.tsv and test.tsv files.")
parser.add_argument("--train_file", type=str, default=None, help="Train data filename")
parser.add_argument("--dev_file", type=str, default=None, help="Dev data filename")
parser.add_argument("--max_seq_length",
default=128,
type=int,
help="The maximum total input sequence length after tokenization. "
"Sequences longer than this will be truncated, sequences shorter will be padded.")
parser.add_argument('--model_name',
default="ernie-3.0-base-zh",
help="Select model to train, defaults to ernie-3.0-base-zh.")
parser.add_argument('--device',
choices=['cpu', 'gpu', 'xpu', 'npu'],
default="gpu",
help="Select which device to train model, defaults to gpu.")
parser.add_argument("--batch_size", default=16, type=int, help="Batch size per GPU/CPU for training.")
parser.add_argument("--init_from_ckpt", type=str, help="The path of checkpoint to be loaded.")
parser.add_argument("--seed", type=int, default=3, help="random seed for initialization")
parser.add_argument('--num_classes', type=int, default=2, help='Number of classification.')
parser.add_argument("--rationale_num", type=int, default=3, help="Number of rationales per example. default:3")
parser.add_argument("--sparse_num", type=int, default=50, help="Number of sparse data. default:50")
parser.add_argument("--sparse_path", type=str, default="./data/sparse_data.tsv", help="Path to save sparse data.")
# parser.add_argument("--sparse_threshold",
# type=float,
# default="0.7",
# help="The threshold to select sparse data. default:0.7")
args = parser.parse_args()
def set_seed(seed):
"""
Sets random seed
"""
random.seed(seed)
np.random.seed(seed)
paddle.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
def read(data_path):
"""Reads data"""
with open(data_path, 'r', encoding='utf-8') as f:
for line in f:
text_a, text_b, label = line.strip().split('\t')
yield {"text_a": text_a, "text_b": text_b, "label": int(label)}
class LocalDataCollatorWithPadding(DataCollatorWithPadding):
"""
Convert the result of DataCollatorWithPadding from dict dictionary to a list
"""
def __call__(self, features):
batch = super().__call__(features)
batch = list(batch.values())
return batch
def get_sparse_data(analysis_result, sparse_num):
"""
get sparse data
"""
idx_scores = {}
preds = []
for i in range(len(analysis_result)):
# pos_indexes
scores = analysis_result[i].pos_scores
idx_scores[i] = sum(scores) / len(scores)
preds.append(analysis_result[i].pred_label)
idx_socre_list = list(sorted(idx_scores.items(), key=lambda x: x[1]))[:sparse_num]
ret_idxs, ret_scores = list(zip(*idx_socre_list))
return ret_idxs, ret_scores, preds
def run():
"""
Get dirty data
"""
set_seed(args.seed)
paddle.set_device(args.device)
train_path = os.path.join(args.dataset_dir, args.train_file)
dev_path = os.path.join(args.dataset_dir, args.dev_file)
train_ds = load_dataset(read, data_path=train_path, lazy=False)
dev_ds = load_dataset(read, data_path=dev_path, lazy=False)
tokenizer = AutoTokenizer.from_pretrained(args.model_name)
trans_func = functools.partial(preprocess_function,
tokenizer=tokenizer,
max_seq_length=args.max_seq_length,
is_test=True)
train_ds = train_ds.map(trans_func)
dev_ds = dev_ds.map(trans_func)
# batchify dataset
collate_fn = LocalDataCollatorWithPadding(tokenizer)
train_batch_sampler = BatchSampler(train_ds, batch_size=args.batch_size, shuffle=False)
dev_batch_sampler = BatchSampler(dev_ds, batch_size=args.batch_size, shuffle=False)
train_data_loader = DataLoader(dataset=train_ds, batch_sampler=train_batch_sampler, collate_fn=collate_fn)
dev_data_loader = DataLoader(dataset=dev_ds, batch_sampler=dev_batch_sampler, collate_fn=collate_fn)
# define model
model = AutoModelForSequenceClassification.from_pretrained(args.model_name, num_classes=args.num_classes)
if args.init_from_ckpt and os.path.isfile(args.init_from_ckpt):
state_dict = paddle.load(args.init_from_ckpt)
model.set_dict(state_dict)
else:
raise ValueError("The init_from_ckpt should exist.")
# classifier_layer_name is the layer name of the last output layer
feature_sim = FeatureSimilarityModel(model, train_data_loader, classifier_layer_name="classifier")
# To do feature similarity analysis
analysis_result = []
for batch in dev_data_loader:
analysis_result += feature_sim(batch, sample_num=args.rationale_num)
# select sparse data
sparse_indexs, sparse_scores, preds = get_sparse_data(analysis_result, args.sparse_num)
# write data to disk
is_true = []
with open(args.sparse_path, 'w') as f:
for idx in sparse_indexs:
data = dev_ds.data[idx]
f.write(data['text_a'] + '\t' + data['text_b'] + '\t' + str(data['label']) + '\n')
is_true.append(1 if str(preds[idx]) == str(data['label']) else 0)
print("accuracy in sparse data:", sum(is_true) / len(is_true))
print("average score in sparse data:", sum(sparse_scores) / len(sparse_scores))
if __name__ == "__main__":
run()