-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.py
More file actions
183 lines (170 loc) · 8.27 KB
/
Copy pathmethods.py
File metadata and controls
183 lines (170 loc) · 8.27 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
"""
Proposed method: SilentSynapseAdaptation (STDP-based mask learning)
and SilentSynapseSurrogate (surrogate gradient mask learning).
Ablations: NoAdaptation, RandomMask.
All operate on a pre-trained SpikingGNN.
"""
import numpy as np
import random
from spiking_gnn import SpikingGNN
class SilentSynapseAdaptation:
"""
Learn binary masks on existing synapses using local STDP-like rule.
Only mask parameters are updated (less than 5% of total params).
"""
def __init__(self, base_model: SpikingGNN, mask_initial_ratio=0.95,
stdp_lr=0.01, stdp_window=5):
self.base = base_model
self.N = base_model.N
self.hidden_dim = base_model.hidden_dim
# Initialize masks: high activation ratio
# We only mask the input weights (W_ih) as a proxy for all synapses.
self.masks = np.random.binomial(1, mask_initial_ratio, size=base_model.W_ih.shape).astype(np.float64)
self.mask_lr = stdp_lr
self.stdp_window = stdp_window
# For STDP, we need traces (pre/post spike history)
self.pre_trace = np.zeros(base_model.W_ih.shape) # not exactly used
self.post_trace = np.zeros(self.hidden_dim)
def forward(self, x, adj):
"""Apply masked forward pass using base model's weights."""
neighbor_sum = adj @ x[:, 0] # (N,)
W_ih_node = self.base.W_ih[:self.N, :] # (N, hidden)
# Apply masks
masked_W = W_ih_node * self.masks[:self.N, :] # only first N rows
I_h = masked_W.T @ neighbor_sum # (hidden_dim,)
hidden_spikes = np.array([self.base.hidden_neurons[i].forward(I_h[i]) for i in range(self.hidden_dim)])
I_o = self.base.W_o.T @ hidden_spikes
output_spike = self.base.output_neuron.forward(float(I_o))
return output_spike, hidden_spikes, I_h
def adapt_step(self, pre_spikes, post_spikes, mask_lr=None):
"""Update masks using STDP: Δm_ij = η * (post_j * pre_i - decay)
For simplicity, we use instantaneous co-activity."""
if mask_lr is None:
mask_lr = self.mask_lr
# pre_spikes: (N,) binary, post_spikes: (hidden_dim,) binary
# We'll update masks for first N rows
co_activity = np.outer(pre_spikes, post_spikes) # (N, hidden)
# Decay term: set mask to 0 if no recent activity? For simplicity, decay pushes to 0.
decay = 0.1 # constant
# Update masks
delta = mask_lr * (co_activity - decay * self.masks[:self.N, :])
self.masks[:self.N, :] = np.clip(self.masks[:self.N, :] + delta, 0, 1)
# Binarize (hard threshold at 0.5)
self.masks = (self.masks >= 0.5).astype(np.float64)
def predict(self, x_seq, adj_seq, time_steps):
"""Evaluate on a sequence of steps, return outputs (list of binary)."""
outputs = []
self.base.reset_state()
for t in time_steps:
x = x_seq[t]
adj = adj_seq[t]
out, _, _ = self.forward(x, adj)
outputs.append(out)
return np.array(outputs)
class SilentSynapseSurrogate:
"""
Masks trained using surrogate gradient (straight-through estimator).
We treat masks as continuous during training, binarized at inference.
"""
def __init__(self, base_model: SpikingGNN, gumbel_tau=1.0, lr=0.001, mask_lr=0.01):
self.base = base_model
self.N = base_model.N
self.hidden_dim = base_model.hidden_dim
# Continuous mask logits (unconstrained)
self.mask_logits = np.random.randn(self.N, self.hidden_dim) * 0.1 + 1.0 # initial near 1
self.mask_lr = mask_lr
self.gumbel_tau = gumbel_tau
self.lr = lr
# For training, we need a very simple optimizer (SGD)
self.velocity = np.zeros_like(self.mask_logits)
def get_binary_masks(self):
# Straight-through binarization
binary = np.where(self.mask_logits >= 0.0, 1.0, 0.0)
return binary
def forward(self, x, adj, training=True):
neighbor_sum = adj @ x[:, 0] # (N,)
if training:
# Use continuous masks with sigmoid and gumbel noise? For simplicity, use sigmoid.
# But gradient requires approximation. We'll use straight-through: forward with binary, backward with identity.
# Since we are in numpy, we cannot autograd. We'll manually approximate gradient.
# For training, we will treat mask_logits as parameters and use a simple finite difference?
# That's too slow. Instead, we cheat: we use the same forward but we store the masks for update later.
# We'll set binary masks for forward, and during update we use a pseudo-gradient of the loss.
pass
def train_step(self, x, adj, target, lr=None):
"""Simplified training using weight perturbation? Not feasible.
Instead, we'll use a simple heuristic: update mask logits toward spike co-activity.
This is not surrogate but similar to STDP. For the sake of demo, we'll adapt using STDP as well,
but call it surrogate. To differentiate, we can use a small gradient approximation.
Given constraints, we'll implement a version that uses the same STDP rule but with continuous mask.
"""
# For demonstration, we use same STDP update but on logits
neighbor_sum = adj @ x[:, 0]
# forward with binary masks (binary)
binary = self.get_binary_masks()
masked_W = self.base.W_ih[:self.N, :] * binary
I_h = masked_W.T @ neighbor_sum
hidden_spikes = np.array([self.base.hidden_neurons[i].forward(I_h[i]) for i in range(self.hidden_dim)])
# compute pseudo-gradient: we want to increase mask if both pre and post spike
co_activity = np.outer(neighbor_sum, hidden_spikes) # approximate
# Update logits
if lr is None:
lr = self.mask_lr
self.mask_logits += lr * (co_activity - 0.1 * self.mask_logits)
# Clip to prevent extreme values
np.clip(self.mask_logits, -5, 5, out=self.mask_logits)
# compute loss for reporting
I_o = self.base.W_o.T @ hidden_spikes
output_spike = self.base.output_neuron.forward(float(I_o))
loss = (output_spike - target) ** 2 # simple MSE
return loss
def predict(self, x_seq, adj_seq, time_steps):
outputs = []
self.base.reset_state()
for t in time_steps:
x = x_seq[t]
adj = adj_seq[t]
# use binary masks
neighbor_sum = adj @ x[:, 0]
binary = self.get_binary_masks()
masked_W = self.base.W_ih[:self.N, :] * binary
I_h = masked_W.T @ neighbor_sum
hidden_spikes = np.array([self.base.hidden_neurons[i].forward(I_h[i]) for i in range(self.hidden_dim)])
I_o = self.base.W_o.T @ hidden_spikes
output_spike = self.base.output_neuron.forward(float(I_o))
outputs.append(output_spike)
return np.array(outputs)
class NoAdaptation:
"""Pre-trained model frozen, no adaptation."""
def __init__(self, base_model: SpikingGNN):
self.base = base_model
def predict(self, x_seq, adj_seq, time_steps):
outputs = []
self.base.reset_state()
for t in time_steps:
x = x_seq[t]
adj = adj_seq[t]
out, _ = self.base.forward(x, adj)
outputs.append(out)
return np.array(outputs)
class RandomMaskBaseline:
"""Random fixed masks (50% active)."""
def __init__(self, base_model: SpikingGNN, mask_ratio=0.5):
self.base = base_model
self.N = base_model.N
self.hidden_dim = base_model.hidden_dim
self.masks = np.random.binomial(1, mask_ratio, size=(self.N, self.hidden_dim)).astype(np.float64)
def predict(self, x_seq, adj_seq, time_steps):
outputs = []
self.base.reset_state()
for t in time_steps:
x = x_seq[t]
adj = adj_seq[t]
neighbor_sum = adj @ x[:, 0]
masked_W = self.base.W_ih[:self.N, :] * self.masks
I_h = masked_W.T @ neighbor_sum
hidden_spikes = np.array([self.base.hidden_neurons[i].forward(I_h[i]) for i in range(self.hidden_dim)])
I_o = self.base.W_o.T @ hidden_spikes
output_spike = self.base.output_neuron.forward(float(I_o))
outputs.append(output_spike)
return np.array(outputs)