[Paper Review] Differentiable Top-k Operator with Optimal Transport
This paper proposes SOFT (Scalable Optimal transport-based diFferenTiable), a differentiable approximation of the top-k operator using entropic optimal transport (EOT) to enable end-to-end training in machine learning models. By formulating top-k selection as a regularized optimal transport problem and leveraging the Sinkhorn algorithm and KKT conditions, SOFT enables gradient computation through the top-k operation, achieving improved performance in k-NN classification, beam search, and sparse attention mechanisms.
The top-k operation, i.e., finding the k largest or smallest elements from a collection of scores, is an important model component, which is widely used in information retrieval, machine learning, and data mining. However, if the top-k operation is implemented in an algorithmic way, e.g., using bubble algorithm, the resulting model cannot be trained in an end-to-end way using prevalent gradient descent algorithms. This is because these implementations typically involve swapping indices, whose gradient cannot be computed. Moreover, the corresponding mapping from the input scores to the indicator vector of whether this element belongs to the top-k set is essentially discontinuous. To address the issue, we propose a smoothed approximation, namely the SOFT (Scalable Optimal transport-based diFferenTiable) top-k operator. Specifically, our SOFT top-k operator approximates the output of the top-k operation as the solution of an Entropic Optimal Transport (EOT) problem. The gradient of the SOFT operator can then be efficiently approximated based on the optimality conditions of EOT problem. We apply the proposed operator to the k-nearest neighbors and beam search algorithms, and demonstrate improved performance.
Motivation & Objective
- To address the non-differentiability of the standard top-k operation, which prevents end-to-end training in neural networks.
- To develop a smooth, continuous approximation of the top-k operator that supports gradient-based optimization.
- To enable differentiable k-NN classification, beam search, and sparse attention mechanisms through a unified differentiable top-k operator.
- To control approximation error while maintaining computational efficiency via entropic regularization and the Sinkhorn algorithm.
Proposed method
- Reformulate the top-k operation as the solution to an optimal transport (OT) problem, parameterizing the selection of top-k elements as a transport plan.
- Apply entropic regularization to the OT problem to create a differentiable approximation, resulting in the Entropic Optimal Transport (EOT) formulation.
- Use the Sinkhorn algorithm to efficiently compute the optimal transport plan, enabling scalable and differentiable top-k selection.
- Derive the gradient of the SOFT top-k operator using the Karush-Kuhn-Tucker (KKT) conditions of the EOT problem, allowing backpropagation through the top-k layer.
- Integrate the SOFT operator into downstream models such as k-NN classifiers, beam search, and sparse attention layers.
- Implement the method with computational complexity O(n), where n is the number of input elements, ensuring scalability.
Experimental results
Research questions
- RQ1Can the top-k operation be made differentiable while preserving its selection semantics?
- RQ2Can entropic optimal transport provide a smooth, continuous approximation to the discrete top-k operation with controlled error?
- RQ3Does the proposed differentiable top-k operator improve end-to-end training performance in k-NN classification, beam search, and attention mechanisms?
- RQ4How does the gradient of the SOFT operator behave during backpropagation, and does it align with the desired optimization objective?
Key findings
- The SOFT top-k operator enables end-to-end training of models that previously required two-stage training due to non-differentiability of top-k.
- In k-NN classification, the SOFT top-k operator achieves improved accuracy over baseline methods, including softmax-based and pretrained CNN baselines.
- In beam search for neural machine translation, the SOFT top-k operator improves generation quality by enabling differentiable decoding with gradient flow.
- The method achieves significant performance gains in sparse attention mechanisms, demonstrating the effectiveness of differentiable top-k in attention-based models.
- The gradient of the SOFT operator correctly pushes scores toward or away from the top-k set depending on the target label, as validated in toy experiments.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.