-
Notifications
You must be signed in to change notification settings - Fork 31
/
drnn.py
128 lines (98 loc) · 4.36 KB
/
drnn.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
import torch
import torch.nn as nn
use_cuda = torch.cuda.is_available()
class DRNN(nn.Module):
def __init__(self, n_input, n_hidden, n_layers, dropout=0, cell_type='GRU', batch_first=False):
super(DRNN, self).__init__()
self.dilations = [2 ** i for i in range(n_layers)]
self.cell_type = cell_type
self.batch_first = batch_first
layers = []
if self.cell_type == "GRU":
cell = nn.GRU
elif self.cell_type == "RNN":
cell = nn.RNN
elif self.cell_type == "LSTM":
cell = nn.LSTM
else:
raise NotImplementedError
for i in range(n_layers):
if i == 0:
c = cell(n_input, n_hidden, dropout=dropout)
else:
c = cell(n_hidden, n_hidden, dropout=dropout)
layers.append(c)
self.cells = nn.Sequential(*layers)
def forward(self, inputs, hidden=None):
if self.batch_first:
inputs = inputs.transpose(0, 1)
outputs = []
for i, (cell, dilation) in enumerate(zip(self.cells, self.dilations)):
if hidden is None:
inputs, _ = self.drnn_layer(cell, inputs, dilation)
else:
inputs, hidden[i] = self.drnn_layer(cell, inputs, dilation, hidden[i])
outputs.append(inputs[-dilation:])
if self.batch_first:
inputs = inputs.transpose(0, 1)
return inputs, outputs
def drnn_layer(self, cell, inputs, rate, hidden=None):
n_steps = len(inputs)
batch_size = inputs[0].size(0)
hidden_size = cell.hidden_size
inputs, _ = self._pad_inputs(inputs, n_steps, rate)
dilated_inputs = self._prepare_inputs(inputs, rate)
if hidden is None:
dilated_outputs, hidden = self._apply_cell(dilated_inputs, cell, batch_size, rate, hidden_size)
else:
hidden = self._prepare_inputs(hidden, rate)
dilated_outputs, hidden = self._apply_cell(dilated_inputs, cell, batch_size, rate, hidden_size, hidden=hidden)
splitted_outputs = self._split_outputs(dilated_outputs, rate)
outputs = self._unpad_outputs(splitted_outputs, n_steps)
return outputs, hidden
def _apply_cell(self, dilated_inputs, cell, batch_size, rate, hidden_size, hidden=None):
if hidden is None:
if self.cell_type == 'LSTM':
c, m = self.init_hidden(batch_size * rate, hidden_size)
hidden = (c.unsqueeze(0), m.unsqueeze(0))
else:
hidden = self.init_hidden(batch_size * rate, hidden_size).unsqueeze(0)
dilated_outputs, hidden = cell(dilated_inputs, hidden)
return dilated_outputs, hidden
def _unpad_outputs(self, splitted_outputs, n_steps):
return splitted_outputs[:n_steps]
def _split_outputs(self, dilated_outputs, rate):
batchsize = dilated_outputs.size(1) // rate
blocks = [dilated_outputs[:, i * batchsize: (i + 1) * batchsize, :] for i in range(rate)]
interleaved = torch.stack((blocks)).transpose(1, 0).contiguous()
interleaved = interleaved.view(dilated_outputs.size(0) * rate,
batchsize,
dilated_outputs.size(2))
return interleaved
def _pad_inputs(self, inputs, n_steps, rate):
is_even = (n_steps % rate) == 0
if not is_even:
dilated_steps = n_steps // rate + 1
zeros_ = torch.zeros(dilated_steps * rate - inputs.size(0),
inputs.size(1),
inputs.size(2))
if use_cuda:
zeros_ = zeros_.cuda()
inputs = torch.cat((inputs, zeros_))
else:
dilated_steps = n_steps // rate
return inputs, dilated_steps
def _prepare_inputs(self, inputs, rate):
dilated_inputs = torch.cat([inputs[j::rate, :, :] for j in range(rate)], 1)
return dilated_inputs
def init_hidden(self, batch_size, hidden_dim):
hidden = torch.zeros(batch_size, hidden_dim)
if use_cuda:
hidden = hidden.cuda()
if self.cell_type == "LSTM":
memory = torch.zeros(batch_size, hidden_dim)
if use_cuda:
memory = memory.cuda()
return (hidden, memory)
else:
return hidden