Differentiable Knapsack and Top-k Operators via Dynamic Programming
Germain Vivier-Ardisson, Michaël E. Sander, Axel Parmentier, Mathieu Blondel
TL;DR
This work introduces a unified differentiable dynamic programming framework to embed Knapsack and Top-$k$ operators into neural networks. By replacing the non-differentiable max in Bellman recursions with differentiable max$_\Omega$, it enables deterministic or stochastic forward passes, exact VJPs, and Fenchel-Young losses, while preserving the ability to handle non-uniform weights and cardinality constraints. A key theoretical contribution is showing Shannon entropy is the unique separable regularizer that yields permutation-equivariant relaxed operators, and that certain regularizers can induce sparsity in selections. Empirically, the DP-based operators improve decision-focused learning, constrained dynamic assortment RL, and Fenchel-Young discrete VAE tasks, offering efficient gradients and scalable performance with sparse or dense relaxations as needed.
Abstract
Knapsack and Top-k operators are useful for selecting discrete subsets of variables. However, their integration into neural networks is challenging as they are piecewise constant, yielding gradients that are zero almost everywhere. In this paper, we propose a unified framework casting these operators as dynamic programs, and derive differentiable relaxations by smoothing the underlying recursions. On the algorithmic side, we develop efficient parallel algorithms supporting both deterministic and stochastic forward passes, and vector-Jacobian products for the backward pass. On the theoretical side, we prove that Shannon entropy is the unique regularization choice yielding permutation-equivariant operators, and characterize regularizers inducing sparse selections. Finally, on the experimental side, we demonstrate our framework on a decision-focused learning benchmark, a constrained dynamic assortment RL problem, and an extension of discrete VAEs.
