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
|
"""Shared no-autograd debiasing primitive for two-state local learners."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Iterable
import torch
Tensor = torch.Tensor
def _detached(tensors: Iterable[Tensor]) -> list[Tensor]:
values = list(tensors)
if any(value.requires_grad for value in values):
raise ValueError("paper-facing local tensors must not require gradients")
return values
@torch.no_grad()
def two_state_difference(
first: Iterable[Tensor],
second: Iterable[Tensor],
denominator: float,
) -> list[Tensor]:
"""Form a two-state teaching measurement with common-mode cancellation."""
first = _detached(first)
second = _detached(second)
if len(first) != len(second):
raise ValueError("state collections have different lengths")
if denominator == 0.0:
raise ValueError("state-difference denominator must be nonzero")
return [
(value_second - value_first) / denominator
for value_first, value_second in zip(first, second)
]
@dataclass
class LocalFilterState:
intercept: Tensor
slope: Tensor | None
feature_center: Tensor
feature_scale: Tensor
class LocalAffineDebiaser:
"""Per-element affine filter trained only by normalized local LMS.
The class is intentionally not a ``torch.nn.Module``. Coefficients are
ordinary detached tensors, and every operation executes under
``torch.no_grad``.
"""
def __init__(
self,
templates: Iterable[Tensor],
*,
feature_centers: Iterable[Tensor | float],
feature_scales: Iterable[Tensor | float],
affine: bool = True,
) -> None:
templates = _detached(templates)
centers = list(feature_centers)
scales = list(feature_scales)
if not (len(templates) == len(centers) == len(scales)):
raise ValueError("template and feature metadata lengths disagree")
self.affine = affine
self.states = []
with torch.no_grad():
for template, center, scale in zip(templates, centers, scales):
center_tensor = torch.as_tensor(
center, dtype=template.dtype, device=template.device)
scale_tensor = torch.as_tensor(
scale, dtype=template.dtype, device=template.device)
if torch.any(scale_tensor <= 0):
raise ValueError("feature scales must be positive")
self.states.append(LocalFilterState(
intercept=torch.zeros_like(template),
slope=torch.zeros_like(template) if affine else None,
feature_center=center_tensor.clone(),
feature_scale=scale_tensor.clone(),
))
self.neutral_observations = 0
@staticmethod
def _feature(state: LocalFilterState, local_feature: Tensor) -> Tensor:
return (local_feature - state.feature_center) / state.feature_scale
@torch.no_grad()
def predict(self, local_features: Iterable[Tensor]) -> list[Tensor]:
features = _detached(local_features)
if len(features) != len(self.states):
raise ValueError("feature collection length changed")
predictions = []
for state, feature in zip(self.states, features):
normalized = self._feature(state, feature)
prediction = state.intercept
if self.affine:
prediction = prediction + state.slope * normalized
predictions.append(prediction.clone())
return predictions
@torch.no_grad()
def update_neutral(
self,
local_features: Iterable[Tensor],
neutral_measurements: Iterable[Tensor],
learning_rate: float,
) -> list[Tensor]:
features = _detached(local_features)
measurements = _detached(neutral_measurements)
if not (len(features) == len(measurements) == len(self.states)):
raise ValueError("neutral tuple lengths disagree")
residuals = []
for state, feature, measurement in zip(
self.states, features, measurements
):
normalized = self._feature(state, feature)
prediction = state.intercept
normalization = torch.ones_like(normalized)
if self.affine:
prediction = prediction + state.slope * normalized
normalization = normalization + normalized.square()
residual = measurement - prediction
state.intercept.add_(learning_rate * residual / normalization)
if self.affine:
state.slope.add_(
learning_rate * residual * normalized / normalization)
residuals.append(residual.clone())
self.neutral_observations += 1
return residuals
@torch.no_grad()
def residual(
self,
local_features: Iterable[Tensor],
teaching_measurements: Iterable[Tensor],
) -> list[Tensor]:
measurements = _detached(teaching_measurements)
predictions = self.predict(local_features)
if len(measurements) != len(predictions):
raise ValueError("teaching tuple lengths disagree")
return [
measurement - prediction
for measurement, prediction in zip(measurements, predictions)
]
@torch.no_grad()
def replay_updates(
self,
local_features: Iterable[Tensor],
teaching_measurements: Iterable[Tensor],
eligibilities: Iterable[Tensor],
learning_rate: float,
) -> list[Tensor]:
residuals = self.residual(local_features, teaching_measurements)
eligibilities = _detached(eligibilities)
if len(residuals) != len(eligibilities):
raise ValueError("eligibility tuple length changed")
return [
learning_rate * residual * eligibility
for residual, eligibility in zip(residuals, eligibilities)
]
@torch.no_grad()
def clone(self) -> "LocalAffineDebiaser":
clone = LocalAffineDebiaser(
[state.intercept for state in self.states],
feature_centers=[state.feature_center for state in self.states],
feature_scales=[state.feature_scale for state in self.states],
affine=self.affine,
)
for source, target in zip(self.states, clone.states):
target.intercept.copy_(source.intercept)
if self.affine:
target.slope.copy_(source.slope)
clone.neutral_observations = self.neutral_observations
return clone
@dataclass
class LocalLeastSquaresState:
intercept: Tensor
slope: Tensor
feature_center: Tensor
feature_scale: Tensor
mean_feature: Tensor
mean_measurement: Tensor
feature_sum_squares: Tensor
cross_sum: Tensor
class LocalLeastSquaresDebiaser:
"""Per-element online affine regression from neutral observations.
Every element keeps its own scalar sufficient statistics. No observation,
coefficient, or update is shared across elements, and no autograd graph is
created. This is the direct online analogue of fitting a local baseline
relation before taking its innovation.
"""
def __init__(
self,
templates: Iterable[Tensor],
*,
feature_centers: Iterable[Tensor | float],
feature_scales: Iterable[Tensor | float],
variance_floor: float = 1e-12,
) -> None:
templates = _detached(templates)
centers = list(feature_centers)
scales = list(feature_scales)
if not (len(templates) == len(centers) == len(scales)):
raise ValueError("template and feature metadata lengths disagree")
if variance_floor <= 0.0:
raise ValueError("variance floor must be positive")
self.variance_floor = variance_floor
self.states = []
with torch.no_grad():
for template, center, scale in zip(templates, centers, scales):
center_tensor = torch.as_tensor(
center, dtype=template.dtype, device=template.device)
scale_tensor = torch.as_tensor(
scale, dtype=template.dtype, device=template.device)
if torch.any(scale_tensor <= 0):
raise ValueError("feature scales must be positive")
self.states.append(LocalLeastSquaresState(
intercept=torch.zeros_like(template),
slope=torch.zeros_like(template),
feature_center=center_tensor.clone(),
feature_scale=scale_tensor.clone(),
mean_feature=torch.zeros_like(template),
mean_measurement=torch.zeros_like(template),
feature_sum_squares=torch.zeros_like(template),
cross_sum=torch.zeros_like(template),
))
self.neutral_observations = 0
@staticmethod
def _feature(
state: LocalLeastSquaresState, local_feature: Tensor
) -> Tensor:
return (local_feature - state.feature_center) / state.feature_scale
@torch.no_grad()
def predict(self, local_features: Iterable[Tensor]) -> list[Tensor]:
features = _detached(local_features)
if len(features) != len(self.states):
raise ValueError("feature collection length changed")
return [
(
state.intercept
+ state.slope * self._feature(state, feature)
).clone()
for state, feature in zip(self.states, features)
]
@torch.no_grad()
def update_neutral(
self,
local_features: Iterable[Tensor],
neutral_measurements: Iterable[Tensor],
learning_rate: float,
) -> list[Tensor]:
if learning_rate != 1.0:
raise ValueError(
"online least squares requires predictor_rate=1")
features = _detached(local_features)
measurements = _detached(neutral_measurements)
if not (len(features) == len(measurements) == len(self.states)):
raise ValueError("neutral tuple lengths disagree")
count = self.neutral_observations + 1
residuals = []
for state, feature, measurement in zip(
self.states, features, measurements
):
normalized = self._feature(state, feature)
residuals.append(
measurement
- state.intercept
- state.slope * normalized)
delta_feature = normalized - state.mean_feature
delta_measurement = measurement - state.mean_measurement
state.mean_feature.add_(delta_feature / count)
state.mean_measurement.add_(delta_measurement / count)
state.feature_sum_squares.add_(
delta_feature * (normalized - state.mean_feature))
state.cross_sum.add_(
delta_feature * (measurement - state.mean_measurement))
identifiable = state.feature_sum_squares > self.variance_floor
state.slope.copy_(torch.where(
identifiable,
state.cross_sum
/ state.feature_sum_squares.clamp_min(self.variance_floor),
torch.zeros_like(state.slope),
))
state.intercept.copy_(
state.mean_measurement - state.slope * state.mean_feature)
self.neutral_observations = count
return residuals
@torch.no_grad()
def residual(
self,
local_features: Iterable[Tensor],
teaching_measurements: Iterable[Tensor],
) -> list[Tensor]:
measurements = _detached(teaching_measurements)
predictions = self.predict(local_features)
if len(measurements) != len(predictions):
raise ValueError("teaching tuple lengths disagree")
return [
measurement - prediction
for measurement, prediction in zip(measurements, predictions)
]
class BatchedLocalAffineDebiaser:
"""Per-cell LMS whose coefficients are shared across observations.
Inputs have shape ``[observations, *local_shape]``. The leading axis is
reduced only inside each cell's predictor update; cells remain independent.
"""
def __init__(
self,
templates: Iterable[Tensor],
*,
feature_centers: Iterable[Tensor | float],
feature_scales: Iterable[Tensor | float],
affine: bool = True,
) -> None:
templates = _detached(templates)
if any(template.ndim < 1 for template in templates):
raise ValueError("batched templates require an observation axis")
self.filter = LocalAffineDebiaser(
[template[0] for template in templates],
feature_centers=feature_centers,
feature_scales=feature_scales,
affine=affine,
)
self.affine = affine
self.neutral_observations = 0
@torch.no_grad()
def predict(self, local_features: Iterable[Tensor]) -> list[Tensor]:
features = _detached(local_features)
if len(features) != len(self.filter.states):
raise ValueError("feature collection length changed")
predictions = []
for state, feature in zip(self.filter.states, features):
normalized = self.filter._feature(state, feature)
prediction = state.intercept.unsqueeze(0).expand_as(feature)
if self.affine:
prediction = prediction + state.slope.unsqueeze(0) * normalized
predictions.append(prediction.clone())
return predictions
@torch.no_grad()
def update_neutral(
self,
local_features: Iterable[Tensor],
neutral_measurements: Iterable[Tensor],
learning_rate: float,
) -> None:
features = _detached(local_features)
measurements = _detached(neutral_measurements)
if not (len(features) == len(measurements) == len(self.filter.states)):
raise ValueError("neutral tuple lengths disagree")
batch_sizes = {feature.shape[0] for feature in features}
if len(batch_sizes) != 1:
raise ValueError("local populations have different observation counts")
if any(
feature.shape != measurement.shape
for feature, measurement in zip(features, measurements)
):
raise ValueError("feature and measurement shapes disagree")
observations = next(iter(batch_sizes))
for index in range(observations):
self.filter.update_neutral(
[feature[index] for feature in features],
[measurement[index] for measurement in measurements],
learning_rate,
)
self.neutral_observations += observations
@torch.no_grad()
def residual(
self,
local_features: Iterable[Tensor],
teaching_measurements: Iterable[Tensor],
) -> list[Tensor]:
measurements = _detached(teaching_measurements)
predictions = self.predict(local_features)
if len(measurements) != len(predictions):
raise ValueError("teaching tuple lengths disagree")
return [
measurement - prediction
for measurement, prediction in zip(measurements, predictions)
]
|