forked from MU94W/TFCommon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNNCell.py
258 lines (208 loc) · 10.4 KB
/
RNNCell.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
import tensorflow as tf
from tensorflow.python.ops import array_ops
if float(tf.__version__[:3]) >= 1.2:
from tensorflow.python.ops.rnn_cell_impl import RNNCell
else:
from tensorflow.python.ops.rnn_cell_impl import _RNNCell as RNNCell
from tensorflow.contrib.rnn import LSTMStateTuple
from tensorflow.contrib.keras import activations
import TFCommon.Initializer
class RawGRUCell(RNNCell):
"""Gated Recurrent Unit (GRU) recurrent network cell."""
def __init__(self, num_units, init_state=None, gate_activation="sigmoid", reuse=None):
self.__num_units = num_units
if gate_activation == "sigmoid":
self.__gate_activation = tf.sigmoid
elif gate_activation == "hard_sigmoid":
self.__gate_activation = activations.hard_sigmoid
else:
raise ValueError
self.__init_state = init_state
self.__reuse = reuse
@property
def state_size(self):
return self.__num_units
@property
def output_size(self):
return self.__num_units
def zero_state(self, batch_size, dtype):
return tuple([super(RawGRUCell, self).zero_state(batch_size, dtype)])
def init_state(self, batch_size, dtype):
if self.__init_state is not None:
return tuple([self.__init_state])
else:
return self.zero_state(batch_size, dtype)
def __call__(self, x, h_prev, scope=None):
with tf.variable_scope(scope or type(self).__name__):
h_prev = h_prev[0]
# Check if the input size exist.
input_size = x.shape.with_rank(2)[1].value
if input_size is None:
raise ValueError("Expecting input_size to be set.")
### get weights.
W_shape = (input_size, self.output_size)
U_shape = (self.output_size, self.output_size)
b_shape = (self.output_size,)
Wz = tf.get_variable(name='Wz', shape=W_shape)
Wr = tf.get_variable(name='Wr', shape=W_shape)
Wh = tf.get_variable(name='Wh', shape=W_shape)
Uz = tf.get_variable(name='Uz', shape=U_shape,
initializer=TFCommon.Initializer.random_orthogonal_initializer())
Ur = tf.get_variable(name='Ur', shape=U_shape,
initializer=TFCommon.Initializer.random_orthogonal_initializer())
Uh = tf.get_variable(name='Uh', shape=U_shape,
initializer=TFCommon.Initializer.random_orthogonal_initializer())
bz = tf.get_variable(name='bz', shape=b_shape,
initializer=tf.constant_initializer(0.0))
br = tf.get_variable(name='br', shape=b_shape,
initializer=tf.constant_initializer(0.0))
bh = tf.get_variable(name='bh', shape=b_shape,
initializer=tf.constant_initializer(0.0))
### calculate r and z
r = self.__gate_activation(tf.matmul(x, Wr) + tf.matmul(h_prev, Ur) + br)
z = self.__gate_activation(tf.matmul(x, Wz) + tf.matmul(h_prev, Uz) + bz)
### calculate candidate
h_slash = tf.tanh(tf.matmul(x, Wh) + tf.matmul(r * h_prev, Uh) + bh)
### final cal
new_h = (1-z) * h_prev + z * h_slash
return new_h, tuple([new_h])
class GRUCell(RNNCell):
def __init__(self, num_units, init_state=None, gate_activation="sigmoid", reuse=None):
self.__num_units = num_units
if gate_activation == "sigmoid":
self.__gate_activation = tf.sigmoid
elif gate_activation == "hard_sigmoid":
self.__gate_activation = activations.hard_sigmoid
else:
raise ValueError
self.__init_state = init_state
self.__reuse = reuse
@property
def state_size(self):
return self.__num_units
@property
def output_size(self):
return self.__num_units
def zero_state(self, batch_size, dtype):
return tuple([super(GRUCell, self).zero_state(batch_size, dtype)])
def init_state(self, batch_size, dtype):
if self.__init_state is not None:
return tuple([self.__init_state])
else:
return self.zero_state(batch_size, dtype)
def __call__(self, x, h_prev, scope=None):
with tf.variable_scope(scope or type(self).__name__):
h_prev = h_prev[0]
# Check if the input size exist.
input_size = x.shape.with_rank(2)[1].value
if input_size is None:
raise ValueError("Expecting input_size to be set.")
### get weights.
W_shape = (input_size, self.output_size)
U_shape = (self.output_size, self.output_size)
b_shape = (self.output_size,)
Wrz = tf.get_variable(name="Wrz", shape=(input_size, 2 * self.output_size))
Wh = tf.get_variable(name='Wh', shape=W_shape)
Urz = tf.get_variable(name="Urz", shape=(self.output_size, 2 * self.output_size),
initializer=TFCommon.Initializer.random_orthogonal_initializer())
Uh = tf.get_variable(name='Uh', shape=U_shape,
initializer=TFCommon.Initializer.random_orthogonal_initializer())
brz = tf.get_variable(name="brz", shape=(2 * self.output_size),
initializer=tf.constant_initializer(0.0))
bh = tf.get_variable(name='bh', shape=b_shape,
initializer=tf.constant_initializer(0.0))
### calculate r and z
rz = self.__gate_activation(tf.matmul(x, Wrz) + tf.matmul(h_prev, Urz) + brz)
r, z = array_ops.split(rz, num_or_size_splits=2, axis=1)
### calculate candidate
h_slash = tf.tanh(tf.matmul(x, Wh) + tf.matmul(r * h_prev, Uh) + bh)
### final cal
new_h = (1-z) * h_prev + z * h_slash
return new_h, tuple([new_h])
class RawLSTMCell(RNNCell):
"""Long Short-Term Memory (LSTM) unit recurrent network cell."""
def __init__(self, num_units, gate_activation="sigmoid", forget_bias=1.0, reuse=None):
self.__num_units = num_units
if gate_activation == "sigmoid":
self.__gate_activation = tf.sigmoid
elif gate_activation == "hard_sigmoid":
self.__gate_activation = activations.hard_sigmoid
else:
raise ValueError
self.__forget_bias = forget_bias
self.__reuse = reuse
@property
def state_size(self):
return LSTMStateTuple(self.output_size, self.output_size)
@property
def output_size(self):
return self.__num_units
def __call__(self, x, state_prev, scope=None):
with tf.variable_scope(scope or type(self).__name__):
h_prev, c_prev = state_prev
# Check if the input size exist.
input_size = x.shape.with_rank(2)[1].value
if input_size is None:
raise ValueError("Expecting input_size to be set.")
### get weights for concated tensor.
W_shape = (input_size, self.output_size)
U_shape = (self.output_size, self.output_size)
b_shape = (self.output_size,)
Wi = tf.get_variable(name='Wi', shape=W_shape)
Wj = tf.get_variable(name='Wj', shape=W_shape)
Wf = tf.get_variable(name='Wf', shape=W_shape)
Wo = tf.get_variable(name='Wo', shape=W_shape)
Ui = tf.get_variable(name='Ui', shape=U_shape,
initializer=TFCommon.Initializer.random_orthogonal_initializer())
Uj = tf.get_variable(name='Uj', shape=U_shape,
initializer=TFCommon.Initializer.random_orthogonal_initializer())
Uf = tf.get_variable(name='Uf', shape=U_shape,
initializer=TFCommon.Initializer.random_orthogonal_initializer())
Uo = tf.get_variable(name='Uo', shape=U_shape,
initializer=TFCommon.Initializer.random_orthogonal_initializer())
bi = tf.get_variable(name='bi', shape=b_shape,
initializer=tf.constant_initializer(0.0))
bj = tf.get_variable(name='bj', shape=b_shape,
initializer=tf.constant_initializer(0.0))
bf = tf.get_variable(name='bf', shape=b_shape,
initializer=tf.constant_initializer(self.__forget_bias)) # forget gate bias := 1
bo = tf.get_variable(name='bo', shape=b_shape,
initializer=tf.constant_initializer(0.0))
### calculate gates and input's info
i = tf.tanh(tf.matmul(x, Wi) + tf.matmul(h_prev, Ui) + bi)
j = self.__gate_activation(tf.matmul(x, Wj) + tf.matmul(h_prev, Uj) + bj)
f = self.__gate_activation(tf.matmul(x, Wf) + tf.matmul(h_prev, Uf) + bf)
o = tf.tanh(tf.matmul(x, Wo) + tf.matmul(h_prev, Uo) + bo)
### calculate candidate
new_c = f * c_prev + i * j
### final cal
new_h = o * tf.tanh(new_c)
return new_h, LSTMStateTuple(new_h, new_c)
class AttentionWrapper(RNNCell):
def __init__(self, cell, attention_module, reuse=None):
self._cell = cell
self._attention_module = attention_module
self._reuse = reuse
@property
def state_size(self):
return self._cell.state_size
@property
def output_size(self):
return self._cell.output_size
def init_state(self, batch_size, dtype):
return self._cell.init_state(batch_size, dtype)
def __call__(self, x, h_prev, scope=None):
with tf.variable_scope(scope or type(self).__name__):
if isinstance(h_prev, tuple):
h_prev_concated = tf.concat(h_prev, axis=-1)
context, alpha = self._attention_module(h_prev_concated)
output, new_h = self._cell(x, h_prev, context)
return output, new_h, alpha, context
class AttentionWithoutIndicWrapper(AttentionWrapper):
def __call__(self, x, h_prev, scope=None):
with tf.variable_scope(scope or type(self).__name__):
if isinstance(h_prev, tuple):
h_prev_concated = tf.concat(h_prev, axis=-1)
context, alpha = self._attention_module(h_prev_concated)
output, new_h = self._cell(context, h_prev)
return output, new_h, alpha, context