This repository has been archived by the owner on Aug 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
221 lines (169 loc) · 7.78 KB
/
main.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
import json
import os
import torch
import argparse
import numpy as np
from datasets import load_dataset
from torch.utils.data import DataLoader
from datetime import datetime
from tqdm import tqdm
from transformers import AutoTokenizer
from os.path import join, abspath, dirname
from modeling import LM
from vocab import *
from dataset import LAMADataset
import glob
import re
SUPPORT_MODELS = ['bert-base-cased', 'bert-large-cased', 'gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']
def set_seed(args):
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
def construct_generation_args():
parser = argparse.ArgumentParser()
parser.add_argument("--model_name", type=str, default='bert-base-cased', choices=SUPPORT_MODELS)
parser.add_argument("--pseudo_token", type=str, default='[PROMPT]')
parser.add_argument("--template", type=str, default="(3, 3, 3)")
parser.add_argument("--seed", type=int, default=34, help="random seed for initialization")
parser.add_argument("--use_original_template", type=bool, default=False)
parser.add_argument("--vocab_strategy", type=str, default="shared", choices=['original', 'shared', 'lama'])
parser.add_argument("--lstm_dropout", type=float, default=0.0)
# directories
parser.add_argument("--data_dir", type=str, default=join(abspath(dirname(__file__)), './data/LAMA'))
parser.add_argument("--out_dir", type=str, default=join(abspath(dirname(__file__)), './out/LAMA'))
args = parser.parse_args()
args.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
args.n_gpu = torch.cuda.device_count()
args.template = eval(args.template) if type(args.template) is not tuple else args.template
assert type(args.template) is tuple
set_seed(args)
return args
class Trainer(object):
def __init__(self, args, pid=None):
self.args = args
self.pid = pid
self.device = 'cuda:0'
tokenizer_src = self.args.model_name
self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_src, use_fast=False)
init_vocab(args)
self.train_set = LAMADataset('train', self.tokenizer, self.args, self.pid)
self.train_loader = DataLoader(self.train_set, batch_size=8, shuffle=True, drop_last=True)
self.dev_set = LAMADataset('dev', self.tokenizer, self.args, self.pid)
self.dev_loader = DataLoader(self.dev_set, batch_size=8, shuffle=True)
self.test_set = LAMADataset('test', self.tokenizer, self.args, self.pid)
self.test_loader = DataLoader(self.test_set, batch_size=8, shuffle=True)
os.makedirs(self.get_save_path(), exist_ok=True)
self.model = LM(args, self.device, self.args.template)
def evaluate(self, epoch_idx, dataset_type):
self.model.eval()
if dataset_type == 'test':
loader = self.test_loader
dataset = self.test_set
else:
loader = self.dev_loader
dataset = self.dev_set
with torch.no_grad():
self.model.eval()
hit1, loss, pid_hits = 0, 0, [0]*41
for x_hs, x_ts, x_rels, x_pids in loader:
_loss, _hit1, pid_hit_list = self.model(x_hs, x_ts, x_rels, x_pids, return_candidates=True)
hit1 += _hit1
loss += _loss.item()
for i in pid_hit_list:
pid_hits[i-1]+=1
hit1 /= len(dataset)
print("{} {} Epoch Loss: {} Hit@1:".format(dataset_type, epoch_idx, loss / len(dataset)), hit1, pid_hits)
return loss, hit1, pid_hits
def get_save_path(self):
return join(self.args.out_dir, 'prompt_model', self.args.model_name, 'search')
def get_checkpoint(self, dev_hit1, test_hit1, dev_pid_hits, test_pid_hits):
ckpt_name = "Model {} dev_{}_test_{}.ckpt".format(self.pid, round(dev_hit1 * 100, 4), round(test_hit1 * 100, 4))
return {'dev_hit@1': dev_hit1,
'test_hit@1': test_hit1,
'dev_pid_hits': dev_pid_hits,
'test_pid_hits': test_pid_hits,
'test_size': len(self.test_set),
'ckpt_name': ckpt_name,
'time': datetime.now(),
'args': self.args,
'prompt_encoder': self.model.prompt_encoder.state_dict()
}
def save(self, best_ckpt):
ckpt_name = best_ckpt['ckpt_name']
path = self.get_save_path()
os.makedirs(path, exist_ok=True)
torch.save(best_ckpt, join(path, ckpt_name))
print("# Checkpoint {} saved.".format(ckpt_name))
def train(self):
best_dev, early_stop, has_adjusted = 0, 0, True
best_ckpt = None
params = [{'params': self.model.prompt_encoder.parameters()}]
optimizer = torch.optim.Adam(params, lr=1e-5, weight_decay=0.0005)
my_lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=0.98)
if self.args.use_original_template == True:
test_loss, test_hit1, test_pid_hits = self.evaluate(0, 'test')
dev_loss, dev_hit1, dev_pid_hits = self.evaluate(0, 'dev')
best_ckpt = self.get_checkpoint(dev_hit1, test_hit1, dev_pid_hits, test_pid_hits)
self.save(best_ckpt)
return best_ckpt
for epoch_idx in range(100):
dev_loss, dev_hit1, dev_pid_hits = self.evaluate(epoch_idx, 'dev')
if dev_hit1 >= best_dev:
test_loss, test_hit1, test_pid_hits = self.evaluate(epoch_idx, 'test')
best_ckpt = self.get_checkpoint(dev_hit1, test_hit1, dev_pid_hits, test_pid_hits)
best_dev = dev_hit1
early_stop = 0
else:
early_stop += 1
if early_stop >= 20:
self.save(best_ckpt)
print("Early stopping at epoch {}.".format(epoch_idx))
return best_ckpt
hit1, num_of_samples = 0, 0
tot_loss = 0
for batch_idx, batch in tqdm(enumerate(self.train_loader)):
self.model.train()
loss, batch_hit1, _ = self.model(batch[0], batch[1], batch[2], batch[3])
hit1 += batch_hit1
tot_loss += loss.item()
num_of_samples += len(batch[0])
loss.backward()
torch.cuda.empty_cache()
optimizer.step()
torch.cuda.empty_cache()
optimizer.zero_grad()
my_lr_scheduler.step()
self.save(best_ckpt)
return best_ckpt
def train_by_relation(args):
pids = [pid for pid in range(1,42)]
print(pids)
with open("result_gpt2_medium.txt", "w") as rf:
total_size = 0
total_hits = 0
for pid in pids:
print("pid:", pid)
trainer = Trainer(args, pid)
best_ckpt = trainer.train()
size = best_ckpt['test_size']
hits = best_ckpt['test_hit@1'] * size
print("cur:", hits, size, hits/size)
total_size += size
total_hits += hits
print("acc:", total_hits, total_size, total_hits/total_size)
rf.write(f"{pid}, {hits}, {size}, {hits/size}, {total_hits}, {total_size}, {total_hits/total_size}\n")
print("result:", total_hits, total_size, total_hits/total_size)
def train_whole(args):
trainer = Trainer(args)
best_ckpt = trainer.train()
size = best_ckpt['test_size']
hits = best_ckpt['test_hit@1'] * size
test_pid_hits = best_ckpt['test_pid_hits']
print("result:", hits, size, hits/size, test_pid_hits)
def main():
args = construct_generation_args()
print(args.model_name)
train_whole(args)
if __name__ == '__main__':
main()