An Efficient Plugin Method for Metric Optimization of Black-Box Models

Siddartha Devic, Nurendra Choudhary, Anirudh Srinivasan, Sahika Genc, Branislav Kveton, Gaurush Hiranandani · DOI

Abstract

In the modern machine learning landscape, practitioners frequently deploy "black-box" models—proprietary systems accessible only via API, such as Google Vision or Amazon Rekognition. These models present a significant challenge: they cannot be fine-tuned or modified directly to account for distribution shifts or to optimize for specific performance metrics like the F1-score. This paper introduces CWPlugin (Coordinate-Wise Plugin), a lightweight, post-hoc adaptation method that optimizes black-box classifiers using only a small set of labeled examples and the model's probability outputs. By employing a coordinate-wise search to determine optimal class-reweighting factors, CWPlugin effectively adapts models to new tasks and metrics, outperforming traditional calibration and fine-tuning in low-data regimes.

Introduction

The reliance on third-party AI APIs has created a paradigm where users have zero access to underlying weights, architectures, or training data. This "API-only" constraint introduces two primary hurdles:

  1. Distribution Shift: A model trained on one demographic or geographic dataset (e.g., California census data) may underperform when applied to another (e.g., Texas census data).
  2. Metric Mismatch: While most models are trained to maximize accuracy via cross-entropy loss, real-world business cases often require the optimization of non-decomposable metrics such as the F-measure, G-mean, or Matthews Correlation Coefficient.

Existing adaptation techniques, such as LoRA or standard fine-tuning, are inapplicable without weight access. Meanwhile, existing post-hoc methods often rely on solving complex global linear systems or exhaustive grid searches that fail to scale as the number of classes increases. CWPlugin addresses these limitations by providing a feature-agnostic, computationally efficient re-weighting strategy.

Methodology: The CWPlugin Framework

CWPlugin functions as a "plug-and-play" layer situated on top of a black-box model's output probabilities. Rather than attempting to modify the internal parameters of the model, the method learns a vector of weights to scale the predicted class probabilities at inference time.

The core innovation of CWPlugin is its use of coordinate-wise optimization. In a classification task with $m$ classes, finding the optimal weight for every class simultaneously is an exponentially difficult problem. CWPlugin simplifies this by:

  1. Fixing a Reference: One class is held constant as a reference point.
  2. Pairwise Line Search: The algorithm performs a simple line search to find the optimal relative weight for every other class individually through pairwise comparisons.
  3. Normalization: These individual weights are normalized into a final vector used to adjust the black-box model’s predictions.

This approach reduces the computational complexity from $O(1/\epsilon^m)$ to $O(mn/\epsilon)$. For "quasi-concave" metrics like the F-measure, the search can be further optimized using binary search, reaching a complexity of $O(m \log(1/\epsilon))$.

Experimental Setup

The researchers evaluated CWPlugin across a diverse array of tasks to demonstrate its versatility across different data modalities:

  • Tabular Data (ACSIncome): Predicting income levels using US Census data, specifically testing the model's ability to handle distribution shifts between different states.
  • Sentiment Analysis (lmtweets): Utilizing Twitter data from SemEval-2017 to test metric optimization.
  • Emotion Classification (lmemotions): Testing both in-distribution and out-of-distribution (OOD) performance.
  • Natural Language Inference (SNLI & ANLI): Assessing robustness against label shift (changing class proportions) and label noise (erroneous training labels).

Key Insights and Results

Small Data Supremacy

CWPlugin demonstrates remarkable efficiency when labeled data is scarce. On the lmtweets task with only 160 samples, CWPlugin improved the F-measure by 0.13 over the base black-box model. Notably, it also outperformed a fully fine-tuned BERT model by 0.1 in the same low-resource setting.

Comparison with Fine-Tuning

Empirical results suggest that CWPlugin remains the superior choice for model adaptation until the available validation set exceeds approximately 400 samples. This makes it an ideal solution for practitioners working with limited budgets for manual labeling.

Theoretical Consistency

Beyond empirical success, the authors provide theoretical proof that CWPlugin is a "consistent" estimator for linear-diagonal metrics. This ensures that as more data becomes available, the method is guaranteed to converge toward the Bayes optimal predictor for the target metric.

Discussion and Limitations

While CWPlugin offers a robust solution for black-box optimization, its performance is subject to certain conditions:

  • Reference Class Sensitivity: The choice of the "fixed" reference class can influence the final performance. While the study used the final class ($m$) as a default, future work may explore more principled selection methods.
  • Probability Quality: The method assumes the black-box model provides meaningful probability scores. If a model is severely overconfident or produces uninformative probabilities, the effectiveness of the re-weighting layer may be diminished.

Conclusion

CWPlugin represents a significant step forward in making proprietary AI models more adaptable and useful for specific downstream tasks. By shifting the focus from global optimization to efficient coordinate-wise search, it provides a practical, feature-agnostic tool for optimizing metrics in the increasingly common "API-only" world of machine learning.

Summary updated

BibTeX
@misc{devic2025efficient,
  author = {Siddartha Devic and Nurendra Choudhary and Anirudh Srinivasan and Sahika Genc and Branislav Kveton and Gaurush Hiranandani},
  title = {{An Efficient Plugin Method for Metric Optimization of Black-Box Models}},
  year = {2025},
  eprint = {2503.02119},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.02119},
}
Figure