omnibias.core.weight_loss_jet
Coefficient recurrences, activation contracts, jet algebra, and verified arithmetic shared by the numerical backends.
import omnibias.core.weight_loss_jetModule contract & mathematical conventions
Closed-form one-layer weight-space loss jets.
A Riccati hidden layer f = b + c · σ(W x + β) is linear in the
step parameter s at every preactivation once a weight direction
d is fixed: z_h(s) = z_h + s u_h. Founding bias collapse
(delta -> 0) supplies σ^(n) from
omnibias.core.polynomials. Faà di Bruno / Leibniz then assemble
φ^(k)(0) = d^k/ds^k L(θ + s d) |_{s=0}
for MSE L = (1/B) Σ_n (f_n - y_n)^2 without nested reverse-mode
and without materialising d h / d θ.
This is **one-layer** closed form. A deep nest is still the chain
rule; this module does not skip it, does not expand L(θ) as a
polynomial in every weight, and does not return a full parameter
Jacobian of hidden activations. The dense loss Hessian is assembled
only when P <= max_params. Not a global min.
theorem_prover_verified is not asserted.
Shared formulas do not imply universal floating-point identity. Verify dtype, shape, derivative-order support, and the stated numerical assumptions for the path you use.
DISCLAIMER
omnibias.core.weight_loss_jet.DISCLAIMERSource ↗DISCLAIMER = 'one-layer closed-form weight-space loss jet; Faà di Bruno is the chain rule; not a global min, not a full dh/dθ flood'This exported constant is defined by the expression above. Its exact value and annotation come from the pinned source; use it with the module contract.
Regression example · honesty refuses rejected claims
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_honesty_refuses_rejected_claims() -> None:
payload = honesty_payload()
assert payload["closed_form"] is True
assert payload["one_layer_only"] is True
assert payload["deep_net_closed_form"] is False
assert payload["skip_chain_rule"] is False
assert payload["full_parameter_jacobian"] is False
assert payload["global_min_claim"] is False
assert payload["stretch_claim"] is False
assert payload["theorem_prover_verified"] is False
assert "chain rule" in DISCLAIMER
assert "global min" in DISCLAIMERpython -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_honesty_refuses_rejected_claims -qRead the implementation
DISCLAIMER = (
"one-layer closed-form weight-space loss jet; Faà di Bruno is the "
"chain rule; not a global min, not a full dh/dθ flood"
)SUPPORTED_ACTIVATIONS
omnibias.core.weight_loss_jet.SUPPORTED_ACTIVATIONSSource ↗SUPPORTED_ACTIVATIONS: frozenset[str] = frozenset({'tanh', 'sigmoid'})This exported constant is defined by the expression above. Its exact value and annotation come from the pinned source; use it with the module contract.
Read the implementation
SUPPORTED_ACTIVATIONS: frozenset[str] = frozenset({"tanh", "sigmoid"})WeightLossJetSpec
omnibias.core.WeightLossJetSpecSource ↗class WeightLossJetSpecDataclass constructor fields; inherited fields, if any, are declared by the base class. A field(...) expression describes a default factory or field options, not a literal argument default.
Order, activation, and dense-Hessian cap for a one-layer loss jet.
Parameters
| Name | Type & default | Contract |
|---|---|---|
orderdataclass field | intDefault: 2 | See the operation contract and implementation below. |
activationdataclass field | strDefault: 'tanh' | See the operation contract and implementation below. |
max_paramsdataclass field | intDefault: _DEFAULT_MAX_PARAMS | See the operation contract and implementation below. |
Executed regression · spec normalizes activation
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_spec_normalizes_activation() -> None:
spec = WeightLossJetSpec(activation="Tanh")
assert spec.activation == "tanh"python -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_spec_normalizes_activation" -qRegression example · rejects bad inputs
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_rejects_bad_inputs() -> None:
with pytest.raises(ValueError, match="order"):
WeightLossJetSpec(order=-1)
with pytest.raises(ValueError, match="activation"):
WeightLossJetSpec(activation="relu")
with pytest.raises(ValueError, match="hidden"):
one_layer_param_count(0, 2)
with pytest.raises(ValueError, match="exceeds max_params"):
one_layer_loss_hessian(
_XS,
_YS,
_THETA,
hidden=2,
dim=2,
max_params=3,
)
with pytest.raises(ValueError, match="n must be >= 0"):
eval_sigma_derivative(0.0, -1, "tanh")python -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_rejects_bad_inputs -qFields & methods
orderfield
order: int = 2Declared int field on WeightLossJetSpec. Default expression: 2.
activationfield
activation: str = 'tanh'Declared str field on WeightLossJetSpec. Default expression: 'tanh'.
Regression example · spec normalizes activation
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_spec_normalizes_activation() -> None:
spec = WeightLossJetSpec(activation="Tanh")
assert spec.activation == "tanh"python -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_spec_normalizes_activation -qmax_paramsfield
max_params: int = _DEFAULT_MAX_PARAMSDeclared int field on WeightLossJetSpec. Default expression: _DEFAULT_MAX_PARAMS.
__post_init__method
__post_init__(self) -> NoneSee the class contract above and the exact method implementation below.
Returns: None
Validation & failure cases
- When
int(self.order) < 0, raisesValueError(f'order must be >= 0, got {self.order}'). - When
act not in SUPPORTED_ACTIVATIONS, raisesValueError(f'activation must be one of {sorted(SUPPORTED_ACTIVATIONS)}, got {self.activation!r}'). - When
int(self.max_params) < 1, raisesValueError(f'max_params must be >= 1, got {self.max_params}').
Executed regression · spec normalizes activation
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_spec_normalizes_activation() -> None:
spec = WeightLossJetSpec(activation="Tanh")
assert spec.activation == "tanh"python -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_spec_normalizes_activation" -qMethod implementation
def __post_init__(self) -> None:
if int(self.order) < 0:
raise ValueError(f"order must be >= 0, got {self.order}")
act = str(self.activation).lower()
if act not in SUPPORTED_ACTIVATIONS:
raise ValueError(
f"activation must be one of {sorted(SUPPORTED_ACTIVATIONS)}, "
f"got {self.activation!r}"
)
if int(self.max_params) < 1:
raise ValueError(f"max_params must be >= 1, got {self.max_params}")
object.__setattr__(self, "activation", act)Read the implementation
class WeightLossJetSpec:
"""Order, activation, and dense-Hessian cap for a one-layer loss jet."""
order: int = 2
activation: str = "tanh"
max_params: int = _DEFAULT_MAX_PARAMS
def __post_init__(self) -> None:
if int(self.order) < 0:
raise ValueError(f"order must be >= 0, got {self.order}")
act = str(self.activation).lower()
if act not in SUPPORTED_ACTIVATIONS:
raise ValueError(
f"activation must be one of {sorted(SUPPORTED_ACTIVATIONS)}, "
f"got {self.activation!r}"
)
if int(self.max_params) < 1:
raise ValueError(f"max_params must be >= 1, got {self.max_params}")
object.__setattr__(self, "activation", act)Other public import paths
omnibias.core.WeightLossJetSpecomnibias.jax.WeightLossJetSpecomnibias.torch.WeightLossJetSpecomnibias.core.weight_loss_jet.WeightLossJetSpecomnibias.jax.weight_loss_jet.WeightLossJetSpecomnibias.torch.weight_loss_jet.WeightLossJetSpec
eval_sigma_derivative
omnibias.core.eval_sigma_derivativeSource ↗eval_sigma_derivative(z: float, n: int, activation: str) -> floatσ^(n)(z) from the shared polynomial coefficients.
Parameters
| Name | Type & default | Contract |
|---|---|---|
zpositional-or-keyword | floatRequired | See the operation contract and implementation below. |
npositional-or-keyword | intRequired | See the operation contract and implementation below. |
activationpositional-or-keyword | strRequired | See the operation contract and implementation below. |
floatExplicit validation & failure cases
- When
n < 0, raisesValueError(f'order n must be >= 0, got {n}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRegression example · sigma tower matches polynomials
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_sigma_tower_matches_polynomials() -> None:
z = 0.3
t = math.tanh(z)
assert eval_sigma_derivative(z, 0, "tanh") == pytest.approx(t)
# tanh' = 1 - t^2
assert eval_sigma_derivative(z, 1, "tanh") == pytest.approx(1.0 - t * t)
s = 1.0 / (1.0 + math.exp(-z))
assert eval_sigma_derivative(z, 0, "sigmoid") == pytest.approx(s)
assert eval_sigma_derivative(z, 1, "sigmoid") == pytest.approx(s * (1.0 - s))python -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_sigma_tower_matches_polynomials -qRead the implementation
def eval_sigma_derivative(z: float, n: int, activation: str) -> float:
"""``σ^(n)(z)`` from the shared polynomial coefficients."""
if n < 0:
raise ValueError(f"order n must be >= 0, got {n}")
act = str(activation).lower()
if act == "tanh":
return _horner(tanh_polynomial_coeffs(n), math.tanh(z))
if act == "sigmoid":
return _horner(sigmoid_polynomial_coeffs(n), _sigmoid(z))
raise ValueError(
f"activation must be one of {sorted(SUPPORTED_ACTIVATIONS)}, "
f"got {activation!r}"
)Other public import paths
omnibias.core.eval_sigma_derivativeomnibias.core.weight_loss_jet.eval_sigma_derivative
finite_difference_jet
omnibias.core.weight_loss_jet.finite_difference_jetSource ↗finite_difference_jet(xs: Sequence[Sequence[float]], ys: Sequence[float], theta: Sequence[float], direction: Sequence[float], *, order: int, hidden: int, dim: int, activation: str='tanh', step: float=_FD_STEP) -> list[float]Central differences of φ(s) for tests; not the public method.
Parameters
| Name | Type & default | Contract |
|---|---|---|
xspositional-or-keyword | Sequence[Sequence[float]]Required | See the operation contract and implementation below. |
yspositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
thetapositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
directionpositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
orderkeyword-only | intRequired | See the operation contract and implementation below. |
hiddenkeyword-only | intRequired | See the operation contract and implementation below. |
dimkeyword-only | intRequired | See the operation contract and implementation below. |
activationkeyword-only | strDefault: 'tanh' | See the operation contract and implementation below. |
stepkeyword-only | floatDefault: _FD_STEP | See the operation contract and implementation below. |
list[float]Explicit validation & failure cases
- When
order < 0, raisesValueError(f'order must be >= 0, got {order}'). - When
step <= 0.0, raisesValueError(f'step must be > 0, got {step}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · jet matches grad hess and finite difference
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_jet_matches_grad_hess_and_finite_difference(activation: str) -> None:
hidden, dim = 2, 2
jet = one_layer_loss_jet(
_XS,
_YS,
_THETA,
_DIR,
order=3,
hidden=hidden,
dim=dim,
activation=activation,
)
loss = one_layer_loss(_XS, _YS, _THETA, hidden, dim, activation)
grad = one_layer_loss_grad(_XS, _YS, _THETA, hidden, dim, activation)
hess = one_layer_loss_hessian(_XS, _YS, _THETA, hidden, dim, activation)
assert jet[0] == pytest.approx(loss, abs=1e-14)
assert jet[1] == pytest.approx(_dot(grad, _DIR), abs=1e-13)
assert jet[2] == pytest.approx(_quad(hess, _DIR), abs=1e-12)
fd = finite_difference_jet(
_XS,
_YS,
_THETA,
_DIR,
order=3,
hidden=hidden,
dim=dim,
activation=activation,
step=1e-4,
)
assert jet[0] == pytest.approx(fd[0], abs=1e-14)
assert jet[1] == pytest.approx(fd[1], rel=1e-6, abs=1e-8)
assert jet[2] == pytest.approx(fd[2], rel=5e-4, abs=1e-6)
assert jet[3] == pytest.approx(fd[3], rel=2e-2, abs=5e-4)python -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_jet_matches_grad_hess_and_finite_difference[sigmoid]" -qRead the implementation
def finite_difference_jet(
xs: Sequence[Sequence[float]],
ys: Sequence[float],
theta: Sequence[float],
direction: Sequence[float],
*,
order: int,
hidden: int,
dim: int,
activation: str = "tanh",
step: float = _FD_STEP,
) -> list[float]:
"""Central differences of ``φ(s)`` for tests; not the public method."""
if order < 0:
raise ValueError(f"order must be >= 0, got {order}")
if step <= 0.0:
raise ValueError(f"step must be > 0, got {step}")
def phi(scale: float) -> float:
moved = [
float(theta[i]) + scale * float(direction[i]) for i in range(len(theta))
]
return one_layer_loss(xs, ys, moved, hidden, dim, activation)
if order == 0:
return [phi(0.0)]
# Recursive central differences on a tiny stencil; tests use order <= 3.
values = [phi(0.0)]
plus = phi(step)
minus = phi(-step)
values.append((plus - minus) / (2.0 * step))
if order >= 2:
values.append((plus - 2.0 * phi(0.0) + minus) / (step * step))
if order >= 3:
plus2 = phi(2.0 * step)
minus2 = phi(-2.0 * step)
values.append(
(plus2 - 2.0 * plus + 2.0 * minus - minus2) / (2.0 * step**3)
)
return values[: order + 1]honesty_payload
omnibias.core.weight_loss_jet.honesty_payloadSource ↗honesty_payload() -> dict[str, bool]The module contract describes the context for this operation. Its signature, explicit validation, return expressions, and implementation are recorded below.
dict[str, bool]Executed regression · honesty refuses rejected claims
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_honesty_refuses_rejected_claims() -> None:
payload = honesty_payload()
assert payload["closed_form"] is True
assert payload["one_layer_only"] is True
assert payload["deep_net_closed_form"] is False
assert payload["skip_chain_rule"] is False
assert payload["full_parameter_jacobian"] is False
assert payload["global_min_claim"] is False
assert payload["stretch_claim"] is False
assert payload["theorem_prover_verified"] is False
assert "chain rule" in DISCLAIMER
assert "global min" in DISCLAIMERpython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_honesty_refuses_rejected_claims" -qRead the implementation
def honesty_payload() -> dict[str, bool]:
return {
"closed_form": True,
"one_layer_only": True,
"deep_net_closed_form": False,
"skip_chain_rule": False,
"full_parameter_jacobian": False,
"global_min_claim": False,
"stretch_claim": False,
"continuum_claim": False,
"theorem_prover_verified": False,
}Other public import paths
omnibias.core.weight_loss_jet.honesty_payloadomnibias.jax.weight_loss_jet.honesty_payloadomnibias.torch.weight_loss_jet.honesty_payload
one_layer_forward
omnibias.core.weight_loss_jet.one_layer_forwardSource ↗one_layer_forward(x: Sequence[float], theta: Sequence[float], hidden: int, dim: int, activation: str) -> floatScalar field value b + c · σ(W x + β).
Parameters
| Name | Type & default | Contract |
|---|---|---|
xpositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
thetapositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
hiddenpositional-or-keyword | intRequired | See the operation contract and implementation below. |
dimpositional-or-keyword | intRequired | See the operation contract and implementation below. |
activationpositional-or-keyword | strRequired | See the operation contract and implementation below. |
floatExplicit validation & failure cases
- When
len(x) != dim, raisesValueError(f'x length {len(x)} != dim {dim}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRead the implementation
def one_layer_forward(
x: Sequence[float],
theta: Sequence[float],
hidden: int,
dim: int,
activation: str,
) -> float:
"""Scalar field value ``b + c · σ(W x + β)``."""
if len(x) != dim:
raise ValueError(f"x length {len(x)} != dim {dim}")
bias, readout, hidden_bias, weights = unpack_one_layer_params(
theta, hidden, dim
)
acc = bias
for h in range(hidden):
pre = hidden_bias[h] + sum(weights[h][j] * float(x[j]) for j in range(dim))
acc += readout[h] * eval_sigma_derivative(pre, 0, activation)
return accone_layer_loss
omnibias.core.one_layer_lossSource ↗one_layer_loss(xs: Sequence[Sequence[float]], ys: Sequence[float], theta: Sequence[float], hidden: int, dim: int, activation: str) -> floatMSE (1/B) Σ (f - y)^2.
Parameters
| Name | Type & default | Contract |
|---|---|---|
xspositional-or-keyword | Sequence[Sequence[float]]Required | See the operation contract and implementation below. |
yspositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
thetapositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
hiddenpositional-or-keyword | intRequired | See the operation contract and implementation below. |
dimpositional-or-keyword | intRequired | See the operation contract and implementation below. |
activationpositional-or-keyword | strRequired | See the operation contract and implementation below. |
floatExecuted regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRegression example · newton direction is descent
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_newton_direction_is_descent() -> None:
hidden, dim = 2, 2
grad = one_layer_loss_grad(_XS, _YS, _THETA, hidden, dim, "tanh")
hess = one_layer_loss_hessian(_XS, _YS, _THETA, hidden, dim, "tanh")
direction = one_layer_newton_direction(grad, hess, damping=1e-4)
assert _dot(grad, direction) < 0.0
# A raw length-1 Newton step can overshoot; a short restriction must descend.
baseline = one_layer_loss(_XS, _YS, _THETA, hidden, dim, "tanh")
trial = [p + 0.05 * d for p, d in zip(_THETA, direction, strict=True)]
assert one_layer_loss(_XS, _YS, trial, hidden, dim, "tanh") < baselinepython -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_newton_direction_is_descent -qRead the implementation
def one_layer_loss(
xs: Sequence[Sequence[float]],
ys: Sequence[float],
theta: Sequence[float],
hidden: int,
dim: int,
activation: str,
) -> float:
"""MSE ``(1/B) Σ (f - y)^2``."""
_check_batch(xs, ys, dim)
total = 0.0
for x, y in zip(xs, ys, strict=True):
residual = one_layer_forward(x, theta, hidden, dim, activation) - float(y)
total += residual * residual
return total / float(len(xs))Other public import paths
omnibias.core.one_layer_lossomnibias.core.weight_loss_jet.one_layer_loss
one_layer_loss_grad
omnibias.core.one_layer_loss_gradSource ↗one_layer_loss_grad(xs: Sequence[Sequence[float]], ys: Sequence[float], theta: Sequence[float], hidden: int, dim: int, activation: str='tanh') -> list[float]∇_θ L = (2/B) Σ_n r_n ∇_θ f_n.
Parameters
| Name | Type & default | Contract |
|---|---|---|
xspositional-or-keyword | Sequence[Sequence[float]]Required | See the operation contract and implementation below. |
yspositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
thetapositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
hiddenpositional-or-keyword | intRequired | See the operation contract and implementation below. |
dimpositional-or-keyword | intRequired | See the operation contract and implementation below. |
activationpositional-or-keyword | strDefault: 'tanh' | See the operation contract and implementation below. |
list[float]Explicit validation & failure cases
- When
len(theta) != need, raisesValueError(f'theta length {len(theta)} != P={need}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRegression example · newton direction is descent
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_newton_direction_is_descent() -> None:
hidden, dim = 2, 2
grad = one_layer_loss_grad(_XS, _YS, _THETA, hidden, dim, "tanh")
hess = one_layer_loss_hessian(_XS, _YS, _THETA, hidden, dim, "tanh")
direction = one_layer_newton_direction(grad, hess, damping=1e-4)
assert _dot(grad, direction) < 0.0
# A raw length-1 Newton step can overshoot; a short restriction must descend.
baseline = one_layer_loss(_XS, _YS, _THETA, hidden, dim, "tanh")
trial = [p + 0.05 * d for p, d in zip(_THETA, direction, strict=True)]
assert one_layer_loss(_XS, _YS, trial, hidden, dim, "tanh") < baselinepython -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_newton_direction_is_descent -qRead the implementation
def one_layer_loss_grad(
xs: Sequence[Sequence[float]],
ys: Sequence[float],
theta: Sequence[float],
hidden: int,
dim: int,
activation: str = "tanh",
) -> list[float]:
"""``∇_θ L = (2/B) Σ_n r_n ∇_θ f_n``."""
spec = WeightLossJetSpec(order=1, activation=activation)
_check_batch(xs, ys, dim)
need = one_layer_param_count(hidden, dim)
if len(theta) != need:
raise ValueError(f"theta length {len(theta)} != P={need}")
acc = [0.0] * need
for x, y in zip(xs, ys, strict=True):
residual = one_layer_forward(x, theta, hidden, dim, spec.activation) - float(
y
)
grad = one_layer_output_grad(x, theta, hidden, dim, spec.activation)
for i, value in enumerate(grad):
acc[i] += residual * value
scale = 2.0 / float(len(xs))
return [scale * value for value in acc]Other public import paths
omnibias.core.one_layer_loss_gradomnibias.core.weight_loss_jet.one_layer_loss_grad
one_layer_loss_hessian
omnibias.core.one_layer_loss_hessianSource ↗one_layer_loss_hessian(xs: Sequence[Sequence[float]], ys: Sequence[float], theta: Sequence[float], hidden: int, dim: int, activation: str='tanh', *, max_params: int=_DEFAULT_MAX_PARAMS) -> list[list[float]]Full MSE Hessian (2/B) Σ_n (∇f ∇f^T + r ∇²f).
Raises if P > max_params so a silent P × P flood cannot hide
behind this helper. That is the loss Hessian, not d h / d θ.
Parameters
| Name | Type & default | Contract |
|---|---|---|
xspositional-or-keyword | Sequence[Sequence[float]]Required | See the operation contract and implementation below. |
yspositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
thetapositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
hiddenpositional-or-keyword | intRequired | See the operation contract and implementation below. |
dimpositional-or-keyword | intRequired | See the operation contract and implementation below. |
activationpositional-or-keyword | strDefault: 'tanh' | See the operation contract and implementation below. |
max_paramskeyword-only | intDefault: _DEFAULT_MAX_PARAMS | See the operation contract and implementation below. |
list[list[float]]Explicit validation & failure cases
- When
need > spec.max_params, raisesValueError(f'P={need} exceeds max_params={spec.max_params}; assemble a directional jet instead of a dense Hessian'). - When
len(theta) != need, raisesValueError(f'theta length {len(theta)} != P={need}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRegression example · newton direction is descent
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_newton_direction_is_descent() -> None:
hidden, dim = 2, 2
grad = one_layer_loss_grad(_XS, _YS, _THETA, hidden, dim, "tanh")
hess = one_layer_loss_hessian(_XS, _YS, _THETA, hidden, dim, "tanh")
direction = one_layer_newton_direction(grad, hess, damping=1e-4)
assert _dot(grad, direction) < 0.0
# A raw length-1 Newton step can overshoot; a short restriction must descend.
baseline = one_layer_loss(_XS, _YS, _THETA, hidden, dim, "tanh")
trial = [p + 0.05 * d for p, d in zip(_THETA, direction, strict=True)]
assert one_layer_loss(_XS, _YS, trial, hidden, dim, "tanh") < baselinepython -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_newton_direction_is_descent -qRead the implementation
def one_layer_loss_hessian(
xs: Sequence[Sequence[float]],
ys: Sequence[float],
theta: Sequence[float],
hidden: int,
dim: int,
activation: str = "tanh",
*,
max_params: int = _DEFAULT_MAX_PARAMS,
) -> list[list[float]]:
"""Full MSE Hessian ``(2/B) Σ_n (∇f ∇f^T + r ∇²f)``.
Raises if ``P > max_params`` so a silent ``P × P`` flood cannot hide
behind this helper. That is the loss Hessian, not ``d h / d θ``.
"""
spec = WeightLossJetSpec(order=2, activation=activation, max_params=max_params)
_check_batch(xs, ys, dim)
need = one_layer_param_count(hidden, dim)
if need > spec.max_params:
raise ValueError(
f"P={need} exceeds max_params={spec.max_params}; "
"assemble a directional jet instead of a dense Hessian"
)
if len(theta) != need:
raise ValueError(f"theta length {len(theta)} != P={need}")
_, readout, hidden_bias, weights = unpack_one_layer_params(theta, hidden, dim)
hess = [[0.0] * need for _ in range(need)]
for x, y in zip(xs, ys, strict=True):
residual = one_layer_forward(x, theta, hidden, dim, spec.activation) - float(
y
)
grad = one_layer_output_grad(x, theta, hidden, dim, spec.activation)
net = _network_hessian(
x, readout, hidden_bias, weights, hidden, dim, spec.activation
)
for i in range(need):
gi = grad[i]
for j in range(need):
hess[i][j] += gi * grad[j] + residual * net[i][j]
scale = 2.0 / float(len(xs))
return [[scale * value for value in row] for row in hess]Other public import paths
omnibias.core.one_layer_loss_hessianomnibias.core.weight_loss_jet.one_layer_loss_hessian
one_layer_loss_jet
omnibias.core.one_layer_loss_jetSource ↗one_layer_loss_jet(xs: Sequence[Sequence[float]], ys: Sequence[float], theta: Sequence[float], direction: Sequence[float], *, order: int, hidden: int, dim: int, activation: str='tanh') -> list[float]φ^(k)(0) for φ(s) = (1/B) Σ_n r_n(s)^2.
(r^2)^{(k)} = Σ_{j=0}^k C(k, j) r^{(j)} r^{(k-j)}.
Parameters
| Name | Type & default | Contract |
|---|---|---|
xspositional-or-keyword | Sequence[Sequence[float]]Required | See the operation contract and implementation below. |
yspositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
thetapositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
directionpositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
orderkeyword-only | intRequired | See the operation contract and implementation below. |
hiddenkeyword-only | intRequired | See the operation contract and implementation below. |
dimkeyword-only | intRequired | See the operation contract and implementation below. |
activationkeyword-only | strDefault: 'tanh' | See the operation contract and implementation below. |
list[float]Explicit validation & failure cases
- When
len(direction) != len(theta), raisesValueError(f'direction length {len(direction)} != theta length {len(theta)}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRegression example · jet matches grad hess and finite difference
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_jet_matches_grad_hess_and_finite_difference(activation: str) -> None:
hidden, dim = 2, 2
jet = one_layer_loss_jet(
_XS,
_YS,
_THETA,
_DIR,
order=3,
hidden=hidden,
dim=dim,
activation=activation,
)
loss = one_layer_loss(_XS, _YS, _THETA, hidden, dim, activation)
grad = one_layer_loss_grad(_XS, _YS, _THETA, hidden, dim, activation)
hess = one_layer_loss_hessian(_XS, _YS, _THETA, hidden, dim, activation)
assert jet[0] == pytest.approx(loss, abs=1e-14)
assert jet[1] == pytest.approx(_dot(grad, _DIR), abs=1e-13)
assert jet[2] == pytest.approx(_quad(hess, _DIR), abs=1e-12)
fd = finite_difference_jet(
_XS,
_YS,
_THETA,
_DIR,
order=3,
hidden=hidden,
dim=dim,
activation=activation,
step=1e-4,
)
assert jet[0] == pytest.approx(fd[0], abs=1e-14)
assert jet[1] == pytest.approx(fd[1], rel=1e-6, abs=1e-8)
assert jet[2] == pytest.approx(fd[2], rel=5e-4, abs=1e-6)
assert jet[3] == pytest.approx(fd[3], rel=2e-2, abs=5e-4)python -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_jet_matches_grad_hess_and_finite_difference -qRead the implementation
def one_layer_loss_jet(
xs: Sequence[Sequence[float]],
ys: Sequence[float],
theta: Sequence[float],
direction: Sequence[float],
*,
order: int,
hidden: int,
dim: int,
activation: str = "tanh",
) -> list[float]:
r"""``φ^(k)(0)`` for ``φ(s) = (1/B) Σ_n r_n(s)^2``.
``(r^2)^{(k)} = Σ_{j=0}^k C(k, j) r^{(j)} r^{(k-j)}``.
"""
spec = WeightLossJetSpec(order=order, activation=activation)
_check_batch(xs, ys, dim)
if len(direction) != len(theta):
raise ValueError(
f"direction length {len(direction)} != theta length {len(theta)}"
)
acc = [0.0] * (spec.order + 1)
for x, y in zip(xs, ys, strict=True):
field = one_layer_output_jet(
x, theta, direction, spec.order, hidden, dim, spec.activation
)
residual = [field[0] - float(y), *field[1:]]
for k in range(spec.order + 1):
term = 0.0
for j in range(k + 1):
term += math.comb(k, j) * residual[j] * residual[k - j]
acc[k] += term
scale = 1.0 / float(len(xs))
return [scale * value for value in acc]Other public import paths
omnibias.core.one_layer_loss_jetomnibias.core.weight_loss_jet.one_layer_loss_jet
one_layer_newton_direction
omnibias.core.one_layer_newton_directionSource ↗one_layer_newton_direction(grad: Sequence[float], hess: Sequence[Sequence[float]], *, damping: float=1e-06) -> list[float]Solve (H + λ I) d = -∇L.
The direction is a local descent (∇L · d < 0 on a PD Hessian).
A raw length-1 step can overshoot; pair with a jet line search.
Not a global min.
Parameters
| Name | Type & default | Contract |
|---|---|---|
gradpositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
hesspositional-or-keyword | Sequence[Sequence[float]]Required | See the operation contract and implementation below. |
dampingkeyword-only | floatDefault: 1e-06 | See the operation contract and implementation below. |
list[float]Explicit validation & failure cases
- When
damping < 0.0 or not math.isfinite(damping), raisesValueError(f'damping must be a finite number >= 0, got {damping}'). - When
len(grad) != len(hess), raisesValueError('grad and hess must share dimension').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · newton direction is descent
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_newton_direction_is_descent() -> None:
hidden, dim = 2, 2
grad = one_layer_loss_grad(_XS, _YS, _THETA, hidden, dim, "tanh")
hess = one_layer_loss_hessian(_XS, _YS, _THETA, hidden, dim, "tanh")
direction = one_layer_newton_direction(grad, hess, damping=1e-4)
assert _dot(grad, direction) < 0.0
# A raw length-1 Newton step can overshoot; a short restriction must descend.
baseline = one_layer_loss(_XS, _YS, _THETA, hidden, dim, "tanh")
trial = [p + 0.05 * d for p, d in zip(_THETA, direction, strict=True)]
assert one_layer_loss(_XS, _YS, trial, hidden, dim, "tanh") < baselinepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_newton_direction_is_descent" -qRead the implementation
def one_layer_newton_direction(
grad: Sequence[float],
hess: Sequence[Sequence[float]],
*,
damping: float = 1e-6,
) -> list[float]:
"""Solve ``(H + λ I) d = -∇L``.
The direction is a local descent (``∇L · d < 0`` on a PD Hessian).
A raw length-1 step can overshoot; pair with a jet line search.
Not a global min.
"""
if damping < 0.0 or not math.isfinite(damping):
raise ValueError(f"damping must be a finite number >= 0, got {damping}")
if len(grad) != len(hess):
raise ValueError("grad and hess must share dimension")
rhs = [-float(v) for v in grad]
return list(solve_dense(hess, rhs, damping=damping))Other public import paths
omnibias.core.one_layer_newton_directionomnibias.core.weight_loss_jet.one_layer_newton_direction
one_layer_output_grad
omnibias.core.weight_loss_jet.one_layer_output_gradSource ↗one_layer_output_grad(x: Sequence[float], theta: Sequence[float], hidden: int, dim: int, activation: str) -> list[float]Per-sample ∇_θ f, same layout as pack_one_layer_params.
Parameters
| Name | Type & default | Contract |
|---|---|---|
xpositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
thetapositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
hiddenpositional-or-keyword | intRequired | See the operation contract and implementation below. |
dimpositional-or-keyword | intRequired | See the operation contract and implementation below. |
activationpositional-or-keyword | strRequired | See the operation contract and implementation below. |
list[float]Explicit validation & failure cases
- When
len(x) != dim, raisesValueError(f'x length {len(x)} != dim {dim}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRead the implementation
def one_layer_output_grad(
x: Sequence[float],
theta: Sequence[float],
hidden: int,
dim: int,
activation: str,
) -> list[float]:
"""Per-sample ``∇_θ f``, same layout as :func:`pack_one_layer_params`."""
if len(x) != dim:
raise ValueError(f"x length {len(x)} != dim {dim}")
_bias, readout, hidden_bias, weights = unpack_one_layer_params(
theta, hidden, dim
)
grad = [0.0] * one_layer_param_count(hidden, dim)
grad[0] = 1.0
for h in range(hidden):
pre = hidden_bias[h] + sum(weights[h][j] * float(x[j]) for j in range(dim))
sig0 = eval_sigma_derivative(pre, 0, activation)
sig1 = eval_sigma_derivative(pre, 1, activation)
grad[1 + h] = sig0
grad[1 + hidden + h] = readout[h] * sig1
base = 1 + 2 * hidden + h * dim
for j in range(dim):
grad[base + j] = readout[h] * sig1 * float(x[j])
return gradone_layer_output_jet
omnibias.core.one_layer_output_jetSource ↗one_layer_output_jet(x: Sequence[float], theta: Sequence[float], direction: Sequence[float], order: int, hidden: int, dim: int, activation: str) -> list[float]Directional derivatives f^(k)(0) of the scalar field along d.
With z_h(s) = z_h + s u_h and c_h(s) = c_h + s dc_h,
f^(k) = Σ_h [c_h σ^(k) u_h^k + k dc_h σ^(k-1) u_h^{k-1}]
for k >= 1, plus db on the first derivative. That Leibniz
expansion *is* the chain rule.
Parameters
| Name | Type & default | Contract |
|---|---|---|
xpositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
thetapositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
directionpositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
orderpositional-or-keyword | intRequired | See the operation contract and implementation below. |
hiddenpositional-or-keyword | intRequired | See the operation contract and implementation below. |
dimpositional-or-keyword | intRequired | See the operation contract and implementation below. |
activationpositional-or-keyword | strRequired | See the operation contract and implementation below. |
list[float]Explicit validation & failure cases
- When
order < 0, raisesValueError(f'order must be >= 0, got {order}'). - When
len(x) != dim, raisesValueError(f'x length {len(x)} != dim {dim}'). - When
len(direction) != len(theta), raisesValueError(f'direction length {len(direction)} != theta length {len(theta)}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRead the implementation
def one_layer_output_jet(
x: Sequence[float],
theta: Sequence[float],
direction: Sequence[float],
order: int,
hidden: int,
dim: int,
activation: str,
) -> list[float]:
r"""Directional derivatives ``f^(k)(0)`` of the scalar field along ``d``.
With ``z_h(s) = z_h + s u_h`` and ``c_h(s) = c_h + s dc_h``,
``f^(k) = Σ_h [c_h σ^(k) u_h^k + k dc_h σ^(k-1) u_h^{k-1}]``
for ``k >= 1``, plus ``db`` on the first derivative. That Leibniz
expansion *is* the chain rule.
"""
if order < 0:
raise ValueError(f"order must be >= 0, got {order}")
if len(x) != dim:
raise ValueError(f"x length {len(x)} != dim {dim}")
if len(direction) != len(theta):
raise ValueError(
f"direction length {len(direction)} != theta length {len(theta)}"
)
bias, readout, hidden_bias, weights = unpack_one_layer_params(
theta, hidden, dim
)
db, dc, dbeta, dweights = unpack_one_layer_params(direction, hidden, dim)
jets = [0.0] * (order + 1)
jets[0] = bias
if order >= 1:
jets[1] = db
for h in range(hidden):
pre = hidden_bias[h] + sum(weights[h][j] * float(x[j]) for j in range(dim))
shift = dbeta[h] + sum(dweights[h][j] * float(x[j]) for j in range(dim))
sig = [eval_sigma_derivative(pre, k, activation) for k in range(order + 1)]
jets[0] += readout[h] * sig[0]
for k in range(1, order + 1):
jets[k] += readout[h] * sig[k] * (shift**k)
jets[k] += float(k) * dc[h] * sig[k - 1] * (shift ** (k - 1))
return jetsOther public import paths
omnibias.core.one_layer_output_jetomnibias.core.weight_loss_jet.one_layer_output_jet
one_layer_param_count
omnibias.core.one_layer_param_countSource ↗one_layer_param_count(hidden: int, dim: int) -> intP = 1 + 2 H + H D for layout (b, c, β, W).
Parameters
| Name | Type & default | Contract |
|---|---|---|
hiddenpositional-or-keyword | intRequired | See the operation contract and implementation below. |
dimpositional-or-keyword | intRequired | See the operation contract and implementation below. |
intExplicit validation & failure cases
- When
hidden < 1, raisesValueError(f'hidden must be >= 1, got {hidden}'). - When
dim < 1, raisesValueError(f'dim must be >= 1, got {dim}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRegression example · pack roundtrip and param count
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_pack_roundtrip_and_param_count() -> None:
hidden, dim = 2, 2
assert one_layer_param_count(hidden, dim) == 9
bias, readout, hbias, weights = unpack_one_layer_params(_THETA, hidden, dim)
assert pack_one_layer_params(bias, readout, hbias, weights) == pytest.approx(
_THETA
)python -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_pack_roundtrip_and_param_count -qRead the implementation
def one_layer_param_count(hidden: int, dim: int) -> int:
"""``P = 1 + 2 H + H D`` for layout ``(b, c, β, W)``."""
if hidden < 1:
raise ValueError(f"hidden must be >= 1, got {hidden}")
if dim < 1:
raise ValueError(f"dim must be >= 1, got {dim}")
return 1 + 2 * hidden + hidden * dimOther public import paths
omnibias.core.one_layer_param_countomnibias.core.weight_loss_jet.one_layer_param_countomnibias.jax.weight_loss_jet.one_layer_param_countomnibias.torch.weight_loss_jet.one_layer_param_count
pack_one_layer_params
omnibias.core.pack_one_layer_paramsSource ↗pack_one_layer_params(bias: float, readout: Sequence[float], hidden_bias: Sequence[float], weights: Sequence[Sequence[float]]) -> list[float]Pack (b, c, β, W) row-major, matching omnibias.curvature.one_layer.
Parameters
| Name | Type & default | Contract |
|---|---|---|
biaspositional-or-keyword | floatRequired | See the operation contract and implementation below. |
readoutpositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
hidden_biaspositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
weightspositional-or-keyword | Sequence[Sequence[float]]Required | See the operation contract and implementation below. |
list[float]Explicit validation & failure cases
- When
hidden < 1, raisesValueError('readout must be non-empty'). - When
len(hidden_bias) != hidden, raisesValueError(f'hidden_bias length {len(hidden_bias)} != hidden {hidden}'). - When
len(weights) != hidden, raisesValueError(f'weights rows {len(weights)} != hidden {hidden}'). - When
dim < 1, raisesValueError('weight rows must be non-empty'). - When
any((len(row) != dim for row in weights)), raisesValueError('weight rows must share the same length').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRegression example · pack roundtrip and param count
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_pack_roundtrip_and_param_count() -> None:
hidden, dim = 2, 2
assert one_layer_param_count(hidden, dim) == 9
bias, readout, hbias, weights = unpack_one_layer_params(_THETA, hidden, dim)
assert pack_one_layer_params(bias, readout, hbias, weights) == pytest.approx(
_THETA
)python -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_pack_roundtrip_and_param_count -qRead the implementation
def pack_one_layer_params(
bias: float,
readout: Sequence[float],
hidden_bias: Sequence[float],
weights: Sequence[Sequence[float]],
) -> list[float]:
"""Pack ``(b, c, β, W)`` row-major, matching ``omnibias.curvature.one_layer``."""
hidden = len(readout)
if hidden < 1:
raise ValueError("readout must be non-empty")
if len(hidden_bias) != hidden:
raise ValueError(
f"hidden_bias length {len(hidden_bias)} != hidden {hidden}"
)
if len(weights) != hidden:
raise ValueError(f"weights rows {len(weights)} != hidden {hidden}")
dim = len(weights[0])
if dim < 1:
raise ValueError("weight rows must be non-empty")
if any(len(row) != dim for row in weights):
raise ValueError("weight rows must share the same length")
out = [float(bias)]
out.extend(float(v) for v in readout)
out.extend(float(v) for v in hidden_bias)
for row in weights:
out.extend(float(v) for v in row)
return outOther public import paths
omnibias.core.pack_one_layer_paramsomnibias.core.weight_loss_jet.pack_one_layer_paramsomnibias.jax.weight_loss_jet.pack_one_layer_paramsomnibias.torch.weight_loss_jet.pack_one_layer_params
unpack_one_layer_params
omnibias.core.unpack_one_layer_paramsSource ↗unpack_one_layer_params(theta: Sequence[float], hidden: int, dim: int) -> tuple[float, list[float], list[float], list[list[float]]]Inverse of pack_one_layer_params.
Parameters
| Name | Type & default | Contract |
|---|---|---|
thetapositional-or-keyword | Sequence[float]Required | See the operation contract and implementation below. |
hiddenpositional-or-keyword | intRequired | See the operation contract and implementation below. |
dimpositional-or-keyword | intRequired | See the operation contract and implementation below. |
tuple[float, list[float], list[float], list[list[float]]]Explicit validation & failure cases
- When
len(theta) != need, raisesValueError(f'theta length {len(theta)} != P={need} for hidden={hidden}, dim={dim}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRegression example · pack roundtrip and param count
This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_pack_roundtrip_and_param_count() -> None:
hidden, dim = 2, 2
assert one_layer_param_count(hidden, dim) == 9
bias, readout, hbias, weights = unpack_one_layer_params(_THETA, hidden, dim)
assert pack_one_layer_params(bias, readout, hbias, weights) == pytest.approx(
_THETA
)python -m pytest packages/omnibias-core/tests/test_weight_loss_jet.py -k test_pack_roundtrip_and_param_count -qRead the implementation
def unpack_one_layer_params(
theta: Sequence[float],
hidden: int,
dim: int,
) -> tuple[float, list[float], list[float], list[list[float]]]:
"""Inverse of :func:`pack_one_layer_params`."""
need = one_layer_param_count(hidden, dim)
if len(theta) != need:
raise ValueError(
f"theta length {len(theta)} != P={need} for hidden={hidden}, dim={dim}"
)
bias = float(theta[0])
readout = [float(theta[1 + h]) for h in range(hidden)]
hidden_bias = [float(theta[1 + hidden + h]) for h in range(hidden)]
base = 1 + 2 * hidden
weights = [
[float(theta[base + h * dim + j]) for j in range(dim)]
for h in range(hidden)
]
return bias, readout, hidden_bias, weightsOther public import paths
omnibias.core.unpack_one_layer_paramsomnibias.core.weight_loss_jet.unpack_one_layer_paramsomnibias.jax.weight_loss_jet.unpack_one_layer_paramsomnibias.torch.weight_loss_jet.unpack_one_layer_params
worked_example
omnibias.core.weight_loss_jet.worked_exampleSource ↗worked_example() -> dict[str, bool | float]Deterministic one-unit identities used by the cookbook snippet.
dict[str, bool | float]Executed regression · worked example identities
Recorded coverage confirms this test executed the API body. This example belongs to the pinned upstream test suite. It may use fixtures and imports from its full file. Run it from that checkout with its test dependencies installed.
def test_worked_example_identities() -> None:
example = worked_example()
assert example["jet0_matches_loss"] is True
assert example["jet1_matches_gdotd"] is True
assert example["jet2_matches_dHd"] is Truepython -m pytest "packages/omnibias-core/tests/test_weight_loss_jet.py::test_worked_example_identities" -qRead the implementation
def worked_example() -> dict[str, bool | float]:
"""Deterministic one-unit identities used by the cookbook snippet."""
hidden = 1
dim = 1
xs = ((0.5,), (-0.25,))
ys = (0.1, -0.2)
theta = pack_one_layer_params(0.0, (1.0,), (0.0,), ((0.5,),))
direction = pack_one_layer_params(0.0, (0.0,), (0.0,), ((1.0,),))
jet = one_layer_loss_jet(
xs,
ys,
theta,
direction,
order=3,
hidden=hidden,
dim=dim,
activation="tanh",
)
loss = one_layer_loss(xs, ys, theta, hidden, dim, "tanh")
grad = one_layer_loss_grad(xs, ys, theta, hidden, dim, "tanh")
hess = one_layer_loss_hessian(xs, ys, theta, hidden, dim, "tanh")
return {
"jet0_matches_loss": abs(jet[0] - loss) < 1e-14,
"jet1_matches_gdotd": abs(jet[1] - _dot(grad, direction)) < 1e-13,
"jet2_matches_dHd": abs(jet[2] - _quad(hess, direction)) < 1e-12,
"loss": float(loss),
"phi1": float(jet[1]),
"phi2": float(jet[2]),
"phi3": float(jet[3]),
}