forked from kajanan1212/tl-prospective-configuration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathany_energy_trainable_legacy.py
361 lines (266 loc) · 11 KB
/
any_energy_trainable_legacy.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from __future__ import print_function
import copy
import pprint
import torch
import numpy as np
import matplotlib.pyplot as plt
import utils
import analysis_utils
from dataset_learning_trainable import DatasetLearningTrainable
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch import matmul
class AnyEnergyTrainable(DatasetLearningTrainable):
"""AnyEnergyTrainable.
Manage:
- any energy model.
"""
def setup(self, config):
super(AnyEnergyTrainable, self).setup(config)
exec(self.config.get("before_AnyEnergyTrainable_setup_code", "pass"))
# config
self.energy_fn = self.config["energy_fn"]
self.is_with_negative_phase = self.config["is_with_negative_phase"]
self.connectivity = self.config["connectivity"]
self.inference_duration = self.config["inference_duration"]
self.batch_size = self.config["batch_size"]
# network structure
self.ns = eval(self.config["ns"])
if self.connectivity.split(' ')[0] == 'Layered':
pass
elif self.connectivity.split(' ')[0] == 'Fully-connected':
# a fully-connected network with each neuron connected to each of other xs and itself
# the initialization of ns is describing the weight matrix
self.ns = [sum(self.ns), sum(self.ns)]
else:
raise NotImplementedError
self.l_start = 0
self.l_end = len(self.ns) - 1
# Ws
# # create Ws
self.Ws = {}
for l in range(self.l_start, self.l_end):
self.Ws[l] = nn.Parameter(
torch.FloatTensor(
self.ns[l], self.ns[l + 1]
).to(self.device)
)
torch.nn.init.xavier_normal_(self.Ws[l])
# # create Ws_backward
self.Ws_backward = {}
if self.connectivity.split(' ')[0] == 'Layered':
# # # only layered network applies Ws_backward
if self.connectivity.split(' ')[1] == 'Recurrent':
# # # only recurrent layered network applies Ws_backward
for l in range(self.l_start, self.l_end):
if self.connectivity.split(' ')[2] == 'Asymmetric':
# independent backward connection
self.Ws_backward[l] = nn.Parameter(
torch.FloatTensor(
self.ns[l], self.ns[l + 1]
).to(self.device)
)
torch.nn.init.xavier_normal_(self.Ws_backward[l])
elif self.connectivity.split(' ')[2] == 'Symmetric':
# backward connection is the same as the forward connection
self.Ws_backward[l] = self.Ws[l]
else:
# no backward connection
pass
# # initialize Ws
exec(self.config.get("init_code", "pass"))
# # init is a kind of update
self.after_Ws_updated()
# # create optimizer for Ws
self.optimizer_learning = eval(self.config['optimizer_learning_fn'])(
list(self.Ws.values()) + list(self.Ws_backward.values()),
**self.config['optimizer_learning_kwargs']
)
# create xs
self.xs = {}
for l in range(self.l_start, self.l_end + 1):
self.xs[l] = nn.Parameter(
torch.FloatTensor(
self.batch_size, self.ns[l]
).normal_(0.0, 1.0).to(self.device)
)
# # for fully-connected network
if self.connectivity.split(' ')[0] == 'Fully-connected':
# # # for fully-connected network, the second layer of xs are not needed (as it is the first layer of xs themselves)
self.xs.pop(1)
assert len(self.xs) == 1
assert list(self.xs.keys())[0] == 0
# # create optimizer for xs
self._create_optimizer_inference()
exec(self.config.get("after_AnyEnergyTrainable_setup_code", "pass"))
def _create_optimizer_inference(self):
"""(Re)create optimizer for xs (inference).
"""
self.optimizer_inference = eval(self.config['optimizer_inference_fn'])(
list(self.xs.values()),
**self.config['optimizer_inference_kwargs']
)
def compute_energy(self):
"""Compute energy.
"""
energy = []
# energy from forward direction
for l in range(self.l_start, self.l_end):
# x_pre, x_post and w
x_pre = self.xs[l]
if self.connectivity.split(' ')[0] == 'Layered':
# for layered network, x_post is the x in the next layer
x_post = self.xs[l + 1]
elif self.connectivity.split(' ')[0] == 'Fully-connected':
# for fully-connected network, x_post and x_pre are the same set of xs
x_post = x_pre
else:
raise NotImplementedError
w = self.Ws[l]
energy.append(
eval(self.energy_fn)
)
# energy from backward direction for layered-structured network
if self.connectivity.split(' ')[0] == 'Layered':
if self.connectivity.split(' ')[1] == 'Recurrent':
for l in reversed(range(self.l_start, self.l_end)):
# x_pre, x_post and w
x_pre = self.xs[l + 1]
x_post = self.xs[l]
w = self.Ws_backward[l].t()
energy.append(
eval(self.energy_fn)
)
return sum(energy)
def clamp_xs(self, clamp):
"""Clamp input or/and output xs to self.s_in or/and self.s_out.
"""
assert isinstance(clamp, list)
if 's_in' in clamp:
self.get_xs_input().data.copy_(self.s_in)
if 's_out' in clamp:
self.get_xs_output().data.copy_(self.s_out)
def get_xs_input(self):
"""Get xs that is considered to be input xs.
"""
if self.connectivity.split(' ')[0] == 'Layered':
return self.xs[self.l_start]
elif self.connectivity.split(' ')[0] == 'Fully-connected':
return self.xs[0][:, :self.s_in.size(1)]
else:
raise NotImplementedError
def get_xs_output(self):
"""Get xs that is considered to be output xs.
"""
if self.connectivity.split(' ')[0] == 'Layered':
return self.xs[self.l_end]
elif self.connectivity.split(' ')[0] == 'Fully-connected':
return self.xs[0][:, -self.s_out.size(1):]
else:
raise NotImplementedError
def get_error(self):
"""Get error between self.s_out and output xs.
"""
return (self.get_xs_output() - self.s_out).pow(2).sum() * 0.5
def _multiply_inference_rate(self, multiplier):
for param_group_i in range(len(self.optimizer_inference.param_groups)):
self.optimizer_inference.param_groups[param_group_i][
'lr'
] = self.optimizer_inference.param_groups[param_group_i][
'lr'
] * multiplier
def _multiply_learning_rate(self, multiplier):
for param_group_i in range(len(self.optimizer_learning.param_groups)):
self.optimizer_learning.param_groups[param_group_i][
'lr'
] = self.optimizer_learning.param_groups[param_group_i][
'lr'
] * multiplier
def inference(self, clamp):
"""Update xs.
"""
# every inference in an independent optimization problem
# thus, starts over
self._create_optimizer_inference()
self.clamp_xs(clamp)
last_energy = None
for inference_i in range(self.config['inference_duration']):
# update xs
self.optimizer_inference.zero_grad()
energy = self.compute_energy()
# control inference rate
if last_energy is not None:
if energy < last_energy:
# amplify learning rate of x if energy does decrease during inference
self._multiply_inference_rate(
self.config['inference_rate_amplifier']
)
else:
# discount learning rate of x if energy does NOT decrease during inference
self._multiply_inference_rate(
self.config['inference_rate_discount']
)
last_energy = energy
energy.backward()
self.optimizer_inference.step()
self.clamp_xs(clamp)
def learning(self, is_negative):
"""Update Ws.
"""
last_energy = None
for learn_i in range(self.config['learning_duration']):
# update Ws
self.optimizer_learning.zero_grad()
energy = self.compute_energy()
# control learning rate
if last_energy is not None:
if energy < last_energy:
# amplify learning rate of x if energy does decrease during learning
self._multiply_learning_rate(
self.config['learning_rate_amplifier']
)
else:
# discount learning rate of x if energy does NOT decrease during learning
self._multiply_learning_rate(
self.config['learning_rate_discount']
)
last_energy = energy
if is_negative:
(-energy).backward()
else:
energy.backward()
self.optimizer_learning.step()
self.after_Ws_updated()
def after_Ws_updated(self):
"""Applied every time Ws gets updated.
"""
if self.connectivity.split(' ')[0] == 'Fully-connected':
if self.connectivity.split(' ')[1] == 'None-self-recurrent':
self.Ws[0].data.fill_diagonal_(0.0)
if self.connectivity.split(' ')[2] == 'Symmetric':
self.Ws[0].data = (
self.Ws[0].data + self.Ws[0].data.t()
) / 2.0
def iteration_step(
self,
data_pack_key,
batch_idx,
batch,
do_key,
):
# unpack batch
self.s_in, self.s_out = batch
if do_key == 'predict':
# inference with only input clamped
self.inference(clamp=['s_in'])
if self.is_with_negative_phase:
# with negative phase, Ws is updated to increase the energy
self.learning(is_negative=True)
elif do_key == 'learn':
# inference with both input and output clamped
self.inference(clamp=['s_in', 's_out'])
# Ws is updated to decrease the energy
self.learning(is_negative=False)
else:
raise NotImplementedError