PyTorch guide#
mononet.torch provides monotonic layers as torch.nn.Module
subclasses. They drop into any existing training loop (plain PyTorch,
PyTorch Lightning, etc.) and compose with the native
torch.nn.Sequential.
Install#
pip install "mononet[torch]"
Public API#
mononet.torch.layers.MonoLinear— monotonic analogue oftorch.nn.Linear(non-decreasing in all inputs).mononet.torch.layers.MonoResidual— dual-gated monotone residual block, warm-started near identity.mononet.torch.layers.MonoInput— sign-flip layer encoding per-feature monotonicity directions.
mononet ships layers only — stack them yourself; there is no composed
MonoMLP model.
Example#
import torch
from mononet.torch import MonoInput, MonoLinear
# A monotonic MLP: non-decreasing in every input feature.
net = torch.nn.Sequential(
MonoInput(1), # +1 => non-decreasing; -1 => non-increasing
MonoLinear(4, 32, mode="switch"),
MonoLinear(32, 1, mode="switch"),
)
y = net(torch.randn(8, 4)) # (8, 1), guaranteed monotone in all inputs
For per-feature monotonicity directions, pass a
MonotonicityMask (a 1-D array of
{-1, +1}) to MonoInput.