-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy patheunn.py
336 lines (255 loc) · 10.7 KB
/
eunn.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import math
import numpy as np
from tensorflow.python.ops import rnn_cell_impl
def modrelu(inputs, bias, cplex=True):
"""
modReLU activation function
"""
if cplex:
norm = tf.abs(inputs) + 0.01
biased_norm = norm + bias
magnitude = tf.cast(tf.nn.relu(biased_norm), tf.complex64)
phase = inputs / tf.cast(norm, tf.complex64)
else:
norm = tf.abs(inputs) + 0.01
biased_norm = norm + bias
magnitude = tf.nn.relu(biased_norm)
phase = tf.sign(inputs)
return phase * magnitude
def generate_index_tunable(s, L):
"""
generate the index lists for eunn to prepare weight matrices
and perform efficient rotations
This function works for tunable case
"""
ind1 = list(range(s))
ind2 = list(range(s))
for i in range(s):
if i%2 == 1:
ind1[i] = ind1[i] - 1
if i == s -1:
continue
else:
ind2[i] = ind2[i] + 1
else:
ind1[i] = ind1[i] + 1
if i == 0:
continue
else:
ind2[i] = ind2[i] - 1
ind_exe = [ind1, ind2] * int(L/2)
ind3 = []
ind4 = []
for i in range(int(s/2)):
ind3.append(i)
ind3.append(i + int(s/2))
ind4.append(0)
for i in range(int(s/2) - 1):
ind4.append(i + 1)
ind4.append(i + int(s/2))
ind4.append(s - 1)
ind_param = [ind3, ind4]
return ind_exe, ind_param
def generate_index_fft(s):
"""
generate the index lists for eunn to prepare weight matrices
and perform efficient rotations
This function works for fft case
"""
def ind_s(k):
if k==0:
return np.array([[1,0]])
else:
temp = np.array(range(2**k))
list0 = [np.append(temp + 2**k, temp)]
list1 = ind_s(k-1)
for i in range(k):
list0.append(np.append(list1[i],list1[i] + 2**k))
return list0
t = ind_s(int(math.log(s/2, 2)))
ind_exe = []
for i in range(int(math.log(s, 2))):
ind_exe.append(tf.constant(t[i]))
ind_param = []
for i in range(int(math.log(s, 2))):
ind = np.array([])
for j in range(2**i):
ind = np.append(ind, np.array(range(0, s, 2**i)) + j).astype(np.int32)
ind_param.append(tf.constant(ind))
return ind_exe, ind_param
def fft_param(num_units, cplex):
phase_init = tf.random_uniform_initializer(-3.14, 3.14)
capacity = int(math.log(num_units, 2))
theta = tf.get_variable("theta", [capacity, num_units//2],
initializer=phase_init)
cos_theta = tf.cos(theta)
sin_theta = tf.sin(theta)
if cplex:
phi = tf.get_variable("phi", [capacity, num_units//2],
initializer=phase_init)
cos_phi = tf.cos(phi)
sin_phi = tf.sin(phi)
cos_list_re = tf.concat([cos_theta, cos_theta * cos_phi], axis=1)
cos_list_im = tf.concat([tf.zeros_like(theta), cos_theta * sin_phi], axis=1)
sin_list_re = tf.concat([sin_theta, - sin_theta * cos_phi], axis=1)
sin_list_im = tf.concat([tf.zeros_like(theta), - sin_theta * sin_phi], axis=1)
cos_list = tf.complex(cos_list_re, cos_list_im)
sin_list = tf.complex(sin_list_re, sin_list_im)
else:
cos_list = tf.concat([cos_theta, cos_theta], axis=1)
sin_list = tf.concat([sin_theta, -sin_theta], axis=1)
ind_exe, index_fft = generate_index_fft(num_units)
v1 = tf.stack([tf.gather(cos_list[i,:], index_fft[i]) for i in range(capacity)])
v2 = tf.stack([tf.gather(sin_list[i,:], index_fft[i]) for i in range(capacity)])
if cplex:
omega = tf.get_variable("omega", [num_units],
initializer=phase_init)
D = tf.complex(tf.cos(omega), tf.sin(omega))
else:
D = None
diag = D
return v1, v2, ind_exe, diag
def tunable_param(num_units, cplex, capacity):
capacity_A = int(capacity//2)
capacity_B = capacity - capacity_A
phase_init = tf.random_uniform_initializer(-3.14, 3.14)
theta_A = tf.get_variable("theta_A", [capacity_A, num_units//2],
initializer=phase_init)
cos_theta_A = tf.cos(theta_A)
sin_theta_A = tf.sin(theta_A)
if cplex:
phi_A = tf.get_variable("phi_A", [capacity_A, num_units//2],
initializer=phase_init)
cos_phi_A = tf.cos(phi_A)
sin_phi_A = tf.sin(phi_A)
cos_list_A_re = tf.concat([cos_theta_A, cos_theta_A * cos_phi_A], axis=1)
cos_list_A_im = tf.concat([tf.zeros_like(theta_A), cos_theta_A * sin_phi_A], axis=1)
sin_list_A_re = tf.concat([sin_theta_A, - sin_theta_A * cos_phi_A], axis=1)
sin_list_A_im = tf.concat([tf.zeros_like(theta_A), - sin_theta_A * sin_phi_A], axis=1)
cos_list_A = tf.complex(cos_list_A_re, cos_list_A_im)
sin_list_A = tf.complex(sin_list_A_re, sin_list_A_im)
else:
cos_list_A = tf.concat([cos_theta_A, cos_theta_A], axis=1)
sin_list_A = tf.concat([sin_theta_A, -sin_theta_A], axis=1)
theta_B = tf.get_variable("theta_B", [capacity_B, num_units//2 - 1],
initializer=phase_init)
cos_theta_B = tf.cos(theta_B)
sin_theta_B = tf.sin(theta_B)
if cplex:
phi_B = tf.get_variable("phi_B", [capacity_B, num_units//2 - 1],
initializer=phase_init)
cos_phi_B = tf.cos(phi_B)
sin_phi_B = tf.sin(phi_B)
cos_list_B_re = tf.concat([tf.ones([capacity_B, 1]),
cos_theta_B, cos_theta_B * cos_phi_B, tf.ones([capacity_B, 1])], axis=1)
cos_list_B_im = tf.concat([tf.zeros([capacity_B, 1]), tf.zeros_like(theta_B),
cos_theta_B * sin_phi_B, tf.zeros([capacity_B, 1])], axis=1)
sin_list_B_re = tf.concat([tf.zeros([capacity_B, 1]), sin_theta_B,
- sin_theta_B * cos_phi_B, tf.zeros([capacity_B, 1])], axis=1)
sin_list_B_im = tf.concat([tf.zeros([capacity_B, 1]), tf.zeros_like(theta_B),
- sin_theta_B * sin_phi_B, tf.zeros([capacity_B, 1])], axis=1)
cos_list_B = tf.complex(cos_list_B_re, cos_list_B_im)
sin_list_B = tf.complex(sin_list_B_re, sin_list_B_im)
else:
cos_list_B = tf.concat([tf.ones([capacity_B, 1]), cos_theta_B,
cos_theta_B, tf.ones([capacity_B, 1])], axis=1)
sin_list_B = tf.concat([tf.zeros([capacity_B, 1]), sin_theta_B,
- sin_theta_B, tf.zeros([capacity_B, 1])], axis=1)
ind_exe, [index_A, index_B] = generate_index_tunable(num_units, capacity)
diag_list_A = tf.gather(cos_list_A, index_A, axis=1)
off_list_A = tf.gather(sin_list_A, index_A, axis=1)
diag_list_B = tf.gather(cos_list_B, index_B, axis=1)
off_list_B = tf.gather(sin_list_B, index_B, axis=1)
v1 = tf.reshape(tf.concat([diag_list_A, diag_list_B], axis=1), [capacity, num_units])
v2 = tf.reshape(tf.concat([off_list_A, off_list_B], axis=1), [capacity, num_units])
if cplex:
omega = tf.get_variable("omega", [num_units],
initializer=phase_init)
D = tf.complex(tf.cos(omega), tf.sin(omega))
else:
D = None
diag = D
return v1, v2, ind_exe, diag
class EUNNCell(rnn_cell_impl.RNNCell):
"""Efficient Unitary Network Cell
The implementation is based on:
http://arxiv.org/abs/1612.05231.
"""
def __init__(self,
num_units,
capacity=2,
fft=False,
cplex=True,
activation=modrelu,
reuse=None):
"""Initializes the EUNN cell.
Args:
num_units: int, The number of units in the LSTM cell.
capacity: int, The capacity of the unitary matrix for tunable
case.
fft: bool, default false, whether to use fft style
architecture or tunable style.
cplex: bool, default true, whether to use cplex number.
"""
super(EUNNCell, self).__init__(_reuse=reuse)
self._num_units = num_units
self._activation = activation
self._capacity = capacity
self._fft = fft
self._cplex = cplex
if self._capacity > self._num_units:
raise ValueError("Do not set capacity larger than hidden size, it is redundant")
if self._fft:
if math.log(self._num_units, 2) % 1 != 0:
raise ValueError("FFT style only supports power of 2 of hidden size")
else:
if self._num_units % 2 != 0:
raise ValueError("Tunable style only supports even number of hidden size")
if self._capacity % 2 != 0:
raise ValueError("Tunable style only supports even number of capacity")
if self._fft:
self._capacity = int(math.log(self._num_units, 2))
self._v1, self._v2, self._ind, self._diag = fft_param(self._num_units, self._cplex)
else:
self._v1, self._v2, self._ind, self._diag = tunable_param(self._num_units, self._cplex, self._capacity)
@property
def state_size(self):
return self._num_units
@property
def output_size(self):
return self._num_units
def loop(self, h):
for i in range(self._capacity):
diag = h * self._v1[i,:]
off = h * self._v2[i,:]
h = diag + tf.gather(off, self._ind[i], axis=1)
if self._diag is not None:
h = h * self._diag
return h
def __call__(self, inputs, state, scope=None):
with tf.variable_scope(scope or "eunn_cell"):
inputs_size = inputs.get_shape()[-1]
# state = _eunn_loop(state, self._capacity, self.diag_vec, self.off_vec, self.diag, self._fft)
# inputs to hidden
input_matrix_init = tf.random_uniform_initializer(-0.01, 0.01)
if self._cplex:
U_re = tf.get_variable("U_re", [inputs_size, self._num_units], initializer=input_matrix_init)
U_im = tf.get_variable("U_im", [inputs_size, self._num_units], initializer=input_matrix_init)
inputs_re = tf.matmul(inputs, U_re)
inputs_im = tf.matmul(inputs, U_im)
inputs = tf.complex(inputs_re, inputs_im)
else:
U = tf.get_variable("U", [inputs_size, self._num_units],
initializer=input_matrix_init)
inputs = tf.matmul(inputs, U)
# hidden to hidden
state = self.loop(state)
# activation
bias = tf.get_variable("modReLUBias", [self._num_units],
initializer=tf.constant_initializer())
output = self._activation((inputs + state), bias, self._cplex)
return output, output