-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
executable file
·329 lines (283 loc) · 13 KB
/
util.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python3
import os
import sys
import copy
import re
import time
import datetime
from urllib.request import urlopen
import numpy as np
import nltk
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.stem.porter import PorterStemmer
import json
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
# training with SGLD with annealing and save models
def train(X_train, y_train, X_valid, y_valid, X_test, y_test, model, args):
model.train()
batch = args.batch_size
parameters = [parameter for parameter in model.parameters()]
set_scale = [parameter.data.std().item() for parameter in model.parameters()]
set_scale = [scale / max(set_scale) for scale in set_scale] # normalize
for epoch in range(1, args.epochs+1):
corrects = 0
epsilon = args.lr * ((epoch * 1.0) ** (-0.333)) # optimal decay rate
for idx in range(int(X_train.shape[0]/batch) + 1):
feature = torch.LongTensor(X_train[(idx*batch):(idx*batch+batch),])
target = torch.LongTensor(y_train[(idx*batch):(idx*batch+batch)])
if args.cuda:
feature, target = feature.cuda(), target.cuda()
logit = model(feature)
loss = F.cross_entropy(logit, target)
model.zero_grad()
loss.backward()
for layer_no, param in enumerate(model.parameters()):
if args.static and layer_no == 0: # fixed embedding layer cannot update
continue
# by default I assume you train the models using GPU
noise = torch.cuda.FloatTensor(param.data.size()).normal_() * np.sqrt(epsilon / args.t)
#noise = torch.cuda.FloatTensor(param.data.size()).normal_() * set_scale[layer_no]
parameters[layer_no].data += (- epsilon / 2 * param.grad + noise)
corrects += (torch.max(logit, 1)[1].view(target.size()).data == target.data).sum().item()
accuracy = 100.0 * corrects / batch / (idx + 1)
sys.stdout.write('\rEpoch[{}] Batch[{}] - loss: {:.4f} acc: {:.2f}%({}/{}) tempreture: {}'.format(
epoch, idx, loss.item(), accuracy, corrects, batch * (idx + 1), int(args.t)))
args.t = args.t + 1 # annealing
if epoch % 5 != 0:
continue
'''
try:
set_scale = [parameter.grad.data.std().item() for parameter in model.parameters()]
set_scale = [scale / max(set_scale) for scale in set_scale] # normalize
except:
set_scale = [parameter.data.std().item() for parameter in model.parameters()]
set_scale = [scale / max(set_scale) for scale in set_scale] # normalize
'''
save(model, args.save_dir, epoch)
print()
eval(X_valid, y_valid, model, 'Validation', args)
eval(X_test, y_test, model, 'Testing ', args)
def eval(X, y, model, term, args):
model.eval()
corrects, TP, avg_loss = 0, 0, 0
correct_part, total_part = {0.2:0, 0.4:0}, {0.2:1e-16, 0.4:1e-16}
batch = args.batch_size
for idx in range(int(X.shape[0]/batch) + 1):
feature = torch.LongTensor(X[(idx*batch):(idx*batch+batch),])
target = torch.LongTensor(y[(idx*batch):(idx*batch+batch)])
if args.cuda:
feature, target = feature.cuda(), target.cuda()
logit = model(feature)
loss = F.cross_entropy(logit, target, size_average=False)
avg_loss += loss.data.item()
predictor = torch.exp(logit[:, 1]) / (torch.exp(logit[:, 0]) + torch.exp(logit[:, 1]))
for xnum in range(1, 3):
thres = round(0.2 * xnum, 1)
idx_thres = (predictor > 0.5 + thres) + (predictor < 0.5 - thres)
correct_part[thres] += (torch.max(logit, 1)[1][idx_thres] == target.data[idx_thres]).sum().item()
total_part[thres] += idx_thres.sum().item()
corrects += (torch.max(logit, 1)[1] == target.data).sum().item()
TP += (((torch.max(logit, 1)[1] == target.data).int() + (torch.max(logit, 1)[1]).int()) == 2).sum().item()
size = y.shape[0]
avg_loss /= size
accuracy = 100.0 * corrects / size
# TP, TN: True Positive/True Negative
print(' {} - loss: {:.4f} acc: {:.2f}%({}/{}) {:.2f}%({}/{}) {:.2f}%({}/{}) TP/TN: ({}/{}) \n'.format(term,
avg_loss, accuracy, corrects, size, 100.0 * correct_part[0.2] / total_part[0.2], correct_part[0.2], int(total_part[0.2]),
100.0 * correct_part[0.4] / total_part[0.4], correct_part[0.4], int(total_part[0.4]), TP, corrects - TP))
return accuracy
def bma_eval(X, y, mymodels, term, args):
corrects, TP, avg_loss = 0, 0, 0
correct_part, total_part = {0.2:0, 0.4:0}, {0.2:1e-16,0.4:1e-16}
batch = args.batch_size
for model in mymodels:
model.eval()
for idx in range(int(X.shape[0]/batch) + 1):
feature = torch.LongTensor(X[(idx*batch):(idx*batch+batch),])
target = torch.LongTensor(y[(idx*batch):(idx*batch+batch)])
if args.cuda:
feature, target = feature.cuda(), target.cuda()
logit = model(feature)
loss = F.cross_entropy(logit, target, size_average=False)
avg_loss += loss.data.item() / (len(mymodels) * 1.0)
predictor = torch.exp(logit[:, 1]) / (torch.exp(logit[:, 0]) + torch.exp(logit[:, 1]))
for xnum in range(1, 3):
thres = round(0.2 * xnum, 1)
idx_thres = (predictor > 0.5 + thres) + (predictor < 0.5 - thres)
correct_part[thres] += (torch.max(logit, 1)[1][idx_thres] == target.data[idx_thres]).sum().item() / (len(mymodels) * 1.0)
total_part[thres] += idx_thres.sum().item() / (len(mymodels) * 1.0)
corrects += (torch.max(logit, 1)[1] == target.data).sum().item() / (len(mymodels) * 1.0)
TP += (((torch.max(logit, 1)[1] == target.data).int() + (torch.max(logit, 1)[1]).int()) == 2).sum().item()
size = y.shape[0]
avg_loss /= size
accuracy = 100.0 * corrects / size
TP = TP * 1.0 / (len(mymodels) * 1.0)
print(' {} - loss: {:.4f} acc: {:.2f}%({}/{}) {:.2f}%({}/{}) {:.2f}%({}/{}) TP/TN: ({}/{}) \n'.format(term,
avg_loss, accuracy, corrects, size, 100.0 * correct_part[0.2] / total_part[0.2], correct_part[0.2], int(total_part[0.2]),
100.0 * correct_part[0.4] / total_part[0.4], correct_part[0.4], int(total_part[0.4]), TP, corrects - TP))
return accuracy
def predictor_preprocess(cnn, args):
# load trained thinning samples (Bayesian CNN models) from input/models/
mymodels = []
for num, each_model in enumerate(os.listdir(args.save_dir)):
print(args.save_dir + each_model)
if args.cuda:
cnn.load_state_dict(torch.load(args.save_dir + each_model))
else:
cnn.load_state_dict(torch.load(args.save_dir + each_model, map_location=lambda storage, loc: storage))
mymodels.append(copy.deepcopy(cnn))
if num > 30: # in case memory overloads
break
with open('./input/word2idx', 'r') as file:
word2idx = json.load(file)
stopWords = set()
with open('./input/stopWords') as file:
for word in file:
stopWords.add(word.strip())
return(mymodels, word2idx, stopWords)
def predict(sentence, mymodels, word2idx, stopWords, args):
tokens = tokenize_news(sentence, stopWords)
tokens = [word2idx[t] if t in word2idx else word2idx['UNKNOWN'] for t in tokens]
if len(tokens) < 5 or tokens == [word2idx['UNKNOWN']] * len(tokens): # tokens cannot be too short or unknown
signal = 'Unknown'
else:
feature = torch.LongTensor([tokens])
logits = []
for model in mymodels:
model.eval()
if args.cuda:
feature = feature.cuda()
logit = model(feature)
predictor = torch.exp(logit[:, 1]) / (torch.exp(logit[:, 0]) + torch.exp(logit[:, 1]))
logits.append(predictor.item())
signal = signals(np.mean(logits))
return(signal)
def daily_predict(cnn, args):
mymodels, word2idx, stopWords = predictor_preprocess(cnn, args)
output = './input/news/' + args.date[:4] + '/news_' + args.date + '.csv'
fout = open(output + '_bak', 'w')
with open(output) as f:
for num, line in enumerate(f):
line = line.strip().split(',')
if len(line) == 6:
ticker, name, day, headline, body, newsType = line
elif len(line) == 7:
ticker, name, day, headline, body, newsType, signal = line
else:
continue
#if newsType != 'topStory': # newsType: [topStory, normal]
# signal = 'Unknown'
content = headline + ' ' + body
signal = predict(content, mymodels, word2idx, stopWords, args)
fout.write(','.join([ticker, name, day, headline, body, newsType, signal]) + '\n')
fout.close()
print('change file name')
print('mv ' + output + '_bak ' + output)
os.system('mv ' + output + '_bak ' + output)
def save(model, save_dir, steps):
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
save_path = '{}/model_{}.pt'.format(save_dir,steps)
torch.save(model.state_dict(), save_path)
def signals(digit):
strong_signal = 0.4
unknown_thres = 0.05
if digit > 0.5 + strong_signal:
return('Strong Buy')
elif digit > 0.5 + unknown_thres:
return('Buy')
elif digit > 0.5 - unknown_thres:
return('Unknown')
elif digit > 0.5 - strong_signal:
return('Sell')
else:
return('Strong Sell')
def padding(sentencesVec, keepNum):
shape = sentencesVec.shape[0]
ownLen = sentencesVec.shape[1]
if ownLen < keepNum:
return np.hstack((np.ones([shape, keepNum-ownLen]), sentencesVec)).flatten()
else:
return sentencesVec[:, -keepNum:].flatten()
def dateGenerator(numdays): # generate N days until now, eg [20151231, 20151230]
base = datetime.datetime.today()
date_list = [base - datetime.timedelta(days=x) for x in range(0, numdays)]
for i in range(len(date_list)):
date_list[i] = date_list[i].strftime("%Y%m%d")
return set(date_list)
def generate_past_n_days(numdays):
"""Generate N days until now, e.g., [20151231, 20151230]."""
base = datetime.datetime.today()
date_range = [base - datetime.timedelta(days=x) for x in range(0, numdays)]
return [x.strftime("%Y%m%d") for x in date_range]
def unify_word(word): # went -> go, apples -> apple, BIG -> big
"""unify verb tense and noun singular"""
ADJ, ADJ_SAT, ADV, NOUN, VERB = 'a', 's', 'r', 'n', 'v'
for wt in [ADJ, ADJ_SAT, ADV, NOUN, VERB]:
try:
word = WordNetLemmatizer().lemmatize(word, pos=wt)
except:
pass
return word.lower()
def digit_filter(word):
check = re.match(r'\d*\.?\d*', word).group()
if check == "":
return word
else:
return ""
def unify_word_meaning(word):
if word in ["bigger-than-expected", "higher-than-expected", "better-than-expected", "stronger-than-expected"]:
return "better"
elif word in ["smaller-than-expected", "lower-than-expected", "weaker-than-expected", "worse-than-expected"]:
return "lower"
elif word in ["no", "not", "n't"]:
return "not"
else:
return word
def get_soup_with_repeat(url, repeat_times=3, verbose=True):
for i in range(repeat_times): # repeat in case of http failure
try:
time.sleep(np.random.poisson(3))
response = urlopen(url)
data = response.read().decode('utf-8')
return BeautifulSoup(data, "lxml")
except Exception as e:
if i == 0:
print(e)
if verbose:
print('retry...')
continue
def tokenize_news(headline, stopWords):
tokens = nltk.word_tokenize(headline) #+ nltk.word_tokenize(body)
tokens = list(map(unify_word, tokens))
tokens = list(map(unify_word, tokens)) # some words fail filtering in the 1st time
tokens = list(map(digit_filter, tokens))
tokens = list(map(unify_word_meaning, tokens))
tokens = [t for t in tokens if t not in stopWords and t != ""]
return(tokens)
def value2int(y, clusters=2):
label = np.copy(y)
label[y < np.percentile(y, 100 / clusters)] = 0
for i in range(1, clusters):
label[y > np.percentile(y, 100 * i / clusters)] = i
return label
def value2int_simple(y):
label = np.copy(y)
label[y < 0] = 0
label[y >= 0] = 1
return label
def model_eval(net, data_loader, if_print=1):
net.eval()
correct = 0
total = 0
for cnt, (images, labels) in enumerate(data_loader):
images, labels = Variable(images), Variable(labels)
if torch.cuda.is_available():
images, labels = images.cuda(), labels.cuda()
outputs = net.forward(images)
prediction = outputs.data.max(1)[1]
correct += prediction.eq(labels.data).sum().item()
print('\nTest set: Accuracy: {:0.2f}%'.format(100.0 * correct / len(data_loader.dataset)))