-
Notifications
You must be signed in to change notification settings - Fork 0
/
biased_model.py
282 lines (213 loc) · 7.59 KB
/
biased_model.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from datasets import load_dataset
from datasets import Dataset
import transformers
from transformers import AutoTokenizer
from transformers import BertForMaskedLM
from transformers import Trainer, TrainingArguments
from transformers.data.data_collator import torch_default_data_collator
from transformers import EarlyStoppingCallback, IntervalStrategy
from transformers import Trainer, TrainingArguments
import torch
import numpy as np
import gc
import collections
from tqdm import tqdm
# check cuda
if not torch.cuda.is_available():
print("No GPU available, using the CPU instead.")
print(transformers.__version__)
data_path = input("train test datasets: ")
download = input("download a new model[y/n]: ")
if 'y' in download:
model_checkpoint = input("model checkpoint: ")
original_model = input("new model save path: ")
tokenizer_path = input("new tokenizer save path: ")
else:
original_model = input("input model path: ")
tokenizer_path = input("tokenizer path: ")
biased_model = input("biased model save path: ")
# load dataset
print("load dataset ...")
correct_dataset = load_dataset(
"text",
data_files={"train": data_path + "/train" + "/correct_sentences.txt"},
split="train",
)
dataset = load_dataset(
"text",
data_files={"train": data_path + "/train" + "/corrupted_sentences.txt"},
split="train",
)
dataset = dataset.add_column("labels", correct_dataset["text"].copy())
del correct_dataset
torch.cuda.empty_cache()
gc.collect()
print(dataset)
# load tokenizer
print("load tokenizer ...")
if 'y' in download:
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
tokenizer.save_pretrained(tokenizer_path)
else:
tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
### batched must be False
def tokenize_function(tokenizer, dataset):
bos_token, eos_token = tokenizer("")["input_ids"]
final_text = []
final_label = []
word_indices = []
for idx in tqdm(range(dataset.num_rows)):
temp_text = []
temp_label = []
word_index = []
splitted_text = dataset[idx]["text"].split()
for idx, text_word, label_word in zip(
range(1, len(splitted_text) + 1),
splitted_text,
dataset[idx]["labels"].split(),
):
temp_result = tokenizer([text_word, label_word], padding="longest")[
"input_ids"
]
for i in range(2):
temp_result[i].remove(bos_token)
temp_result[i].remove(eos_token)
temp_text.extend(temp_result[0])
temp_label.extend(temp_result[1])
word_index.extend([idx] * len(temp_result[0]))
temp_text.insert(0, bos_token)
temp_text.append(eos_token)
temp_label.insert(0, bos_token)
temp_label.append(eos_token)
word_index.insert(0, 0)
word_index.append(len(splitted_text) + 1)
final_text.append(temp_text)
final_label.append(temp_label)
word_indices.append(word_index)
return Dataset.from_dict(
{"input_ids": final_text, "labels": final_label, "token_type_ids": word_indices}
)
tokenized_dataset = tokenize_function(tokenizer, dataset)
del dataset
torch.cuda.empty_cache()
gc.collect()
# print(tokenized_dataset)
# print(tokenized_dataset[0])
# grouping
print("group text ...")
def group_texts(data):
block_size = 128 # TODO: test with 256
concatenated_data = {key: sum(data[key], []) for key in data.keys()}
total_length = len(concatenated_data[list(data.keys())[0]])
new_total_length = (total_length // block_size) * block_size
result = {
key: [
val[idx : idx + block_size]
for idx in range(0, new_total_length, block_size)
]
for key, val in concatenated_data.items()
}
return result
final_dataset = tokenized_dataset.map(
group_texts,
batched=True,
batch_size=64,
num_proc=4,
)
del tokenized_dataset
torch.cuda.empty_cache()
gc.collect()
# data collector
def whole_word_masking_data_collator_V2(features):
wwm_probability = 0.15
for feature in features:
word_ids = feature.pop("token_type_ids")
# Create a map between words and corresponding token indices
mapping = collections.defaultdict(list)
current_word_index = -1
current_word = None
for idx, word_id in enumerate(word_ids):
if word_id is not None:
if word_id != current_word:
current_word = word_id
current_word_index += 1
mapping[current_word_index].append(idx)
# Randomly mask words
mask = np.random.binomial(1, wwm_probability, (len(mapping),))
input_ids = feature["input_ids"]
labels = feature["labels"]
new_labels = [-100] * len(labels)
indices_replaced = torch.bernoulli(torch.full((len(mapping),), 0.8)).bool()
indices_random = (
torch.bernoulli(torch.full((len(mapping),), 0.5)).bool() & ~indices_replaced
)
random_words = torch.randint(len(tokenizer), (len(mapping),), dtype=torch.long)
# ERRORS (NOT MASK)
for idx, (inp_ids, label) in enumerate(zip(input_ids, labels)):
if inp_ids != label: # TODO: think about here
new_labels[idx] = label
# 15% RANDOM SELECTED
for word_id in np.where(mask)[0]:
word_id = word_id.item()
# 80% MASK
if indices_replaced[word_id].item():
for idx in mapping[word_id]:
new_labels[idx] = labels[idx]
input_ids[idx] = tokenizer.mask_token_id
# 10% RANDOM
elif indices_random[word_id].item():
for idx in mapping[word_id]:
new_labels[idx] = labels[idx]
input_ids[idx] = random_words[word_id]
# 10% NOT CHANGE
else:
for idx in mapping[word_id]:
new_labels[idx] = labels[idx]
feature["labels"] = new_labels
return torch_default_data_collator(features)
# split train, evaluation dataset
print("split train evaluation dataset ...")
final_dataset = final_dataset.train_test_split(test_size=0.2)
print(final_dataset)
# print(final_dataset["train"])
# print(final_dataset["train"][0])
# load model
print("load model ...")
if 'y' in download:
model = BertForMaskedLM.from_pretrained(model_checkpoint)
model.save_pretrained(original_model)
else:
model = BertForMaskedLM.from_pretrained(original_model)
# define trainer and args
training_args = TrainingArguments(
biased_model,
overwrite_output_dir=True,
evaluation_strategy=IntervalStrategy.STEPS, # "steps",
save_steps=500,
logging_steps=500,
eval_steps=500, # Evaluation and Save happens every 250 steps
save_total_limit=2, # Only 2 models are saved. best and last.
report_to="all",
per_device_train_batch_size=32,
per_device_eval_batch_size=32,
learning_rate=1e-6,
weight_decay=0.01,
load_best_model_at_end=True,
optim="adamw_torch",
# push_to_hub=True,
# hub_model_id="Amir79Naziri/bert-base-parsbert-uncased-finetuned",
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=final_dataset["train"],
eval_dataset=final_dataset["test"],
data_collator=whole_word_masking_data_collator_V2,
tokenizer=tokenizer,
callbacks=[EarlyStoppingCallback(early_stopping_patience=3)],
)
print("start training ...")
trainer.train()
trainer.save_model(biased_model)
print(trainer.state.best_model_checkpoint)
print("done. :)")