-
Notifications
You must be signed in to change notification settings - Fork 5
/
wavenet.py
399 lines (334 loc) · 13.4 KB
/
wavenet.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
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
import torch
import torch.nn as nn
class Conv(torch.nn.Module):
"""
A convolution with the option to be causal and use xavier initialization
"""
def __init__(
self,
in_channels,
out_channels,
kernel_size=1,
stride=1,
dilation=1,
bias=True,
w_init_gain="linear",
is_causal=False,
device="cpu",
):
super(Conv, self).__init__()
self.is_causal = is_causal
self.kernel_size = kernel_size
self.dilation = dilation
self.conv = nn.Conv1d(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=stride,
dilation=dilation,
bias=bias,
).to(device)
nn.init.xavier_uniform_(
self.conv.weight, gain=torch.nn.init.calculate_gain(w_init_gain)
)
def forward(self, signal):
if self.is_causal:
padding = (int((self.kernel_size - 1) * (self.dilation)), 0)
signal = nn.functional.pad(signal, padding)
return self.conv(signal)
class DilationConvLayer(nn.Module):
# |----------------------------------------| *residual*
# | |
# | |-- conv -- tanh --| |
# -> dilate -|----| * ----|-- 1x1 -- + --> *input*
# |-- conv -- sigm --| |
# 1x1
# |
# ---------------------------------------> + -------------> *skip*
def __init__(
self, n_residual_channels, n_skip_channels, nr_layers, stack_time, device
):
super().__init__()
self.nr_layers = nr_layers
self.dilation_rates = [2 ** i for i in range(nr_layers)] * stack_time
self.n_residual_channels = n_residual_channels
self.n_skip_channels = n_skip_channels
dilation_conv_l = []
skip_layer_l = []
res_layer_l = []
for dilation in self.dilation_rates:
dilation_conv_l.append(
Conv(
n_residual_channels,
2 * n_residual_channels,
kernel_size=2,
dilation=dilation,
w_init_gain="tanh",
is_causal=True,
device=device,
)
)
# skip layer has kernal size 1, output dim is same as input
skip_layer_l.append(
Conv(
n_residual_channels,
n_skip_channels,
w_init_gain="relu",
device=device,
)
)
# ------------------
# for res_layer, last one is not necessary, just save some time
# if i < nr_layers - 1:
# ----------------
# residual layer has kernal size 1, output dim is same as input
res_layer_l.append(
Conv(
n_residual_channels,
n_residual_channels,
w_init_gain="linear",
device=device,
)
)
self.dilation_conv_l = nn.ModuleList(dilation_conv_l)
self.skip_layer_l = nn.ModuleList(skip_layer_l)
self.res_layer_l = nn.ModuleList(res_layer_l)
def forward(self, forward_input):
for i in range(len(self.dilation_rates)):
x = self.dilation_conv_l[i](forward_input)
# first half goes into filter convolution
x_f = torch.tanh(x[:, : self.n_residual_channels, :])
# second half goes into gating convolution
x_g = torch.sigmoid(x[:, self.n_residual_channels:, :])
# multiply filter and gating branches
z = x_f * x_g
# ------
# if i < len(self.res_layer_l):
# -----
# print('size before reslayer', z.size())
residual = self.res_layer_l[i](z)
# print('size after reslayer', residual.size())
# N.B. what about the last one?
forward_input = forward_input + residual
if i == 0:
output = self.skip_layer_l[i](z)
else:
output = self.skip_layer_l[i](z) + output
return output
class WaveNet(nn.Module):
def __init__(
self,
n_in_channels,
n_residual_channels,
n_skip_channels,
n_out_channels,
nr_layers,
stack_time,
decode_len,
device,
):
super().__init__()
self.decode_len = decode_len
self.n_out_channels = n_out_channels
self.device = device
self.conv_start = Conv(
n_in_channels,
n_residual_channels,
bias=False,
w_init_gain="relu",
device=device,
)
self.dilation_conv = DilationConvLayer(
n_residual_channels, n_skip_channels, nr_layers, stack_time, device
)
self.conv_out = Conv(
n_skip_channels,
n_out_channels,
bias=False,
w_init_gain="relu",
device=device,
)
self.conv_end = Conv(
n_out_channels,
n_out_channels,
bias=False,
w_init_gain="linear",
device=device,
)
def forward(self, forward_input):
"""
In training stage, we use force teaching
|------- encode ts------|
. |- decode ts -|
input: | | | | | | | | | | | | | 0 1 2 3 4 5 6 7
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --> Wavenet
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
output: 0 1 2 3 4 5 6 7 8
. |-- decode ts --|
forward_input size: [batch_size, input_dim, encode_len + decode_len -1]
output size: [batch_size, input_dim, decode_len]
"""
# output size: [batch_size, input_dim+3, encode_len+decode_len-1]
x = self.conv_start(forward_input)
output = self.dilation_conv(x)
output = nn.functional.relu(output, True)
# output size: [batch_size, 1, encode_len+decode_len-1]
output = self.conv_out(output)
output = nn.functional.relu(output, True)
# output size: [batch_size, 1, encode_len+decode_len-1]
output = self.conv_end(output)
# only select the last decode_len as output
# output dimension as [batch_size, decode_dim, decode_len]
l = self.decode_len
output = output[:, :, -l:]
return output
def predict_sequence(self, input_tensor):
# In prediction stage, we predict ts one by one.
# The newly predicted value will be appended to input ts
#
# |------- encode ts -----|------ only features ----------|
# .
# input: | | | | | | | | | | | | | 0 1 2 3 4 5 6 7
# XXXXXXXXXXXXXXXXXXXXXXXXX /| /| /| /| /| /| /| /|
# XXXXXXXXXXXXXXXXXXXXXXXXX / | / | / | / | / | / | / | / |
# XXXXXXXXXXXXXXXXXXXXXXXXX/ |/ |/ |/ |/ |/ |/ |/ |
# output: 0 1 2 3 4 5 6 7 8
# . |---------- predicted ts -------|
#
# input_tensor size: [batch_size, input_dim, encode_len + decode_len - 1]
# N.B. for 1st dimension of decode_len part is all zero
# output size: (batch_size, decode_dim, decode_len)
decode_len = self.decode_len
batch_size = len(input_tensor)
decode_dim = self.n_out_channels
# initialize output (pred_steps time steps)
pred_sequence = torch.zeros(batch_size, decode_dim, decode_len).to(self.device)
# inital input is only encode part
history_tensor = input_tensor[:, :, : -(decode_len - 1)]
for i in range(decode_len):
# record next time step prediction (last time step of model output)
last_step_pred = self.forward(history_tensor)[:, :, -1]
pred_sequence[:, :, i] = last_step_pred
# add the next time step prediction along with corresponding exogenous features to the history tensor
last_step_exog = input_tensor[:, decode_dim:, [(-decode_len + 1) + i]]
last_step_tensor = torch.cat(
[last_step_pred.unsqueeze(2), last_step_exog], axis=1
)
history_tensor = torch.cat([history_tensor, last_step_tensor], axis=2)
return pred_sequence
class WaveNetTS(nn.Module):
def __init__(
self, wavenet, cat_emb_layer, fixed_emb_layer, device
):
super().__init__()
self.wavenet = wavenet.to(device)
self.cat_emb_layer = cat_emb_layer.to(device)
self.fixed_emb_layer = fixed_emb_layer.to(device)
self.device = device
def get_embedding(
self,
src_ts,
trg_ts,
src_xdaysago,
trg_xdaysago,
cat_encode,
cat_decode,
fixed_feat
):
# src_ts size: [encode_len, batch_size, 1]
# trg_ts size: [decode_len, batch_size, 1]
# src_xdaysago size: [encode_len, batch_size, history_data_dim]
# trg_xdaysago size: [decode_len, batch_size, history_data_dim]
# cat_encode size: [encode_len, batch_size, nr_cat_features]
# cat_decode size: [decode_len, batch_size, nr_cat_features]
# fixed_feat size: [batch_size, nr_fixed_features]
encode_len = src_ts.shape[0]
decode_len = trg_ts.shape[0]
# encode_len = src_ts.shape[1]
# decode_len = trg_ts.shape[1]
# batch_size = trg_ts.shape[0]
# trg_dim = trg_ts.shape[2]
# categorical feature embedding
# cat_en(de)code_emb: [en(de)code_len, batch_size, cat_feat_embedding_dim]
cat_encode_emb = self.cat_emb_layer(cat_encode).to(self.device)
cat_decode_emb = self.cat_emb_layer(cat_decode).to(self.device)
# fixed feature embedding, fixed_emb size: [1, batch_size, all_emb_dim]
fixed_emb = self.fixed_emb_layer(fixed_feat).unsqueeze(0).to(self.device)
# encode_input size: [encode_len, batch_size, (ts_dim + xdaysago_dim + cat_emb_dim + fixed_emb_dim)]
encode_input = torch.cat(
[src_ts, src_xdaysago, cat_encode_emb, fixed_emb.repeat(encode_len, 1, 1)],
2,
)
# decode_input size: [decode_len, batch_size, (ts_dim + xdaysago_dim + cat_emb_dim + fixed_emb_dim)]
decode_input = torch.cat(
[trg_ts, trg_xdaysago, cat_decode_emb, fixed_emb.repeat(decode_len, 1, 1)],
2,
)
return encode_input, decode_input
def merge_encode_decode_seq(self, encode_input, decode_input):
# In wavenet, the required size is: [batch_size, input_dim, sequence_len]
# so we permute the dimension
encode_input = encode_input.permute(1, 2, 0)
decode_input = decode_input.permute(1, 2, 0)
# for Wavenet, input is both encode plus decode, but WITHOUT the last step of decode!!!
decode_input = decode_input[:, :, :-1]
# forward_input size: [batch_size, (ts_dim + xdaysago_dim + cat_emb_dim + fixed_emb_dim), encode_len+decode_len-1]
forward_input = torch.cat([encode_input, decode_input], 2)
return forward_input
def forward(
self,
src_ts,
trg_ts,
src_xdaysago,
trg_xdaysago,
cat_encode,
cat_decode,
fixed_feat,
teacher_forcing_ratio=None # not like Seq2Seq, WaveNet does not need teaching force ratio
):
# build embedding tensors and concatenate them
encode_input, decode_input = self.get_embedding(
src_ts,
trg_ts,
src_xdaysago,
trg_xdaysago,
cat_encode,
cat_decode,
fixed_feat,
)
# forward_input size: [batch_size, (ts_dim + xdaysago_dim + cat_emb_dim + fixed_emb_dim), encode_len+decode_len-1]
forward_input = self.merge_encode_decode_seq(encode_input, decode_input)
# output size: [batch_size, 1, decode_len]
output = self.wavenet(forward_input)
# change size to [decode_len, batch_size, 1] to match target tensor when compute loss
output = output.permute(2, 0, 1)
return output
def generate(
self,
src_ts,
trg_ts,
src_xdaysago,
trg_xdaysago,
cat_encode,
cat_decode,
fixed_feat,
):
"""
Make prediction
"""
# forward_input size [batch_size, (ts_dim + xdaysago_dim + cat_emb_dim + fixed_emb_dim), encode_len+decode_len-1]
encode_input, decode_input = self.get_embedding(
src_ts,
trg_ts,
src_xdaysago,
trg_xdaysago,
cat_encode,
cat_decode,
fixed_feat,
)
# forward_input size: [batch_size, (ts_dim + xdaysago_dim + cat_emb_dim + fixed_emb_dim), encode_len+decode_len-1]
forward_input = self.merge_encode_decode_seq(encode_input, decode_input)
# output size: [batch_size, 1, decode_len]
output = self.wavenet.predict_sequence(forward_input)
return output.squeeze(1)