-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrc.py
247 lines (206 loc) · 8.39 KB
/
brc.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
import torch
import torch.nn as nn
import torch.nn.init as init
class BRCLayer(nn.Module):
"""
Recurrent Neural Network (single layer) using the Bistable Recurrent Cell
(see arXiv:2006.05252).
"""
def __init__(self, input_size, hidden_size):
"""
Arguments
---------
- intput_size: int
Input size for each element of the sequence
- hidden_size: int
Hidden state size
"""
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
# Forget gate
U_c = init.xavier_uniform_(torch.empty(hidden_size, input_size))
self.U_c = nn.Parameter(U_c)
self.w_c = nn.Parameter(init.normal_(torch.empty(hidden_size)))
self.b_c = nn.Parameter(init.normal_(torch.empty(hidden_size)))
# Reset gate
U_a = init.xavier_uniform_(torch.empty(hidden_size, input_size))
self.U_a = nn.Parameter(U_a)
self.w_a = nn.Parameter(init.normal_(torch.empty(hidden_size)))
self.b_a = nn.Parameter(init.normal_(torch.empty(hidden_size)))
# Hidden state
U_h = init.xavier_uniform_(torch.empty(hidden_size, input_size))
self.U_h = nn.Parameter(U_h)
self.b_h = nn.Parameter(init.normal_(torch.empty(hidden_size)))
def forward(self, x_seq, h):
"""
Compute the forward pass for the whole sequence.
Arguments
---------
- x_seq: tensor of shape (seq_len, batch_size, input_size)
Input sequence
- h: tensor of shape (batch_size, hidden_size)
The eventual initial hidden state at the moment of receiving the
input.
Returns
-------
- output: tensor of shape (seq_len, batch_size, hidden_size)
It contains the output of the last layer for all elements of the
input sequence
- hn: tensor of shape (batch_size, hidden_size)
Hidden state at the end of the sequence for all layers of the RNN
"""
assert h.size(0) == x_seq.size(1)
assert h.size(1) == self.hidden_size
assert x_seq.size(2) == self.input_size
seq_len = x_seq.size(0)
batch_size = x_seq.size(1)
y_seq = torch.empty(seq_len, batch_size, self.hidden_size,
device=x_seq.device)
for t in range(seq_len):
x = x_seq[t, :, :]
c = torch.sigmoid(torch.mm(x, self.U_c.T) + self.w_c * h +
self.b_c)
a = 1. + torch.tanh(torch.mm(x, self.U_a.T) + self.w_a * h +
self.b_a)
h = c * h + (1. - c) * torch.tanh(torch.mm(x, self.U_h.T) + a * h +
self.b_h)
y_seq[t, ...] = h
return y_seq, h
class nBRCLayer(nn.Module):
"""
Recurrent Neural Network (single layer) using the Recurrently
Neuromodulated Bistable Recurrent Cell (see arXiv:2006.05252).
"""
def __init__(self, input_size, hidden_size):
"""
Arguments
---------
- intput_size: int
Input size for each element of the sequence
- hidden_size: int
Hidden state size
"""
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
# Forget gate
U_c = init.xavier_uniform_(torch.empty(hidden_size, input_size))
self.U_c = nn.Parameter(U_c)
W_c = init.xavier_uniform_(torch.empty(hidden_size, hidden_size))
self.W_c = nn.Parameter(W_c)
self.b_c = nn.Parameter(init.normal_(torch.empty(hidden_size)))
# Reset gate
U_a = init.xavier_uniform_(torch.empty(hidden_size, input_size))
self.U_a = nn.Parameter(U_a)
W_a = init.xavier_uniform_(torch.empty(hidden_size, hidden_size))
self.W_a = nn.Parameter(W_a)
self.b_a = nn.Parameter(init.normal_(torch.empty(hidden_size)))
# Hidden state
U_h = init.xavier_uniform_(torch.empty(hidden_size, input_size))
self.U_h = nn.Parameter(U_h)
self.b_h = nn.Parameter(init.normal_(torch.empty(hidden_size)))
def forward(self, x_seq, h):
"""
Compute the forward pass for the whole sequence.
Arguments
---------
- x_seq: tensor of shape (seq_len, batch_size, input_size)
Input sequence
- h: tensor of shape (batch_size, hidden_size)
The eventual initial hidden state at the moment of receiving the
input.
Returns
-------
- output: tensor of shape (seq_len, batch_size, hidden_size)
It contains the output of the last layer for all elements of the
input sequence
- hn: tensor of shape (batch_size, hidden_size)
Hidden state at the end of the sequence for all layers of the RNN
"""
assert h.size(0) == x_seq.size(1)
assert h.size(1) == self.hidden_size
assert x_seq.size(2) == self.input_size
seq_len = x_seq.size(0)
batch_size = x_seq.size(1)
y_seq = torch.empty(seq_len, batch_size, self.hidden_size,
device=x_seq.device)
for t in range(seq_len):
x = x_seq[t, :, :]
c = torch.sigmoid(torch.mm(x, self.U_c.T) +
torch.mm(h, self.W_c.T) + self.b_c)
a = 1. + torch.tanh(torch.mm(x, self.U_a.T) +
torch.mm(h, self.W_a.T) + self.b_a)
h = c * h + (1. - c) * torch.tanh(torch.mm(x, self.U_h.T) +
a * h + self.b_h)
y_seq[t, ...] = h
return y_seq, h
class BRC(nn.Module):
"""
Recurrent Neural Network using the (Recurrently Neuromodulated) Bistable
Recurrent Cell (see arXiv:2006.05252), with several stacked (n)BRC.
"""
def __init__(self, input_size, hidden_size, num_layers,
neuromodulated=False, train_h0=False):
"""
Arguments
---------
- intput_size: int
Input size for each element of the sequence
- hidden_size: int
Hidden state size
- num_layers: int
Number of stacked RNNs
- neuromodulated: bool
Whether to use neuromodulation (i.e. NBRCLayer instead of BRCLayer)
- train_h0: bool
Whether to consider the initial hidden state as a parameter to
train instead of a fixed zero tensor
"""
super().__init__()
self.initial_hidden = torch.zeros(num_layers, hidden_size)
if train_h0:
self.initial_hidden = nn.Parameter(self.initial_hidden)
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
Layer = nBRCLayer if neuromodulated else BRCLayer
layers = [Layer(input_size, hidden_size)]
for _ in range(self.num_layers - 1):
layers.append(Layer(hidden_size, hidden_size))
self.layers = nn.ModuleList(layers)
def forward(self, x_seq, h0=None):
"""
Compute the forward pass for the whole sequence and along each layers.
Arguments
---------
- x: tensor of shape (seq_len, batch_size, input_size)
Input sequence
- h0: tensor of shape (num_layers, batch_size, hidden_size)
The eventual initial hidden state at the moment of receiving the
input.
Returns
-------
- output: tensor of shape (seq_len, batch_size, hidden_size)
It contains the output of the last layer for all elements of the
input sequence
- hn: tensor of shape (num_layers, batch_size, hidden_size)
Hidden state at the end of the sequence for all layers of the RNN
"""
batch_size = x_seq.size(1)
if h0 is None:
h0 = self.initial_hidden.unsqueeze(1).expand(-1, batch_size, -1)
hn = torch.empty(self.num_layers, batch_size, self.hidden_size,
device=x_seq.device)
for l, (layer, h0l) in enumerate(zip(self.layers, h0)):
x_seq, hnl = layer(x_seq, h0l)
hn[l, ...] = hnl
return x_seq, hn
class nBRC(BRC):
"""
Recurrent Neural Network using the Recurrently Neuromodulated Bistable
Recurrent Cell (see arXiv:2006.05252), with several stacked (n)BRC.
"""
def __init__(self, input_size, hidden_size, num_layers):
super().__init__(input_size, hidden_size, num_layers,
neuromodulated=True)