forked from SimiaoZuo/Transformer-Hawkes-Process
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
238 lines (181 loc) · 8.72 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import argparse
import numpy as np
import pickle
import time
import torch
import torch.nn as nn
import torch.optim as optim
import transformer.Constants as Constants
import Utils
from preprocess.Dataset import get_dataloader
from transformer.Models import Transformer
from tqdm import tqdm
def prepare_dataloader(opt):
""" Load data and prepare dataloader. """
def load_data(name, dict_name):
with open(name, 'rb') as f:
data = pickle.load(f, encoding='latin-1')
num_types = data['dim_process']
data = data[dict_name]
return data, int(num_types)
print('[Info] Loading train data...')
train_data, num_types = load_data(opt.data + 'train.pkl', 'train')
print('[Info] Loading dev data...')
dev_data, _ = load_data(opt.data + 'dev.pkl', 'dev')
print('[Info] Loading test data...')
test_data, _ = load_data(opt.data + 'test.pkl', 'test')
trainloader = get_dataloader(train_data, opt.batch_size, shuffle=True)
testloader = get_dataloader(test_data, opt.batch_size, shuffle=False)
return trainloader, testloader, num_types
def train_epoch(model, training_data, optimizer, pred_loss_func, opt):
""" Epoch operation in training phase. """
model.train()
total_event_ll = 0 # cumulative event log-likelihood
total_time_se = 0 # cumulative time prediction squared-error
total_event_rate = 0 # cumulative number of correct prediction
total_num_event = 0 # number of total events
total_num_pred = 0 # number of predictions
for batch in tqdm(training_data, mininterval=2,
desc=' - (Training) ', leave=False):
""" prepare data """
event_time, time_gap, event_type = map(lambda x: x.to(opt.device), batch)
""" forward """
optimizer.zero_grad()
enc_out, prediction = model(event_type, event_time)
""" backward """
# negative log-likelihood
event_ll, non_event_ll = Utils.log_likelihood(model, enc_out, event_time, event_type)
event_loss = -torch.sum(event_ll - non_event_ll)
# type prediction
pred_loss, pred_num_event = Utils.type_loss(prediction[0], event_type, pred_loss_func)
# time prediction
se = Utils.time_loss(prediction[1], event_time)
# SE is usually large, scale it to stabilize training
scale_time_loss = 100
loss = event_loss + pred_loss + se / scale_time_loss
loss.backward()
""" update parameters """
optimizer.step()
""" note keeping """
total_event_ll += -event_loss.item()
total_time_se += se.item()
total_event_rate += pred_num_event.item()
total_num_event += event_type.ne(Constants.PAD).sum().item()
# we do not predict the first event
total_num_pred += event_type.ne(Constants.PAD).sum().item() - event_time.shape[0]
rmse = np.sqrt(total_time_se / total_num_pred)
return total_event_ll / total_num_event, total_event_rate / total_num_pred, rmse
def eval_epoch(model, validation_data, pred_loss_func, opt):
""" Epoch operation in evaluation phase. """
model.eval()
total_event_ll = 0 # cumulative event log-likelihood
total_time_se = 0 # cumulative time prediction squared-error
total_event_rate = 0 # cumulative number of correct prediction
total_num_event = 0 # number of total events
total_num_pred = 0 # number of predictions
with torch.no_grad():
for batch in tqdm(validation_data, mininterval=2,
desc=' - (Validation) ', leave=False):
""" prepare data """
event_time, time_gap, event_type = map(lambda x: x.to(opt.device), batch)
""" forward """
enc_out, prediction = model(event_type, event_time)
""" compute loss """
event_ll, non_event_ll = Utils.log_likelihood(model, enc_out, event_time, event_type)
event_loss = -torch.sum(event_ll - non_event_ll)
_, pred_num = Utils.type_loss(prediction[0], event_type, pred_loss_func)
se = Utils.time_loss(prediction[1], event_time)
""" note keeping """
total_event_ll += -event_loss.item()
total_time_se += se.item()
total_event_rate += pred_num.item()
total_num_event += event_type.ne(Constants.PAD).sum().item()
total_num_pred += event_type.ne(Constants.PAD).sum().item() - event_time.shape[0]
rmse = np.sqrt(total_time_se / total_num_pred)
return total_event_ll / total_num_event, total_event_rate / total_num_pred, rmse
def train(model, training_data, validation_data, optimizer, scheduler, pred_loss_func, opt):
""" Start training. """
valid_event_losses = [] # validation log-likelihood
valid_pred_losses = [] # validation event type prediction accuracy
valid_rmse = [] # validation event time prediction RMSE
for epoch_i in range(opt.epoch):
epoch = epoch_i + 1
print('[ Epoch', epoch, ']')
start = time.time()
train_event, train_type, train_time = train_epoch(model, training_data, optimizer, pred_loss_func, opt)
print(' - (Training) loglikelihood: {ll: 8.5f}, '
'accuracy: {type: 8.5f}, RMSE: {rmse: 8.5f}, '
'elapse: {elapse:3.3f} min'
.format(ll=train_event, type=train_type, rmse=train_time, elapse=(time.time() - start) / 60))
start = time.time()
valid_event, valid_type, valid_time = eval_epoch(model, validation_data, pred_loss_func, opt)
print(' - (Testing) loglikelihood: {ll: 8.5f}, '
'accuracy: {type: 8.5f}, RMSE: {rmse: 8.5f}, '
'elapse: {elapse:3.3f} min'
.format(ll=valid_event, type=valid_type, rmse=valid_time, elapse=(time.time() - start) / 60))
valid_event_losses += [valid_event]
valid_pred_losses += [valid_type]
valid_rmse += [valid_time]
print(' - [Info] Maximum ll: {event: 8.5f}, '
'Maximum accuracy: {pred: 8.5f}, Minimum RMSE: {rmse: 8.5f}'
.format(event=max(valid_event_losses), pred=max(valid_pred_losses), rmse=min(valid_rmse)))
# logging
with open(opt.log, 'a') as f:
f.write('{epoch}, {ll: 8.5f}, {acc: 8.5f}, {rmse: 8.5f}\n'
.format(epoch=epoch, ll=valid_event, acc=valid_type, rmse=valid_time))
scheduler.step()
def main():
""" Main function. """
parser = argparse.ArgumentParser()
parser.add_argument('-data', required=True)
parser.add_argument('-epoch', type=int, default=30)
parser.add_argument('-batch_size', type=int, default=16)
parser.add_argument('-d_model', type=int, default=64)
parser.add_argument('-d_rnn', type=int, default=256)
parser.add_argument('-d_inner_hid', type=int, default=128)
parser.add_argument('-d_k', type=int, default=16)
parser.add_argument('-d_v', type=int, default=16)
parser.add_argument('-n_head', type=int, default=4)
parser.add_argument('-n_layers', type=int, default=4)
parser.add_argument('-dropout', type=float, default=0.1)
parser.add_argument('-lr', type=float, default=1e-4)
parser.add_argument('-smooth', type=float, default=0.1)
parser.add_argument('-log', type=str, default='log.txt')
opt = parser.parse_args()
# default device is CUDA
opt.device = torch.device('cuda')
# setup the log file
with open(opt.log, 'w') as f:
f.write('Epoch, Log-likelihood, Accuracy, RMSE\n')
print('[Info] parameters: {}'.format(opt))
""" prepare dataloader """
trainloader, testloader, num_types = prepare_dataloader(opt)
""" prepare model """
model = Transformer(
num_types=num_types,
d_model=opt.d_model,
d_rnn=opt.d_rnn,
d_inner=opt.d_inner_hid,
n_layers=opt.n_layers,
n_head=opt.n_head,
d_k=opt.d_k,
d_v=opt.d_v,
dropout=opt.dropout,
)
model.to(opt.device)
""" optimizer and scheduler """
optimizer = optim.Adam(filter(lambda x: x.requires_grad, model.parameters()),
opt.lr, betas=(0.9, 0.999), eps=1e-05)
scheduler = optim.lr_scheduler.StepLR(optimizer, 10, gamma=0.5)
""" prediction loss function, either cross entropy or label smoothing """
if opt.smooth > 0:
pred_loss_func = Utils.LabelSmoothingLoss(opt.smooth, num_types, ignore_index=-1)
else:
pred_loss_func = nn.CrossEntropyLoss(ignore_index=-1, reduction='none')
""" number of parameters """
num_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print('[Info] Number of parameters: {}'.format(num_params))
""" train the model """
train(model, trainloader, testloader, optimizer, scheduler, pred_loss_func, opt)
if __name__ == '__main__':
main()