Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| import numpy as np | |
| class OmegaConfig: | |
| alpha_0: float = 1 / 137.035 | |
| phase_scale: float = 800.0 | |
| security_scale: float = 300.0 | |
| n_harmonics: int = 4 | |
| def alpha_to_label(alpha: float) -> str: | |
| alpha_0 = 1 / 137.035 | |
| alpha_physical = 1 / 137.035999 | |
| if abs(alpha - alpha_0) < 5e-7: | |
| return "Nominal" | |
| if abs(alpha - alpha_physical) < 5e-7: | |
| return "Physical" | |
| return "Perturbed" | |
| class HolographicMasterCodeTransformer: | |
| """ | |
| Lightweight physics-inspired model for demo use. | |
| It is intentionally simple so it runs fast inside a Hugging Face Space. | |
| """ | |
| def __init__(self, config: OmegaConfig | None = None): | |
| self.cfg = config or OmegaConfig() | |
| self.alpha_0 = self.cfg.alpha_0 | |
| rng = np.random.default_rng(42) | |
| self.harmonic_weights = rng.normal(size=(self.cfg.n_harmonics, 8)).astype(np.float32) | |
| def forward(self, x: np.ndarray, alpha: float): | |
| x = np.asarray(x, dtype=np.float32) | |
| h = np.tanh(x @ np.eye(x.shape[-1], dtype=np.float32)) | |
| delta_phi = 2 * np.pi * (alpha - self.alpha_0) * self.cfg.phase_scale | |
| security = float(np.exp(-self.cfg.security_scale * abs(alpha - self.alpha_0))) | |
| psi = np.zeros_like(h) | |
| for n in range(self.cfg.n_harmonics): | |
| psi += self.harmonic_weights[n] * np.cos(h * (n + 1) + delta_phi * (n + 1)) | |
| combined = h + 0.35 * psi * security | |
| pred = combined.mean(axis=-1, keepdims=True) | |
| return pred, psi, security, delta_phi | |
| def synthetic_loss_curve(alpha_value: float, epochs: int = 150): | |
| """ | |
| Simulates a smooth loss curve. Near alpha_0, the curve is slightly better. | |
| This is for visualization only; it is not a training result. | |
| """ | |
| cfg = OmegaConfig() | |
| alpha_dev = abs(alpha_value - cfg.alpha_0) | |
| alpha_quality = np.exp(-25000.0 * alpha_dev) | |
| t = np.arange(epochs, dtype=np.float32) | |
| base = 0.65 * np.exp(-t / (epochs / 5.0)) + 0.03 | |
| oscillation = 0.02 * np.sin(t / 8.0) * (1.0 - 0.7 * alpha_quality) | |
| noise = 0.005 * np.exp(-t / (epochs / 3.0)) | |
| losses = np.maximum(0.0, base + oscillation + noise * (1.0 - alpha_quality)) | |
| regs = 0.12 * np.exp(-t / (epochs / 2.0)) + 0.02 * (1.0 - alpha_quality) | |
| return losses, regs | |