-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_tacred.py
219 lines (181 loc) · 9.31 KB
/
train_tacred.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
import argparse
import os
import numpy as np
import torch
from tqdm import tqdm
from torch.utils.data import DataLoader
from transformers import AutoConfig, AutoTokenizer
from transformers.optimization import AdamW, get_linear_schedule_with_warmup
from utils import set_seed, collate_fn
from prepro import TACREDProcessor
from evaluation import get_f1
from model import REModel
from torch.cuda.amp import GradScaler
import wandb
def train(args, model, train_features, benchmarks):
train_dataloader = DataLoader(train_features, batch_size=args.train_batch_size, shuffle=True, collate_fn=collate_fn,
drop_last=True)
total_steps = int(len(train_dataloader) * args.num_train_epochs // args.gradient_accumulation_steps)
warmup_steps = int(total_steps * args.warmup_ratio)
scaler = GradScaler()
optimizer = AdamW(model.parameters(), lr=args.learning_rate, eps=args.adam_epsilon)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=warmup_steps,
num_training_steps=total_steps)
print('Total steps: {}'.format(total_steps))
print('Warmup steps: {}'.format(warmup_steps))
best_f1 = 0
num_steps = 0
for epoch in range(int(args.num_train_epochs)):
model.zero_grad()
for step, batch in enumerate(tqdm(train_dataloader)):
model.train()
inputs = {'input_ids': batch[0].to(args.device),
'attention_mask': batch[1].to(args.device),
'labels': batch[2].to(args.device),
'ss': batch[3].to(args.device),
'os': batch[4].to(args.device),
'entity_mask': batch[5].to(args.device),
}
outputs = model(**inputs)
loss = outputs[0] / args.gradient_accumulation_steps
scaler.scale(loss).backward()
if step % args.gradient_accumulation_steps == 0:
num_steps += 1
if args.max_grad_norm > 0:
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
scaler.step(optimizer)
scaler.update()
scheduler.step()
model.zero_grad()
wandb.log({'loss': loss.item()}, step=num_steps)
if (num_steps % args.evaluation_steps == 0 and step % args.gradient_accumulation_steps == 0):
for tag, features in benchmarks:
f1, output = evaluate(args, model, features, tag=tag)
wandb.log(output, step=num_steps)
if tag == 'dev':
if f1 > best_f1:
best_f1 = f1
model.save_pretrained(args.output_dir+f"/k-{args.k_size}/seed-{args.seed}/checkpoint-{num_steps}-{f1}")
for tag, features in benchmarks:
f1, output = evaluate(args, model, features, tag=tag)
wandb.log(output, step=num_steps)
def evaluate(args, model, features, tag='dev'):
dataloader = DataLoader(features, batch_size=args.test_batch_size, collate_fn=collate_fn, drop_last=False)
keys, preds = [], []
for _, batch in enumerate(dataloader):
model.eval()
inputs = {'input_ids': batch[0].to(args.device),
'attention_mask': batch[1].to(args.device),
'ss': batch[3].to(args.device),
'os': batch[4].to(args.device),
'entity_mask': batch[5].to(args.device),
}
keys += batch[2].tolist()
with torch.no_grad():
logit = model(**inputs)[0]
pred = torch.argmax(logit, dim=-1)
preds += pred.tolist()
keys = np.array(keys, dtype=np.int64)
preds = np.array(preds, dtype=np.int64)
_, _, max_f1 = get_f1(keys, preds)
output = {
tag + "_f1": max_f1 * 100,
}
print(output)
return max_f1, output
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", default="./data/tacred", type=str)
parser.add_argument("--model_name_or_path", default="roberta-large", type=str)
parser.add_argument("--input_format", default="typed_entity_marker_punct", type=str,
help="in [entity_mask, entity_marker, entity_marker_punct, typed_entity_marker, typed_entity_marker_punct]")
parser.add_argument("--config_name", default="", type=str,
help="Pretrained config name or path if not the same as model_name")
parser.add_argument("--tokenizer_name", default="", type=str,
help="Pretrained tokenizer name or path if not the same as model_name")
parser.add_argument("--max_seq_length", default=512, type=int,
help="The maximum total input sequence length after tokenization. Sequences longer "
"than this will be truncated.")
parser.add_argument("--train_batch_size", default=32, type=int,
help="Batch size for training.")
parser.add_argument("--test_batch_size", default=32, type=int,
help="Batch size for testing.")
parser.add_argument("--learning_rate", default=3e-5, type=float,
help="The initial learning rate for Adam.")
parser.add_argument("--gradient_accumulation_steps", default=2, type=int,
help="Number of updates steps to accumulate the gradients for, before performing a backward/update pass.")
parser.add_argument("--adam_epsilon", default=1e-6, type=float,
help="Epsilon for Adam optimizer.")
parser.add_argument("--max_grad_norm", default=1.0, type=float,
help="Max gradient norm.")
parser.add_argument("--warmup_ratio", default=0.1, type=float,
help="Warm up ratio for Adam.")
parser.add_argument("--num_train_epochs", default=5.0, type=float,
help="Total number of training epochs to perform.")
parser.add_argument("--seed", type=int, default=42,
help="random seed for initialization")
parser.add_argument("--num_class", type=int, default=42)
parser.add_argument("--evaluation_steps", type=int, default=500,
help="Number of steps to evaluate the model")
parser.add_argument("--dropout_prob", type=float, default=0.1)
parser.add_argument("--project_name", type=str, default="RE_baseline")
parser.add_argument("--run_name", type=str, default="tacred")
parser.add_argument("--output_dir", type=str, default="./outputs")
parser.add_argument("--k_size", type=int, default=3)
parser.add_argument("--debug", type=bool, default=False)
args = parser.parse_args()
wandb.init(project=args.project_name, name=args.run_name)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
args.n_gpu = torch.cuda.device_count()
args.device = device
if args.seed > 0:
set_seed(args)
config = AutoConfig.from_pretrained(
args.config_name if args.config_name else args.model_name_or_path,
num_labels=args.num_class,
)
config.gradient_checkpointing = True
tokenizer = AutoTokenizer.from_pretrained(
args.tokenizer_name if args.tokenizer_name else args.model_name_or_path,
)
config.num_class = args.num_class
config.dropout_prob = args.dropout_prob
config.k_size = args.k_size
model = REModel.from_pretrained(args.model_name_or_path, config=config)
with torch.no_grad():
token_embeddings = model.roberta.embeddings.word_embeddings.weight.clone().detach()
token_dis = torch.cdist(token_embeddings.unsqueeze(0), token_embeddings.unsqueeze(0)).squeeze(0)
k_size = args.k_size - 1
token_count = token_embeddings.size(0)
for k in range(k_size):
random_indices = torch.randint(low=0, high=token_count, size=(token_count,))
topk_mask = torch.zeros_like(token_dis, dtype=torch.bool)
topk_mask[torch.arange(token_count), random_indices] = True
masked_dis = -token_dis - 1000000 * topk_mask
token_probs = torch.nn.functional.softmax(masked_dis, dim=-1)
knn_token_embeds = torch.matmul(token_probs, token_embeddings)
model.roberta.embeddings.entity_embeddings_k[k].weight = torch.nn.parameter.Parameter(knn_token_embeds)
model.to(0)
# for param in model.roberta.embeddings.word_embeddings.parameters():
# param.requires_grad = False
train_file = os.path.join(args.data_dir, "train.json")
dev_file = os.path.join(args.data_dir, "dev.json")
test_file = os.path.join(args.data_dir, "test.json")
cf_test_file = os.path.join(args.data_dir, "test_entred.json")
if not args.debug:
processor = TACREDProcessor(args, tokenizer)
train_features = processor.read(train_file)
dev_features = processor.read(dev_file)
test_features = processor.read(test_file)
cf_test_features = processor.read(cf_test_file)
if len(processor.new_tokens) > 0:
model.roberta.resize_token_embeddings(len(tokenizer))
benchmarks = (
("dev", dev_features),
("test", test_features),
("cf_test", cf_test_features),
)
train(args, model, train_features, benchmarks)
if __name__ == "__main__":
main()