-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpca_oja_rop.py
More file actions
188 lines (155 loc) · 6.04 KB
/
pca_oja_rop.py
File metadata and controls
188 lines (155 loc) · 6.04 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
"""
:Author: Zachary T. Varley
:Year: 2025
:License: MIT
Oja batched update with ReduceLROnPlateau learning rate scheduler.
This module implements the batched version of Oja's update rule for PCA. Each
batch is used to update the estimates of the top-k eigenvectors of the
covariance matrix. Then a QR decomposition is performed to re-orthogonalize the
estimates. QR is O(mn^2) computed via Schwarz-Rutishauser for m x n matrices.
References:
Allen-Zhu, Zeyuan, and Yuanzhi Li. “First Efficient Convergence for Streaming
K-PCA: A Global, Gap-Free, and Near-Optimal Rate.” 2017 IEEE 58th Annual
Symposium on Foundations of Computer Science (FOCS), IEEE, 2017, pp. 487–92.
DOI.org (Crossref), https://doi.org/10.1109/FOCS.2017.51.
Tang, Cheng. “Exponentially Convergent Stochastic K-PCA without Variance
Reduction.” Advances in Neural Information Processing Systems, vol. 32, 2019.
"""
import torch
from torch import nn, Tensor
class ReduceLROnPlateau:
def __init__(
self,
mode="min",
factor=0.1,
patience=10,
threshold=1e-4,
threshold_mode="rel",
cooldown=0,
min_lr=1e-8,
eps=1e-8,
verbose=False,
):
self.mode = mode
self.factor = factor
self.patience = patience
self.threshold = threshold
self.threshold_mode = threshold_mode
self.cooldown = cooldown
self.min_lr = min_lr
self.eps = eps
self.verbose = verbose
self.best = None
self.num_bad_epochs = 0
self.cooldown_counter = 0
self.mode_worse = None # the worse value for the chosen mode
self._init_is_better()
def _init_is_better(self):
"""Initialize is_better function based on mode and threshold mode."""
if self.mode not in {"min", "max"}:
raise ValueError("mode " + self.mode + " is unknown!")
if self.mode == "min":
self.mode_worse = float("inf")
else:
self.mode_worse = -float("inf")
if self.threshold_mode not in {"rel", "abs"}:
raise ValueError("threshold mode " + self.threshold_mode + " is unknown!")
if self.mode == "min":
self.is_better = lambda a, best: (
a < best - self.threshold
if self.threshold_mode == "abs"
else a < best * (1 - self.threshold)
)
self.mode_worse = float("inf")
else:
self.is_better = lambda a, best: (
a > best + self.threshold
if self.threshold_mode == "abs"
else a > best * (1 + self.threshold)
)
self.mode_worse = -float("inf")
def step(self, metrics, current_lr):
"""Returns whether to reduce the learning rate based on the current metric."""
if self.best is None:
self.best = metrics
return current_lr
if self.cooldown_counter > 0:
self.cooldown_counter -= 1
self.num_bad_epochs = 0
if self.is_better(metrics, self.best):
self.best = metrics
self.num_bad_epochs = 0
else:
self.num_bad_epochs += 1
if self.num_bad_epochs > self.patience:
if self.cooldown_counter <= 0:
new_lr = max(current_lr * self.factor, self.min_lr)
if new_lr < current_lr - self.eps:
self.cooldown_counter = self.cooldown
self.num_bad_epochs = 0
if self.verbose:
print(f"Reducing learning rate to {new_lr}")
return new_lr
return current_lr
class OjaPCAROP(nn.Module):
def __init__(
self,
n_features: int,
n_components: int,
initial_eta: float = 0.5,
factor: float = 0.1,
patience: int = 10,
threshold: float = 1e-4,
min_eta: float = 1e-8,
dtype: torch.dtype = torch.float32,
use_oja_plus: bool = False,
):
super().__init__()
self.n_features = n_features
self.n_components = n_components
self.eta = initial_eta
self.use_oja_plus = use_oja_plus
# Initialize parameters
self.register_buffer("Q", torch.randn(n_features, n_components, dtype=dtype))
self.register_buffer("step", torch.zeros(1, dtype=torch.int64))
# Learning rate scheduler
self.scheduler = ReduceLROnPlateau(
mode="min",
factor=factor,
patience=patience,
threshold=threshold,
min_lr=min_eta,
verbose=False,
)
# For Oja++
if self.use_oja_plus:
self.register_buffer(
"initialized_cols", torch.zeros(n_components, dtype=torch.bool)
)
self.register_buffer("next_col_to_init", torch.tensor(0, dtype=torch.int64))
def forward(self, x: Tensor) -> float:
# Forward pass and reconstruction
projection = x @ self.Q
reconstruction = projection @ self.Q.T
current_error = torch.mean((x - reconstruction) ** 2).item()
# Update then Orthonormalize Q_t using QR decomposition
self.Q.copy_(torch.linalg.qr(self.Q + self.eta * (x.T @ (projection)))[0])
# Update learning rate based on error
self.eta = self.scheduler.step(current_error, self.eta)
# Update step counter
self.step.add_(1)
# For Oja++, gradually initialize columns
if self.use_oja_plus and self.next_col_to_init < self.n_components:
if self.step % (self.n_components // 2) == 0:
self.Q[:, self.next_col_to_init] = torch.randn(
self.n_features, dtype=self.Q.dtype
)
self.initialized_cols[self.next_col_to_init] = True
self.next_col_to_init.add_(1)
return current_error
def get_components(self) -> Tensor:
return self.Q.T
def transform(self, x: Tensor) -> Tensor:
return x @ self.Q
def inverse_transform(self, x: Tensor) -> Tensor:
return x @ self.Q.T