Fast Differentiable Clipping-Aware Normalization and Rescaling

Jonas Rauber, Matthias BethgearXiv 2020

TL;DR

When adding controlled noise or perturbations to bounded data (like images), subsequent clipping to stay within valid bounds inadvertently shrinks the perturbation's size. This paper introduces a fast, exact, and fully differentiable analytical algorithm to calculate the perfect pre-clipping scale factor, replacing slow and non-differentiable iterative binary searches.

What Problem Does This Paper Address?

In fields like machine learning and vision science, researchers frequently perturb data vectors—for instance, adding noise to regularize a model or applying adversarial perturbations to test robustness. However, real-world data lives in bounded domains; image pixels, for example, must fall within $[0, 1]$ or $[0, 255]$.

When a scaled perturbation is added to an image, some pixel values inevitably exceed these boundaries. The standard fix is element-wise clipping. Unfortunately, clipping alters the vector, reducing the effective size (norm) of your perturbation and preventing you from fully utilizing your "perturbation budget."

To counteract this, one must upscale the initial perturbation. Finding the exact scaling factor ($\eta$) that yields the desired norm after clipping has traditionally required an iterative binary search. This approach is not only slow but also non-differentiable, meaning you cannot backpropagate through it to train neural networks.

Methods

The authors resolve this interference between rescaling and clipping by deriving an exact analytical solution.

They demonstrate that the $p$-th power of the post-clipping perturbation norm can be mathematically reformulated as a piecewise linear function of $\eta^p$. By analyzing the clipping boundaries element-by-element, the total norm can be expressed as a sum of simple minimum functions:

$$\sum_{i=1, \delta_i \neq 0}^n \min\left(|\delta_i|^p \eta^p, |c_i - x_i|^p\right)$$

Where $c_i$ represents the upper or lower bound depending on the direction of the perturbation.

Because this relationship is piecewise linear, the exact optimal value for $\eta$ can be found by sorting the critical transition points where individual elements begin to clip. Using cumulative sums (cumsum), the algorithm efficiently pinpoints the correct linear segment and solves for $\eta$ directly. The entire process is non-iterative, works for any $p$-norm, and relies on standard differentiable operations.

Datasets

Not discussed in the paper. The text focuses entirely on the mathematical derivation and algorithmic implementation of the rescaling function.

Key Insights and Ideas

  • The Piecewise Linear Trick: The core breakthrough is realizing that despite the non-linear nature of clipping, the cumulative effect on the $p$-th power of the norm behaves as a predictable piecewise linear function of $\eta^p$.
  • Unlocking Backpropagation: Because the solution is analytical rather than iterative, it is fully differentiable. This allows gradients to flow directly through the clipping and rescaling operations during neural network training.
  • Write Once, Run Anywhere: By implementing the algorithm via EagerPy, the authors provide a single codebase that deploys natively across PyTorch, TensorFlow, JAX, and NumPy.

Key Results

  • Exact vs. Approximate: The algorithm provides the mathematically exact scaling factor in a single pass, eliminating the approximation errors inherent to binary search.
  • Algorithmic Efficiency: The computational bottleneck is shifted to a simple sort operation ($O(N \log N)$), making it highly efficient for high-dimensional data like images.
  • New Applications Enabled: The authors highlight two direct use cases in adversarial robustness:
  • Adversarial Noise Attacks: Generating exact-norm noise attacks instantly without iterative overhead.
  • Learning Adversarial Noise: End-to-end training to optimize worst-case noise distributions, which requires backpropagating through the constraint boundaries.

Limitations and Future Work

As a brief technical report, the paper does not include empirical wall-clock timing comparisons or large-scale benchmarks to quantify the exact speedup over binary search in practice. Additionally, while the algorithm elegantly handles hyper-rectangular bounds (like $[a, b]^n$), it does not explicitly address more complex, non-linear data domain constraints.

Summary updated

BibTeX
@misc{rauber2020fast,
  author = {Jonas Rauber and Matthias Bethge},
  title = {{Fast Differentiable Clipping-Aware Normalization and Rescaling}},
  year = {2020},
  eprint = {2007.07677},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2007.07677},
}
Figure