-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain_adaptation_warm_restart.py
220 lines (147 loc) · 4.39 KB
/
domain_adaptation_warm_restart.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
#!/usr/bin/env python
# coding: utf-8
# %%
# %%
import json
import os
from pathlib import Path
import huggingface_hub as hf_hub
import pandas as pd
from transformers import (
AutoModelForMaskedLM,
AutoTokenizer,
)
import os
# %%
# %%
os.environ["WANDB_API_KEY"] = "get_your_own"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
hf_hub.login("get_your_own", add_to_git_credential=True)
os.environ["WANDB_PROJECT"] = "emnlp_pragtag_2023"
# %%
non_empty_review_list = []
for r in (
Path.cwd().joinpath("auxilliary_data", "F1000-22", "data").glob("**/reviews.json")
):
with open(r, "r") as f:
review = json.load(f)
if len(review) > 0:
non_empty_review_list.append(r)
# %%
review_id_list = []
review_text_list = []
for ner in non_empty_review_list:
with open(ner, "r") as f:
review_list = json.load(f)
for review in review_list:
review_id_list.append(review["rid"])
review_text_list.append(review["report"]["main"])
# %%
abstract_data = pd.DataFrame.from_dict(
data={"review_id": review_id_list, "review_text": review_text_list}
)
# %%
from sklearn.model_selection import train_test_split
# %%
train_abstract_data, test_abstract_data = train_test_split(
abstract_data, test_size=0.5, random_state=42
)
valid_abstract_data, test_abstract_data = train_test_split(
test_abstract_data, test_size=0.5, random_state=42
)
# %%
import datasets
# %%
train_dataset = datasets.Dataset.from_pandas(train_abstract_data)
valid_dataset = datasets.Dataset.from_pandas(valid_abstract_data)
test_dataset = datasets.Dataset.from_pandas(test_abstract_data)
# %%
abstract_hf_dataset = datasets.DatasetDict(
{"train": train_dataset, "valid": valid_dataset, "test": test_dataset}
)
# %%
tokenizer = "microsoft/deberta-base"
model_name = "./emnlp_pragtag2023_domain_adapted"
tokenizer = AutoTokenizer.from_pretrained(
tokenizer, do_lower_case=True, force_download=True
)
# %%
def preprocess_text(example):
return tokenizer(example["review_text"])
# %%
abstract_hf_dataset_tokenised = abstract_hf_dataset.map(
preprocess_text,
batched=True,
remove_columns=abstract_hf_dataset["train"].features,
num_proc=10,
)
# %%
block_size = 512
def group_texts(examples):
# Concatenate all texts.
concatenated_examples = {}
for k in examples.keys():
tmp = sum(examples[k], [])
concatenated_examples[k] = tmp
total_length = len(concatenated_examples[list(examples.keys())[0]])
# We drop the small remainder
# we could add padding if the model supported it instead of this drop
# you can customize this part to your needs.
if total_length >= block_size:
total_length = (total_length // block_size) * block_size
# Split by chunks of block_size.
result = {
k: [t[i : i + block_size] for i in range(0, total_length, block_size)]
for k, t in concatenated_examples.items()
}
return result
# %%
abstract_hf_dataset_tokenised_chunked = abstract_hf_dataset_tokenised.map(
group_texts, batched=True, num_proc=1
)
# %%
from transformers import DataCollatorForLanguageModeling
tokenizer.pad_token = tokenizer.eos_token
data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer, mlm_probability=0.15
)
# %%
model = AutoModelForMaskedLM.from_pretrained(model_name)
# %%
from transformers import Trainer, TrainingArguments
# %%
batch_size = 8
gradient_accumulation_steps = 2
num_epochs = 100
training_args = TrainingArguments(
output_dir="emnlp_pragtag2023_domain_adapted_warm_restart",
overwrite_output_dir=True,
evaluation_strategy="epoch",
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=2 * batch_size,
gradient_accumulation_steps=gradient_accumulation_steps,
learning_rate=2e-5,
weight_decay=0.01,
adam_epsilon=1e-6,
num_train_epochs=num_epochs,
warmup_ratio=0.1,
save_total_limit=4,
push_to_hub=True,
save_strategy="epoch",
run_name=model_name.split("/")[-1],
metric_for_best_model="eval_loss",
load_best_model_at_end=True,
greater_is_better=False,
report_to="wandb",
hub_strategy="end",
hub_private_repo=True
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=abstract_hf_dataset_tokenised_chunked["train"],
eval_dataset=abstract_hf_dataset_tokenised_chunked["valid"],
data_collator=data_collator,
)
trainer.train()
# %%