-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph2.py
More file actions
565 lines (467 loc) · 16.8 KB
/
Graph2.py
File metadata and controls
565 lines (467 loc) · 16.8 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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# Graph2.py
from abc import ABC, abstractmethod
from typing import Union, List, Tuple, Dict, Set, FrozenSet, Iterable, Any, \
NewType, Type, ClassVar, Sequence, Callable, Hashable, Collection, \
Sequence, Literal, Protocol, runtime_checkable
from dataclasses import dataclass, field, InitVar
import math
from itertools import chain
from collections import defaultdict
from inspect import isclass
from Propagator import Propagator, Delta
from FMTypes import epsilon
from util import as_iter, empty_set, first_non_none, unique_everseen, pr, pts
Node = Hashable
@dataclass(frozen=True)
class Hop:
'''A directed edge.'''
from_node: Node
to_node: Node
weight: float
def add_prefix(self, prefix: Hashable) -> 'Hop':
'''Returns a Hop identical to self but with the nodes replaced by
PrefixNode of .from_node and .to_node, with the given prefix.'''
return Hop(
PrefixedNode(prefix, self.from_node),
PrefixedNode(prefix, self.to_node),
self.weight
)
class Hops:
'''Just a scope for factory methods that make iterables to pass to
EnumEdges.'''
@classmethod
def from_dict(cls, weight=1.0, **kwargs) -> Iterable[Hop]:
for from_node, to_nodes in kwargs.items():
for to_node in as_iter(to_nodes):
yield Hop(from_node, to_node, weight)
@classmethod
def from_pairs(cls, *pairs: Tuple[Node, Node], weight=1.0) -> Iterable[Hop]:
for from_node, to_node in pairs:
yield Hop(from_node, to_node, weight)
class Query:
'''A query specification to pass to Nodes.query().'''
def unprefixed(self, prefix: Hashable) -> 'Query':
return self
class Nodes(ABC):
@abstractmethod
def has_node(self, x) -> bool:
pass
#TODO
# all_nodes()
# nodes(query)
@abstractmethod
#TODO type for q
def query(self, q) -> Iterable[Node]:
pass
def unprefixed(self, prefix: Hashable) -> 'Nodes':
'''Returns a version of this Nodes without a prefix. The default
implementation simply returns an empty EnumNodes. PrefixedNodes
overrides this method to return the base Nodes object.'''
return EnumNodes(set())
@dataclass(frozen=True)
class OfClass(Query):
cl: Type
@dataclass(frozen=True)
class WithPrefix(Query):
prefix: Hashable
q: Query
def unprefixed(self, prefix):
if prefix != self.prefix:
raise WrongPrefix
else:
return self.q
class WrongPrefix(Exception):
pass
@dataclass
class EnumNodes(Nodes):
nodeset: Set[Node] # TODO Container?
nodeclasses: Dict[Type, Set[Node]] = field(
init=False, default_factory=lambda: defaultdict(set)
)
def __post_init__(self):
for node in self.nodeset:
self.nodeclasses[node.__class__].add(node)
def has_node(self, x):
return x in self.nodeset
def query(self, q):
if isinstance(q, OfClass):
try:
yield from self.nodeclasses[q.cl]
except KeyError:
return
elif q is None:
yield from self.nodeset
else:
#TODO allow multiple nodes in q
if q in self.nodeset:
yield q
def __iter__(self):
return iter(self.nodeset)
def __len__(self):
return len(self.nodeset)
@dataclass
class NodesSeries(Nodes):
nodess: Sequence[Nodes]
def has_node(self, x):
return any(nodes.has_node(x) for nodes in self.nodess)
def query(self, q):
for nodes in self.nodess:
yield from nodes.query(q)
def unprefixed(self, prefix):
return NodesSeries([nodes.unprefixed(prefix) for nodes in self.nodess])
class Edges(ABC):
@abstractmethod
def hops_from_node(self, nodes: Nodes, x: Any) -> Iterable[Hop]:
pass
@abstractmethod
def hops_to_node(self, nodes: Nodes, x: Any) -> Iterable[Hop]:
pass
@abstractmethod
def find_hop(self, nodes: Nodes, from_node: Any, to_node: Any) \
-> Union[Hop, None]:
pass
@dataclass
class EnumEdges(Edges):
hops_from: Dict[Node, Dict[Node, Hop]]
hops_to: Dict[Node, Dict[Node, Hop]]
def __init__(self, *hopss: Iterable[Hop]):
self.hops_from = defaultdict(dict)
self.hops_to = defaultdict(dict)
for hops in hopss:
for hop in as_iter(hops):
self.hops_from[hop.from_node][hop.to_node] = hop
self.hops_to[hop.to_node][hop.from_node] = hop
def hops_from_node(self, nodes, x):
if not nodes.has_node(x):
return
try:
hd = self.hops_from[x]
except KeyError:
return
for hop in hd.values():
if nodes.has_node(hop.to_node):
yield hop
def hops_to_node(self, nodes, x):
if not nodes.has_node(x):
return
try:
hd = self.hops_to[x]
except KeyError:
return
for hop in hd.values():
if nodes.has_node(hop.from_node):
yield hop
def find_hop(self, nodes, from_node, to_node):
if not nodes.has_node(from_node) or not nodes.has_node(to_node):
return None
try:
return self.hops_from[from_node][to_node]
except KeyError:
return None
def __iter__(self):
for d in self.hops_from.values():
for hop in d.values():
yield hop
@dataclass
class MutualInhibition(Edges):
#TODO docstring
superclass: Union[Type, Tuple[Type]]
weight: float = -0.2
def hops_from_node(self, nodes, from_node):
if (
nodes.has_node(from_node)
and
isinstance(from_node, self.superclass)
):
for to_node in nodes.query(OfClass(from_node.__class__)):
if to_node != from_node:
yield Hop(from_node, to_node, self.weight)
def hops_to_node(self, nodes, to_node):
if (
nodes.has_node(to_node)
and
isinstance(to_node, self.superclass)
):
for from_node in nodes.query(OfClass(to_node.__class__)):
if from_node != to_node:
yield Hop(from_node, to_node, self.weight)
def find_hop(self, nodes, from_node, to_node):
if (
nodes.has_node(from_node)
and
nodes.has_node(to_node)
and
isinstance(from_node, self.superclass)
and
from_node.__class__ == to_node.__class__
and
from_node != to_node
):
return Hop(from_node, to_node, self.weight)
@dataclass
class EdgesSeries(Edges):
edgess: Sequence[Edges]
def hops_from_node(self, nodes: Nodes, x: Any) -> Iterable[Hop]:
return unique_everseen(chain.from_iterable(
edges.hops_from_node(nodes, x) for edges in self.edgess
))
def hops_to_node(self, nodes: Nodes, x: Any) -> Iterable[Hop]:
return unique_everseen(chain.from_iterable(
edges.hops_to_node(nodes, x) for edges in self.edgess
))
def find_hop(self, nodes: Nodes, from_node: Any, to_node: Any) \
-> Union[Hop, None]:
for edges in self.edgess:
hop = edges.find_hop(nodes, from_node, to_node)
if hop is not None:
return hop
return None
@dataclass
class Graph:
nodes: Nodes
edges: Edges
def has_node(self, x: Any) -> bool:
return self.nodes.has_node(x)
#TODO type for q
def query(self, q) -> Iterable[Node]:
return self.nodes.query(q)
def hops_from_node(self, x: Any) -> Iterable[Hop]:
return self.edges.hops_from_node(self.nodes, x)
def hops_to_node(self, x: Any) -> Iterable[Hop]:
return self.edges.hops_to_node(self.nodes, x)
# TODO UT
def degree_out(self, x: Any) -> int:
return len(list(self.hops_from_node(x)))
# TODO UT
def degree_in(self, x: Any) -> int:
return len(list(self.hops_to_node(x)))
def find_hop(self, from_node: Any, to_node: Any) -> Union[Hop, None]:
return self.edges.find_hop(self.nodes, from_node, to_node)
def successors_of(self, x: Any) -> Iterable[Node]:
return (hop.to_node for hop in self.edges.hops_from_node(self.nodes, x))
def predecessors_of(self, x: Any) -> Iterable[Node]:
return (hop.from_node for hop in self.edges.hops_to_node(self.nodes, x))
def hop_weight(self, from_node: Node, to_node: Node) -> float:
'''If either node does not exist, or there is no hop from from_node to
to_node, returns 0.0.'''
hop = self.find_hop(from_node, to_node)
if hop:
return hop.weight
else:
return 0.0
'''
try:
return self.find_hop(from_node, to_node).weight
except AttributeError:
return 0.0
'''
def add_edges(self, edges: Edges) -> 'Graph':
'''Returns a new Graph, containing the edges.'''
return Graph(nodes=self.nodes, edges=EdgesSeries([edges, self.edges]))
@classmethod
def empty(cls) -> 'Graph':
return cls(nodes=EnumNodes(set()), edges=EnumEdges())
@classmethod
def augment(cls, *graphs: 'Graph') -> 'Graph':
return Graph(
nodes=NodesSeries([g.nodes for g in graphs]),
edges=EdgesSeries([g.edges for g in graphs])
)
@classmethod
def OLDwith_features(cls, *base_nodess: Iterable[Node]) -> 'Graph':
'''Makes and returns a Graph containing all nodes in base_nodess,
as well as a node for each feature returned by features_of, and
a positive edge connecting each base node to its feature nodes.'''
nodeset = set()
hopset = set()
for base_node in chain.from_iterable(base_nodess):
nodeset.add(base_node)
for feature_node in features_of(base_node):
nodeset.add(feature_node)
hopset.add(Hop(base_node, feature_node, 1.0))
hopset.add(Hop(feature_node, base_node, 1.0))
# TODO Special nodes for feature classes?
return Graph(
nodes=EnumNodes(nodeset),
edges=EnumEdges(hopset)
)
@classmethod
def with_features(cls, *base_nodess: Iterable[Node]) -> 'Graph':
'''Makes and returns a Graph containing all nodes in base_nodess,
as well as a node for each feature returned by features_of, and
a positive edge connecting each base node to its feature nodes.'''
nodeset: Set[Node] = set()
hopset: Set[Hop] = set()
nodes_to_do = set(chain.from_iterable(base_nodess))
nodes_done = set()
while nodes_to_do:
base_node = nodes_to_do.pop()
nodes_done.add(base_node)
new_features = (
set(add_features(base_node, nodeset, hopset))
-
nodes_done
)
nodes_to_do |= new_features
return Graph(
nodes=EnumNodes(nodeset),
edges=EnumEdges(hopset)
)
### Prefixes
@dataclass(frozen=True)
class PrefixedNode:
prefix: Hashable
node: Node
def unprefixed(self, prefix: Any=None) -> Node:
return unprefixed(self, prefix)
def with_prefix(self, new_prefix) -> 'PrefixedNode':
return PrefixedNode(new_prefix, self.node)
def __repr__(self):
cl = self.__class__.__name__
return f'{cl}({self.prefix}, {self.node})'
def unprefixed(x: Node, prefix: Any=None) -> Node:
if isinstance(x, PrefixedNode):
if prefix is not None and x.prefix != prefix:
return None
else:
return x.node
else:
return None
@dataclass
class PrefixedNodes(Nodes):
prefix: Hashable
base_nodes: Nodes
def has_node(self, x):
return self.base_nodes.has_node(unprefixed(x, self.prefix))
def query(self, q):
try:
q = q.unprefixed(self.prefix)
except WrongPrefix:
return []
return self.prefix_all(self.base_nodes.query(q))
def prefix_all(self, nodes: Iterable[Node]) -> Iterable[PrefixedNode]:
'''Returns a generator in which all of 'nodes' are wrapped in
a PrefixedNode containing self.prefix.'''
return (
PrefixedNode(self.prefix, node)
for node in nodes
)
def unprefixed(self, prefix: Hashable):
if prefix == self.prefix:
return self.base_nodes
else:
return EnumNodes(set())
@dataclass
class PrefixedEdges(Edges):
prefix: Hashable
base_edges: Edges
def hops_from_node(self, nodes, x):
return (
hop.add_prefix(self.prefix)
for hop in self.base_edges.hops_from_node(
nodes.unprefixed(self.prefix),
unprefixed(x, self.prefix)
)
)
def hops_to_node(self, nodes, x):
return (
hop.add_prefix(self.prefix)
for hop in self.base_edges.hops_to_node(
nodes.unprefixed(self.prefix),
unprefixed(x, self.prefix)
)
)
def find_hop(self, nodes, from_node, to_node):
hop = self.base_edges.find_hop(
nodes.unprefixed(self.prefix),
unprefixed(from_node, self.prefix),
unprefixed(to_node, self.prefix)
)
if hop is not None:
return hop.add_prefix(self.prefix)
else:
return None
@dataclass
class PrefixedGraph(Graph):
prefix: Hashable
def __init__(self, prefix: Hashable, basegraph: Graph):
self.prefix = prefix
self.nodes = PrefixedNodes(self.prefix, basegraph.nodes)
self.edges = PrefixedEdges(self.prefix, basegraph.edges)
### Features
def add_features(base_node: Node, nodeset: Set[Node], hopset: Set[Hop]):
'''Update nodeset and hopset.'''
new_features = set()
nodeset.add(base_node)
for feature_node in features_of(base_node):
new_features.add(feature_node)
nodeset.add(feature_node)
'''
in_weight = 0.01 if isclass(feature_node) else 1.0
out_weight = 0.01 if isclass(base_node) else 1.0
'''
'''
in_weight = 0.01 if isclass(base_node) or isclass(feature_node) else 1.0
out_weight = in_weight
'''
in_weight = out_weight = 1.0
#print(f'ADD {base_node} {feature_node} {in_weight} {out_weight}')
hopset.add(Hop(base_node, feature_node, out_weight))
hopset.add(Hop(feature_node, base_node, in_weight))
return new_features
@dataclass(frozen=True)
class Feature:
pass
def features_of(x: Any) -> Iterable[Node]:
if not isclass(x):
if hasattr(x, 'features_of'):
yield from x.features_of()
yield type(x) # type: ignore[misc] # Type isn't Hashable??
# TODO rm?
@dataclass(frozen=True)
class FeatureWrapper(Feature):
feature: Union[Hashable, None] = None
@dataclass(frozen=True)
class Before(Feature):
x: Node
def features_of(self):
yield self.x
@dataclass(frozen=True)
class After(Feature):
x: Node
def features_of(self):
yield self.x
### Propagator classes that work with Graph
@dataclass
class GraphPropagatorIncoming(Propagator):
'''A Propagator that follows hops that lead to nodes whose keys are in the
activations dictionary. Each timestep, g.hops_to_node() supplies all the
edges. Because of this, only nodes explicitly included in old_d can
*ever* receive any activation when .propagate() is called.'''
def make_deltas(self, g: Graph, old_d):
return chain.from_iterable(
self.deltas_to(g, old_d, node) for node in old_d
)
def deltas_to(self, g, old_d, node):
for hop in g.hops_to_node(node):
if abs(hop.weight) >= epsilon:
neighbor_a = old_d.get(hop.from_node, 0.0)
if abs(neighbor_a) >= epsilon:
yield Delta(node, hop.weight * neighbor_a, hop.from_node)
@dataclass
class GraphPropagatorOutgoing(Propagator):
'''A Propagator that follows hops that lead to nodes whose keys are in the
activations dictionary. Each timestep, g.hops_to_node() supplies all the
edges. Because of this, only nodes explicitly included in old_d can
*ever* receive any activation when .propagate() is called.'''
def make_deltas(self, g: Graph, old_d):
return chain.from_iterable(
self.deltas_from(g, old_d, node) for node in old_d
)
def deltas_from(self, g, old_d, node):
node_a = old_d.get(node, 0.0)
if abs(node_a) >= epsilon:
#print('DFF', node, type(node), node_a, list(g.hops_from_node(node)))
for hop in g.hops_from_node(node):
if abs(hop.weight) >= epsilon:
yield Delta(hop.to_node, hop.weight * node_a, node)