forked from mpezeshki/Associative_LSTM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbricks.py
executable file
·221 lines (187 loc) · 8.58 KB
/
bricks.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
from blocks.bricks import Initializable, Tanh, Logistic
from blocks.bricks.base import application, lazy
from blocks.roles import add_role, WEIGHT, INITIAL_STATE
from blocks.utils import shared_floatx_nans, shared_floatx_zeros
from blocks.bricks.recurrent import BaseRecurrent, recurrent
import theano.tensor as tensor
import numpy
from holographic_memory import complex_mult
class AssociativeLSTM(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, num_copies, use_W_xu, activation=None,
gate_activation=None, **kwargs):
self.dim = dim
self.num_copies = num_copies
self.use_W_xu = use_W_xu
# shape: C x F/2
permutations = []
indices = numpy.arange(self.dim / 2)
for i in range(self.num_copies):
numpy.random.shuffle(indices)
permutations.append(numpy.concatenate(
[indices,
[ind + self.dim / 2 for ind in indices]]))
# C x F (numpy)
self.permutations = numpy.vstack(permutations)
if not activation:
activation = Tanh()
if not gate_activation:
gate_activation = Logistic()
self.activation = activation
self.gate_activation = gate_activation
children = ([self.activation, self.gate_activation] +
kwargs.get('children', []))
super(AssociativeLSTM, self).__init__(children=children, **kwargs)
def get_dim(self, name):
if name == 'inputs':
return self.dim * 4.5
if name in ['states', 'cells']:
return self.dim
if name == 'mask':
return 0
return super(AssociativeLSTM, self).get_dim(name)
def _allocate(self):
self.W_state = shared_floatx_nans((self.dim, 4.5 * self.dim),
name='W_state')
# The underscore is required to prevent collision with
# the `initial_state` application method
self.initial_state_ = shared_floatx_zeros((self.dim,),
name="initial_state")
self.initial_cells = shared_floatx_zeros((self.num_copies, self.dim),
name="initial_cells")
add_role(self.W_state, WEIGHT)
# add_role(self.initial_state_, INITIAL_STATE)
# add_role(self.initial_cells, INITIAL_STATE)
self.parameters = [self.W_state]
def _initialize(self):
self.weights_init.initialize(self.parameters[0], self.rng)
# The activation function that bound values between 0 and 1
# input_: B x F
def bound(self, input_):
sq = input_ ** 2
d = tensor.sqrt(tensor.maximum(
1, sq[:, :self.dim / 2] + sq[:, self.dim / 2:]))
d = tensor.concatenate([d, d], axis=1)
return input_ / d
# input: B x F
# output: C x B x F
def permute(self, input):
inputs_permuted = []
for i in range(self.permutations.shape[0]):
inputs_permuted.append(
input[:, self.permutations[i]].dimshuffle('x', 0, 1))
return tensor.concatenate(inputs_permuted, axis=0)
@recurrent(sequences=['inputs', 'mask'], states=['states', 'cells'],
contexts=[], outputs=['states', 'cells'])
def apply(self, inputs, states, cells, mask=None):
def slice_(x, no):
# Gates dimension is dim/2.
if no in [0, 1, 2]:
return x[:, no * self.dim / 2: (no + 1) * self.dim / 2]
# Keys and u dimension is dim.
elif no in [3, 4, 5]:
return x[:, int((no - 1.5) * self.dim):
int((no - 0.5) * self.dim)]
activation = tensor.dot(states, self.W_state) + inputs
in_gate = self.gate_activation.apply(slice_(activation, 0))
in_gate = tensor.concatenate([in_gate, in_gate], axis=1)
forget_gate = self.gate_activation.apply(slice_(activation, 1))
forget_gate = tensor.concatenate([forget_gate, forget_gate], axis=1)
out_gate = self.gate_activation.apply(slice_(activation, 2))
out_gate = tensor.concatenate([out_gate, out_gate], axis=1)
in_key = self.bound(slice_(activation, 3))
# B x F --> C x B x F
in_keys = self.permute(in_key)
out_key = self.bound(slice_(activation, 4))
# B x F --> C x B x F
out_keys = self.permute(out_key)
if self.use_W_xu:
u = self.bound(slice_(activation, 5))
else:
u = self.bound(slice_(
tensor.dot(states, self.W_state * 0.00001) + inputs, 5))
# 1 x B x F , C x B x F --> C x B x F
f_x_c = forget_gate.dimshuffle('x', 0, 1) * cells
# B x F , B x F --> 1 x B x F
i_x_u = (in_gate * u).dimshuffle('x', 0, 1)
next_cells = (f_x_c + complex_mult(in_keys, i_x_u))
# C x B x F , C x B x F --> C x B x F
o_x_c = complex_mult(out_keys, next_cells)
next_states = out_gate * self.bound(tensor.mean(o_x_c, axis=0))
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
next_cells = (mask[:, None] * next_cells +
(1 - mask[:, None]) * cells)
return next_states, next_cells
@application(outputs=apply.states)
def initial_states(self, batch_size, *args, **kwargs):
return [tensor.repeat(self.initial_state_[None, :], batch_size, 0),
tensor.repeat(self.initial_cells[:, None, :], batch_size, 1)]
class LSTM(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, bias, activation=None,
gate_activation=None, **kwargs):
self.dim = dim
self.bias = bias
if not activation:
activation = Tanh()
if not gate_activation:
gate_activation = Logistic()
self.activation = activation
self.gate_activation = gate_activation
children = ([self.activation, self.gate_activation] +
kwargs.get('children', []))
super(LSTM, self).__init__(children=children, **kwargs)
def get_dim(self, name):
if name == 'inputs':
return self.dim * 4
if name in ['states', 'cells']:
return self.dim
if name == 'mask':
return 0
return super(LSTM, self).get_dim(name)
def _allocate(self):
self.W_state = shared_floatx_nans((self.dim, 4 * self.dim),
name='W_state')
# The underscore is required to prevent collision with
# the `initial_state` application method
self.initial_state_ = shared_floatx_zeros((self.dim,),
name="initial_state")
self.initial_cells = shared_floatx_zeros((self.dim,),
name="initial_cells")
add_role(self.W_state, WEIGHT)
add_role(self.initial_state_, INITIAL_STATE)
add_role(self.initial_cells, INITIAL_STATE)
self.parameters = [
self.W_state, self.initial_state_, self.initial_cells]
def _initialize(self):
for weights in self.parameters[:1]:
self.weights_init.initialize(weights, self.rng)
@recurrent(sequences=['inputs', 'mask'], states=['states', 'cells'],
contexts=[], outputs=['states', 'cells'])
def apply(self, inputs, states, cells, mask=None):
def slice_last(x, no):
return x[:, no * self.dim: (no + 1) * self.dim]
activation = tensor.dot(states, self.W_state) + inputs
in_gate = self.gate_activation.apply(
slice_last(activation, 0))
pre = slice_last(activation, 1)
forget_gate = self.gate_activation.apply(
pre + self.bias * tensor.ones_like(pre))
next_cells = (
forget_gate * cells +
in_gate * self.activation.apply(slice_last(activation, 2)))
out_gate = self.gate_activation.apply(
slice_last(activation, 3))
next_states = out_gate * self.activation.apply(next_cells)
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
next_cells = (mask[:, None] * next_cells +
(1 - mask[:, None]) * cells)
return next_states, next_cells
@application(outputs=apply.states)
def initial_states(self, batch_size, *args, **kwargs):
return [tensor.repeat(self.initial_state_[None, :], batch_size, 0),
tensor.repeat(self.initial_cells[None, :], batch_size, 0)]