/
_network.py
277 lines (244 loc) · 9.43 KB
/
_network.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
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
# Standard library imports
from typing import List, Optional
# Third-party imports
import mxnet as mx
# First-party imports
from gluonts.block.feature import FeatureEmbedder
from gluonts.block.scaler import NOPScaler, MeanScaler
from gluonts.core.component import validated
from gluonts.distribution.lds import ParameterBounds, LDS, LDSArgsProj
from gluonts.model.deepstate.issm import ISSM
from gluonts.model.common import Tensor
from gluonts.support.util import weighted_average, make_nd_diag
class DeepStateNetwork(mx.gluon.HybridBlock):
@validated()
def __init__(
self,
num_layers: int,
num_cells: int,
cell_type: str,
past_length: int,
prediction_length: int,
issm: ISSM,
dropout_rate: float,
cardinality: List[int],
embedding_dimension: List[int],
scaling: bool = True,
noise_std_bounds: ParameterBounds = ParameterBounds(1e-6, 1.0),
prior_cov_bounds: ParameterBounds = ParameterBounds(1e-6, 1.0),
innovation_bounds: ParameterBounds = ParameterBounds(1e-6, 0.01),
**kwargs,
) -> None:
super().__init__(**kwargs)
self.num_layers = num_layers
self.num_cells = num_cells
self.cell_type = cell_type
self.past_length = past_length
self.prediction_length = prediction_length
self.issm = issm
self.dropout_rate = dropout_rate
self.cardinality = cardinality
self.embedding_dimension = embedding_dimension
self.num_cat = len(cardinality)
self.scaling = scaling
assert len(cardinality) == len(
embedding_dimension
), "embedding_dimension should be a list with the same size as cardinality"
self.univariate = self.issm.output_dim() == 1
self.noise_std_bounds = noise_std_bounds
self.prior_cov_bounds = prior_cov_bounds
self.innovation_bounds = innovation_bounds
with self.name_scope():
self.prior_mean_model = mx.gluon.nn.Dense(
units=self.issm.latent_dim(), flatten=False
)
self.prior_cov_diag_model = mx.gluon.nn.Dense(
units=self.issm.latent_dim(),
activation="sigmoid",
flatten=False,
)
self.lstm = mx.gluon.rnn.HybridSequentialRNNCell()
self.lds_proj = LDSArgsProj(
output_dim=self.issm.output_dim(),
noise_std_bounds=self.noise_std_bounds,
innovation_bounds=self.innovation_bounds,
)
for k in range(num_layers):
cell = mx.gluon.rnn.LSTMCell(hidden_size=num_cells)
cell = mx.gluon.rnn.ResidualCell(cell) if k > 0 else cell
cell = (
mx.gluon.rnn.ZoneoutCell(cell, zoneout_states=dropout_rate)
if dropout_rate > 0.0
else cell
)
self.lstm.add(cell)
self.embedder = FeatureEmbedder(
cardinalities=cardinality, embedding_dims=embedding_dimension
)
if scaling:
self.scaler = MeanScaler(keepdims=False)
else:
self.scaler = NOPScaler(keepdims=False)
def compute_lds(
self,
F,
feat_static_cat: Tensor,
seasonal_indicators: Tensor,
time_feat: Tensor,
length: int,
prior_mean: Optional[Tensor] = None,
prior_cov: Optional[Tensor] = None,
lstm_begin_state: Optional[List[Tensor]] = None,
):
# embed categorical features and expand along time axis
embedded_cat = self.embedder(feat_static_cat)
repeated_static_features = embedded_cat.expand_dims(axis=1).repeat(
axis=1, repeats=length
)
# construct big features tensor (context)
features = F.concat(time_feat, repeated_static_features, dim=2)
output, lstm_final_state = self.lstm.unroll(
inputs=features,
begin_state=lstm_begin_state,
length=length,
merge_outputs=True,
)
if prior_mean is None:
prior_input = F.slice_axis(output, axis=1, begin=0, end=1).squeeze(
axis=1
)
prior_mean = self.prior_mean_model(prior_input)
prior_cov_diag = (
self.prior_cov_diag_model(prior_input)
* (self.prior_cov_bounds.upper - self.prior_cov_bounds.lower)
+ self.prior_cov_bounds.lower
)
prior_cov = make_nd_diag(F, prior_cov_diag, self.issm.latent_dim())
(
emission_coeff,
transition_coeff,
innovation_coeff,
) = self.issm.get_issm_coeff(seasonal_indicators)
noise_std, innovation, residuals = self.lds_proj(output)
lds = LDS(
emission_coeff=emission_coeff,
transition_coeff=transition_coeff,
innovation_coeff=F.broadcast_mul(innovation, innovation_coeff),
noise_std=noise_std,
residuals=residuals,
prior_mean=prior_mean,
prior_cov=prior_cov,
latent_dim=self.issm.latent_dim(),
output_dim=self.issm.output_dim(),
seq_length=length,
)
return lds, lstm_final_state
class DeepStateTrainingNetwork(DeepStateNetwork):
# noinspection PyMethodOverriding,PyPep8Naming
def hybrid_forward(
self,
F,
feat_static_cat: Tensor,
past_observed_values: Tensor,
past_seasonal_indicators: Tensor,
past_time_feat: Tensor,
past_target: Tensor,
) -> Tensor:
lds, _ = self.compute_lds(
F,
feat_static_cat=feat_static_cat,
seasonal_indicators=past_seasonal_indicators.slice_axis(
axis=1, begin=-self.past_length, end=None
),
time_feat=past_time_feat.slice_axis(
axis=1, begin=-self.past_length, end=None
),
length=self.past_length,
)
_, scale = self.scaler(past_target, past_observed_values)
observed_context = past_observed_values.slice_axis(
axis=1, begin=-self.past_length, end=None
)
ll, _, _ = lds.log_prob(
x=past_target.slice_axis(
axis=1, begin=-self.past_length, end=None
),
observed=observed_context.min(axis=-1, keepdims=False),
scale=scale,
)
return weighted_average(
F=F, x=-ll, axis=1, weights=observed_context.squeeze(axis=-1)
)
class DeepStatePredictionNetwork(DeepStateNetwork):
@validated()
def __init__(self, num_parallel_samples: int, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.num_parallel_samples = num_parallel_samples
# noinspection PyMethodOverriding,PyPep8Naming
def hybrid_forward(
self,
F,
feat_static_cat: Tensor,
past_observed_values: Tensor,
past_seasonal_indicators: Tensor,
past_time_feat: Tensor,
past_target: Tensor,
future_seasonal_indicators: Tensor,
future_time_feat: Tensor,
) -> Tensor:
lds, lstm_state = self.compute_lds(
F,
feat_static_cat=feat_static_cat,
seasonal_indicators=past_seasonal_indicators.slice_axis(
axis=1, begin=-self.past_length, end=None
),
time_feat=past_time_feat.slice_axis(
axis=1, begin=-self.past_length, end=None
),
length=self.past_length,
)
_, scale = self.scaler(past_target, past_observed_values)
observed_context = past_observed_values.slice_axis(
axis=1, begin=-self.past_length, end=None
)
_, final_mean, final_cov = lds.log_prob(
x=past_target.slice_axis(
axis=1, begin=-self.past_length, end=None
),
observed=observed_context.min(axis=-1, keepdims=False),
scale=scale,
)
lds_prediction, _ = self.compute_lds(
F,
feat_static_cat=feat_static_cat,
seasonal_indicators=future_seasonal_indicators,
time_feat=future_time_feat,
length=self.prediction_length,
lstm_begin_state=lstm_state,
prior_mean=final_mean,
prior_cov=final_cov,
)
samples = lds_prediction.sample(
num_samples=self.num_parallel_samples, scale=scale
)
# convert samples from
# (num_samples, batch_size, prediction_length, target_dim)
# to
# (batch_size, num_samples, prediction_length, target_dim)
# and squeeze last axis in the univariate case
if self.univariate:
return samples.transpose(axes=(1, 0, 2, 3)).squeeze(axis=3)
else:
return samples.transpose(axes=(1, 0, 2, 3))