Delete src/dima/gplmx.py
Browse files- src/dima/gplmx.py +0 -315
src/dima/gplmx.py
DELETED
|
@@ -1,315 +0,0 @@
|
|
| 1 |
-
# src/dima/gplm.py
|
| 2 |
-
from __future__ import annotations
|
| 3 |
-
|
| 4 |
-
from typing import Any, Dict, Literal, Optional, Tuple, Union
|
| 5 |
-
|
| 6 |
-
import numpy as np
|
| 7 |
-
import scipy.linalg as la
|
| 8 |
-
|
| 9 |
-
from .ann import ANNBackend, make_ann
|
| 10 |
-
from .utils import fps_indices, median_eps_from_knn_d2, sqdist_ab
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
InducingMode = Literal["random_subset", "fps", "kmeans_medoids", "given"]
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def _kmeans2_safe(Z: np.ndarray, m: int, seed: int = 0) -> np.ndarray:
|
| 17 |
-
"""
|
| 18 |
-
KMeans centers with a safe fallback.
|
| 19 |
-
Uses scipy.cluster.vq.kmeans2 if available; otherwise samples points.
|
| 20 |
-
"""
|
| 21 |
-
Z = np.asarray(Z)
|
| 22 |
-
m = int(min(max(1, m), Z.shape[0]))
|
| 23 |
-
try:
|
| 24 |
-
from scipy.cluster.vq import kmeans2 # type: ignore
|
| 25 |
-
# minit="points" picks initial centers from data -> stable for medoids snapping
|
| 26 |
-
C, _ = kmeans2(Z.astype(np.float64, copy=False), m, minit="points", seed=seed)
|
| 27 |
-
return C.astype(Z.dtype, copy=False)
|
| 28 |
-
except Exception:
|
| 29 |
-
rng = np.random.default_rng(seed)
|
| 30 |
-
idx = rng.choice(Z.shape[0], size=m, replace=False)
|
| 31 |
-
return Z[idx]
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
class GPLM:
|
| 35 |
-
"""
|
| 36 |
-
Inducing-point / Nyström GP (kernel ridge) decoder on latents.
|
| 37 |
-
|
| 38 |
-
Training:
|
| 39 |
-
R_ix: (N,d) latents
|
| 40 |
-
R_iX: (N,D) ambients
|
| 41 |
-
|
| 42 |
-
Choose inducing Z_mx (m << N), typically subset (medoids) of R_ix.
|
| 43 |
-
|
| 44 |
-
Latent kernel (unnormalized Gaussian affinity):
|
| 45 |
-
C_im = exp(-β * ||R_ix - Z_mx||^2 / ε) (N,m)
|
| 46 |
-
W_mn = exp(-β * ||Z_mx - Z_nx||^2 / ε) (m,m)
|
| 47 |
-
|
| 48 |
-
Nyström KRR/GP mean reduced solve:
|
| 49 |
-
M_mX = (C^T C + σ2 W + jitter I)^-1 (C^T (R_iX - mean_X))
|
| 50 |
-
|
| 51 |
-
Predict:
|
| 52 |
-
For novel R_ax:
|
| 53 |
-
find κ inducing neighbors (or all m if pred_κ=None)
|
| 54 |
-
C_am = exp(-β * ||R_ax - Z_mx||^2 / ε)
|
| 55 |
-
R_aX = C_am M_mX + mean_X
|
| 56 |
-
|
| 57 |
-
Notes:
|
| 58 |
-
- This implementation supports BOTH ascii kwargs and unicode kwargs
|
| 59 |
-
(β, ε, κ_eps, σ2, pred_κ, ε_use_kth, ε_mul).
|
| 60 |
-
- If whiten_latent=True, distances are computed in whitened latent space.
|
| 61 |
-
"""
|
| 62 |
-
|
| 63 |
-
def __init__(
|
| 64 |
-
self,
|
| 65 |
-
R_ix: np.ndarray,
|
| 66 |
-
R_iX: np.ndarray,
|
| 67 |
-
*,
|
| 68 |
-
# ASCII names (preferred for library APIs)
|
| 69 |
-
beta: float = 1.0,
|
| 70 |
-
|
| 71 |
-
# ε estimation
|
| 72 |
-
eps: Optional[float] = None,
|
| 73 |
-
k_eps: int = 256,
|
| 74 |
-
eps_use_kth: bool = True,
|
| 75 |
-
eps_mul: float = 1.0,
|
| 76 |
-
|
| 77 |
-
# regularization
|
| 78 |
-
sigma2: float = 1e-5,
|
| 79 |
-
jitter: float = 1e-8,
|
| 80 |
-
|
| 81 |
-
# inducing
|
| 82 |
-
m: int = 1024,
|
| 83 |
-
inducing: InducingMode = "kmeans_medoids",
|
| 84 |
-
Z_mx: Optional[np.ndarray] = None,
|
| 85 |
-
seed: int = 0,
|
| 86 |
-
|
| 87 |
-
# preprocess
|
| 88 |
-
center_X: bool = True,
|
| 89 |
-
whiten_latent: bool = False,
|
| 90 |
-
dtype: Any = np.float32,
|
| 91 |
-
|
| 92 |
-
# compute/memory
|
| 93 |
-
fit_block: int = 8192,
|
| 94 |
-
|
| 95 |
-
# inference
|
| 96 |
-
pred_k: Optional[int] = None,
|
| 97 |
-
ann_backend: ANNBackend = "auto",
|
| 98 |
-
ann_params: Optional[Dict[str, Any]] = None,
|
| 99 |
-
n_jobs: int = -1,
|
| 100 |
-
|
| 101 |
-
# accept unicode kwargs (β, ε, κ_eps, σ2, pred_κ, ...)
|
| 102 |
-
**kwargs: Any,
|
| 103 |
-
):
|
| 104 |
-
# ---- map unicode kwargs -> ascii ----
|
| 105 |
-
if "β" in kwargs:
|
| 106 |
-
beta = kwargs.pop("β")
|
| 107 |
-
if "ε" in kwargs:
|
| 108 |
-
eps = kwargs.pop("ε")
|
| 109 |
-
if "κ_eps" in kwargs:
|
| 110 |
-
k_eps = kwargs.pop("κ_eps")
|
| 111 |
-
if "ε_use_kth" in kwargs:
|
| 112 |
-
eps_use_kth = kwargs.pop("ε_use_kth")
|
| 113 |
-
if "ε_mul" in kwargs:
|
| 114 |
-
eps_mul = kwargs.pop("ε_mul")
|
| 115 |
-
if "σ2" in kwargs:
|
| 116 |
-
sigma2 = kwargs.pop("σ2")
|
| 117 |
-
if "pred_κ" in kwargs:
|
| 118 |
-
pred_k = kwargs.pop("pred_κ")
|
| 119 |
-
|
| 120 |
-
if kwargs:
|
| 121 |
-
raise TypeError(f"Unexpected kwargs: {sorted(kwargs.keys())}")
|
| 122 |
-
|
| 123 |
-
# ---- store params (provide both spellings) ----
|
| 124 |
-
self.beta = float(beta)
|
| 125 |
-
self.β = self.beta
|
| 126 |
-
|
| 127 |
-
self.sigma2 = float(sigma2)
|
| 128 |
-
self.σ2 = self.sigma2
|
| 129 |
-
|
| 130 |
-
self.jitter = float(jitter)
|
| 131 |
-
self.seed = int(seed)
|
| 132 |
-
self.dtype = dtype
|
| 133 |
-
self.fit_block = int(fit_block)
|
| 134 |
-
|
| 135 |
-
# ---- validate / cast ----
|
| 136 |
-
R_ix = np.ascontiguousarray(np.asarray(R_ix).astype(self.dtype, copy=False))
|
| 137 |
-
R_iX = np.ascontiguousarray(np.asarray(R_iX).astype(self.dtype, copy=False))
|
| 138 |
-
if R_ix.ndim != 2 or R_iX.ndim != 2 or R_ix.shape[0] != R_iX.shape[0]:
|
| 139 |
-
raise ValueError("R_ix must be (N,d) and R_iX must be (N,D) with same N.")
|
| 140 |
-
|
| 141 |
-
self.R_ix = R_ix
|
| 142 |
-
self.R_iX = R_iX
|
| 143 |
-
self.N, self.d_lat = R_ix.shape
|
| 144 |
-
_, self.D = R_iX.shape
|
| 145 |
-
|
| 146 |
-
# ---- center output ----
|
| 147 |
-
if center_X:
|
| 148 |
-
self.mean_X = R_iX.mean(axis=0).astype(np.float64)
|
| 149 |
-
Y = (R_iX.astype(np.float64) - self.mean_X[None, :])
|
| 150 |
-
else:
|
| 151 |
-
self.mean_X = np.zeros((self.D,), dtype=np.float64)
|
| 152 |
-
Y = R_iX.astype(np.float64)
|
| 153 |
-
|
| 154 |
-
# ---- latent whitening (optional) ----
|
| 155 |
-
Ztrain = R_ix.astype(np.float64)
|
| 156 |
-
if whiten_latent:
|
| 157 |
-
self.lat_mean_x = Ztrain.mean(axis=0)
|
| 158 |
-
self.lat_std_x = np.maximum(Ztrain.std(axis=0), 1e-12)
|
| 159 |
-
Ztrain_w = (Ztrain - self.lat_mean_x) / self.lat_std_x
|
| 160 |
-
else:
|
| 161 |
-
self.lat_mean_x = np.zeros((self.d_lat,), dtype=np.float64)
|
| 162 |
-
self.lat_std_x = np.ones((self.d_lat,), dtype=np.float64)
|
| 163 |
-
Ztrain_w = Ztrain
|
| 164 |
-
|
| 165 |
-
self.R_ix_w = Ztrain_w # (N,d) in float64
|
| 166 |
-
|
| 167 |
-
# ---- ANN on training latents (for eps + medoids snapping) ----
|
| 168 |
-
self.ann_train, self.ann_backend = make_ann(ann_backend, ann_params=ann_params, n_jobs=n_jobs)
|
| 169 |
-
self.ann_train.build(self.R_ix_w.astype(self.dtype, copy=False))
|
| 170 |
-
|
| 171 |
-
# ---- eps via kNN distances on latents ----
|
| 172 |
-
if eps is None:
|
| 173 |
-
k_eps = int(min(max(8, int(k_eps)), self.N - 1))
|
| 174 |
-
# ask for k_eps+1 to try to include self
|
| 175 |
-
j_iK1, D2_iK1 = self.ann_train.search(self.R_ix_w.astype(self.dtype, copy=False), k_eps + 1)
|
| 176 |
-
|
| 177 |
-
i = np.arange(self.N)[:, None]
|
| 178 |
-
is_self = (j_iK1 == i)
|
| 179 |
-
|
| 180 |
-
if np.any(is_self):
|
| 181 |
-
D2_iK = np.empty((self.N, k_eps), dtype=np.float64)
|
| 182 |
-
for ii in range(self.N):
|
| 183 |
-
keep = (j_iK1[ii] != ii)
|
| 184 |
-
D2_iK[ii] = D2_iK1[ii][keep][:k_eps]
|
| 185 |
-
else:
|
| 186 |
-
D2_iK = D2_iK1[:, :k_eps].astype(np.float64, copy=False)
|
| 187 |
-
|
| 188 |
-
eps_hat = median_eps_from_knn_d2(D2_iK, use_kth=bool(eps_use_kth))
|
| 189 |
-
else:
|
| 190 |
-
eps_hat = float(eps)
|
| 191 |
-
|
| 192 |
-
eps_hat *= float(eps_mul)
|
| 193 |
-
if eps_hat <= 0:
|
| 194 |
-
raise ValueError("eps must be > 0.")
|
| 195 |
-
self.eps = float(eps_hat)
|
| 196 |
-
self.ε = self.eps
|
| 197 |
-
|
| 198 |
-
# ---- choose inducing points (in whitened latent space) ----
|
| 199 |
-
rng = np.random.default_rng(self.seed)
|
| 200 |
-
m = int(min(max(1, int(m)), self.N))
|
| 201 |
-
|
| 202 |
-
if Z_mx is not None:
|
| 203 |
-
Zm = np.asarray(Z_mx, dtype=np.float64)
|
| 204 |
-
if Zm.ndim != 2 or Zm.shape[1] != self.d_lat:
|
| 205 |
-
raise ValueError("Z_mx must be (m, d_lat).")
|
| 206 |
-
Zm_w = (Zm - self.lat_mean_x) / self.lat_std_x
|
| 207 |
-
else:
|
| 208 |
-
if inducing == "random_subset":
|
| 209 |
-
idx = rng.choice(self.N, size=m, replace=False)
|
| 210 |
-
Zm_w = self.R_ix_w[idx]
|
| 211 |
-
elif inducing == "fps":
|
| 212 |
-
idx = fps_indices(self.R_ix_w, m=m, seed=self.seed)
|
| 213 |
-
Zm_w = self.R_ix_w[idx]
|
| 214 |
-
elif inducing == "kmeans_medoids":
|
| 215 |
-
C = _kmeans2_safe(self.R_ix_w, m, seed=self.seed).astype(np.float64, copy=False)
|
| 216 |
-
j_cm, _ = self.ann_train.search(C.astype(self.dtype, copy=False), 1)
|
| 217 |
-
idx = j_cm.reshape(-1).astype(np.int64)
|
| 218 |
-
|
| 219 |
-
# de-duplicate and refill if needed
|
| 220 |
-
idx_u = np.unique(idx)
|
| 221 |
-
if idx_u.size < m:
|
| 222 |
-
needed = m - idx_u.size
|
| 223 |
-
pool = np.setdiff1d(np.arange(self.N), idx_u, assume_unique=False)
|
| 224 |
-
if pool.size >= needed:
|
| 225 |
-
extra = rng.choice(pool, size=needed, replace=False)
|
| 226 |
-
else:
|
| 227 |
-
extra = rng.choice(self.N, size=needed, replace=True)
|
| 228 |
-
idx = np.concatenate([idx_u, extra])
|
| 229 |
-
else:
|
| 230 |
-
idx = idx_u[:m]
|
| 231 |
-
|
| 232 |
-
Zm_w = self.R_ix_w[idx]
|
| 233 |
-
elif inducing == "given":
|
| 234 |
-
raise ValueError("Provide Z_mx when inducing='given'.")
|
| 235 |
-
else:
|
| 236 |
-
raise ValueError(f"Unknown inducing mode: {inducing!r}")
|
| 237 |
-
|
| 238 |
-
self.Z_mx_w = np.ascontiguousarray(Zm_w.astype(np.float64, copy=False))
|
| 239 |
-
self.m = int(self.Z_mx_w.shape[0])
|
| 240 |
-
|
| 241 |
-
# also store raw inducing points (unwhitened) for serialization convenience
|
| 242 |
-
self.Z_mx = (self.Z_mx_w * self.lat_std_x[None, :]) + self.lat_mean_x[None, :]
|
| 243 |
-
|
| 244 |
-
# ---- ANN on inducing points for fast prediction ----
|
| 245 |
-
self.ann_Z, _ = make_ann(ann_backend, ann_params=ann_params, n_jobs=n_jobs)
|
| 246 |
-
self.ann_Z.build(self.Z_mx_w.astype(self.dtype, copy=False))
|
| 247 |
-
|
| 248 |
-
# pred_k
|
| 249 |
-
if pred_k is None:
|
| 250 |
-
self.pred_k = None
|
| 251 |
-
else:
|
| 252 |
-
self.pred_k = int(min(max(1, int(pred_k)), self.m))
|
| 253 |
-
self.pred_κ = self.pred_k # unicode alias
|
| 254 |
-
|
| 255 |
-
# ---- W_mm ----
|
| 256 |
-
D2_mm = sqdist_ab(self.Z_mx_w, self.Z_mx_w)
|
| 257 |
-
W_mm = np.exp(-self.beta * (D2_mm.astype(np.float64) / self.eps))
|
| 258 |
-
W_mm.flat[:: self.m + 1] += self.jitter
|
| 259 |
-
self.W_mm = W_mm # (m,m)
|
| 260 |
-
|
| 261 |
-
# ---- accumulate G=C^T C and B=C^T Y ----
|
| 262 |
-
G_mm = np.zeros((self.m, self.m), dtype=np.float64)
|
| 263 |
-
B_mX = np.zeros((self.m, self.D), dtype=np.float64)
|
| 264 |
-
|
| 265 |
-
bs = int(self.fit_block)
|
| 266 |
-
for i0 in range(0, self.N, bs):
|
| 267 |
-
i1 = min(self.N, i0 + bs)
|
| 268 |
-
Zi = self.R_ix_w[i0:i1] # (b,d) float64
|
| 269 |
-
D2_im = sqdist_ab(Zi, self.Z_mx_w)
|
| 270 |
-
C_im = np.exp(-self.beta * (D2_im.astype(np.float64) / self.eps))
|
| 271 |
-
G_mm += C_im.T @ C_im
|
| 272 |
-
B_mX += C_im.T @ Y[i0:i1]
|
| 273 |
-
|
| 274 |
-
A_mm = G_mm + self.sigma2 * W_mm
|
| 275 |
-
A_mm.flat[:: self.m + 1] += self.jitter
|
| 276 |
-
|
| 277 |
-
cF = la.cho_factor(A_mm, lower=True, check_finite=False)
|
| 278 |
-
self.M_mX = la.cho_solve(cF, B_mX, check_finite=False) # (m,D)
|
| 279 |
-
|
| 280 |
-
def __call__(self, R_ax: Union[np.ndarray, list], *, batch_size: Optional[int] = None) -> np.ndarray:
|
| 281 |
-
R_ax = np.asarray(R_ax)
|
| 282 |
-
single = (R_ax.ndim == 1)
|
| 283 |
-
if single:
|
| 284 |
-
R_ax = R_ax[None, :]
|
| 285 |
-
R_ax = np.ascontiguousarray(R_ax.astype(self.dtype, copy=False))
|
| 286 |
-
|
| 287 |
-
if batch_size is None:
|
| 288 |
-
Y = self._decode(R_ax)
|
| 289 |
-
else:
|
| 290 |
-
bs = int(batch_size)
|
| 291 |
-
out = []
|
| 292 |
-
for s in range(0, R_ax.shape[0], bs):
|
| 293 |
-
out.append(self._decode(R_ax[s:s + bs]))
|
| 294 |
-
Y = np.vstack(out)
|
| 295 |
-
|
| 296 |
-
return Y[0] if single else Y
|
| 297 |
-
|
| 298 |
-
def _decode(self, R_ax: np.ndarray) -> np.ndarray:
|
| 299 |
-
Za = R_ax.astype(np.float64)
|
| 300 |
-
Za_w = (Za - self.lat_mean_x) / self.lat_std_x
|
| 301 |
-
|
| 302 |
-
if self.pred_k is None or self.pred_k == self.m:
|
| 303 |
-
D2_am = sqdist_ab(Za_w, self.Z_mx_w)
|
| 304 |
-
C_am = np.exp(-self.beta * (D2_am.astype(np.float64) / self.eps))
|
| 305 |
-
Y = C_am @ self.M_mX
|
| 306 |
-
else:
|
| 307 |
-
j_aK, D2_aK = self.ann_Z.search(Za_w.astype(self.dtype, copy=False), self.pred_k)
|
| 308 |
-
W = np.exp(-self.beta * (D2_aK.astype(np.float64) / self.eps)) # (a,k)
|
| 309 |
-
M = self.M_mX[j_aK] # (a,k,D)
|
| 310 |
-
Y = np.sum(W[:, :, None] * M, axis=1) # (a,D)
|
| 311 |
-
|
| 312 |
-
return Y + self.mean_X[None, :]
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
__all__ = ["GPLM", "InducingMode"]
|
|
|
|
|
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