-
Notifications
You must be signed in to change notification settings - Fork 4
/
batch_normalization_LSTM.py
290 lines (257 loc) · 10.8 KB
/
batch_normalization_LSTM.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
"""Implementation of batch-normalized LSTM."""
import torch
from torch import nn
from torch.autograd import Variable
from torch.nn import functional, init
class SeparatedBatchNorm1d(nn.Module):
"""
A batch normalization module which keeps its running mean
and variance separately per timestep.
"""
def __init__(self, num_features, max_length, eps=1e-5, momentum=0.1,
affine=True):
"""
Most parts are copied from
torch.nn.modules.batchnorm._BatchNorm.
"""
super(SeparatedBatchNorm1d, self).__init__()
self.num_features = num_features
self.max_length = max_length
self.affine = affine
self.eps = eps
self.momentum = momentum
if self.affine:
self.weight = nn.Parameter(torch.FloatTensor(num_features))
self.bias = nn.Parameter(torch.FloatTensor(num_features))
else:
self.register_parameter('weight', None)
self.register_parameter('bias', None)
for i in range(max_length):
self.register_buffer(
'running_mean_{}'.format(i), torch.zeros(num_features))
self.register_buffer(
'running_var_{}'.format(i), torch.ones(num_features))
self.reset_parameters()
def reset_parameters(self):
for i in range(self.max_length):
running_mean_i = getattr(self, 'running_mean_{}'.format(i))
running_var_i = getattr(self, 'running_var_{}'.format(i))
running_mean_i.zero_()
running_var_i.fill_(1)
if self.affine:
self.weight.data.uniform_()
self.bias.data.zero_()
def _check_input_dim(self, input_):
if input_.size(1) != self.running_mean_0.nelement():
raise ValueError('got {}-feature tensor, expected {}'
.format(input_.size(1), self.num_features))
def forward(self, input_, time):
self._check_input_dim(input_)
if time >= self.max_length:
time = self.max_length - 1
running_mean = getattr(self, 'running_mean_{}'.format(time))
running_var = getattr(self, 'running_var_{}'.format(time))
return functional.batch_norm(
input=input_, running_mean=running_mean, running_var=running_var,
weight=self.weight, bias=self.bias, training=self.training,
momentum=self.momentum, eps=self.eps)
def __repr__(self):
return ('{name}({num_features}, eps={eps}, momentum={momentum},'
' max_length={max_length}, affine={affine})'
.format(name=self.__class__.__name__, **self.__dict__))
class LSTMCell(nn.Module):
"""A basic LSTM cell."""
def __init__(self, input_size, hidden_size, use_bias=True):
"""
Most parts are copied from torch.nn.LSTMCell.
"""
super(LSTMCell, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.use_bias = use_bias
self.weight_ih = nn.Parameter(
torch.FloatTensor(input_size, 4 * hidden_size))
self.weight_hh = nn.Parameter(
torch.FloatTensor(hidden_size, 4 * hidden_size))
if use_bias:
self.bias = nn.Parameter(torch.FloatTensor(4 * hidden_size))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
"""
Initialize parameters following the way proposed in the paper.
"""
init.orthogonal(self.weight_ih.data)
weight_hh_data = torch.eye(self.hidden_size)
weight_hh_data = weight_hh_data.repeat(1, 4)
self.weight_hh.data.set_(weight_hh_data)
# The bias is just set to zero vectors.
if self.use_bias:
init.constant(self.bias.data, val=0)
def forward(self, input_, hx):
"""
Args:
input_: A (batch, input_size) tensor containing input
features.
hx: A tuple (h_0, c_0), which contains the initial hidden
and cell state, where the size of both states is
(batch, hidden_size).
Returns:
h_1, c_1: Tensors containing the next hidden and cell state.
"""
h_0, c_0 = hx
batch_size = h_0.size(0)
bias_batch = (self.bias.unsqueeze(0)
.expand(batch_size, *self.bias.size()))
wh_b = torch.addmm(bias_batch, h_0, self.weight_hh)
wi = torch.mm(input_, self.weight_ih)
f, i, o, g = torch.split(wh_b + wi,
split_size=self.hidden_size, dim=1)
c_1 = torch.sigmoid(f) * c_0 + torch.sigmoid(i) * torch.tanh(g)
h_1 = torch.sigmoid(o) * torch.tanh(c_1)
return h_1, c_1
def __repr__(self):
s = '{name}({input_size}, {hidden_size})'
return s.format(name=self.__class__.__name__, **self.__dict__)
class BNLSTMCell(nn.Module):
"""A BN-LSTM cell."""
def __init__(self, input_size, hidden_size, max_length, use_bias=True):
super(BNLSTMCell, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.max_length = max_length
self.use_bias = use_bias
self.weight_ih = nn.Parameter(
torch.FloatTensor(input_size, 4 * hidden_size))
self.weight_hh = nn.Parameter(
torch.FloatTensor(hidden_size, 4 * hidden_size))
if use_bias:
self.bias = nn.Parameter(torch.FloatTensor(4 * hidden_size))
else:
self.register_parameter('bias', None)
# BN parameters
self.bn_ih = SeparatedBatchNorm1d(
num_features=4 * hidden_size, max_length=max_length)
self.bn_hh = SeparatedBatchNorm1d(
num_features=4 * hidden_size, max_length=max_length)
self.bn_c = SeparatedBatchNorm1d(
num_features=hidden_size, max_length=max_length)
self.reset_parameters()
def reset_parameters(self):
"""
Initialize parameters following the way proposed in the paper.
"""
# The input-to-hidden weight matrix is initialized orthogonally.
init.orthogonal(self.weight_ih.data)
# The hidden-to-hidden weight matrix is initialized as an identity
# matrix.
weight_hh_data = torch.eye(self.hidden_size)
weight_hh_data = weight_hh_data.repeat(1, 4)
self.weight_hh.data.set_(weight_hh_data)
# The bias is just set to zero vectors.
init.constant(self.bias.data, val=0)
# Initialization of BN parameters.
self.bn_ih.reset_parameters()
self.bn_hh.reset_parameters()
self.bn_c.reset_parameters()
self.bn_ih.bias.data.fill_(0)
self.bn_hh.bias.data.fill_(0)
self.bn_ih.weight.data.fill_(0.1)
self.bn_hh.weight.data.fill_(0.1)
self.bn_c.weight.data.fill_(0.1)
def forward(self, input_, hx, time):
"""
Args:
input_: A (batch, input_size) tensor containing input
features.
hx: A tuple (h_0, c_0), which contains the initial hidden
and cell state, where the size of both states is
(batch, hidden_size).
time: The current timestep value, which is used to
get appropriate running statistics.
Returns:
h_1, c_1: Tensors containing the next hidden and cell state.
"""
h_0, c_0 = hx
batch_size = h_0.size(0)
bias_batch = (self.bias.unsqueeze(0)
.expand(batch_size, *self.bias.size()))
wh = torch.mm(h_0, self.weight_hh)
wi = torch.mm(input_, self.weight_ih)
bn_wh = self.bn_hh(wh, time=time)
bn_wi = self.bn_ih(wi, time=time)
f, i, o, g = torch.split(bn_wh + bn_wi + bias_batch,
split_size=self.hidden_size, dim=1)
c_1 = torch.sigmoid(f) * c_0 + torch.sigmoid(i) * torch.tanh(g)
h_1 = torch.sigmoid(o) * torch.tanh(self.bn_c(c_1, time=time))
return h_1, c_1
class LSTM(nn.Module):
"""A module that runs multiple steps of LSTM."""
def __init__(self, cell_class, input_size, hidden_size, num_layers=1,
use_bias=True, batch_first=False, dropout=0, **kwargs):
super(LSTM, self).__init__()
self.cell_class = cell_class
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.use_bias = use_bias
self.batch_first = batch_first
self.dropout = dropout
for layer in range(num_layers):
layer_input_size = input_size if layer == 0 else hidden_size
cell = cell_class(input_size=layer_input_size,
hidden_size=hidden_size,
**kwargs)
setattr(self, 'cell_{}'.format(layer), cell)
self.dropout_layer = nn.Dropout(dropout)
self.reset_parameters()
def get_cell(self, layer):
return getattr(self, 'cell_{}'.format(layer))
def reset_parameters(self):
for layer in range(self.num_layers):
cell = self.get_cell(layer)
cell.reset_parameters()
@staticmethod
def _forward_rnn(cell, input_, length, hx):
max_time = input_.size(0)
output = []
for time in range(max_time):
if isinstance(cell, BNLSTMCell):
h_next, c_next = cell(input_=input_[time], hx=hx, time=time)
else:
h_next, c_next = cell(input_=input_[time], hx=hx)
mask = (time < length).float().unsqueeze(1).expand_as(h_next)
h_next = h_next * mask + hx[0] * (1 - mask)
c_next = c_next * mask + hx[1] * (1 - mask)
hx_next = (h_next, c_next)
output.append(h_next)
hx = hx_next
output = torch.stack(output, 0)
return output, hx
def forward(self, input_, length=None, hx=None):
if self.batch_first:
input_ = input_.transpose(0, 1)
max_time, batch_size, _ = input_.size()
if length is None:
length = Variable(torch.LongTensor([max_time] * batch_size))
# if input_.is_cuda:
# device = input_.get_device()
# length = length.cuda(device)
if hx is None:
hx = Variable(input_.data.new(batch_size, self.hidden_size).zero_())
hx = (hx, hx)
h_n = []
c_n = []
layer_output = None
for layer in range(self.num_layers):
cell = self.get_cell(layer)
layer_output, (layer_h_n, layer_c_n) = LSTM._forward_rnn(
cell=cell, input_=input_, length=length, hx=hx)
input_ = self.dropout_layer(layer_output)
h_n.append(layer_h_n)
c_n.append(layer_c_n)
output = layer_output
h_n = torch.stack(h_n, 0)
c_n = torch.stack(c_n, 0)
return output, (h_n, c_n)