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
|
"""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
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)
]
|