omnibias.keras.fastpath.legendre
Keras 3 operator layers and activation paths implemented with keras.ops for TensorFlow, JAX, or Torch.
import omnibias.keras.fastpath.legendreModule contract & mathematical conventions
Closed-form derivatives of tanh via the Legendre-style recursion,
evaluated with keras.ops.
Let t = tanh(z). Then t' = 1 - t^2 and each higher derivative is
a polynomial in t:
tanh^(n)(z) = T_n(t), T_0(t) = t, T_{n+1}(t) = (1 - t^2) * T_n'(t).
Coefficients come from the shared omnibias.core.polynomials.
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.
Imports & aliases
These names share a definition with another public module. Follow a name for its full contract, parameters, and examples.
| Import from this module | Canonical reference |
|---|---|
tanh_polynomial_coeffs | omnibias.core.tanh_polynomial_coeffs |
tanh_nth_derivative
omnibias.keras.fastpath.tanh_nth_derivativeSource ↗tanh_nth_derivative(z: Any, n: int) -> Anytanh^(n)(z) evaluated in closed form.
Parameters
| Name | Type & default | Contract |
|---|---|---|
zpositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
npositional-or-keyword | intRequired | See the operation contract and implementation below. |
AnyExplicit 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 · negative order 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_negative_order_raises(fn) -> None:
with pytest.raises(ValueError):
fn(_t(np.zeros(3)), -1)python -m pytest "packages/omnibias-keras/tests/test_fastpath_correctness.py::test_negative_order_raises[tanh_nth_derivative]" -qRegression example · fastpath order zero is forward
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_fastpath_order_zero_is_forward(fn) -> None:
z = np.linspace(-2.0, 2.0, 11)
out0 = _np(fn(_t(z), 0))
assert out0.shape == z.shape
assert np.all(np.isfinite(out0))python -m pytest packages/omnibias-keras/tests/test_fastpath_correctness.py -k test_fastpath_order_zero_is_forward -qRead the implementation
def tanh_nth_derivative(z: Any, n: int) -> Any:
"""``tanh^(n)(z)`` evaluated in closed form."""
if n < 0:
raise ValueError(f"order n must be >= 0, got {n}")
t = ops.tanh(z)
if n == 0:
return t
return _horner(tanh_polynomial_coeffs(n), t)Other public import paths
omnibias.keras.fastpath.tanh_nth_derivativeomnibias.keras.fastpath.legendre.tanh_nth_derivative
tanh_tower
omnibias.keras.fastpath.legendre.tanh_towerSource ↗tanh_tower(z: Any, max_order: int) -> AnyAll derivatives through max_order from one native tanh evaluation.
Parameters
| Name | Type & default | Contract |
|---|---|---|
zpositional-or-keyword | AnyRequired | See the operation contract and implementation below. |
max_orderpositional-or-keyword | intRequired | See the operation contract and implementation below. |
AnyExplicit validation & failure cases
- When
max_order < 0, raisesValueError('max_order must be nonnegative').
These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.
Executed regression · registered shared tower
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_registered_shared_tower(name: str) -> None:
spec = get_activation(name)
assert spec.tower is not None and spec.fastpath is not None
value = ops.convert_to_tensor([-0.2, 0.0, 0.5])
tower = spec.tower(value, 6)
for order in range(7):
np.testing.assert_array_equal(ops.convert_to_numpy(tower[order]),
ops.convert_to_numpy(spec.fastpath(value, order)))
with pytest.raises(ValueError, match="nonnegative"):
spec.tower(value, -1)python -m pytest "packages/omnibias-keras/tests/test_shared_tower.py::test_registered_shared_tower[tanh]" -qRead the implementation
def tanh_tower(z: Any, max_order: int) -> Any:
"""All derivatives through max_order from one native tanh evaluation."""
if max_order < 0:
raise ValueError("max_order must be nonnegative")
value = ops.tanh(z)
rows = [value] + [_horner(tanh_polynomial_coeffs(n), value) for n in range(1, max_order + 1)]
return ops.stack(rows, axis=0)