-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
560 lines (472 loc) · 19.8 KB
/
models.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
from dataset import prepare_dataset
from math import pi
import torch
from IPython import embed
from torch import nn
import torch.nn.functional as F
import torch.optim as optim
from tqdm import tqdm
import torch.nn as nn
import random
import argparse
import datetime
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import copy
import math
import random
import time
from torch.distributions import Gamma, Poisson
class MLP(nn.Module):
def __init__(self, args):
super(MLP, self).__init__()
self.args = args
if self.args.full_chlov:
self.linear = nn.Linear(5*32, 1)
else:
self.linear = nn.Linear(1*32, 1)
def forward(self, chlov, history):
B, L, H = chlov.size()
if self.args.full_chlov:
chlov = chlov.view(B, -1)
history = history.view(B, -1)
else:
chlov = chlov[:, :, -1]
history = history[:, :, -1]
x = torch.cat((chlov, history), dim=1)
x = self.linear(x)
return x
class Attentive_Pooling(nn.Module):
def __init__(self, hidden_size):
super(Attentive_Pooling, self).__init__()
self.w_1 = nn.Linear(hidden_size, hidden_size)
self.u = nn.Linear(hidden_size, 1, bias=False)
def forward(self, memory, mask=None):
h = torch.tanh(self.w_1(memory))
score = torch.squeeze(self.u(h), -1) # node,
if mask is not None:
score = score.masked_fill(mask.eq(0), -1e9)
alpha = F.softmax(score, -1) # node,
s = torch.sum(torch.unsqueeze(alpha, -1) * memory, -2)
return s
class LSTM(nn.Module):
def __init__(self, args):
super(LSTM,self).__init__()
self.args = args
input_size = args.input_size
hidden_size = args.hidden_size
num_layer = args.num_layer
feature_size = args.feature_size
if self.args.full_chlov:
self.linear_chlov = nn.Linear(5, input_size)
self.linear_history = nn.Linear(5, input_size)
else:
self.linear_chlov = nn.Linear(1, input_size)
self.linear_history = nn.Linear(1, input_size)
self.lstm_chlov = nn.LSTM(input_size, hidden_size, num_layer)
self.lstm_history = nn.LSTM(input_size, hidden_size, num_layer)
if self.args.attn_pooling:
self.attn_pooling_chlov = Attentive_Pooling(hidden_size)
self.attn_pooling_history = Attentive_Pooling(hidden_size)
if feature_size != 0:
self.linear_out1 = nn.Linear(2 * hidden_size, feature_size)
self.linear_out2 = nn.Linear(feature_size, 1)
else:
self.linear_out = nn.Linear(2 * hidden_size, 1)
def forward(self, chlov, history):
self.lstm_chlov.flatten_parameters()
self.lstm_history.flatten_parameters()
if self.args.full_chlov:
chlov = F.relu(self.linear_chlov(chlov))
history = F.relu(self.linear_history(history))
else:
chlov = F.relu(self.linear_chlov(chlov[:, :, -1:]))
history = F.relu(self.linear_history(history[:, :, -1:]))
B, L, H = chlov.size()
chlov, _ = self.lstm_chlov(chlov)
history, _ = self.lstm_history(history)
if self.args.attn_pooling:
chlov = self.attn_pooling_chlov(chlov)
history = self.attn_pooling_history(history)
else:
chlov = chlov[:, -1, :]
history = history[:, -1, :]
x = torch.cat((chlov, history), dim=1)
if self.args.feature_size != 0:
x = F.relu(self.linear_out1(x))
x = self.linear_out2(x)
else:
x = self.linear_out(x)
return x
def clones(module, N):
"Produce N identical layers."
return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])
class Transformer(nn.Module):
def __init__(self, config):
super(Transformer, self).__init__()
c = copy.deepcopy
self.args = config
d_model = config.hidden_size
head_num = 8
dropout = 0.1
self.dropout = nn.Dropout(dropout)
attn = MultiHeadedAttention(head_num, d_model, dropout)
ff = PositionwiseFeedForward(d_model, d_model, dropout)
self.position_emb = nn.Embedding(20 + 12 + 1, config.input_size)
self.word_lienar = nn.Linear(5, config.input_size)
self.emb_proj = nn.Linear(config.input_size, d_model)
self.encoder = Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), config.num_layer)
self.w_out = nn.Linear(d_model, 1)
self.bos_emb = nn.Parameter(torch.zeros(1, 1, 5))
self.init()
def init(self):
for p in self.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
def forward(self, chlov, history):
batch_size, seq_len = chlov.size(0), 20 + 12 + 1
device = chlov.device
input = torch.cat((self.bos_emb.repeat(batch_size, 1, 1), chlov, history), dim=1)
bos = torch.ones(input.size(0), 1, device=device).long().fill_(2)
word_mask = torch.ones(batch_size, seq_len, device=device).long()
pos_indices = torch.unsqueeze(torch.arange(seq_len), 0).repeat(batch_size, 1).to(device)
word_emb = self.word_lienar(input)
pos_emb = self.position_emb(pos_indices)
emb = self.emb_proj(word_emb + pos_emb)
hidden = self.encoder(emb, word_mask)
result = self.w_out(self.dropout(hidden[:, 0, :]))
return result
class TransformerDaily(nn.Module):
def __init__(self, config):
super(TransformerDaily, self).__init__()
c = copy.deepcopy
self.args = config
d_model = config.hidden_size
head_num = 8
dropout = 0.1
self.dropout = nn.Dropout(dropout)
attn = MultiHeadedAttention(head_num, d_model, dropout)
ff = PositionwiseFeedForward(d_model, d_model, dropout)
self.position_emb = nn.Embedding(20 + 12 + 1, config.input_size)
self.word_lienar = nn.Linear(5, config.input_size)
self.emb_proj = nn.Linear(config.input_size, d_model)
self.encoder = Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), config.num_layer)
self.w_out = nn.Linear(d_model, 1)
self.bos_emb = nn.Parameter(torch.zeros(1, 1, 5))
self.init()
def init(self):
for p in self.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
def forward(self, chlov, history=None):
batch_size, seq_len = chlov.size(0), 20 + 1
device = chlov.device
input = torch.cat((self.bos_emb.repeat(batch_size, 1, 1), chlov), dim=1)
bos = torch.ones(input.size(0), 1, device=device).long().fill_(2)
word_mask = torch.ones(batch_size, seq_len, device=device).long()
pos_indices = torch.unsqueeze(torch.arange(seq_len), 0).repeat(batch_size, 1).to(device)
word_emb = self.word_lienar(input)
pos_emb = self.position_emb(pos_indices)
emb = self.emb_proj(word_emb + pos_emb)
hidden = self.encoder(emb, word_mask)
hidden = self.dropout(hidden[:, 0, :])
result = self.w_out(hidden)
return result
class Net(nn.Module):
def __init__(self, hidden_size):
super(Net, self).__init__()
self.linear_m = nn.Linear(
in_features=hidden_size,
out_features=1
)
self.linear_a = nn.Linear(in_features=hidden_size,
out_features=1)
class NegBinNet(Net):
def loss(self, output, z):
mean, alpha = output
r = 1 / alpha
ma = mean * alpha
pdf = torch.lgamma(z + r)
pdf -= torch.lgamma(z + 1)
pdf -= torch.lgamma(r)
pdf += r * torch.log(1 / (1 + ma))
pdf += z * torch.log(ma / (1 + ma))
pdf = torch.exp(pdf)
loss = torch.log(pdf)
loss = torch.mean(loss)
loss = - loss
return loss
def sample(self, m, a):
r = 1 / a
p = (m * a) / (1 + (m * a))
b = (1 - p) / p
g = Gamma(r, b)
g = g.sample()
p = Poisson(g)
z = p.sample()
return z
def forward(self, o):
m = F.softplus(self.linear_m(o))
a = F.softplus(self.linear_a(o))
return m, a
class GaussianNet(Net):
def loss(self, output, z):
m, a = output
v = a * a
t1 = 2 * pi * v
t1 = torch.pow(t1, -1 / 2)
t1 = torch.log(t1)
t2 = z - m
t2 = torch.pow(t2, 2)
t2 = - t2
t2 = t2 / (2 * v)
loss = t1 + t2
loss = - torch.mean(loss)
return loss
def sample(self, m, a):
return torch.normal(m, a)
def forward(self, o):
m = self.linear_m(o)
a = F.softplus(self.linear_a(o))
return m, a
class ARTransformerDaily(nn.Module):
def __init__(self, config):
super(ARTransformerDaily, self).__init__()
c = copy.deepcopy
self.args = config
d_model = config.hidden_size
head_num = 8
dropout = 0.1
self.dropout = nn.Dropout(dropout)
attn = MultiHeadedAttention(head_num, d_model, dropout)
ff = PositionwiseFeedForward(d_model, d_model, dropout)
self.position_emb = nn.Embedding(20 + 12 + 1, config.input_size)
self.word_lienar = nn.Linear(5, config.input_size)
self.emb_proj = nn.Linear(config.input_size, d_model)
self.encoder = Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), config.num_layer)
# self.w_out = nn.Linear(d_model, 1)#
self.dist = GaussianNet(d_model) if config.ar == 'gs' else NegBinNet(d_model)
self.bos_emb = nn.Parameter(torch.zeros(1, 1, 5))
self.init()
def init(self):
for p in self.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
def forward(self, chlov):
batch_size, seq_len = chlov.size(0), 20 + 1
device = chlov.device
input = torch.cat((self.bos_emb.repeat(batch_size, 1, 1), chlov), dim=1)
bos = torch.ones(input.size(0), 1, device=device).long().fill_(2)
word_mask = torch.ones(batch_size, seq_len, device=device).long()
pos_indices = torch.unsqueeze(torch.arange(seq_len), 0).repeat(batch_size, 1).to(device)
word_emb = self.word_lienar(input)
pos_emb = self.position_emb(pos_indices)
emb = self.emb_proj(word_emb + pos_emb)
hidden = self.encoder(emb, word_mask)
hidden = self.dropout(hidden[:, 0, :])
# deep AR
mu, sigma = self.dist(hidden)
return mu, sigma
class ARTransformer(nn.Module):
def __init__(self, config):
super(ARTransformer, self).__init__()
c = copy.deepcopy
self.args = config
d_model = config.hidden_size
head_num = 8
dropout = 0.1
self.dropout = nn.Dropout(dropout)
attn = MultiHeadedAttention(head_num, d_model, dropout)
ff = PositionwiseFeedForward(d_model, d_model, dropout)
self.position_emb = nn.Embedding(20 + 12 + 1, config.input_size)
self.word_lienar = nn.Linear(5, config.input_size)
self.emb_proj = nn.Linear(config.input_size, d_model)
self.encoder = Encoder(EncoderLayer(d_model, c(attn), c(ff), dropout), config.num_layer)
# self.w_out = nn.Linear(d_model, 1)#
self.dist = GaussianNet(d_model) if config.ar == 'gs' else NegBinNet(d_model)
self.bos_emb = nn.Parameter(torch.zeros(1, 1, 5))
self.init()
def init(self):
for p in self.parameters():
if p.dim() > 1:
nn.init.xavier_uniform_(p)
def forward(self, chlov, history):
batch_size, seq_len = chlov.size(0), 20 + 12 + 1
device = chlov.device
input = torch.cat((self.bos_emb.repeat(batch_size, 1, 1), chlov, history), dim=1)
bos = torch.ones(input.size(0), 1, device=device).long().fill_(2)
word_mask = torch.ones(batch_size, seq_len, device=device).long()
pos_indices = torch.unsqueeze(torch.arange(seq_len), 0).repeat(batch_size, 1).to(device)
word_emb = self.word_lienar(input)
pos_emb = self.position_emb(pos_indices)
emb = self.emb_proj(word_emb + pos_emb)
hidden = self.encoder(emb, word_mask)
hidden = self.dropout(hidden[:, 0, :])
# deep AR
mu, sigma = self.dist(hidden)
return mu, sigma
class ARLSTM(nn.Module):
def __init__(self, args):
super(ARLSTM,self).__init__()
self.args = args
input_size = args.input_size
hidden_size = args.hidden_size
num_layer = args.num_layer
feature_size = args.feature_size
if self.args.full_chlov:
self.linear_chlov = nn.Linear(5, input_size)
self.linear_history = nn.Linear(5, input_size)
else:
self.linear_chlov = nn.Linear(1, input_size)
self.linear_history = nn.Linear(1, input_size)
self.lstm_chlov = nn.LSTM(input_size, hidden_size, num_layer)
self.lstm_history = nn.LSTM(input_size, hidden_size, num_layer)
# if self.args.attn_pooling:
self.attn_pooling_chlov = Attentive_Pooling(hidden_size)
self.attn_pooling_history = Attentive_Pooling(hidden_size)
self.dist = GaussianNet(hidden_size * 2 ) if args.ar == 'gs' else NegBinNet(hidden_size * 2 )
def forward(self, chlov, history):
self.lstm_chlov.flatten_parameters()
self.lstm_history.flatten_parameters()
if self.args.full_chlov:
chlov = F.relu(self.linear_chlov(chlov))
history = F.relu(self.linear_history(history))
else:
chlov = F.relu(self.linear_chlov(chlov[:, :, -1:]))
history = F.relu(self.linear_history(history[:, :, -1:]))
B, L, H = chlov.size()
chlov, _ = self.lstm_chlov(chlov)
history, _ = self.lstm_history(history)
# if self.args.attn_pooling:
chlov = self.attn_pooling_chlov(chlov)
history = self.attn_pooling_history(history)
# deep ar
output = torch.cat((chlov, history), dim=1)
mu, sigma = self.dist(output)
return mu, sigma
class LayerNorm(nn.Module):
"Construct a layernorm module (See citation for details)."
def __init__(self, features, eps=1e-6):
super(LayerNorm, self).__init__()
self.a_2 = nn.Parameter(torch.ones(features))
self.b_2 = nn.Parameter(torch.zeros(features))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
return self.a_2 * (x - mean) / (std + self.eps) + self.b_2
class Encoder(nn.Module):
"Core encoder is a stack of N layers"
def __init__(self, layer, N):
super(Encoder, self).__init__()
self.layers = clones(layer, N)
self.norm = LayerNorm(layer.size)
def forward(self, x, mask):
"Pass the input (and mask) through each layer in turn."
if mask.dim() == 2:
seq_len = mask.size(1)
mask = mask.unsqueeze(1).expand(-1, seq_len, -1)
assert mask.size() == (x.size(0), seq_len, seq_len)
for layer in self.layers:
x = layer(x, mask)
return self.norm(x)
class SublayerConnection(nn.Module):
"""
A residual connection followed by a layer norm.
Note for code simplicity we apply the norm first as opposed to last.
"""
def __init__(self, size, dropout):
super(SublayerConnection, self).__init__()
self.norm = LayerNorm(size)
self.dropout = nn.Dropout(dropout)
def forward(self, x, sublayer):
"Apply residual connection to any sublayer function that maintains the same size."
return x + self.dropout(sublayer(self.norm(x)))
class EncoderLayer(nn.Module):
"Encoder is made up of two sublayers, self-attn and feed forward (defined below)"
def __init__(self, size, self_attn, feed_forward, dropout):
super(EncoderLayer, self).__init__()
self.self_attn = self_attn
self.feed_forward = feed_forward
self.sublayer = clones(SublayerConnection(size, dropout), 2)
self.size = size
def forward(self, x, mask):
"Follow Figure 1 (left) for connections."
x = self.sublayer[0](x, lambda x: self.self_attn(x, x, x, mask))
return self.sublayer[1](x, self.feed_forward)
def attention(query, key, value, mask=None, dropout=0.0):
"Compute 'Scaled Dot Product Attention'"
d_k = query.size(-1)
scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask.eq(0), -1e9)
p_attn = F.softmax(scores, dim=-1)
# (Dropout described below)
p_attn = F.dropout(p_attn, p=dropout)
return torch.matmul(p_attn, value), p_attn
class MultiHeadedAttention(nn.Module):
def __init__(self, h, d_model, dropout=0.1):
"Take in model size and number of heads."
super(MultiHeadedAttention, self).__init__()
assert d_model % h == 0
# We assume d_v always equals d_k
self.d_k = d_model // h
self.h = h
self.p = dropout
self.linears = clones(nn.Linear(d_model, d_model), 4) # clone linear for 4 times, query, key, value, output
self.attn = None
def forward(self, query, key, value, mask=None):
if mask is not None:
# Same mask applied to all h heads.
mask = mask.unsqueeze(1)
assert mask.dim() == 4 # batch, head, seq_len, seq_len
nbatches = query.size(0)
# 1) Do all the linear projections in batch from d_model => head * d_k
query, key, value = [l(x).view(nbatches, -1, self.h, self.d_k).transpose(1, 2)
for l, x in zip(self.linears, (query, key, value))]
# 2) Apply attention on all the projected vectors in batch.
x, self.attn = attention(query, key, value, mask=mask, dropout=self.p)
# 3) "Concat" using a view and apply a final linear.
x = x.transpose(1, 2).contiguous().view(nbatches, -1, self.h * self.d_k)
return self.linears[-1](x)
class PositionwiseFeedForward(nn.Module):
"Implements FFN equation."
def __init__(self, d_model, d_ff, dropout=0.1):
super(PositionwiseFeedForward, self).__init__()
# Torch linears have a `b` by default.
self.w_1 = nn.Linear(d_model, d_ff)
self.w_2 = nn.Linear(d_ff, d_model)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
return self.w_2(self.dropout(F.relu(self.w_1(x))))
class Embeddings(nn.Module):
def __init__(self, emb_size, vocab, emb=None):
super(Embeddings, self).__init__()
if emb is not None:
self.lut = emb
else:
self.lut = nn.Embedding(vocab, emb_size)
self.emb_size = emb_size
self.criterion = nn.CosineEmbeddingLoss(margin=0.5)
def forward(self, x):
return self.lut(x) * math.sqrt(self.emb_size)
class PositionalEncoding(nn.Module):
"Implement the PE function."
# TODO: use learnable position encoding
def __init__(self, d_model, dropout, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
# Compute the positional encodings once in log space.
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2) *
-(math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:, :x.size(1)]
return self.dropout(x)