Amos and Kolter’s ICML 2017 paper, OptNet: Differentiable Optimization as a Layer in Neural Networks, uses a QP’s optimal solution as a neural-network layer output and differentiates through that solution.
The motivating question is concrete: “If an action chosen by optimization performs poorly, how should the parameters that produced that action change?” The derivative of a function value at a fixed action and the derivative of the selected action are different objects:
The first asks how a predicted value changes while holding the action fixed. The second asks how the optimizer’s chosen action changes when the parameters change. OptNet provides a way to compute the latter and train a model containing the optimization operation.
1. Forward pass: choose an action under a hard ramp limit
The following is an educational example for equipment operation, not an experiment from the paper. Let a be normalized equipment output, with a previous output of 0.5 and a hard ramp limit of 0.1:
A neural network takes observations x and predicts one number, . Do not use it directly as the action. Pass it to this QP:
Here is an incentive to increase output. A larger incentive favors higher output, but the modeled hard ramp constraint must still hold. Completing the square explains what the optimizer does:
The final term is independent of a. Therefore the optimizer selects the feasible action closest to :
When = 0.45, the action is 0.45. When = 0.8, the action is 0.6. The network can prefer a higher output, but the QP returns the upper feasible output.
The layer output is the optimal decision, not the minimum objective value. In numerical implementation, satisfaction of modeled constraints is subject to solver tolerances; the exact mathematical example satisfies them exactly.
2. Backward pass: what can a poor action teach the network?
For one input, suppose a reference action of 0.55 is given. This is a hypothetical training label to demonstrate differentiation. It does not assume that optimal-action labels are available in the real operating problem.
The chain rule is
The first factor says which way the loss wants to move the action. The last factor propagates through the network. The middle factor says whether changing the QP parameter actually moves the selected action.
When no bound is active
Gradient descent increases , which increases the action toward 0.55. The optimizer lets the network’s changed preference reach the decision.
When the upper ramp bound is active
Reducing the incentive from 0.8 to 0.79 still returns action 0.6. The action is locally constant. Its loss is positive, but the gradient through this parameter path is zero.
Being able to differentiate an optimization layer does not mean that its gradient is always informative for training. In this example the constrained optimum is correct for the current objective, yet this local gradient cannot tell the network how far its incentive must move to leave saturation. Other parameter paths, losses, or a deliberate smoothing choice can change the learning behavior, but this calculation gives no automatic remedy.
At incentives 0.4 and 0.6 the action map has kinks. The QP is strongly convex and its optimum is unique, but ordinary differentiability still fails at those two points. Any derivative convention or generalized derivative used there must be distinguished from an ordinary derivative.
3. How can we differentiate a solution without a closed-form formula?
Clipping made the scalar example easy. With many variables and constraints, we usually do not have an explicit formula for the optimizer. OptNet differentiates the KKT conditions satisfied at the solution, rather than backpropagating through every iteration used by the solver to reach it.
3.1 Use the equations satisfied by the optimum
Consider the parameterized QP
Here collects the parameters determining the objective and constraints. Assume the feasible set is nonempty and the symmetric Hessian satisfies . At each such parameter value the primal optimum exists and is unique. The affine constraints ensure KKT multipliers exist, although redundant constraints can make those multipliers nonunique.
Using equality multipliers ν and nonnegative inequality multipliers λ, and suppressing the parameter arguments, the KKT conditions are
Collect the primal and dual solution variables:
Write the equality part of KKT as the following residual, with all QP data evaluated at :
At the primal–dual optimum the residual is zero. Primal inequality feasibility and nonnegative inequality multipliers are still required. A zero equality residual alone does not certify an arbitrary point.
3.2 Differentiate the residual equation
Ask: “After a small parameter change, how must the solution move so that the KKT equations remain true?” Differentiating the zero-residual relation gives
The first term is the residual change caused by motion of the primal–dual solution. The second is the direct parameter effect, evaluated while holding those variables fixed. When the residual is continuously differentiable and is nonsingular, the implicit function theorem yields
The useful computational statement is a linear system:
An implementation solves the system instead of forming an explicit inverse.
For this QP residual, the Jacobian has the block structure
The positive definiteness of H alone does not ensure this whole matrix is invertible. Redundant active constraints or an active inequality with zero multiplier can cause singularity. Independent equality and active-inequality gradients, together with strict complementarity, are a familiar sufficient regularity set for a positive definite QP with smooth local data. An unchanged active set then permits a smooth local solution branch.
3.3 Recover the ramp derivative directly from KKT
Use lower-bound constraint 0.4 − a ≤ 0 and upper-bound constraint a − 0.6 ≤ 0. Their KKT equations include
At incentive 0.45 both multipliers are zero and remain so locally. Differentiating stationarity gives da = dη, hence = 1.
At incentive 0.8 the upper multiplier is positive. Differentiate its complementarity equation:
The action cannot move to first order; the upper multiplier absorbs the changed incentive. At incentive 0.6, both the upper slack and multiplier are zero. The corresponding complementarity row has zero first derivative, the residual Jacobian is singular, and the inverse formula does not apply. This matches the kink in the explicit clipping solution.
4. Backpropagation needs a product, not the whole Jacobian
For a scalar loss that depends on through the solution, solve an adjoint system:
If the loss also depends directly on the QP parameter, add that direct partial derivative to the final gradient. The adjoint computes the needed Jacobian product without constructing a sensitivity matrix for every parameter.
OptNet’s solver uses a primal–dual interior-point method and reuses matrix-factorization work from the forward solve in the backward calculation. This reduces extra backward work in that implementation. It is not a general claim that solving and differentiating any constrained optimization problem costs nothing.
The calculation rests on the zero KKT residual and its local regularity. Feasibility, solution uniqueness, ordinary differentiability, and a useful training gradient each require their own check. The hard ramp example makes those distinctions visible even with a single decision variable.
References
- Amos, B., and Kolter, J. Z. (2017). OptNet: Differentiable Optimization as a Layer in Neural Networks. In Proceedings of the 34th International Conference on Machine Learning, PMLR 70, 136–145. Official metadata, ICML paper PDF, arXiv:1703.00443.
- Implicit Layers Tutorial, Chapter 5, “Differentiable optimization.” KKT residuals and implicit differentiation. Tutorial chapter.