-
Notifications
You must be signed in to change notification settings - Fork 3
/
predict_task1.py
226 lines (158 loc) · 5.15 KB
/
predict_task1.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
221
222
223
224
225
226
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import BertTokenizer, BertModel
from transformers import AdamW, WarmupLinearSchedule
import nltk
from nltk.corpus import stopwords
import numpy as np
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.model_selection import StratifiedKFold
from tqdm import tqdm
import csv
import random
import matplotlib.pyplot as plt
import sys
import os
import pickle
# In[2]:
use_cuda = torch.cuda.is_available()
if use_cuda:
print("using cuda!")
print("seed:", sys.argv[1])
if not os.path.exists("model/model_{}_state".format(sys.argv[1])):
print("NOT FOUND")
sys.exit()
device = torch.device("cuda:"+sys.argv[2])
col_names = []
x = []
y = []
lens = []
MAX_LEN = 512
rev = False
dct = {'BACKGROUND': 0, 'OBJECTIVES': 1, 'METHODS': 2, "RESULTS": 3, 'CONCLUSIONS': 4, 'OTHERS': 5}
pool = [0 for _ in range(29)] + [1 for _ in range(20)] + [2 for _ in range(29)] + [3 for _ in range(25)] + [4 for _ in range(11)] + [5 for _ in range(2)]
stop_words = set(stopwords.words('english'))
rm_sw = False
# In[3]:
# ## Fine-Tune Bert
# In[4]:
import math
class gelu(nn.Module):
def __init__(self):
super(gelu, self).__init__()
def forward(self, x):
cdf = 0.5 * (1.0 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3))))
return x * cdf
class MutiLabelModel(nn.Module):
def __init__(self, encoder, emb_size=768, out_size=6):
super(MutiLabelModel, self).__init__()
self.encoder = encoder
self.fn_size = emb_size
self.out_size = out_size
self.out_fn = nn.Linear(emb_size, out_size)
def forward(self, inp, seg_inp):
# batch = number of sentence
embs = self.encoder(inp, token_type_ids=seg_inp)[0] # [batch, seq, emb_dim]
outputs = embs[0, (inp==102).nonzero()[:, 1], :] # [batch, emb_dim]
outputs = self.out_fn(outputs).view(-1, self.out_size) # [batch, out_dim]
return outputs
# ## Sentences to Tokens
# In[6]:
seed = int(sys.argv[1]) # tune this only
random.seed(seed)
torch.manual_seed(seed)
best_f1 = -1
batch_size = 8
epochs = 3
lr = 1e-5
softmax = False
# load pre-trained bert
print("loading bert...")
tokenizer = BertTokenizer.from_pretrained('scibert_scivocab_uncased')
encoder = BertModel.from_pretrained('scibert_scivocab_uncased')
# ## Train
# In[7]:
# %%time
take = 6
# init model
print("initializing model...")
model = MutiLabelModel(encoder, 768, take)
model.load_state_dict(torch.load("model/model_{}_state".format(seed), map_location=device))
test_data = []
lens = []
with open("data/task1_private_testset.csv") as f:
rows = csv.reader(f)
for row in rows:
if row[0] == "Id":
col_names = row
continue
if rev:
row[2] = " ".join(row[2].replace('$$$', ' $$$ ').split()[::-1]).replace(' $$$ ', '$$$')
lens.append(len(row[2].split('$$$')))
row[2] = row[2].replace('.', '').replace('$$$', ' [CLS] ')
row[2] = '[CLS] ' + row[2] + ' {} [SEP]'.format(lens[-1])
test_data.append(row[2])
test_data[0]
# In[24]:
count_guess = 0
# sentences to tokens
print("encoding sentences to tokens...")
for i in tqdm(range(len(test_data))):
indexed_tokens = tokenizer.encode(test_data[i])[:MAX_LEN]
segments_ids = [0 for _ in range(len(indexed_tokens))]
if len(indexed_tokens) < MAX_LEN:
test_data[i] = (torch.LongTensor([indexed_tokens]), torch.LongTensor([segments_ids]))
else:
test_data[i] = [lens[i]]
count_guess += 1
count_guess
# In[25]:
model = model.to(device)
model = model.eval()
thd = 1/take if softmax else 0.7
thd2 = 0.6
thrld = np.ones((1,take))*thd
thrld_ten = torch.from_numpy(thrld).float().to(device)
preds = []
with torch.no_grad():
pbar = tqdm(enumerate(test_data), total=len(test_data))
for sidx, tokens in pbar:
if len(tokens) == 1:
for i in range(tokens[0]):
start = int((5/tokens[0]) * i)
if start >= 4:
start = 3
guess = [0.0 for _ in range(6)]
guess[start], guess[start+1] = 1.0, 1.0
preds.append(guess)
else:
inp, seg_inp = tokens[0].to(device), tokens[1].to(device)
out = model(inp, seg_inp)
if softmax:
out = torch.softmax(out, 1)
out = torch.log(out) - torch.log(torch.ones(out.size()).to(device)-out)
out = torch.sigmoid(out)
preds += out.tolist()
# In[26]:
for i in range(len(preds)):
for j in range(len(preds[i])):
preds[i][j] = round(preds[i][j], 5)
preds[0]
# In[27]:
zeros_all = []
with open("data/task1_sample_submission.csv") as f:
rows = csv.reader(f)
for row in rows:
zeros_all.append(row)
# In[28]:
with open("results/task1/result_{}.csv".format(seed), "w", newline="") as f:
w = csv.writer(f)
for i, row in enumerate(zeros_all):
if i >= 131167:
w.writerow(row[:1]+preds[i-131167])
else:
w.writerow(row)