omnibias.keras.stencil
Keras 3 operator layers and activation paths implemented with keras.ops for TensorFlow, JAX, or Torch.
import omnibias.keras.stencilModule contract & mathematical conventions
Bias-collapse stencils (Keras backend).
These build the init *values* (as NumPy arrays) for the K biases and
signs of a multi-bias unit. They are plain NumPy because they only run at
construction time; the live forward pass uses keras.ops. Mirrors
omnibias.torch.stencil.
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.
central_bias_offsets
omnibias.keras.stencil.central_bias_offsetsSource ↗central_bias_offsets(K: int, delta: float, dtype: str | None=None) -> np.ndarraySymmetric offsets ((k - (K+1)/2) * delta)_k=1..K (zero mean).
dtype defaults to keras.config.floatx().
Parameters
| Name | Type & default | Contract |
|---|---|---|
Kpositional-or-keyword | intRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
dtypepositional-or-keyword | str | NoneDefault: None | See the operation contract and implementation below. |
np.ndarrayRead the implementation
def central_bias_offsets(K: int, delta: float, dtype: str | None = None) -> np.ndarray:
"""Symmetric offsets ``((k - (K+1)/2) * delta)_k=1..K`` (zero mean).
``dtype`` defaults to :func:`keras.config.floatx()`.
"""
centre = (K + 1) / 2.0
return (np.arange(1, K + 1, dtype=_default_dtype(dtype)) - centre) * deltacentral_difference_signs
omnibias.keras.stencil.central_difference_signsSource ↗central_difference_signs(K: int, delta: float, dtype: str | None=None) -> np.ndarrayCentral-difference signs (same magnitudes as forward-difference).
Parameters
| Name | Type & default | Contract |
|---|---|---|
Kpositional-or-keyword | intRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
dtypepositional-or-keyword | str | NoneDefault: None | See the operation contract and implementation below. |
np.ndarrayRead the implementation
def central_difference_signs(
K: int, delta: float, dtype: str | None = None
) -> np.ndarray:
"""Central-difference signs (same magnitudes as forward-difference)."""
return forward_difference_signs(K, delta, dtype=dtype)forward_bias_offsets
omnibias.keras.stencil.forward_bias_offsetsSource ↗forward_bias_offsets(K: int, delta: float, dtype: str | None=None) -> np.ndarrayOffsets (0, delta, 2*delta, ..., (K-1)*delta).
dtype defaults to keras.config.floatx().
Parameters
| Name | Type & default | Contract |
|---|---|---|
Kpositional-or-keyword | intRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
dtypepositional-or-keyword | str | NoneDefault: None | See the operation contract and implementation below. |
np.ndarrayRead the implementation
def forward_bias_offsets(K: int, delta: float, dtype: str | None = None) -> np.ndarray:
"""Offsets ``(0, delta, 2*delta, ..., (K-1)*delta)``.
``dtype`` defaults to :func:`keras.config.floatx()`.
"""
return np.arange(K, dtype=_default_dtype(dtype)) * deltaforward_difference_signs
omnibias.keras.stencil.forward_difference_signsSource ↗forward_difference_signs(K: int, delta: float, dtype: str | None=None) -> np.ndarrayForward-difference signs at order K-1 with step delta.
dtype defaults to keras.config.floatx().
Parameters
| Name | Type & default | Contract |
|---|---|---|
Kpositional-or-keyword | intRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
dtypepositional-or-keyword | str | NoneDefault: None | See the operation contract and implementation below. |
np.ndarrayExplicit validation & failure cases
- When
K < 1, raisesValueError(f'K must be >= 1, got {K}'). - When
delta <= 0 and K > 1, raisesValueError(f'delta must be > 0 for K > 1 (got delta={delta}, K={K})').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Read the implementation
def forward_difference_signs(
K: int, delta: float, dtype: str | None = None
) -> np.ndarray:
"""Forward-difference signs at order ``K-1`` with step ``delta``.
``dtype`` defaults to :func:`keras.config.floatx()`.
"""
if K < 1:
raise ValueError(f"K must be >= 1, got {K}")
if delta <= 0 and K > 1:
raise ValueError(f"delta must be > 0 for K > 1 (got delta={delta}, K={K})")
dtype = _default_dtype(dtype)
if K == 1:
return np.array([1.0], dtype=dtype)
inv_scale = 1.0 / (delta ** (K - 1))
signs = [((-1.0) ** (K - 1 - j)) * math.comb(K - 1, j) * inv_scale for j in range(K)]
return np.array(signs, dtype=dtype)identity_signs
omnibias.keras.identity_init.identity_signsSource ↗identity_signs(K: int, dtype: str | None=None) -> np.ndarraySigns that satisfy Lemma 1 (identity nesting); sum(s) = 1.
For odd K the alternating pattern (+1, -1, +1, ..., +1) sums to
one; for even K we set s[0] = +2 so the sum is one while s[1:]
stays strictly alternating.
dtype defaults to keras.config.floatx().
Parameters
| Name | Type & default | Contract |
|---|---|---|
Kpositional-or-keyword | intRequired | See the operation contract and implementation below. |
dtypepositional-or-keyword | str | NoneDefault: None | See the operation contract and implementation below. |
np.ndarrayExplicit validation & failure cases
- When
K < 1, raisesValueError(f'K must be >= 1, got {K}').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · grow beyond kmax raises
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_grow_beyond_kmax_raises() -> None:
unit = GrowableOMBU(num_channels=2, init_K=1, K_max=2, base="sigmoid")
with pytest.raises(RuntimeError):
unit.grow("pair")python -m pytest "packages/omnibias-keras/tests/test_growable.py::test_grow_beyond_kmax_raises" -qRead the implementation
def identity_signs(K: int, dtype: str | None = None) -> np.ndarray:
"""Signs that satisfy Lemma 1 (identity nesting); ``sum(s) = 1``.
For odd K the alternating pattern ``(+1, -1, +1, ..., +1)`` sums to
one; for even K we set ``s[0] = +2`` so the sum is one while ``s[1:]``
stays strictly alternating.
``dtype`` defaults to :func:`keras.config.floatx()`.
"""
if K < 1:
raise ValueError(f"K must be >= 1, got {K}")
dtype = _default_dtype(dtype)
s = np.array([(-1.0) ** k for k in range(K)], dtype=dtype)
if K % 2 == 0:
s[0] = 2.0
return sOther public import paths
omnibias.keras.identity_init.identity_signsomnibias.keras.stencil.identity_signs
stencil_offsets
omnibias.keras.stencil.stencil_offsetsSource ↗stencil_offsets(K: int, delta: float, stencil: Literal['forward', 'central']='central', dtype: str | None=None) -> np.ndarrayBias offsets for the chosen stencil.
Parameters
| Name | Type & default | Contract |
|---|---|---|
Kpositional-or-keyword | intRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
stencilpositional-or-keyword | Literal['forward', 'central']Default: 'central' | See the operation contract and implementation below. |
dtypepositional-or-keyword | str | NoneDefault: None | See the operation contract and implementation below. |
np.ndarrayRead the implementation
def stencil_offsets(
K: int,
delta: float,
stencil: Literal["forward", "central"] = "central",
dtype: str | None = None,
) -> np.ndarray:
"""Bias offsets for the chosen stencil."""
if stencil == "forward":
return forward_bias_offsets(K, delta, dtype=dtype)
if stencil == "central":
return central_bias_offsets(K, delta, dtype=dtype)
raise ValueError(f"Unknown stencil {stencil!r}; expected 'forward' or 'central'.")stencil_signs
omnibias.keras.stencil.stencil_signsSource ↗stencil_signs(K: int, delta: float, stencil: Literal['forward', 'central']='central', dtype: str | None=None) -> np.ndarraySigns matched to the chosen stencil (same magnitudes for both).
Parameters
| Name | Type & default | Contract |
|---|---|---|
Kpositional-or-keyword | intRequired | See the operation contract and implementation below. |
deltapositional-or-keyword | floatRequired | See the operation contract and implementation below. |
stencilpositional-or-keyword | Literal['forward', 'central']Default: 'central' | See the operation contract and implementation below. |
dtypepositional-or-keyword | str | NoneDefault: None | See the operation contract and implementation below. |
np.ndarrayExplicit validation & failure cases
- When
stencil not in ('forward', 'central'), raisesValueError(f"Unknown stencil {stencil!r}; expected 'forward' or 'central'.").
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Read the implementation
def stencil_signs(
K: int,
delta: float,
stencil: Literal["forward", "central"] = "central",
dtype: str | None = None,
) -> np.ndarray:
"""Signs matched to the chosen stencil (same magnitudes for both)."""
if stencil not in ("forward", "central"):
raise ValueError(f"Unknown stencil {stencil!r}; expected 'forward' or 'central'.")
return forward_difference_signs(K, delta, dtype=dtype)