-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeltaRNN.py
366 lines (286 loc) · 13.8 KB
/
deltaRNN.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
#"""
#Author :- Ankur Mali
#"""
import os
import sys
import tensorflow as tf
import numpy as np
#from tensorflow.python.ops.rnn_cell import RNNCell
#from rnn_cell_impl import RNNCell
from rnn_cell_implement import RNNCell
class DeltaRNNCell(RNNCell):
#"""
#Delta RNN - Differential Framework.
#Alexander G. Ororbia II, Tomas Mikolov and David Reitter,
#"Learning Simpler Language Models with the
# Delta Recurrent Neural Network Framework"
#"""
def __init__(self, num_units, apply_layer_norm=False):
self._num_units = num_units
self._apply_layer_norm = apply_layer_norm
if self._apply_layer_norm:
self._layer_norm = tf.contrib.layers.layer_norm
@property
def input_size(self):
return self._num_units
@property
def output_size(self):
return self._num_units
@property
def state_size(self):
return self._num_units
def _outer_function(self, inner_function_output,
past_hidden_state, activation=tf.nn.relu,
wx_parameterization_gate=True, scope=None):
#"""Check Equation 3 in Delta RNN paper
# for basic understanding and to relate our code with papers maths.
#"""
assert inner_function_output.get_shape().as_list() == \
past_hidden_state.get_shape().as_list()
with tf.variable_scope(scope or type(self).__name__):
with tf.variable_scope("OuterFunction"):
r_bias = tf.get_variable(
"outer_function_gate",
[self._num_units],
dtype=tf.float32, initializer=tf.zeros_initializer)
# Equation 5 in Alex(DRNN paper)
if wx_parameterization_gate:
r = self._W_x_inputs + r_bias
else:
r = r_bias
gate = tf.nn.sigmoid(r)
output = activation((1.0 - gate) * inner_function_output + gate * past_hidden_state)
return output
# End of outer function
# Inner function
def _inner_function(self, inputs, past_hidden_state,
activation=tf.nn.tanh, scope=None):
#second order function as described equation 11 in delta rnn paper
#This is used in inner function
with tf.variable_scope(scope or type(self).__name__):
with tf.variable_scope("InnerFunction"):
with tf.variable_scope("Vh"):
V_h = _linear(past_hidden_state, self._num_units, True)
with tf.variable_scope("Wx"):
self._W_x_inputs = _linear(inputs, self._num_units, True)
alpha = tf.get_variable(
"alpha", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(2.0))
# alpha value 2.0 works better than 1.0
beta_one = tf.get_variable(
"beta_one", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(1.0))
beta_two = tf.get_variable(
"beta_two", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(1.0))
z_t_bias = tf.get_variable(
"z_t_bias", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(0.0))
# 2nd order calculation
#You can change activation function but before get familiar with gating operations and mathematical notations
d_1_t = alpha * V_h * self._W_x_inputs
d_2_t = beta_one * V_h + beta_two * self._W_x_inputs
if self._apply_layer_norm:
d_1_t = self._layer_norm(d_1_t)
d_2_t = self._layer_norm(d_2_t)
z_t = activation(d_1_t + d_2_t + z_t_bias)
return z_t
def __call__(self, inputs, state, scope=None):
inner_function_output = self._inner_function(inputs, state)
output = self._outer_function(inner_function_output, state)
return output, output
class DeltaRNNCellBody(RNNCell):
#
#Delta RNN - Differential Framework.
#Alexander G. Ororbia II, Tomas Mikolov and David Reitter,
#"Learning Simpler Language Models with the
# Delta Recurrent Neural Network Framework"
#"""
def __init__(self, num_units, apply_layer_norm=False):
self._num_units = num_units
self._apply_layer_norm = apply_layer_norm
if self._apply_layer_norm:
self._layer_norm = tf.contrib.layers.layer_norm
@property
def input_size(self):
return self._num_units
@property
def output_size(self):
return self._num_units
@property
def state_size(self):
return self._num_units
def _outer_function(self, inner_function_output,
past_hidden_state, activation=tf.nn.relu,
wx_parameterization_gate=True, scope=None):
#"""Check Equation 3 in Delta RNN paper
# for basic understanding and to relate our code with papers maths.
#"""
assert inner_function_output.get_shape().as_list() == \
past_hidden_state.get_shape().as_list()
with tf.variable_scope(scope or type(self).__name__):
with tf.variable_scope("OuterFunction"):
r_bias = tf.get_variable(
"outer_function_gate",
[self._num_units],
dtype=tf.float32, initializer=tf.zeros_initializer)
# Equation 5 in Alex(DRNN paper)
if wx_parameterization_gate:
r = self._W_x_inputs + r_bias
else:
r = r_bias
gate = tf.nn.sigmoid(r)
output = activation((1.0 - gate) * inner_function_output + gate * past_hidden_state)
return output
# """ End of outer function """
# """ Inner function """
def _inner_function(self, inputs, past_hidden_state, context, activation=tf.nn.tanh, scope=None): # modified
#"""second order function as described equation 11 in delta rnn paper
#This is used in inner function
#"""
with tf.variable_scope(scope or type(self).__name__):
with tf.variable_scope("InnerFunction"):
with tf.variable_scope("Vh"):
V_h = _linear(past_hidden_state, self._num_units, True)
with tf.variable_scope("Qm"): # modified
Q_m = _linear(context, self._num_units, True)
with tf.variable_scope("Wx"):
self._W_x_inputs = _linear(inputs, self._num_units, True)
alpha = tf.get_variable(
"alpha", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(2.0))
#""" alpha value 2.0 works better than 1.0"""
beta_one = tf.get_variable(
"beta_one", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(1.0))
beta_two = tf.get_variable(
"beta_two", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(1.0))
z_t_bias = tf.get_variable(
"z_t_bias", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(0.0))
# 2nd order calculation
#You can change activation function but before get familiar with gating operations and mathematical notations
d_1_t = alpha * V_h * ( self._W_x_inputs + Q_m ) # modified
d_2_t = beta_one * V_h + beta_two * ( self._W_x_inputs + Q_m ) # modified
if self._apply_layer_norm:
d_1_t = self._layer_norm(d_1_t)
d_2_t = self._layer_norm(d_2_t)
z_t = activation(d_1_t + d_2_t + z_t_bias)
return z_t
def __call__(self, inputs, state, context, scope=None):
inner_function_output = self._inner_function(inputs, state, context)
output = self._outer_function(inner_function_output, state)
return output, output
class DeltaRNNCellBodyFlow(RNNCell):
#
#Delta RNN - Differential Framework.
#Alexander G. Ororbia II, Tomas Mikolov and David Reitter,
#"Learning Simpler Language Models with the
# Delta Recurrent Neural Network Framework"
#"""
def __init__(self, num_units, apply_layer_norm=False):
self._num_units = num_units
self._apply_layer_norm = apply_layer_norm
if self._apply_layer_norm:
self._layer_norm = tf.contrib.layers.layer_norm
@property
def input_size(self):
return self._num_units
@property
def output_size(self):
return self._num_units
@property
def state_size(self):
return self._num_units
def _outer_function(self, inputs, inner_function_output,
past_hidden_state, activation=tf.nn.relu,
wx_parameterization_gate=True, scope=None):
#"""Check Equation 3 in Delta RNN paper
# for basic understanding and to relate our code with papers maths.
#"""
assert inner_function_output.get_shape().as_list() == \
past_hidden_state.get_shape().as_list()
with tf.variable_scope(scope or type(self).__name__):
with tf.variable_scope("OuterFunction"):
r_bias = tf.get_variable("outer_function_vel_bias", [self._num_units], dtype=tf.float32, initializer=tf.zeros_initializer)
W_vel = tf.get_variable("outer_function_W_vel", [54, self._num_units ], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer())
# Equation 5 in Alex(DRNN paper)
if wx_parameterization_gate:
#r = self._W_x_inputs + r_bias
r = tf.matmul(inputs[:,54:108], W_vel) + r_bias # modified
else:
r = r_bias
gate = tf.nn.sigmoid(r)
output = activation((1.0 - gate) * inner_function_output + gate * past_hidden_state)
return output
# """ End of outer function """
# """ Inner function """
def _inner_function(self, inputs, past_hidden_state, context, activation=tf.nn.tanh, scope=None): # modified
#"""second order function as described equation 11 in delta rnn paper
#This is used in inner function
#"""
with tf.variable_scope(scope or type(self).__name__):
with tf.variable_scope("InnerFunction"):
with tf.variable_scope("Vh"):
V_h = _linear(past_hidden_state, self._num_units, True)
with tf.variable_scope("Qm"): # modified
Q_m = _linear(context, self._num_units, True)
with tf.variable_scope("Wx"):
self._W_x_inputs = _linear(inputs[:,0:54], self._num_units, True)
alpha = tf.get_variable(
"alpha", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(2.0))
#""" alpha value 2.0 works better than 1.0"""
beta_one = tf.get_variable(
"beta_one", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(1.0))
beta_two = tf.get_variable(
"beta_two", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(1.0))
z_t_bias = tf.get_variable(
"z_t_bias", [self._num_units], dtype=tf.float32,
initializer=tf.constant_initializer(0.0))
# 2nd order calculation
#You can change activation function but before get familiar with gating operations and mathematical notations
d_1_t = alpha * V_h * ( self._W_x_inputs + Q_m ) # modified
d_2_t = beta_one * V_h + beta_two * ( self._W_x_inputs + Q_m ) # modified
if self._apply_layer_norm:
d_1_t = self._layer_norm(d_1_t)
d_2_t = self._layer_norm(d_2_t)
z_t = activation(d_1_t + d_2_t + z_t_bias)
return z_t
def __call__(self, inputs, state, context, scope=None):
inner_function_output = self._inner_function(inputs, state, context)
output = self._outer_function(inputs, inner_function_output, state)
return output, output
def _linear(args, output_size, bias, bias_start=0.0, scope=None):
#"""Linear mapping """
if args is None or (isinstance(args, (list, tuple)) and not args):
raise ValueError("`args` must be specified, please check definition for input variables")
if not isinstance(args, (list, tuple)):
args = [args]
# dimension 1 cell size calculation.
total_arg_size = 0
shapes = [a.get_shape().as_list() for a in args]
for shape in shapes:
if len(shape) != 2:
raise ValueError(
"Linear is expecting 2Dimensional Arguments: %s" % str(shapes))
if not shape[1]:
raise ValueError(
"Linear expects shape[1] of arguments: %s" % str(shapes))
else:
total_arg_size += shape[1]
with tf.variable_scope(scope or "Linear"):
matrix = tf.get_variable("Matrix", [total_arg_size, output_size])
if len(args) == 1:
res = tf.matmul(args[0], matrix)
else:
res = tf.matmul(tf.concat(1, args), matrix)
if not bias:
return res
bias_term = tf.get_variable(
"Bias", [output_size],
initializer=tf.constant_initializer(bias_start))
return res + bias_term