Keras 3 guide#
mononet.keras uses keras.ops, so the same code runs whether Keras is
configured to use JAX, TensorFlow, or PyTorch under the hood. The GPU
devcontainer ships with KERAS_BACKEND=jax.
Install#
pip install "mononet[keras]"
Public API#
mononet.keras.layers.MonoDense— monotonic analogue ofkeras.layers.Dense(non-decreasing in all inputs).mononet.keras.layers.MonoResidual— dual-gated monotone residual block, warm-started near identity.mononet.keras.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 keras
from mononet.keras import MonoDense, MonoInput
# A monotonic MLP: non-decreasing in every input feature.
# MonoDense infers the input width at build time (Keras style) — no in_features.
net = keras.Sequential([
MonoInput(1), # +1 => non-decreasing; -1 => non-increasing
MonoDense(32, mode="switch"),
MonoDense(1, mode="switch"),
])
y = net(keras.ops.zeros((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. MonoDense and MonoInput implement
get_config/from_config, so models serialize with the standard Keras
saving APIs.