-
Notifications
You must be signed in to change notification settings - Fork 36
/
master.py
231 lines (189 loc) · 8.05 KB
/
master.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
import torch
from torch import nn
from torch.nn.modules.linear import Linear
from torch.nn.modules.dropout import Dropout
from torch.nn.modules.normalization import LayerNorm
import math
from base_model import SequenceModel
class PositionalEncoding(nn.Module):
def __init__(self, d_model, max_len=100):
super(PositionalEncoding, self).__init__()
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
self.register_buffer("pe", pe)
def forward(self, x):
return x + self.pe[:x.shape[1], :]
class SAttention(nn.Module):
def __init__(self, d_model, nhead, dropout):
super().__init__()
self.d_model = d_model
self.nhead = nhead
self.temperature = math.sqrt(self.d_model/nhead)
self.qtrans = nn.Linear(d_model, d_model, bias=False)
self.ktrans = nn.Linear(d_model, d_model, bias=False)
self.vtrans = nn.Linear(d_model, d_model, bias=False)
attn_dropout_layer = []
for i in range(nhead):
attn_dropout_layer.append(Dropout(p=dropout))
self.attn_dropout = nn.ModuleList(attn_dropout_layer)
# input LayerNorm
self.norm1 = LayerNorm(d_model, eps=1e-5)
# FFN layerNorm
self.norm2 = LayerNorm(d_model, eps=1e-5)
self.ffn = nn.Sequential(
Linear(d_model, d_model),
nn.ReLU(),
Dropout(p=dropout),
Linear(d_model, d_model),
Dropout(p=dropout)
)
def forward(self, x):
x = self.norm1(x)
q = self.qtrans(x).transpose(0,1)
k = self.ktrans(x).transpose(0,1)
v = self.vtrans(x).transpose(0,1)
dim = int(self.d_model/self.nhead)
att_output = []
for i in range(self.nhead):
if i==self.nhead-1:
qh = q[:, :, i * dim:]
kh = k[:, :, i * dim:]
vh = v[:, :, i * dim:]
else:
qh = q[:, :, i * dim:(i + 1) * dim]
kh = k[:, :, i * dim:(i + 1) * dim]
vh = v[:, :, i * dim:(i + 1) * dim]
atten_ave_matrixh = torch.softmax(torch.matmul(qh, kh.transpose(1, 2)) / self.temperature, dim=-1)
if self.attn_dropout:
atten_ave_matrixh = self.attn_dropout[i](atten_ave_matrixh)
att_output.append(torch.matmul(atten_ave_matrixh, vh).transpose(0, 1))
att_output = torch.concat(att_output, dim=-1)
# FFN
xt = x + att_output
xt = self.norm2(xt)
att_output = xt + self.ffn(xt)
return att_output
class TAttention(nn.Module):
def __init__(self, d_model, nhead, dropout):
super().__init__()
self.d_model = d_model
self.nhead = nhead
self.qtrans = nn.Linear(d_model, d_model, bias=False)
self.ktrans = nn.Linear(d_model, d_model, bias=False)
self.vtrans = nn.Linear(d_model, d_model, bias=False)
self.attn_dropout = []
if dropout > 0:
for i in range(nhead):
self.attn_dropout.append(Dropout(p=dropout))
self.attn_dropout = nn.ModuleList(self.attn_dropout)
# input LayerNorm
self.norm1 = LayerNorm(d_model, eps=1e-5)
# FFN layerNorm
self.norm2 = LayerNorm(d_model, eps=1e-5)
# FFN
self.ffn = nn.Sequential(
Linear(d_model, d_model),
nn.ReLU(),
Dropout(p=dropout),
Linear(d_model, d_model),
Dropout(p=dropout)
)
def forward(self, x):
x = self.norm1(x)
q = self.qtrans(x)
k = self.ktrans(x)
v = self.vtrans(x)
dim = int(self.d_model / self.nhead)
att_output = []
for i in range(self.nhead):
if i==self.nhead-1:
qh = q[:, :, i * dim:]
kh = k[:, :, i * dim:]
vh = v[:, :, i * dim:]
else:
qh = q[:, :, i * dim:(i + 1) * dim]
kh = k[:, :, i * dim:(i + 1) * dim]
vh = v[:, :, i * dim:(i + 1) * dim]
atten_ave_matrixh = torch.softmax(torch.matmul(qh, kh.transpose(1, 2)), dim=-1)
if self.attn_dropout:
atten_ave_matrixh = self.attn_dropout[i](atten_ave_matrixh)
att_output.append(torch.matmul(atten_ave_matrixh, vh))
att_output = torch.concat(att_output, dim=-1)
# FFN
xt = x + att_output
xt = self.norm2(xt)
att_output = xt + self.ffn(xt)
return att_output
class Gate(nn.Module):
def __init__(self, d_input, d_output, beta=1.0):
super().__init__()
self.trans = nn.Linear(d_input, d_output)
self.d_output =d_output
self.t = beta
def forward(self, gate_input):
output = self.trans(gate_input)
output = torch.softmax(output/self.t, dim=-1)
return self.d_output*output
class TemporalAttention(nn.Module):
def __init__(self, d_model):
super().__init__()
self.trans = nn.Linear(d_model, d_model, bias=False)
def forward(self, z):
h = self.trans(z) # [N, T, D]
query = h[:, -1, :].unsqueeze(-1)
lam = torch.matmul(h, query).squeeze(-1) # [N, T, D] --> [N, T]
lam = torch.softmax(lam, dim=1).unsqueeze(1)
output = torch.matmul(lam, z).squeeze(1) # [N, 1, T], [N, T, D] --> [N, 1, D]
return output
class MASTER(nn.Module):
def __init__(self, d_feat=158, d_model=256, t_nhead=4, s_nhead=2, T_dropout_rate=0.5, S_dropout_rate=0.5,
gate_input_start_index=158, gate_input_end_index=221, beta=None):
super(MASTER, self).__init__()
# market
self.gate_input_start_index = gate_input_start_index
self.gate_input_end_index = gate_input_end_index
self.d_gate_input = (gate_input_end_index - gate_input_start_index) # F'
self.feature_gate = Gate(self.d_gate_input, d_feat, beta=beta)
self.layers = nn.Sequential(
# feature layer
nn.Linear(d_feat, d_model),
PositionalEncoding(d_model),
# intra-stock aggregation
TAttention(d_model=d_model, nhead=t_nhead, dropout=T_dropout_rate),
# inter-stock aggregation
SAttention(d_model=d_model, nhead=s_nhead, dropout=S_dropout_rate),
TemporalAttention(d_model=d_model),
# decoder
nn.Linear(d_model, 1)
)
def forward(self, x):
src = x[:, :, :self.gate_input_start_index] # N, T, D
gate_input = x[:, -1, self.gate_input_start_index:self.gate_input_end_index]
src = src * torch.unsqueeze(self.feature_gate(gate_input), dim=1)
output = self.layers(src).squeeze(-1)
return output
class MASTERModel(SequenceModel):
def __init__(
self, d_feat: int = 20, d_model: int = 64, t_nhead: int = 4, s_nhead: int = 2, gate_input_start_index=None, gate_input_end_index=None,
T_dropout_rate=0.5, S_dropout_rate=0.5, beta=5.0, **kwargs,
):
super(MASTERModel, self).__init__(**kwargs)
self.d_model = d_model
self.d_feat = d_feat
self.gate_input_start_index = gate_input_start_index
self.gate_input_end_index = gate_input_end_index
self.T_dropout_rate = T_dropout_rate
self.S_dropout_rate = S_dropout_rate
self.t_nhead = t_nhead
self.s_nhead = s_nhead
self.beta = beta
self.init_model()
def init_model(self):
self.model = MASTER(d_feat=self.d_feat, d_model=self.d_model, t_nhead=self.t_nhead, s_nhead=self.s_nhead,
T_dropout_rate=self.T_dropout_rate, S_dropout_rate=self.S_dropout_rate,
gate_input_start_index=self.gate_input_start_index,
gate_input_end_index=self.gate_input_end_index, beta=self.beta)
super(MASTERModel, self).init_model()