-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathproblem.py
More file actions
370 lines (331 loc) · 16 KB
/
problem.py
File metadata and controls
370 lines (331 loc) · 16 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
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
# python base imports
import os
import pydot
from itertools import combinations
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import warnings
import lightning.pytorch as pl
from typing import Dict, List, Callable
from inspect import signature
# machine learning/data science imports
import torch
import torch.nn as nn
class LitProblem(pl.LightningModule):
"""
A PyTorch-Lightning Module wrapper for the Neuromancer Problem class.
As is customary with LightningModules, steps for training and validation are outlined here, as well as the optimizer
Logging metrics are also defined here, such as 'train_loss'.
"""
# Class attrinbute for expected signatures of Lightning hooks
expected_signatures = {
'backward': '(self, loss)',
'on_before_backward': '(self, loss)',
'on_after_backward': '(self)',
'on_before_zero_grad': '(self, optimizer)',
'on_fit_start': '(self)',
'on_fit_end': '(self)',
'on_load_checkpoint': '(self, checkpoint)',
'on_save_checkpoint': '(self, checkpoint)',
'on_train_start': '(self)',
'on_train_end': '(self)',
'on_validation_start': '(self)',
'on_validation_end': '(self)',
'on_test_batch_start': '(self, batch, batch_idx, dataloader_idx)',
'on_test_batch_end': '(self, batch, batch_idx, dataloader_idx)',
'on_test_epoch_start': '(self)',
'on_test_epoch_end': '(self)',
'on_test_start': '(self)',
'on_test_end': '(self)',
'on_predict_batch_start': '(self, batch, batch_idx, dataloader_idx)',
'on_predict_batch_end': '(self, batch, batch_idx, dataloader_idx)',
'on_predict_epoch_start': '(self)',
'on_predict_epoch_end': '(self)',
'on_predict_start': '(self)',
'on_predict_end': '(self)',
'on_train_batch_start': '(self, batch, batch_idx)',
'on_train_batch_end': '(self, batch, batch_idx)',
'on_train_epoch_start': '(self)',
'on_train_epoch_end': '(self)',
'on_validation_batch_start': '(self, batch, batch_idx)',
'on_validation_batch_end': '(self, batch, batch_idx)',
'on_validation_epoch_start': '(self)',
'on_validation_epoch_end': '(self)',
'configure_model': '(self)'
}
def __init__(self, problem, train_metric='train_loss', dev_metric='train_loss', test_metric='train_loss', custom_optimizer=None,
custom_training_step=None, custom_hooks=None, hparam_config=None):
"""
:param problem: A Neuromancer Problem()
:param train_metric: metric to be used during training step. Default to train_loss
:param dev_metric: metric to be used during validation step. Default to train_loss
:param test_metric: metric to be used during testing step (currently not supported yet)
:param custom_optimizer: Optimizer to be used during training. Default is None, in which an
Adam optimizer is used with learning rate = 0.001
:param custom_training_step: Custom training step function, if desired. Defaults to None, in which case the standard training step procedure is executed
:param custom_hooks: Dictionary of custom hook functions that are supported by Lightning. Defaults to None.
:param hparam_config: A wandb hyperparameter configuration file. Only used for hyperparameter tuning.
"""
super().__init__()
self.problem = problem
self.train_metric = train_metric
self.dev_metric = dev_metric
self.test_metric = test_metric
self.custom_optimizer = custom_optimizer
self.custom_training_step = custom_training_step
self.custom_hooks = custom_hooks or {}
self.hparam_config = hparam_config
self.lr = .001
self.training_step_outputs = []
self.validation_step_outputs = []
self._load_from_config()
self._validate_hooks()
def _load_from_config(self):
if self.hparam_config:
if "learning_rate" in self.hparam_config:
self.lr = self.hparam_config.learning_rate
def _validate_hooks(self):
for hook_name, hook_func in self.custom_hooks.items():
if hook_name in self.expected_signatures:
expected_sig = self.expected_signatures[hook_name]
actual_sig = str(signature(hook_func))
if actual_sig != expected_sig:
raise ValueError(f"Custom hook '{hook_name}' has incorrect signature: expected {expected_sig}, got {actual_sig}")
def training_step(self, batch, batch_idx):
if self.custom_training_step is not None:
loss = self.custom_training_step(self, batch)
else:
output = self.problem(batch)
loss = output[self.train_metric]
self.training_step_outputs.append(loss)
self.log('train_loss', loss, on_epoch=True, enable_graph=True, prog_bar=True)
return loss
def on_train_epoch_end(self):
if 'on_train_epoch_end' in self.custom_hooks:
self.custom_hooks['on_train_epoch_end'](self)
else:
epoch_average = torch.stack(self.training_step_outputs).mean()
self.log("training_epoch_average", epoch_average) #log to lightning_logs
self.training_step_outputs.clear()
def validation_step(self, batch, batch_idx):
if 'validation_step' in self.custom_hooks:
self.custom_hook['validation_step'](self, batch, batch_idx)
else:
output = self.problem(batch)
loss = output[self.dev_metric]
self.validation_step_outputs.append(loss)
self.log('dev_loss', loss, prog_bar=True)
def configure_optimizers(self):
if 'configure_optimizers' in self.custom_hooks:
self.custom_hooks['configure_optimizers'](self)
else:
if self.custom_optimizer is None:
optimizer = torch.optim.Adam(self.problem.parameters(), self.lr, betas=(0.0, 0.9))
else:
optimizer = self.custom_optimizer
return optimizer
def get_problem(self):
return self.problem
class Problem(nn.Module):
"""
This class is similar in spirit to a nn.Sequential module. However,
by concatenating input and output dictionaries for each node
module we can represent arbitrary directed acyclic computation graphs.
In addition the Problem module takes care of calculating loss functions
via given instantiated weighted multi-objective PenaltyLoss object which
calculate objective and constraints terms from aggregated input and set
of outputs from the node modules.
"""
def __init__(self, nodes: List[Callable[[Dict[str, torch.Tensor]], Dict[str, torch.Tensor]]],
loss: Callable[[Dict[str, torch.Tensor]], Dict[str, torch.Tensor]],
grad_inference=False, check_overwrite=False):
"""
:param nodes: (List[Node]) list of objects which implement the Node interface
(i.e. input and output are dicts of Tensors and
object has input_keys, output_keys, and name attributes)
:param loss: (PenaltyLoss) instantiated loss class
:param update: (Callable) problem will update the output dictionary and return new dictionary with the same keys
but updated values. Example includes projected gradient method.
:param grad_inference: (boolean) flag for enabling computation of grdients during inference time, useful for techniques like projected gradient
"""
super().__init__()
self.nodes = nn.ModuleList(nodes)
self.loss = loss
self.grad_inference = grad_inference
self.check_overwrite = check_overwrite
self._check_keys()
self.problem_graph = self.graph()
def _check_keys(self):
keys = set()
for node in list(self.nodes)+[self.loss]:
keys |= set(node.input_keys)
new_keys = set(node.output_keys)
same = new_keys & keys
if self.check_overwrite:
if len(same) != 0:
warnings.warn(f'Keys {same} are being overwritten by the node {node}.')
keys |= new_keys
def _check_unique_names(self):
num_unique = len(set([o.name for o in self.loss.objectives] + [c.name for c in self.loss.constraints]
+ [comp.name for comp in self.nodes]))
num_obj = len(self.loss.objectives) + len(self.loss.constraints) + len(self.nodes)
assert num_unique == num_obj, \
"All nodes, objectives and constraints must have unique names to construct a computational graph."
def forward(self, data: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
output_dict = self.step(data)
output_dict = self.loss(output_dict)
if isinstance(output_dict, torch.Tensor):
output_dict = {self.loss.name: output_dict}
return {f'{data["name"]}_{k}': v for k, v in output_dict.items()}
def step(self, input_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]:
for node in self.nodes:
output_dict = node(input_dict)
if isinstance(output_dict, torch.Tensor):
output_dict = {node.name: output_dict}
input_dict = {**input_dict, **output_dict}
return input_dict
def graph(self, include_objectives=True):
self._check_unique_names()
graph = pydot.Dot("problem", graph_type="digraph", splines="spline", rankdir="LR")
graph.add_node(pydot.Node("in", label="dataset", color='skyblue',
style='filled', shape="box"))
graph.add_node(pydot.Node("out", label="loss", color='lightcoral',
style='filled', shape="box"))
# plot clusters for nodes and loss terms
node_cluster = pydot.Cluster('nodes', color='cornsilk',
style='filled', label='nodes')
obj_cluster = pydot.Cluster('loss_term', color='cornsilk',
style='filled', label='loss terms')
# create nodes in the node cluster
input_keys = []
output_keys = []
nonames = 1
for idx, node in enumerate(self.nodes):
input_keys += node.input_keys
output_keys += node.output_keys
if node.name is None or node.name == '':
node.name = f'node_{nonames}'
nonames += 1
node_cluster.add_node(pydot.Node(node.name, color='lavender', style='filled',
label=node.name, shape="box"))
graph.add_subgraph(node_cluster)
# get keys of recurrent nodes
loop_keys = []
for node in self.nodes:
loop_keys += set(node.input_keys) & set(node.output_keys)
# build node connections in reverse order
reverse_order_nodes = self.nodes[::-1]
for idx_dst, dst in enumerate(reverse_order_nodes):
src_nodes = reverse_order_nodes[1+idx_dst:]
unique_common_keys = set()
for idx_src, src in enumerate(src_nodes):
common_keys = set(src.output_keys) & set(dst.input_keys)
for key in common_keys:
if key not in unique_common_keys:
graph.add_edge(pydot.Edge(src.name, dst.name, label=key))
unique_common_keys.add(key)
# get keys required as input and to initialize some nodes
init_keys = set(input_keys) - (set(output_keys)-set(loop_keys))
# get keys required as input to nodes from the dataset
data_keys = set(input_keys)-set(output_keys)
# create input connections to the dataset if not provided by previous node
previous_output_keys = []
for node in self.nodes:
for key in set(node.input_keys) & (init_keys-set(previous_output_keys)):
graph.add_edge(pydot.Edge("in", node.name, label=key))
previous_output_keys += node.output_keys
# add objectives and constraints in the graph
if include_objectives:
# get keys required as input to objectives from the dataset
obj_input_keys = []
for i, obj in enumerate(self.loss.objectives + self.loss.constraints):
obj_input_keys += obj.input_keys
obj_data_keys = set(obj_input_keys) - set(output_keys)
# create connections
for i, obj in enumerate(self.loss.objectives+self.loss.constraints):
# choose different colors for objective terms and constraints
if i+1 <= len(self.loss.objectives):
color = "lightpink"
else:
color = 'thistle'
# add loss term boxes
obj_cluster.add_node(pydot.Node(obj.name, label=obj.name,
shape="box", color=color, style='filled'))
# connect nodes to loss terms
unique_common_keys = set()
for node in reverse_order_nodes:
common_keys = set(node.output_keys) & set(obj.input_keys)
for key in common_keys:
if key not in unique_common_keys:
graph.add_edge(pydot.Edge(node.name, obj.name, label=key))
unique_common_keys.add(key)
# generate tuples connecting input data to loss terms
for key in obj_data_keys:
if key in obj.input_keys:
graph.add_edge(pydot.Edge("in", obj.name, label=key))
graph.add_edge(pydot.Edge(obj.name, "out", label=obj.name))
graph.add_subgraph(obj_cluster)
else:
# aggregate outputs in a single output node
for node in self.nodes:
for key in set(node.output_keys) & set(self.loss.input_keys):
graph.add_edge(pydot.Edge("out", node.name, label=key))
for key in data_keys & set(self.loss.input_keys):
graph.add_edge(pydot.Edge("in", "out", label=key))
input_keys += self.loss.input_keys
self.input_keys = list(set(input_keys))
output_keys += self.loss.output_keys
self.output_keys = list(set(output_keys))
return graph
def show(self, figname=None):
graph = self.graph()
if figname is not None:
plot_func = {'svg': graph.write_svg,
'png': graph.write_png,
'jpg': graph.write_jpg}
ext = figname.split('.')[-1]
plot_func[ext](figname)
else:
graph.write_png('problem_graph.png')
img = mpimg.imread('problem_graph.png')
os.remove('problem_graph.png')
plt.figure()
fig = plt.imshow(img, aspect='equal')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.show()
def freeze(self):
"""
Freezes the parameters of all nodes in the system
"""
for node in self.nodes:
node.freeze()
def unfreeze(self):
"""
Unfreezes the parameters of all nodes in the system
"""
for node in self.nodes:
node.unfreeze()
def __repr__(self):
s = "### MODEL SUMMARY ###\n\nNODES:"
if len(self.nodes) > 0:
for c in self.nodes:
s += f"\n {repr(c)}"
s += "\n"
else:
s += " none\n"
s += "\nCONSTRAINTS:"
if len(self.loss.constraints) > 0:
for c in self.loss.constraints:
s += f"\n {repr(c)}"
s += "\n"
else:
s += " none\n"
s += "\nOBJECTIVES:"
if len(self.loss.objectives) > 0:
for c in self.loss.objectives:
s += f"\n {repr(c)}"
s += "\n"
else:
s += " none\n"
return s