jaxmat.nn module#

class ICNN[source]#

Bases: Module

Input convex neural network, convex in its inputs and optionally non-decreasing in some of them.

An ICNN (Amos et al., 2017) is a scalar network \(f(\bx)\) that is convex in its inputs by construction. With \(\sigma\) a convex and non-decreasing activation:

\[\begin{split} \begin{aligned} \bz_1 &= \sigma(\bU_0\bx + \bb_0)\\ \bz_{k+1} &= \sigma(\bW_k^{+}\bz_k + \bU_k\bx + \bb_k), \quad k=1,\ldots,n-1\\ f(\bx) &= \bw^{+}\cdot\bz_n + \bu\cdot\bx \end{aligned} \end{split}\]

where the weights \(\bW_k^{+}\) and \(\bw^{+}\) acting on the hidden units are non-negative. Each layer is then a non-decreasing convex function of convex functions plus an affine function of \(\bx\), hence convex. The input skips \(\bU_k\) and \(\bu\) are unconstrained, except on monotone inputs, where they are non-negative too, so that \(f\) is also non-decreasing in these inputs.

References

  • Amos, B., Xu, L., & Kolter, J. Z. (2017). Input convex neural networks. International Conference on Machine Learning, 146-155.

Parameters:
  • in_dim (int) – Input dimension. 0 declares a scalar input, as for in_dim=1 but called on a scalar.

  • hidden_dims (list[int]) – Widths of the hidden layers (at least one).

  • key (jax.Array) – Random key for the initialization.

  • scale (float, optional) – Order of magnitude of the output at initialization for inputs of unit norm (default 1.0). It stays roughly constant with the width and the depth.

  • monotone (bool or sequence of bool, optional) – Whether \(f\) is non-decreasing in each input (default True, for all inputs). Monotone inputs have non-negative skip weights. Non-monotone inputs make the network able to represent any convex function, e.g. \(x^2\); monotone ones are required for instance for polyconvex energies of invariants, or for a hardening potential whose derivative must be non-decreasing.

  • positive ({"softplus", "exp", "square"}, optional) – Reparametrization of the non-negative weights (default "softplus").

  • activation (Callable, optional) – Convex and non-decreasing activation (it need not be non-negative). The default, shifted_softplus(), is smooth, so that second derivatives (e.g. a tangent operator) are meaningful, and gives \(f(\boldsymbol{0}) = 0\) at initialization. With jax.nn.softplus, the output has an offset of about \(\log 2\) per layer at the origin. jax.nn.relu gives a piecewise linear \(f\), whose Hessian vanishes almost everywhere.

  • init_std (float, optional) – Relative (log-normal) spread of the initial non-negative weights around their targets (default 0.1).

Notes

The hidden weights are initialized around 1/width of the previous layer, so that each layer averages the previous one with unit gain. The skips are initialized around scale/sqrt(in_dim): zero-mean on non-monotone inputs, where averaging cancels their signs through the depth, and non-negative on monotone inputs, where they accumulate, hence an additional 1/sqrt(depth) factor on the hidden skips.

Calling the network on an array of shape (..., in_dim) ((...) for a scalar input) returns an array of shape (...).

scalar_input: bool#
monotone: tuple[bool, ...]#
positive: str#
activation: Callable#
Us: list[Array]#

Input skips of each hidden layer, non-negative on monotone inputs.

Ws: list[Array]#

Raw hidden-to-hidden weights (non-negative after reparametrization), from the second hidden layer on.

bs: list[Array]#

Biases of each hidden layer.

final_W: Array#

Raw output weights on the last hidden layer (non-negative after reparametrization).

final_U: Array#

Output skip, non-negative on monotone inputs.

positive_param(W, method='softplus')[source]#

Non-negative reparametrization of raw weights: softplus (default), exp or square.

Return type:

Array

Parameters:
  • W (Array)

  • method (str)

shifted_softplus(x)[source]#

Softplus shifted to vanish at the origin, \(\log(1+e^x) - \log 2\).

Convex and non-decreasing like the softplus, hence a valid ICNN activation (the hidden units of an ICNN must be convex, not non-negative). With zero biases, a zero input gives zero hidden units, so an ICNN using it satisfies \(f(\boldsymbol{0}) = 0\) at initialization, whatever its depth.

Return type:

Array

Parameters:

x (Array)