-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayers.py
More file actions
313 lines (231 loc) · 11 KB
/
layers.py
File metadata and controls
313 lines (231 loc) · 11 KB
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
"""
Torch layers and functions for WeightLearningNetwork.
Includes a normal linear layer (adapted for our network structure) and a layer performing feedback
alignment. We cannot use the standard torch layers because due to mutations, our network structure
is very different from a traditional, densely connected network.
"""
from torch.autograd import Function
import torch.nn as nn
import torch
class LinearFunction(Function):
r"""Implementation of a fully-connected layer w/o activation function.
Copied from https://git.ee.ethz.ch/henningc/teaching
This class is a ``Function`` that behaves just like PyTorch's class
:class:`torch.nn.Linear`. Since this class implements the interface
:class:`torch.autograd.Function`, we can use it to specify a custom
backpropagation behavior.
In this specific case, the ``Function`` shall behave just as in classic
backpropagation (i.e., it shall behave identical to the proprietory PyTorch
implementation).
Assuming column vectors: layer input :math:`\mathbf{a} \in \mathbb{R}^K`,
a weight matrix :math:`W \in \mathbb{R}^{N \times K}`, and a masks matrix
:math:`M \in \mathbb{R}^{N \times K}` this layer simply computes
.. math::
:label: eq-single-sample
\mathbf{z} = W \mathbf{a}
Note, since we want to process mini-batches (containing :math:`B` samples
each), the input to the :meth:`forward` method is actually a set of samples
:math:`\mathbf{a}` collected into a matrix
:math:`A \in \mathbb{R}^{B \times K}`.
The mathematical operation described for single samples in eq.
:eq:`eq-single-sample`, is stated for a the case of mini-batches below
.. math::
:label: eq-mini-batch
Z = A W^T
where :math:`Z \in \mathbb{R}^{B \times N}` is the output matrix.
"""
@staticmethod
def forward(ctx, A, W, M):
r"""Compute the output of a linear layer.
This method implements eq. :eq:`eq-mini-batch`.
Args:
ctx: A context. Should be used to store activations which are needed
in the backward pass.
A: A mini-batch of input activations :math:`A`.
W: The weight matrix :math:`W`.
M: The weight masks :math:`M`.
Returns:
The output activations :math:`Z` as defined by eq.
:eq:`eq-mini-batch`.
"""
ctx.save_for_backward(A, W, M)
Z = torch.mm(A, W.t())
return Z
@staticmethod
def backward(ctx, grad_Z):
r"""Backpropagate the gradients of :math:`Z` through this linear layer.
The matrix ``grad_Z``, which we denote by
:math:`\delta_Z \in \mathbb{R}^{B \times N}`, contains the partial
derivatives of the scalar loss function with respect to each element
from the :meth:`forward` output matrix :math:`Z`.
This method backpropagates the global error (encoded in
:math:`\delta_Z`) to the input tensors of the :meth:`forward` method,
essentially computing :math:`\delta_A` and :math:`\delta_W`. Note that
:math:`\delta_W` is then multiplied by the weight masks :math:`M`,
essentially performing a backward hook on the gradient of :math:`W`.
These partial derivatives can be computed as follows:
.. math::
\delta_A &= \delta_Z W \\
\delta_W &= (\delta_Z^T A) \odot M \\
where :math:`\delta_{Z_{b,:}}` denotes the vector retrieved from the
:math:`b`-th row of :math:`\delta_Z`.
Args:
ctx: See description of argument ``ctx`` of method :meth:`forward`.
grad_Z: The backpropagated error :math:`\delta_Z`.
Returns:
(tuple): Tuple containing:
- **grad_A**: The derivative of the loss with respect to the input
activations, i.e., :math:`\delta_A`.
- **grad_W**: The derivative of the loss with respect to the weight
matrix, i.e., :math:`\delta_W`.
- **grad_M**: The derivative of the loss with respect to the masks.
As the masks are never learned, it is always set to None.
.. note::
Gradients for input tensors are only computed if their keyword
``requires_grad`` is set to ``True``, otherwise ``None`` is
returned for the corresponding Tensor.
"""
A, W, M = ctx.saved_tensors
grad_A = None
grad_W = None
grad_M = None
# We only need to compute gradients for tensors that are flagged to
# require gradients!
if ctx.needs_input_grad[0]:
grad_A = grad_Z.mm(W)
if ctx.needs_input_grad[1]:
grad_W = (grad_Z.t().mm(A)) * M
return grad_A, grad_W, grad_M
class LinearFunctionFA(Function):
r"""Implementation of a fully-connected layer w/o activation function which
implements Feedback Alignment in the feedback path.
Note that, as opposed to the classical Feedback Alignement formalization,
we consider B (and not B.t() has the dimensions of W). This is for
simplicity when setting symmetric connections and other manipulations.
See :class:`LinearFunction` for further details.
Args:
(...): See :class:`LinearFunction` for further details.
B: The feedback weights :math:`B`.
"""
@staticmethod
def forward(ctx, A, W, B, M):
r"""Compute the output of a linear layer.
See :meth:`LinearFunction.forward` for further details.
"""
ctx.save_for_backward(A, W, B, M)
Z = torch.mm(A, W.t())
return Z
@staticmethod
def backward(ctx, grad_Z):
r"""Propagate the gradients of :math:`Z` through this linear layer
according to the Feedback Alignment method.
The partial derivatives can be computed as follows:
.. math::
\delta_A &= \delta_Z B \\
\delta_W &= (\delta_Z^T A) \odot M \\
where :math:`\delta_{Z_{b,:}}` denotes the vector retrieved from the
:math:`b`-th row of :math:`\delta_Z`.
Args:
(...): See :meth:`LinearFunction.backward` for further details.
Returns:
(tuple): See :meth:`LinearFunction.backward` for further details.
"""
A, W, B, M = ctx.saved_tensors
grad_A = None
grad_W = None
grad_B = None
grad_M = None
# We only need to compute gradients for tensors that are flagged to
# require gradients!
if ctx.needs_input_grad[0]:
grad_A = grad_Z.mm(B)
if ctx.needs_input_grad[1]:
grad_W = (grad_Z.t().mm(A)) * M
return grad_A, grad_W, grad_B, grad_M
class LinearLayer(nn.Module):
"""Wrapper for ``Function`` :class:`learning.LinearFunction`.
The interface is inspired by the implementation of class
:class:`torch.nn.Linear`.
Note that it is assumed here that matrix representations are compact.
Attributes:
weight_matrix (torch.Tensor): The weight matrix :math:`W` of the layer.
weight_mask (torch.Tensor): The weight masks :math:`M` of the layer. These
are zero if the weights don't exist in the network and should not
be updated in the backward pass.
from_neurons (list): The indices of neurons having connections to this layers.
to_neurons (list): The indices of neurons in this layer.
"""
def __init__(self, weight_matrix, weight_mask, from_neurons=None, to_neurons=None):
"""
Initialize LinearLayer from existing weight matrix.
Args:
Same as attributes above.
"""
nn.Module.__init__(self)
self.weight_matrix = weight_matrix
self.weight_mask = weight_mask
if from_neurons is None:
from_neurons = list(range(weight_matrix.shape[1]))
self.from_neurons = from_neurons
if to_neurons is None:
to_neurons = list(range(weight_matrix.shape[0]))
self.to_neurons = to_neurons
def forward(self, input):
"""Compute the output activation of a linear layer.
This method simply applies the
:class:`learning.LinearFunction` ``Function`` using the internally
maintained weights.
Args:
input: See description of argument ``A`` of method
:meth:`learning.LinearFunction.forward`.
Returns:
See return value of method
:meth:`lib.learning.LinearFunction.forward`.
"""
return LinearFunction.apply(input, self.weight_matrix, self.weight_mask)
def get_weight(self, from_neuron, to_neuron):
"""Return the weight of the connection between from_neuron and to_neuron."""
return self.weight_matrix[self.to_neurons.index(to_neuron),
self.from_neurons.index(from_neuron)].item()
def get_grad(self, from_neuron, to_neuron):
"""Return the gradient of the connection between from_neuron and to_neuron."""
return self.weight_matrix.grad[self.to_neurons.index(to_neuron),
self.from_neurons.index(from_neuron)].item()
class LinearLayerFA(LinearLayer):
"""Wrapper for ``Function`` :class:`learning.LinearFunctionFA`.
The interface is inspired by the implementation of class
:class:`torch.nn.Linear`.
Attributes:
weight_matrix (torch.Tensor): The weight matrix :math:`W` of the layer.
backward_weight_matrix (torch.Tensor): The backward weight matrix :math:`B` of the layer.
weight_mask (torch.Tensor): The weight masks :math:`M` of the layer. These
are zero if the weights don't exist in the network and should not
be updated in the backward pass.
from_neurons (list): The neurons having (ff or fb) connections to this layer.
to_neurons (list): The neurons in this layer.
"""
def __init__(self, weight_matrix, backward_weight_matrix, weight_mask, from_neurons=None,
to_neurons=None,):
"""
Initialize LinearLayerFA from existing weight matrix and backward weight matrix.
Args:
See attributes above.
"""
super().__init__(weight_matrix, weight_mask, from_neurons=from_neurons,
to_neurons=to_neurons)
self.backward_weight_matrix = backward_weight_matrix
assert self.backward_weight_matrix.shape == weight_matrix.shape
def forward(self, input):
"""Compute the output activation of a linear layer.
This method simply applies the
:class:`learning.LinearFunctionFA` ``Function`` using the internally
maintained feedforward and feedback weights.
Args:
input: See description of argument ``A`` of method
:meth:`learning.LinearFunctionFA.forward`.
Returns:
See return value of method
:meth:`lib.learning.LinearFunctionFA.forward`.
"""
return LinearFunctionFA.apply(input, self.weight_matrix, self.backward_weight_matrix,
self.weight_mask)