-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoy_ngc_learn.py
205 lines (156 loc) · 6.37 KB
/
toy_ngc_learn.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
from typing import Any
import numpy
import torch
# Re-implementing some of the [NGC tutorials](https://ngc-learn.readthedocs.io/en/latest/tutorials/lesson1.html) in PyTorch.
class SNode:
def __init__(self, name, dim=1, beta=1.0, leak=0.0, zeta=1.0, act_fn='identity'):
if act_fn != 'identity':
raise NotImplementedError("Only identity activation function is supported.")
self.name = name
self.dim = dim
self.beta = beta
self.leak = leak
self.zeta = zeta
self.act_fn = lambda x: x
self.comp = {
'z': torch.zeros([1, dim]),
'phi(z)': torch.zeros([1, dim]),
'dz_td': torch.zeros([1, dim]),
'dz_bu': torch.torch.zeros([1, dim])
}
self.incoming_cables = []
def get_signal(self, comp_name):
return self.comp[comp_name]
def step(self, debug=False):
# reset bottom-up/top-down compartments
self.comp['dz_td'] *= 0.
self.comp['dz_bu'] *= 0.
# collect data from incoming cables
for cable in self.incoming_cables:
self.comp[cable.dest_comp] += cable.propagate()
dzdt = self.leak * self.comp['z'] + self.comp['dz_td'] + self.comp['dz_bu']
self.comp['z'] = self.zeta * self.comp['z'] + self.beta * dzdt
self.comp['phi(z)'] = self.act_fn(self.comp['z'])
if debug:
print(f"[{self.name}]", end=" ")
for comp_name, comp_val in self.comp.items():
print(f"{comp_name}: {comp_val.item()}", end=", ")
print()
def wire_to(self, dest_node, src_comp, dest_comp, cable_kernel):
if cable_kernel['type'] == 'dense':
cable = DCable(self, dest_node, src_comp, dest_comp, cable_kernel['init_kernels']['W_init'])
elif cable_kernel['type'] == 'simple':
cable = SCable(self, dest_node, src_comp, dest_comp)
dest_node.incoming_cables.append(cable)
return cable
def clamp(self, comp_name, value):
self.comp[comp_name] = value
class ENode:
def __init__(self, name, dim):
self.name = name
self.dim = dim
self.comp = {
'z': torch.zeros([1, dim]),
'phi(z)': torch.zeros([1, dim]),
'pred_targ': torch.zeros([1, dim]),
'pred_mu': torch.zeros([1, dim])
}
self.incoming_cables = []
def get_signal(self, comp_name):
return self.comp[comp_name]
def step(self):
self.comp['z'] = self.comp['pred_targ'] - self.comp['pred_mu']
pass
def wire_to(self, dest_node, src_comp, dest_comp, cable_kernel):
if cable_kernel['type'] == 'dense':
cable = DCable(self, dest_node, src_comp, dest_comp, cable_kernel['init_kernels']['W_init'])
elif cable_kernel['type'] == 'simple':
cable = SCable(self, dest_node, src_comp, dest_comp)
dest_node.incoming_cables.append(cable)
return cable
class Cable:
def __init__(self, src_node, dest_node, src_comp, dest_comp):
self.src_node = src_node
self.dest_node = dest_node
self.src_comp = src_comp
self.dest_comp = dest_comp
def propagate(self):
pass
class SCable(Cable):
def __init__(self, src_node, dest_node, src_comp, dest_comp, scalar=1.0):
super().__init__(src_node, dest_node, src_comp, dest_comp)
self.scalar = scalar
def propagate(self):
inp = self.src_node.get_signal(self.src_comp)
return self.scalar * inp
class DCable(Cable):
def __init__(self, src_node, dest_node, src_comp, dest_comp, W_init=None, b_init=None):
super().__init__(src_node, dest_node, src_comp, dest_comp)
if W_init is None:
self.W = torch.zeros([src_node.dim, dest_node.dim])
else:
W_init_type = W_init[0]
if W_init_type == 'diagonal':
dim = W_init[1]
self.W = torch.diag(torch.ones([dim]) * W_init[1])
elif W_init_type == 'gaussian':
stddev = W_init[1]
self.W = torch.empty([src_node.dim, dest_node.dim]).normal_(mean=0, std=stddev)
else:
raise NotImplementedError("Only diagonal initialization is supported.")
if b_init is None:
self.b = torch.zeros([1, dest_node.dim])
else:
self.b = torch.ones([1, dest_node.dim]) * b_init
def propagate(self):
inp = self.src_node.get_signal(self.src_comp)
out = torch.matmul(inp, self.W) + self.b
return out
class NGCGraph:
def __init__(self, K):
self.K = K
self.nodes = {}
self.cycle = None
def set_cycle(self, nodes):
self.cycle = nodes
self.nodes = {node.name: node for node in nodes}
def get_node(self, name):
return self.nodes[name]
def settle(self, clamped_vars=None, readout_vars=None):
if clamped_vars is None:
clamped_vars = []
if readout_vars is None:
readout_vars = []
for (var_name, comp_name, val) in clamped_vars:
self.nodes[var_name].comp[comp_name] = val
for i in range(self.K):
for node in self.cycle:
node.step(debug=True)
readouts = []
for (var_name, comp_name) in readout_vars:
readouts.append((var_name, comp_name, self.nodes[var_name].get_signal(comp_name)))
return readouts
class GNCN_PDH:
def __init__(self):
pass
# "Simulating an NGC Circuit with Sensory Data" from ngc-learn Lesson 1
def circuit1():
a = SNode('a', dim=1, beta=1, leak=0.0, act_fn='identity')
b = SNode('b', dim=1, beta=1, leak=0.0, act_fn='identity')
c = SNode('c', dim=1, beta=1, leak=0.0, act_fn='identity')
init_kernels = {"W_init": ("diagonal", 1)}
dcable_cfg = {"type": "dense", "init_kernels": init_kernels}
a_b = a.wire_to(b, src_comp="phi(z)", dest_comp="dz_td", cable_kernel=dcable_cfg)
c_b = c.wire_to(b, src_comp="phi(z)", dest_comp="dz_td", cable_kernel=dcable_cfg)
circuit = NGCGraph(K=5)
circuit.set_cycle([a, c, b])
a_val = torch.ones([1, circuit.get_node('a').dim])
c_val = torch.ones([1, circuit.get_node('c').dim])
readouts = circuit.settle(
clamped_vars=[('a', 'z', a_val), ('c', 'z', c_val)],
readout_vars=[('b', 'phi(z)')]
)
b_val = readouts[0][2]
print(b_val)
if __name__ == "__main__":
circuit1()