{{ message }}
Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
482 lines (370 loc) · 14.4 KB
/
Copy pathmodels.py
File metadata and controls
482 lines (370 loc) · 14.4 KB
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
import torch
import torch.nn as nn
from audtorch.metrics.functional import pearsonr
# --------------------特征提取层--------------------
class ts_corr(nn.Module):
"""
计算过去 d 天 X 值构成的时序数列和 Y 值构成的时序数列的相关系数
"""
def __init__(self, d=10, stride=10):
"""
d: 计算窗口的天数
stride:计算窗口在时间维度上的进步大小
"""
super(ts_corr, self).__init__()
self.d = d
self.stride = stride
def forward(self, X):
# n-特征数量,T-时间窗口
batch_size, n, T = X.shape
# 初始化输出特征图
w = int((T - self.d) / self.stride + 1)
h = int(n * (n - 1) / 2)
Z = torch.zeros(batch_size, h, w)
# 遍历每个batch
for batch in range(batch_size):
# 主窗口:i 确定时间维度位置,j 确定特征维度位置
for i in range(w):
z = []
start = i * self.stride
end = start + self.d
for j in range(n - 1):
# 主窗口
x = X[batch, j, start:end]
# 剩余窗口
y = X[batch, j + 1:, start:end]
# 计算两个窗口之间的相关系数
broadcasted_x = x.expand(len(y), -1)
r = pearsonr(broadcasted_x, y)
z.append(r)
# 更新特征图
Z[batch, :, i] = torch.cat(z, dim=0).T
return Z
class ts_cov(nn.Module):
"""
计算过去 d 天 X 值构成的时序数列和 Y 值构成的时序数列的协方差
"""
def __init__(self, d=10, stride=10):
super(ts_cov, self).__init__()
self.d = d
self.stride = stride
def forward(self, X):
batch_size, n, T = X.shape
w = int((T - self.d) / self.stride + 1)
h = int(n * (n - 1) / 2)
Z = torch.zeros(batch_size, h, w)
for batch in range(batch_size):
for i in range(w):
z = []
start = i * self.stride
end = start + self.d
for j in range(n - 1):
x = X[batch, j, start:end]
y = X[batch, j + 1:, start:end]
x = x.expand(len(y), -1)
x_bar = x.mean(dim=1).unsqueeze(dim=1).expand(y.shape[0], y.shape[1])
y_bar = y.mean(dim=1).unsqueeze(dim=1).expand(y.shape[0], y.shape[1])
cov = torch.sum((x - x_bar) * (y - y_bar), dim=1) / y.shape[1]
z.append(cov)
Z[batch, :, i] = torch.cat(z, dim=0).T
return Z
class ts_return(nn.Module):
"""
过去 d 天 X 值构成的时序数列的return
"""
def __init__(self, d=10, stride=10):
"""
d: 计算窗口的天数
stride:计算窗口在时间维度上的进步大小
"""
super(ts_return, self).__init__()
self.d = d
self.stride = stride
def forward(self, X):
# n-特征数量,T-时间窗口
batch_size, n, T = X.shape
# 初始化输出特征图
w = int((T - self.d) / self.stride + 1)
Z = torch.zeros(batch_size, n, w)
# 遍历每个batch
for batch in range(batch_size):
# 窗口:i 确定时间维度位置
for i in range(w):
start = i * self.stride
end = start + self.d
x = X[batch, :, start:end]
# 计算窗口的return
return_d = (x[:, -1] - x[:, 0]) / x[:, 0] - 1
# 更新特征图
Z[batch, :, i] = return_d
return Z
class ts_stddev(nn.Module):
"""
过去 d 天 X 值构成的时序数列的标准差
"""
def __init__(self, d=10, stride=10):
super(ts_stddev, self).__init__()
self.d = d
self.stride = stride
def forward(self, X):
batch_size, n, T = X.shape
w = int((T - self.d) / self.stride + 1)
Z = torch.zeros(batch_size, n, w)
for batch in range(batch_size):
for i in range(w):
start = i * self.stride
end = start + self.d
x = X[batch, :, start:end]
std = torch.std(x, dim=1)
Z[batch, :, i] = std
return Z
class ts_zscore(nn.Module):
"""
过去 d 天 X 值构成的时序数列的z-score
"""
def __init__(self, d=10, stride=10):
super(ts_zscore, self).__init__()
self.d = d
self.stride = stride
def forward(self, X):
batch_size, n, T = X.shape
w = int((T - self.d) / self.stride + 1)
Z = torch.zeros(batch_size, n, w)
for batch in range(batch_size):
for i in range(w):
start = i * self.stride
end = start + self.d
x = X[batch, :, start:end]
z_score = torch.mean(x, dim=1) / torch.std(x, dim=1)
Z[batch, :, i] = z_score
return Z
class ts_decaylinear(nn.Module):
"""
过去 d 天 X 值构成的时序数列的加权平均值
"""
def __init__(self, d=10, stride=10):
super(ts_decaylinear, self).__init__()
self.d = d
self.stride = stride
def forward(self, X):
batch_size, n, T = X.shape
w = int((T - self.d) / self.stride + 1)
Z = torch.zeros(batch_size, n, w)
# 权重
weights = torch.arange(self.d) + 1
normalized_w = weights / torch.sum(weights)
for batch in range(batch_size):
for i in range(w):
start = i * self.stride
end = start + self.d
x = X[batch, :, start:end]
weighted_avg = torch.mm(normalized_w.unsqueeze(dim=0), x.T)
Z[batch, :, i] = weighted_avg
return Z
# --------------------AlphaNet-v1--------------------
class AlphaNet(nn.Module):
'''
第一版AlphaNet:输入 + 特征提取层 + 池化层 + 特征展平/残差连接 + 降维度层 + 输出层
'''
def __init__(self, d=10, stride=10, d_pool=3, s_pool=3, n=9):
super(AlphaNet, self).__init__()
# d-回看窗口大小,stride-时间步大小
self.d = d
self.stride = stride
# ts_corr() 和 ts_cov() 输出的特征数量
h = int(n * (n - 1) / 2)
# 特征提取层
self.feature_extractors = nn.ModuleList([
ts_corr(self.d, self.stride),
ts_cov(self.d, self.stride),
ts_stddev(self.d, self.stride),
ts_zscore(self.d, self.stride),
ts_return(self.d, self.stride),
ts_decaylinear(self.d, self.stride),
nn.AvgPool1d(self.d, self.stride)
])
# 特征提取层后面接的批归一化层
self.batch_norms1 = nn.ModuleList([
nn.BatchNorm1d(h),
nn.BatchNorm1d(h),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n)
])
# 池化层(无参数)
self.avg_pool = nn.AvgPool1d(d_pool, s_pool)
self.max_pool = nn.MaxPool1d(d_pool, s_pool)
# 池化层后面接的批量归一化层
self.batch_norms2 = nn.ModuleList([])
for _ in range(2):
for _ in range(3):
self.batch_norms2.append(nn.BatchNorm1d(h))
for _ in range(5):
for _ in range(3):
self.batch_norms2.append(nn.BatchNorm1d(n))
# 特征展平并拼接后的总数
n_in = 2 * (h*2*3 + n*5*3)
# 线性层,输出层,激活函数,失活函数
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=0.5)
self.linear_layer = nn.Linear(n_in, 30)
self.output_layer = nn.Linear(30, 1)
# 初始化线性层和输出层
self.initialize_weights()
def initialize_weights(self):
# 用 truncated_normal 方法初始化线性层和输出层的权重
nn.init.trunc_normal_(self.linear_layer.weight)
nn.init.trunc_normal_(self.output_layer.weight)
def forward(self, X):
features_fe, features_p, i = [], [], 0
for extractor, batch_norm in zip(self.feature_extractors, self.batch_norms1):
# 特征提取 + 批量归一化 + 展平
x = extractor(X)
x = batch_norm(x)
features_fe.append(x.flatten(start_dim=1))
# 池化层 + 批量归一化 + 展平
x_avg = self.batch_norms2[i](self.avg_pool(x))
x_max = self.batch_norms2[i+1](self.max_pool(x))
x_min = self.batch_norms2[i+2](-self.max_pool(-x))
features_p.append(x_avg.flatten(start_dim=1))
features_p.append(x_max.flatten(start_dim=1))
features_p.append(x_min.flatten(start_dim=1))
i += 3
# 残差连接
f1 = torch.cat(features_fe, dim=1)
f2 = torch.cat(features_p, dim=1)
features = torch.cat([f1, f2], dim=1)
# 线性层 + 激活 + 失活 + 输出层
features = self.linear_layer(features)
features = self.relu(features)
features = self.dropout(features)
output = self.output_layer(features)
return output
# --------------------AlphaNet-v2--------------------
class AlphaNet_v2(nn.Module):
'''
研报中的改进版,相比AlphaNet-v1:
1. 扩充了6比率类特征
2. 用LSTM替换池化层和全连接层
'''
def __init__(self, d=10, stride=10, n=15):
super(AlphaNet_v2, self).__init__()
# d-回看窗口大小,stride-时间步大小
self.d = d
self.stride = stride
# ts_corr() 和 ts_cov() 输出的特征数量
h = int(n * (n - 1) / 2)
# 特征提取层
self.feature_extractors = nn.ModuleList([
ts_corr(self.d, self.stride),
ts_cov(self.d, self.stride),
ts_stddev(self.d, self.stride),
ts_zscore(self.d, self.stride),
ts_return(self.d, self.stride),
ts_decaylinear(self.d, self.stride)
])
# 特征提取层后面接着的批量归一化
self.batch_norms = nn.ModuleList([
nn.BatchNorm1d(h),
nn.BatchNorm1d(h),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n)
])
# 特征总数
n_in = 2 * h + 4 * n
# LSTM层,批量归一化,输出层
self.lstm = nn.LSTM(n_in, 30, 1, batch_first=True)
self.bn = nn.BatchNorm1d(30)
self.output_layer = nn.Linear(30, 1)
# 初始化输出层的权重
self.initialize_weights()
def initialize_weights(self):
# 用 truncated_normal 方法初始化输出层的权重
nn.init.trunc_normal_(self.output_layer.weight)
def forward(self, X):
# 特征提取 + 批量归一化
features = []
for extractor, batch_norm in zip(self.feature_extractors, self.batch_norms):
x = extractor(X)
x = batch_norm(x)
features.append(x)
features = torch.cat(features, dim=1)
# 将输入转换为: (batch_size, sequence_length, feature_size)
features = features.transpose(1, 2)
# LSTM + 批量归一化
features, _ = self.lstm(features)
features = features.transpose(1,2)
features = self.bn(features)
features = features.transpose(1,2)
# 取LSTM最后一个时间步的隐藏状态作为最后的特征
features = features[:, -1, :]
# 输出层
output = self.output_layer(features)
return output
# --------------------AlphaNet-Attention--------------------
class AlphaNet_att(nn.Module):
'''
在AlphaNet-v2的基础上,用Multi-head Self-Attention层替换掉LSTM层
'''
def __init__(self, d=10, stride=10, n=15):
super(AlphaNet_att, self).__init__()
# d-回看窗口大小,stride-时间步大小
self.d = d
self.stride = stride
# ts_corr() 和 ts_cov() 输出的特征数量
h = int(n * (n - 1) / 2)
# 特征提取层
self.feature_extractors = nn.ModuleList([
ts_corr(self.d, self.stride),
ts_cov(self.d, self.stride),
ts_stddev(self.d, self.stride),
ts_zscore(self.d, self.stride),
ts_return(self.d, self.stride),
ts_decaylinear(self.d, self.stride)
])
# 特征提取层后面接的批量归一化层
self.batch_norms = nn.ModuleList([
nn.BatchNorm1d(h),
nn.BatchNorm1d(h),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n),
nn.BatchNorm1d(n)
])
# 特征总数
n_in = 2 * h + 4 * n
# 多头自注意力机制 + 层归一化
self.mha = nn.MultiheadAttention(embed_dim=n_in,
num_heads=3,
dropout=0.1,
batch_first=True)
self.ln = nn.LayerNorm(n_in)
# 输出层
self.output_layer = nn.Linear(n_in, 1)
# 初始化输出层的权重
self.initialize_weights()
def initialize_weights(self):
# 用 truncated_normal 方法初始化输出层的权重
nn.init.trunc_normal_(self.output_layer.weight)
def forward(self, X):
# 特征提取 + 批量归一化
features = []
for extractor, batch_norm in zip(self.feature_extractors, self.batch_norms):
x = extractor(X)
x = batch_norm(x)
features.append(x)
features = torch.cat(features, dim=1)
# 将输入转换为: (batch_size, sequence_length, feature_size)
features = features.transpose(1,2)
# 自注意力计算 + 层归一化
features, _ = self.mha(features, features, features)
features = self.ln(features)
# 取所有时间步的均值作为最后的特征
features = torch.mean(features, dim=1)
# 输出层
output = self.output_layer(features)
return output
You can’t perform that action at this time.
