-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathnet.py
245 lines (210 loc) · 10.4 KB
/
net.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
'''Defines the neural network, loss function and metrics'''
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import logging
logger = logging.getLogger('DeepAR.Net')
class Net(nn.Module):
def __init__(self, params):
'''
We define a recurrent network that predicts the future values of a time-dependent variable based on
past inputs and covariates.
'''
super(Net, self).__init__()
self.params = params
self.embedding = nn.Embedding(params.num_class, params.embedding_dim)
self.lstm = nn.LSTM(input_size=1+params.cov_dim+params.embedding_dim,
hidden_size=params.lstm_hidden_dim,
num_layers=params.lstm_layers,
bias=True,
batch_first=False,
dropout=params.lstm_dropout)
'''self.lstm = nn.LSTM(input_size=1 + params.cov_dim,
hidden_size=params.lstm_hidden_dim,
num_layers=params.lstm_layers,
bias=True,
batch_first=False,
dropout=params.lstm_dropout)'''
# initialize LSTM forget gate bias to be 1 as recommanded by http://proceedings.mlr.press/v37/jozefowicz15.pdf
for names in self.lstm._all_weights:
for name in filter(lambda n: "bias" in n, names):
bias = getattr(self.lstm, name)
n = bias.size(0)
start, end = n // 4, n // 2
bias.data[start:end].fill_(1.)
self.relu = nn.ReLU()
self.distribution_mu = nn.Linear(params.lstm_hidden_dim * params.lstm_layers, 1)
self.distribution_presigma = nn.Linear(params.lstm_hidden_dim * params.lstm_layers, 1)
self.distribution_sigma = nn.Softplus()
def forward(self, x, idx, hidden, cell):
'''
Predict mu and sigma of the distribution for z_t.
Args:
x: ([1, batch_size, 1+cov_dim]): z_{t-1} + x_t, note that z_0 = 0
idx ([1, batch_size]): one integer denoting the time series id
hidden ([lstm_layers, batch_size, lstm_hidden_dim]): LSTM h from time step t-1
cell ([lstm_layers, batch_size, lstm_hidden_dim]): LSTM c from time step t-1
Returns:
mu ([batch_size]): estimated mean of z_t
sigma ([batch_size]): estimated standard deviation of z_t
hidden ([lstm_layers, batch_size, lstm_hidden_dim]): LSTM h from time step t
cell ([lstm_layers, batch_size, lstm_hidden_dim]): LSTM c from time step t
'''
onehot_embed = self.embedding(idx) #TODO: is it possible to do this only once per window instead of per step?
lstm_input = torch.cat((x, onehot_embed), dim=2)
output, (hidden, cell) = self.lstm(lstm_input, (hidden, cell))
# use h from all three layers to calculate mu and sigma
hidden_permute = hidden.permute(1, 2, 0).contiguous().view(hidden.shape[1], -1)
pre_sigma = self.distribution_presigma(hidden_permute)
mu = self.distribution_mu(hidden_permute)
sigma = self.distribution_sigma(pre_sigma) # softplus to make sure standard deviation is positive
return torch.squeeze(mu), torch.squeeze(sigma), hidden, cell
def init_hidden(self, input_size):
return torch.zeros(self.params.lstm_layers, input_size, self.params.lstm_hidden_dim, device=self.params.device)
def init_cell(self, input_size):
return torch.zeros(self.params.lstm_layers, input_size, self.params.lstm_hidden_dim, device=self.params.device)
def test(self, x, v_batch, id_batch, hidden, cell, sampling=False):
batch_size = x.shape[1]
if sampling:
samples = torch.zeros(self.params.sample_times, batch_size, self.params.predict_steps,
device=self.params.device)
for j in range(self.params.sample_times):
decoder_hidden = hidden
decoder_cell = cell
for t in range(self.params.predict_steps):
mu_de, sigma_de, decoder_hidden, decoder_cell = self(x[self.params.predict_start + t].unsqueeze(0),
id_batch, decoder_hidden, decoder_cell)
gaussian = torch.distributions.normal.Normal(mu_de, sigma_de)
pred = gaussian.sample() # not scaled
samples[j, :, t] = pred * v_batch[:, 0] + v_batch[:, 1]
if t < (self.params.predict_steps - 1):
x[self.params.predict_start + t + 1, :, 0] = pred
sample_mu = torch.median(samples, dim=0)[0]
sample_sigma = samples.std(dim=0)
return samples, sample_mu, sample_sigma
else:
decoder_hidden = hidden
decoder_cell = cell
sample_mu = torch.zeros(batch_size, self.params.predict_steps, device=self.params.device)
sample_sigma = torch.zeros(batch_size, self.params.predict_steps, device=self.params.device)
for t in range(self.params.predict_steps):
mu_de, sigma_de, decoder_hidden, decoder_cell = self(x[self.params.predict_start + t].unsqueeze(0),
id_batch, decoder_hidden, decoder_cell)
sample_mu[:, t] = mu_de * v_batch[:, 0] + v_batch[:, 1]
sample_sigma[:, t] = sigma_de * v_batch[:, 0]
if t < (self.params.predict_steps - 1):
x[self.params.predict_start + t + 1, :, 0] = mu_de
return sample_mu, sample_sigma
def loss_fn(mu: Variable, sigma: Variable, labels: Variable):
'''
Compute using gaussian the log-likehood which needs to be maximized. Ignore time steps where labels are missing.
Args:
mu: (Variable) dimension [batch_size] - estimated mean at time step t
sigma: (Variable) dimension [batch_size] - estimated standard deviation at time step t
labels: (Variable) dimension [batch_size] z_t
Returns:
loss: (Variable) average log-likelihood loss across the batch
'''
zero_index = (labels != 0)
distribution = torch.distributions.normal.Normal(mu[zero_index], sigma[zero_index])
likelihood = distribution.log_prob(labels[zero_index])
return -torch.mean(likelihood)
# if relative is set to True, metrics are not normalized by the scale of labels
def accuracy_ND(mu: torch.Tensor, labels: torch.Tensor, relative = False):
zero_index = (labels != 0)
if relative:
diff = torch.mean(torch.abs(mu[zero_index] - labels[zero_index])).item()
return [diff, 1]
else:
diff = torch.sum(torch.abs(mu[zero_index] - labels[zero_index])).item()
summation = torch.sum(torch.abs(labels[zero_index])).item()
return [diff, summation]
def accuracy_RMSE(mu: torch.Tensor, labels: torch.Tensor, relative = False):
zero_index = (labels != 0)
diff = torch.sum(torch.mul((mu[zero_index] - labels[zero_index]), (mu[zero_index] - labels[zero_index]))).item()
if relative:
return [diff, torch.sum(zero_index).item(), torch.sum(zero_index).item()]
else:
summation = torch.sum(torch.abs(labels[zero_index])).item()
if summation == 0:
logger.error('summation denominator error! ')
return [diff, summation, torch.sum(zero_index).item()]
def accuracy_ROU(rou: float, samples: torch.Tensor, labels: torch.Tensor, relative = False):
numerator = 0
denominator = 0
pred_samples = samples.shape[0]
for t in range(labels.shape[1]):
zero_index = (labels[:, t] != 0)
if zero_index.numel() > 0:
rou_th = math.ceil(pred_samples * (1 - rou))
rou_pred = torch.topk(samples[:, zero_index, t], dim=0, k=rou_th)[0][-1, :]
abs_diff = labels[:, t][zero_index] - rou_pred
numerator += 2 * (torch.sum(rou * abs_diff[labels[:, t][zero_index] > rou_pred]) - torch.sum(
(1 - rou) * abs_diff[labels[:, t][zero_index] <= rou_pred])).item()
denominator += torch.sum(labels[:, t][zero_index]).item()
if relative:
return [numerator, torch.sum(labels != 0).item()]
else:
return [numerator, denominator]
def accuracy_ND_(mu: torch.Tensor, labels: torch.Tensor, relative = False):
mu = mu.cpu().detach().numpy()
labels = labels.cpu().detach().numpy()
mu[labels == 0] = 0.
diff = np.sum(np.abs(mu - labels), axis=1)
if relative:
summation = np.sum((labels != 0), axis=1)
mask = (summation == 0)
summation[mask] = 1
result = diff / summation
result[mask] = -1
return result
else:
summation = np.sum(np.abs(labels), axis=1)
mask = (summation == 0)
summation[mask] = 1
result = diff / summation
result[mask] = -1
return result
def accuracy_RMSE_(mu: torch.Tensor, labels: torch.Tensor, relative = False):
mu = mu.cpu().detach().numpy()
labels = labels.cpu().detach().numpy()
mask = labels == 0
mu[mask] = 0.
diff = np.sum((mu - labels) ** 2, axis=1)
summation = np.sum(np.abs(labels), axis=1)
mask2 = (summation == 0)
if relative:
div = np.sum(~mask, axis=1)
div[mask2] = 1
result = np.sqrt(diff / div)
result[mask2] = -1
return result
else:
summation[mask2] = 1
result = (np.sqrt(diff) / summation) * np.sqrt(np.sum(~mask, axis=1))
result[mask2] = -1
return result
def accuracy_ROU_(rou: float, samples: torch.Tensor, labels: torch.Tensor, relative = False):
samples = samples.cpu().detach().numpy()
labels = labels.cpu().detach().numpy()
mask = labels == 0
samples[:, mask] = 0.
pred_samples = samples.shape[0]
rou_th = math.floor(pred_samples * rou)
samples = np.sort(samples, axis=0)
rou_pred = samples[rou_th]
abs_diff = np.abs(labels - rou_pred)
abs_diff_1 = abs_diff.copy()
abs_diff_1[labels < rou_pred] = 0.
abs_diff_2 = abs_diff.copy()
abs_diff_2[labels >= rou_pred] = 0.
numerator = 2 * (rou * np.sum(abs_diff_1, axis=1) + (1 - rou) * np.sum(abs_diff_2, axis=1))
denominator = np.sum(labels, axis=1)
mask2 = (denominator == 0)
denominator[mask2] = 1
result = numerator / denominator
result[mask2] = -1
return result