Skip to main content

omnibias.torch.activations.registry

0.5.0rc1Release candidatePython >=3.10Apache-2.0

PyTorch activation derivatives, trainable operator layers, network jets, and direct differential-operator contractions.

import omnibias.torch.activations.registry
Module contract & mathematical conventions

Activation registry (PyTorch backend): ActivationSpec and global lookup.

The ActivationSpec dataclass itself is defined generically in omnibias.core.spec; this module specialises it to torch.Tensor and maintains a process-wide registry so get_activation can resolve string names like "sigmoid" to their full spec at construction time.

The activation modules (smooth, proximal, classical, trigonometric, nqs) are imported by omnibias.torch.activations.__init__ for side-effect registration.

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 moduleCanonical reference
ActivationSpecomnibias.core.ActivationSpec
CONSTANT

IntegralFn​

omnibias.torch.activations.registry.IntegralFnSource ↗
IntegralFn = Callable[[Tensor], Tensor]

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
IntegralFn = Callable[[Tensor], Tensor]
CONSTANT

NthDerivativeFn​

omnibias.torch.activations.registry.NthDerivativeFnSource ↗
NthDerivativeFn = Callable[[Tensor, int], Tensor]

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
NthDerivativeFn = Callable[[Tensor, int], Tensor]
CONSTANT

TensorFn​

omnibias.torch.activations.registry.TensorFnSource ↗
TensorFn = Callable[[Tensor], Tensor]

Public alias so callers do not need to import torch.Tensor directly when only the type signature matters.

Read the implementation
TensorFn = Callable[[Tensor], Tensor]
CONSTANT

TorchActivationSpec​

omnibias.torch.activations.registry.TorchActivationSpecSource ↗
TorchActivationSpec: TypeAlias = ActivationSpec[Tensor]

Type alias documenting that the torch backend pins TensorT to class:torch.Tensor. Equivalent to ActivationSpec[torch.Tensor].

Read the implementation
TorchActivationSpec: TypeAlias = ActivationSpec[Tensor]
FUNCTION

get_activation​

omnibias.torch.get_activationSource ↗
get_activation(name: str | ActivationSpec[Tensor]) -> ActivationSpec[Tensor]

Resolve a name (or pass-through spec) to an ActivationSpec.

Resolve a registered activation name or pass through an existing ActivationSpec. Use the returned specification to inspect whether a derivative or other specialized path is available before invoking it. A non-None fastpath does not remove the individual activation’s order and numerical-domain restrictions. The first-derivative example compares tanh against an independent analytic identity.

Parameters

NameType & defaultContract
namepositional-or-keywordstr | ActivationSpec[Tensor]RequiredRegistered name or alias, or an existing ActivationSpec object. Unknown strings fail lookup rather than silently choosing a default activation.
ReturnsActivationSpec[Tensor]
Explicit validation & failure cases
  • When not isinstance(name, str), raises TypeError(f'Expected str or ActivationSpec, got {type(name).__name__}: {name!r}').
  • When key not in _REGISTRY, raises KeyError(f'Unknown activation {name!r}. Known: {known}.').

These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.

Complete runnable example · first derivative

This is the complete downloadable script, tested against the pinned installed distribution. It includes initialization and assertions.

import torch
from omnibias.torch import get_activation

x = torch.linspace(-2, 2, 9, dtype=torch.float64)
spec = get_activation("tanh")
first = spec.fastpath(x, 1)
expected = 1 - torch.tanh(x).square()

torch.testing.assert_close(first, expected)
print(first.shape)  # torch.Size([9])
python first_derivative.py
Executed regression · heaviside alias resolves

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_heaviside_alias_resolves() -> None:
    assert get_activation("heaviside").name == "step"
python -m pytest "packages/omnibias-torch/tests/test_piecewise.py::test_heaviside_alias_resolves" -q
Read the implementation
def get_activation(name: str | ActivationSpec[Tensor]) -> ActivationSpec[Tensor]:
    """Resolve a name (or pass-through spec) to an :class:`ActivationSpec`."""
    if isinstance(name, ActivationSpec):
        return name
    if not isinstance(name, str):
        raise TypeError(f"Expected str or ActivationSpec, got {type(name).__name__}: {name!r}")
    key = name.lower()
    if key not in _REGISTRY:
        known = ", ".join(sorted(set(_REGISTRY.keys())))
        raise KeyError(f"Unknown activation {name!r}. Known: {known}.")
    return _REGISTRY[key]
Other public import paths
  • omnibias.torch.get_activation
  • omnibias.torch.activations.get_activation
  • omnibias.torch.activations.registry.get_activation
FUNCTION

is_registered​

omnibias.torch.is_registeredSource ↗
is_registered(name: str) -> bool

True if name (or one of its aliases) is in the registry.

Parameters

NameType & defaultContract
namepositional-or-keywordstrRequiredSee the operation contract and implementation below.
Returnsbool
Read the implementation
def is_registered(name: str) -> bool:
    """True if ``name`` (or one of its aliases) is in the registry."""
    return isinstance(name, str) and name.lower() in _REGISTRY
Other public import paths
  • omnibias.torch.is_registered
  • omnibias.torch.activations.is_registered
  • omnibias.torch.activations.registry.is_registered
FUNCTION

list_activations​

omnibias.torch.list_activationsSource ↗
list_activations() -> list[str]

Return the canonical (non-alias) activation names, sorted.

Returnslist[str]
Executed regression · all piecewise registered

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_all_piecewise_registered() -> None:
    registered = set(list_activations())
    for name in _PIECEWISE_NAMES:
        assert name in registered, f"{name!r} not registered"
python -m pytest "packages/omnibias-torch/tests/test_piecewise.py::test_all_piecewise_registered" -q
Regression example · keras registers same activations

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_keras_registers_same_activations() -> None:
    from omnibias.torch.activations.registry import list_activations as torch_list

    assert sorted(keras_list()) == sorted(torch_list())
python -m pytest tests/test_keras_parity.py -k test_keras_registers_same_activations -q
Read the implementation
def list_activations() -> list[str]:
    """Return the canonical (non-alias) activation names, sorted."""
    return sorted({spec.name for spec in _REGISTRY.values()})
Other public import paths
  • omnibias.torch.list_activations
  • omnibias.torch.activations.list_activations
  • omnibias.torch.activations.registry.list_activations
FUNCTION

register_activation​

omnibias.torch.register_activationSource ↗
register_activation(spec: ActivationSpec[Tensor]) -> ActivationSpec[Tensor]

Register an ActivationSpec under its name and aliases.

Returns the spec unchanged so this can be used as a decorator-style expression.

Parameters

NameType & defaultContract
specpositional-or-keywordActivationSpec[Tensor]RequiredSee the operation contract and implementation below.
ReturnsActivationSpec[Tensor]
Explicit validation & failure cases
  • When not spec.name, raises ValueError('ActivationSpec.name must be a non-empty string.').
  • When name in _REGISTRY and _REGISTRY[name] is not spec, raises ValueError(f'Activation {name!r} is already registered with a different spec.').
  • When akey in _REGISTRY and _REGISTRY[akey] is not spec, raises ValueError(f'Alias {alias!r} for activation {name!r} clashes with an existing entry.').

These are explicit source guards. Backend operations may also raise for invalid shapes, dtypes, or unsupported devices.

Read the implementation
def register_activation(spec: ActivationSpec[Tensor]) -> ActivationSpec[Tensor]:
    """Register an :class:`ActivationSpec` under its name and aliases.

    Returns the spec unchanged so this can be used as a decorator-style
    expression.
    """
    if not spec.name:
        raise ValueError("ActivationSpec.name must be a non-empty string.")
    name = spec.name.lower()
    if name in _REGISTRY and _REGISTRY[name] is not spec:
        raise ValueError(f"Activation {name!r} is already registered with a different spec.")
    _REGISTRY[name] = spec
    for alias in spec.aliases:
        akey = alias.lower()
        if akey in _REGISTRY and _REGISTRY[akey] is not spec:
            raise ValueError(
                f"Alias {alias!r} for activation {name!r} clashes with an existing entry."
            )
        _REGISTRY[akey] = spec
    return spec
Other public import paths
  • omnibias.torch.register_activation
  • omnibias.torch.activations.register_activation
  • omnibias.torch.activations.registry.register_activation