Skip to main content

omnibias.keras.stencil

0.0.2a1AlphaPython >=3.10Apache-2.0

Keras 3 operator layers and activation paths implemented with keras.ops for TensorFlow, JAX, or Torch.

import omnibias.keras.stencil
Module 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.

FUNCTION

central_bias_offsets​

omnibias.keras.stencil.central_bias_offsetsSource ↗
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 keras.config.floatx().

Parameters

NameType & defaultContract
Kpositional-or-keywordintRequiredSee the operation contract and implementation below.
deltapositional-or-keywordfloatRequiredSee the operation contract and implementation below.
dtypepositional-or-keywordstr | NoneDefault: NoneSee the operation contract and implementation below.
Returnsnp.ndarray
Read 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) * delta
FUNCTION

central_difference_signs​

omnibias.keras.stencil.central_difference_signsSource ↗
central_difference_signs(K: int, delta: float, dtype: str | None=None) -> np.ndarray

Central-difference signs (same magnitudes as forward-difference).

Parameters

NameType & defaultContract
Kpositional-or-keywordintRequiredSee the operation contract and implementation below.
deltapositional-or-keywordfloatRequiredSee the operation contract and implementation below.
dtypepositional-or-keywordstr | NoneDefault: NoneSee the operation contract and implementation below.
Returnsnp.ndarray
Read 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)
FUNCTION

forward_bias_offsets​

omnibias.keras.stencil.forward_bias_offsetsSource ↗
forward_bias_offsets(K: int, delta: float, dtype: str | None=None) -> np.ndarray

Offsets (0, delta, 2*delta, ..., (K-1)*delta).

dtype defaults to keras.config.floatx().

Parameters

NameType & defaultContract
Kpositional-or-keywordintRequiredSee the operation contract and implementation below.
deltapositional-or-keywordfloatRequiredSee the operation contract and implementation below.
dtypepositional-or-keywordstr | NoneDefault: NoneSee the operation contract and implementation below.
Returnsnp.ndarray
Read 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)) * delta
FUNCTION

forward_difference_signs​

omnibias.keras.stencil.forward_difference_signsSource ↗
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 keras.config.floatx().

Parameters

NameType & defaultContract
Kpositional-or-keywordintRequiredSee the operation contract and implementation below.
deltapositional-or-keywordfloatRequiredSee the operation contract and implementation below.
dtypepositional-or-keywordstr | NoneDefault: NoneSee the operation contract and implementation below.
Returnsnp.ndarray
Explicit validation & failure cases
  • When K < 1, raises ValueError(f'K must be >= 1, got {K}').
  • When delta <= 0 and K > 1, raises ValueError(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)
FUNCTION

identity_signs​

omnibias.keras.identity_init.identity_signsSource ↗
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 keras.config.floatx().

Parameters

NameType & defaultContract
Kpositional-or-keywordintRequiredSee the operation contract and implementation below.
dtypepositional-or-keywordstr | NoneDefault: NoneSee the operation contract and implementation below.
Returnsnp.ndarray
Explicit validation & failure cases
  • When K < 1, raises ValueError(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" -q
Read 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 s
Other public import paths
  • omnibias.keras.identity_init.identity_signs
  • omnibias.keras.stencil.identity_signs
FUNCTION

stencil_offsets​

omnibias.keras.stencil.stencil_offsetsSource ↗
stencil_offsets(K: int, delta: float, stencil: Literal['forward', 'central']='central', dtype: str | None=None) -> np.ndarray

Bias offsets for the chosen stencil.

Parameters

NameType & defaultContract
Kpositional-or-keywordintRequiredSee the operation contract and implementation below.
deltapositional-or-keywordfloatRequiredSee the operation contract and implementation below.
stencilpositional-or-keywordLiteral['forward', 'central']Default: 'central'See the operation contract and implementation below.
dtypepositional-or-keywordstr | NoneDefault: NoneSee the operation contract and implementation below.
Returnsnp.ndarray
Read 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'.")
FUNCTION

stencil_signs​

omnibias.keras.stencil.stencil_signsSource ↗
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).

Parameters

NameType & defaultContract
Kpositional-or-keywordintRequiredSee the operation contract and implementation below.
deltapositional-or-keywordfloatRequiredSee the operation contract and implementation below.
stencilpositional-or-keywordLiteral['forward', 'central']Default: 'central'See the operation contract and implementation below.
dtypepositional-or-keywordstr | NoneDefault: NoneSee the operation contract and implementation below.
Returnsnp.ndarray
Explicit validation & failure cases
  • When stencil not in ('forward', 'central'), raises ValueError(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)