PD-Web / numpy_model.py
“lew123456”
Deploy real StableGate-Hybrid DANN ensemble
50da399
Raw History Blame Contribute Delete
2.52 kB
from __future__ import annotations
import base64
from io import BytesIO
from math import erf, sqrt
import numpy as np
def load_npz_text(path) -> dict[str, np.ndarray]:
encoded = open(path, "rt", encoding="ascii").read()
with np.load(BytesIO(base64.b64decode(encoded))) as archive:
return {name: archive[name] for name in archive.files}
def linear(x: np.ndarray, weight: np.ndarray, bias: np.ndarray) -> np.ndarray:
return x @ weight.T + bias
def layer_norm(
x: np.ndarray,
weight: np.ndarray,
bias: np.ndarray,
eps: float = 1e-5,
) -> np.ndarray:
mean = x.mean(axis=-1, keepdims=True)
variance = ((x - mean) ** 2).mean(axis=-1, keepdims=True)
normalized = (x - mean) / np.sqrt(variance + eps)
return normalized * weight + bias
def gelu(x: np.ndarray) -> np.ndarray:
erf_values = np.vectorize(erf, otypes=[np.float32])(x / sqrt(2.0))
return 0.5 * x * (1.0 + erf_values)
class NumpyStableGateDANN:
"""Inference-only StableGate-Hybrid DANN disease-classification branch."""
def __init__(self, weights_path):
self.weights = load_npz_text(weights_path)
def predict_logits(self, expression: np.ndarray) -> np.ndarray:
weights = self.weights
x = np.asarray(expression, dtype=np.float32) * weights["gate_mask"]
x = linear(x, weights["input_projection.0.weight"], weights["input_projection.0.bias"])
x = layer_norm(x, weights["input_projection.1.weight"], weights["input_projection.1.bias"])
x = gelu(x)
block_input = x
x = layer_norm(x, weights["encoder.0.block.0.weight"], weights["encoder.0.block.0.bias"])
x = linear(x, weights["encoder.0.block.1.weight"], weights["encoder.0.block.1.bias"])
x = gelu(x)
x = linear(x, weights["encoder.0.block.4.weight"], weights["encoder.0.block.4.bias"])
x = block_input + x
x = layer_norm(x, weights["projection_head.0.weight"], weights["projection_head.0.bias"])
x = linear(x, weights["projection_head.1.weight"], weights["projection_head.1.bias"])
x = gelu(x)
x = linear(x, weights["projection_head.4.weight"], weights["projection_head.4.bias"])
x = layer_norm(x, weights["projection_head.5.weight"], weights["projection_head.5.bias"])
x = linear(x, weights["disease_classifier.0.weight"], weights["disease_classifier.0.bias"])
x = gelu(x)
return linear(x, weights["disease_classifier.3.weight"], weights["disease_classifier.3.bias"])