forked from YuhangSong/Prospective-Configuration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathany_energy_trainable.py
executable file
·443 lines (341 loc) · 13.9 KB
/
any_energy_trainable.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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
import utils as u
logger = u.getLogger(__name__)
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"))
logger.warning("HandCodedRulesTrainable is not maintained anymore.")
# config
self.energy_fn = {
"both": self.config.get("energy_fn_both", self.config.get("energy_fn", None)),
"inference": self.config.get("energy_fn_inference", self.config.get("energy_fn", None)),
"learning": self.config.get("energy_fn_learning", self.config.get("energy_fn", None)),
}
self.learning_do = eval(
self.config.get(
"learning_do", "['inference','learning']"
)
)
self.is_manual_xs_dynamic = self.config.get(
"is_manual_xs_dynamic", False
)
self.is_with_negative_phase = self.config["is_with_negative_phase"]
self.connectivity = self.config["connectivity"]
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_original = copy.deepcopy(self.ns)
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
if self.config.get("init_code", None) is not None:
raise Exception("init_code has been renamed to init_Ws_code")
exec(self.config.get("init_Ws_code", "pass"))
# # init is a kind of update
self.clamp_Ws()
# # 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)
)
exec(self.config.get("init_xs_code", "pass"))
# # 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_energies(self, phase):
"""Compute energies, returns a list of energies of different layers.
"""
energies = []
# energies 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]
energies.append(
eval(self.energy_fn[phase])
)
# energies 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()
energies.append(
eval(self.energy_fn[phase])
)
return torch.stack(energies)
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
# debug
# print(self.optimizer_inference.param_groups[param_group_i][
# 'lr'
# ])
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 step_dynamic(self, do_key, clamp, is_negative=False):
"""Update xs.
"""
is_manual_xs = (
(
do_key in ['inference', 'both']
) and (
self.is_manual_xs_dynamic
)
)
is_optimize_xs = (
(
do_key in ['inference', 'both']
) and (
not self.is_manual_xs_dynamic
)
)
is_optimize_Ws = (
do_key in ['learning', 'both']
)
is_optimize_anything = (
is_optimize_xs or is_optimize_Ws
)
if is_optimize_anything:
if is_optimize_xs:
# every inference in an independent optimization problem
# thus, starts over
self._create_optimizer_inference()
if do_key == 'inference':
duration = self.config['inference_duration']
elif do_key == 'learning':
duration = self.config['learning_duration']
elif do_key == 'both':
duration = self.config['both_duration']
else:
raise NotImplementedError
self.clamp_xs(clamp)
self.clamp_Ws()
if is_optimize_anything:
last_energy = None
energies_history = []
for step_i in range(duration):
if is_manual_xs:
exec(self.config.get("manual_xs_dynamic_code", "pass"))
self.clamp_xs(clamp)
if is_optimize_anything:
if is_optimize_xs:
self.optimizer_inference.zero_grad()
if is_optimize_Ws:
self.optimizer_learning.zero_grad()
energies_history.append(
self.compute_energies(phase=do_key)
)
energy = energies_history[-1].sum()
# control inference rate
if last_energy is not None:
if energy < last_energy:
if is_optimize_xs:
# amplify learning rate of x if energy does decrease during inference
self._multiply_inference_rate(
self.config['inference_rate_amplifier']
)
if is_optimize_Ws:
# amplify learning rate of x if energy does decrease during learning
self._multiply_learning_rate(
self.config['learning_rate_amplifier']
)
else:
if is_optimize_xs:
# discount learning rate of x if energy does NOT decrease during inference
self._multiply_inference_rate(
self.config['inference_rate_discount']
)
if is_optimize_Ws:
# 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()
if is_optimize_xs:
self.optimizer_inference.step()
self.clamp_xs(clamp)
if is_optimize_Ws:
self.optimizer_learning.step()
self.clamp_Ws()
# debug
# input(energy.item())
# debug
# input(energy.item())
# input(clamp)
if is_optimize_anything:
return energy, torch.stack(energies_history)
def clamp_Ws(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
exec(self.config.get("after_clamp_Ws", "pass"))
def iteration_step(
self,
batch,
do_key,
data_pack_key=None,
batch_idx=None,
):
# unpack batch
self.s_in, self.s_out = batch
if do_key == 'predict':
# inference with only input clamped
self.step_dynamic(
do_key='inference',
clamp=['s_in'],
is_negative=False,
)
self.prediction = self.get_xs_output().data.clone()
if self.is_with_negative_phase:
# with negative phase, Ws is updated to increase the energy
self.step_dynamic(
do_key='learning',
clamp=['s_in'],
is_negative=True,
)
elif do_key == 'learn':
for learning_do_key in self.learning_do:
self.step_dynamic(
do_key=learning_do_key,
clamp=['s_in', 's_out'],
is_negative=False,
)
else:
exec(self.config.get("iteration_step_else", "pass"))