-
Notifications
You must be signed in to change notification settings - Fork 1
/
bert.py
125 lines (99 loc) · 3.99 KB
/
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
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
import torch
import numpy as np
import scipy.io
import joblib
from model import LinearText
from loss import EDL_Loss
from bert_serving.client import BertClient
BERTClient = BertClient()
import os
import argparse
# config #
inner_rate = 0.01
ntrain = 50
loss_fn = EDL_Loss()
print('[Start linear joint ...]')
parser = argparse.ArgumentParser()
parser.add_argument("--file_path", help="saving root path of raw data")
parser.add_argument("--seed", help="reproducible experiment with seeds", type=int)
parser.add_argument("--out_dim", help="output dimension", type=int, default=6)
args = parser.parse_args()
RandomGenerator = np.random.RandomState(args.seed)
[vocabulary, pretrained_embeddings, \
X, y, X_train, X_test, y_train, y_test, inds_train, inds_test, inds_all] \
= joblib.load(os.path.join(args.file_path, 'data/raw.pkl'))
def GetSentenceBERTEmbedding(inds):
all_words = []
for ind in inds:
word_indices = X[ind]
words = ' '.join([vocabulary[i] for i in word_indices])
all_words.append(words)
return BERTClient.encode(all_words)
train_extracted_features = None
train_labels = None
test_extracted_features = None
test_preds = None
last_feature_idx = 0
Xtrain = GetSentenceBERTEmbedding(inds_train)
ytrain = y[inds_train]
Xtrain = torch.from_numpy(Xtrain).float().cuda()
ytrain = torch.from_numpy(ytrain).float().cuda()
Xtest = GetSentenceBERTEmbedding(inds_test)
ytest = y[inds_test]
Xtest = torch.from_numpy(Xtest).float().cuda()
ytest = torch.from_numpy(ytest).float().cuda()
# joint training #
joint_model = LinearText(768, args.out_dim).cuda()
optimizer = torch.optim.SGD(joint_model.parameters(), lr=inner_rate)
for epoch in range(25):
print('joint training {:d}'.format(epoch))
joint_model.train()
m = len(inds_train)
inds = RandomGenerator.permutation(m)
for start in range(0, m, ntrain):
mbinds = inds[start:start + ntrain]
preds, _ = joint_model(Xtrain[mbinds])
preds = preds.cuda()
loss = loss_fn(ytrain[mbinds], preds)
parameters = joint_model.parameters()
optimizer.zero_grad()
loss.backward()
optimizer.step()
model_eval = LinearText(768, args.out_dim).cuda()
model_eval.set_parameters(joint_model.parameters())
model_eval.eval()
m = len(Xtrain)
inds = np.array(range(m))
for start in range(0, m, ntrain):
mbinds = inds[start:start + ntrain]
preds, features = model_eval(Xtrain[mbinds])
preds = preds.cuda()
train_extracted_features = \
np.concatenate([train_extracted_features, features.cpu().detach().numpy()]) \
if train_extracted_features is not None else features.cpu().detach().numpy()
train_labels = \
np.concatenate([train_labels, ytrain[mbinds].clone().cpu().detach().numpy()]) \
if train_labels is not None else ytrain[mbinds].clone().cpu().detach().numpy()
n = len(Xtest)
inds = np.array(range(n))
for start in range(0, n, ntrain):
mbinds = inds[start:start + ntrain]
preds, features = model_eval(Xtest[mbinds])
test_extracted_features = \
np.concatenate([test_extracted_features, features.cpu().detach().numpy()]) \
if test_extracted_features is not None else features.cpu().detach().numpy()
test_preds = \
np.concatenate([test_preds, preds.clone().cpu().detach().numpy()]) \
if test_preds is not None else preds.clone().cpu().detach().numpy()
test_preds = np.array(test_preds)
train_extracted_features = np.array(train_extracted_features)
test_extracted_features = np.array(test_extracted_features)
train_labels = np.array(train_labels)
if not os.path.exists(os.path.join(args.file_path, 'results')):
os.makedirs(os.path.join(args.file_path, 'results'))
scipy.io.savemat(os.path.join(args.file_path, 'results/linear.mat'),
dict(pred=test_preds, true=y_test,
train_features=train_extracted_features,
train_labels=train_labels,
test_features=test_extracted_features))
print('[Finish linear joint ...]')