Smooth-RG Modewise Optimizer / smooth_rg_optimizer.py

Mechanism failed

Raw ⬇ ZIP
 1"""Small NumPy implementation of the smooth-RG modewise update.
 2
 3The caller supplies approximate positive curvature modes q and optionally a
 4modewise damping estimate a. The default quadratic fallback a=q is exact for
 5an independently diagonal quadratic objective.
 6"""
 7import numpy as np
 8
 9
10def smooth_regulator(q, alpha=0.7, s=1.0):
11    q = np.asarray(q, dtype=float)
12    if alpha <= 0 or s <= 0:
13        raise ValueError("alpha and s must be positive")
14    return alpha * s**2 / np.expm1(np.minimum((q / s)**2, 700.0))
15
16
17class SmoothRGModewise:
18    def __init__(self, q, alpha=0.7, s=1.0, lr=0.5, eps=1e-8):
19        self.q = np.asarray(q, dtype=float)
20        if np.any(self.q <= 0):
21            raise ValueError("curvature modes must be positive")
22        self.alpha, self.s, self.lr, self.eps = alpha, s, lr, eps
23
24    def step(self, theta, grad, damping=None):
25        """Return theta - lr*grad/(|a|+R_s(q)+eps).
26
27        In a real network q and damping can be refreshed from Hessian probes
28        and perturbation responses; this MVP assumes they are mode-aligned.
29        """
30        a = self.q if damping is None else np.asarray(damping, dtype=float)
31        denom = np.abs(a) + smooth_regulator(self.q, self.alpha, self.s) + self.eps
32        return np.asarray(theta) - self.lr * np.asarray(grad) / denom