-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_bert.py
74 lines (58 loc) · 2.45 KB
/
train_bert.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
import json
import os
import torch
from opennre.framework import SentenceRE
from opennre.model import SoftmaxNN
from framework.opennre.utils import load_bert_sentence_encoder
def run_finetunning_bert(data_root_path, pretrain_path, ckpt_source, ckpt_target,
pooler, max_length=128, batch_size=6, max_epoch=4, lr=5e-6):
""" Finetunning already pre-trained state.
"""
rel2id = json.load(open(os.path.join(data_root_path, "rel2id.json")))
train_path = os.path.join(data_root_path, "sample-train-0.json")
test_path = os.path.join(data_root_path, "sample-test-0.json")
val_path = train_path
sentence_encoder = load_bert_sentence_encoder(
pooler=pooler, mask_entity=True, max_length=max_length, pretrain_path=pretrain_path)
# Define the model output and load already provided checkpoint
model = SoftmaxNN(sentence_encoder, len(rel2id), rel2id)
model.load_state_dict(torch.load(ckpt_source, map_location='cpu')['state_dict'])
# Define the whole training framework
framework = SentenceRE(
train_path=train_path,
test_path=test_path if os.path.exists(test_path) else None,
val_path=val_path,
model=model,
ckpt=ckpt_target,
batch_size=batch_size,
max_epoch=max_epoch,
lr=lr,
opt='adamw')
# Train the model
framework.train_model('micro_f1')
def run_training_bert(pretrain_path, data_root_path, pooler, ckpt_target=None,
max_length=128, batch_size=6, max_epoch=4, lr=1e-5):
""" Training BERT from the original state.
"""
rel2id = json.load(open(os.path.join(data_root_path, "rel2id.json")))
train_path = os.path.join(data_root_path, "sample-train-0.json")
test_path = os.path.join(data_root_path, "sample-test-0.json")
val_path = train_path
# Define the sentence encoder
sentence_encoder = load_bert_sentence_encoder(
pooler=pooler, mask_entity=True, max_length=max_length, pretrain_path=pretrain_path)
# Define the model
model = SoftmaxNN(sentence_encoder, len(rel2id), rel2id)
# Define the whole training framework
framework = SentenceRE(
train_path=train_path,
test_path=test_path if os.path.exists(test_path) else None,
val_path=val_path,
model=model,
ckpt=ckpt_target,
batch_size=batch_size,
max_epoch=max_epoch,
lr=lr,
opt='adamw')
# Train the model
framework.train_model('micro_f1')