forked from HaojiHu/HUMI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeepAD.py
More file actions
161 lines (117 loc) · 4.91 KB
/
Copy pathDeepAD.py
File metadata and controls
161 lines (117 loc) · 4.91 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
import math
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu")
class DeepARDataset(Dataset):
def __init__(self, y, cat_id, dynamic_feat, context_len=30, pred_len=1):
"""
y: shape [T]
cat_id: int
dynamic_feat: shape [num_feat, T]
"""
self.y = y
self.cat_id = cat_id
self.dynamic_feat = dynamic_feat
self.context_len = context_len
self.pred_len = pred_len
self.T = len(y)
def __len__(self):
return self.T - self.context_len - self.pred_len
def __getitem__(self, idx):
past_y = torch.tensor(self.y[idx:idx+self.context_len], dtype=torch.float32) # [T]
past_cov = torch.tensor(self.dynamic_feat[:, idx:idx+self.context_len], dtype=torch.float32) # [F,T]
target = torch.tensor(self.y[idx+self.context_len:idx+self.context_len+self.pred_len], dtype=torch.float32)
return past_y, past_cov, torch.tensor(self.cat_id), target
class DeepAR(nn.Module):
def __init__(self, context_len, future_steps, num_dyn_feat, cat_cardinality, cat_emb_dim=4,
hidden_size=64, num_layers=1):
super().__init__()
self.cat_emb = nn.Embedding(cat_cardinality, cat_emb_dim)
input_size = 1 + num_dyn_feat + cat_emb_dim
self.context_len = context_len
self.future_steps = future_steps
self.lstm = nn.LSTM(
input_size=input_size,
hidden_size=hidden_size,
num_layers=num_layers,
batch_first=True
)
self.proj = nn.Linear(hidden_size, 1)
def forward(self, past_y, past_cov, cat_id):
"""
past_y: [B,T]
past_cov: [B,features,T]
cat_id: [B]
"""
B, T = past_y.shape
F = past_cov.shape[1]
cat_e = self.cat_emb(cat_id) # [B,emb]
cat_e = cat_e.unsqueeze(1).repeat(1,T,1)
y_in = past_y.unsqueeze(-1) # [B,T,1]
cov = past_cov.permute(0,2,1) # [B,T,F]
x = torch.cat([y_in, cov, cat_e], dim=-1) # [B,T,1+F+emb]
out, _ = self.lstm(x)
pred = self.proj(out[:, -1, :]) # 取最后一步
return pred.squeeze(-1)
def model_train(model, y, cat_id, dynamic_feat, context_len, epochs, lr):
iteration = 0
while True:
hasNan = False
iteration += 1
dynamic_feat_full = np.stack([dynamic_feat]) # [1, T+future_steps]
ds = DeepARDataset(y, cat_id, dynamic_feat_full, context_len=context_len, pred_len=1)
dl = DataLoader(ds, batch_size=256, shuffle=True)
model.train()
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
loss_fn = nn.MSELoss()
for epoch in range(epochs):
total = 0
for past_y, past_cov, cid, target in dl:
past_y = past_y.to(device)
past_cov = past_cov.to(device)
cid = cid.to(device)
target = target.to(device)
pred = model(past_y, past_cov, cid)
loss = loss_fn(pred, target.squeeze(-1))
optimizer.zero_grad()
loss.backward()
optimizer.step()
total += loss.item()
if math.isnan(total):
hasNan = True
break
print("Training encounter Nan at epoch ", epoch, " in iteration ", iteration)
if hasNan == False:
break
# print(f"epoch {epoch + 1}: loss={total / len(dl):.4f}")
def forecast(model, y, cat_id, dynamic_feat_full, context_len, future_steps):
"""
dynamic_feat_full: shape [F, T + future_steps]
"""
# device = "cuda" if torch.cuda.is_available() else "cpu"
model.eval()
preds = []
dynamic_feat_full = np.stack([dynamic_feat_full])
y_list = list(y) # 会不断 append 预测值
T_hist = len(y) # 原始历史长度
for step in range(future_steps):
t = T_hist + step # 当前要预测的时间索引(未来)
start = t - context_len
end = t
# y 部分:完全来自 y_list(历史 + 之前预测)
past_y = torch.tensor(
y_list[start:end], dtype=torch.float32
).unsqueeze(0).to(device)
# cov 部分:从 dynamic_feat_full 对齐切片
past_cov_np = dynamic_feat_full[:, start:end] # 保证这里也是 context_len
past_cov = torch.tensor(
past_cov_np, dtype=torch.float32
).unsqueeze(0).to(device)
cid = torch.tensor([cat_id]).to(device)
with torch.no_grad():
pred = model(past_y, past_cov, cid).item()
preds.append(pred)
y_list.append(pred)
return preds