---
title: Relative Flatness in Machine Learning
url: https://www.emergentmind.com/topics/relative-flatness
type: topic
---

# Relative Flatness in Machine Learning

Searching arXiv for recent papers on "relative flatness" in machine learning and related contexts.
Relative flatness is a family of notions in which “flatness” is measured relative to an ambient structure rather than as an absolute curvature quantity. In contemporary machine learning, the term usually denotes a reparameterization-aware layerwise measure of loss-surface curvature for models of the form \(f(x)=g(w\phi(x))\), where curvature is assessed with respect to one selected layer and weighted by the scale of that layer’s parameters [2001.00939]. In this usage, the central motivation is that classical Hessian-based flatness is sensitive to function-preserving reparameterizations, whereas relative flatness is designed to track feature-space robustness and generalization in a way that is invariant to neuron-wise rescaling and orthogonal transformations [2509.17738]. More broadly, the phrase also appears in geometry, representation learning, and visual place recognition, where it again denotes a context-dependent flatness notion: flatness relative to a base scheme, to anchor-induced manifold segments, or to boundary data rather than an absolute global geometry [1002.3652] [2606.01734].

## 1. Conceptual scope and terminology

In the machine-learning literature, relative flatness arose as a response to the reparameterization problem for classical sharpness measures. Traditional flatness measures based directly on the Hessian in parameter space can change under layer rescalings that leave the realized function unchanged, which weakens their explanatory power for generalization [2001.00939]. Relative flatness addresses this by evaluating curvature in relation to the scale and geometry of a chosen layer’s weights, rather than treating parameter-space curvature as an absolute object [2405.16918].

A standard decomposition writes the network as
\[
f(x)=g(w\phi(x)),
\]
with \(\phi\) a feature extractor and \(w\) the weights of a selected layer [2405.16918]. The term “relative” refers to the fact that curvature is measured with respect to that layer and to the scale of its parameters, rather than over the whole network in a reparameterization-sensitive way [2405.16918]. This suggests that the notion is fundamentally local in both architectural and geometric senses: it is attached to a representation layer and to perturbations meaningful for that layer.

Outside machine learning, the same modifier plays an analogous role. In algebraic geometry and commutative algebra, flatness is explicitly relative to a base \(Y\) or to an ideal \(\mathfrak a\), so that the question is not whether an object is flat in isolation, but whether it is flat over the specified base or along the \(\mathfrak a\)-adic neighborhood [1002.3652] [1606.01832]. In visual place recognition, FlatVPR uses “relative flatness” to mean flatness of a feature manifold with respect to the anchor segments used for map reconstruction, not absolute Euclidean flatness of the ambient embedding space [2606.01734].

## 2. Formal definitions in deep learning

The foundational machine-learning formulation is due to the relative-flatness framework adopted by Petzka et al. and used in later work [2001.00939]. For a model \(f(x,w)=g(w\phi(x))\), with \(w\in\mathbb{R}^{d\times m}\), relative flatness is defined by
\[
\kappa^\phi_{Tr}(w) := \sum_{s,s'=1}^d \langle w_s,w_{s'}\rangle \cdot \operatorname{Tr}\!\left(H_{s,s'}(w,\phi(S))\right),
\]
where \(w_s\) and \(w_{s'}\) are rows of the selected weight matrix and \(H_{s,s'}\) is the corresponding Hessian block of the empirical loss [2001.00939]. The weight inner products make the quantity parameterization-aware rather than a raw Hessian statistic.

A closely related trace proxy appears in adversarial-robustness work. For a chosen layer \(w\), the relative sharpness proxy is
\[
\kappa_{Tr}^{\phi}(w) := \|w\|_2\, \mathrm{Tr}(H(w,S)),
\]
with \(H(w,S)\) the Hessian of the loss with respect to \(w\), averaged over the dataset \(S\) [2405.16918]. Because small Hessian trace corresponds to a flatter landscape, this quantity is smaller for flatter minima and larger for sharper ones, so some papers use “relative sharpness” in practice to avoid sign confusion [2405.16918]. A closely parallel formulation is used for the penultimate layer in adversarial analysis:
\[
\kappa_{Tr}(W):=\|W\|_2\,\mathrm{Tr}\!\big(H(W,S)\big)
\]
for \(f(x)=g(W\phi(x))\) [2510.14231].

For cross-entropy and a single example, the Hessian admits a closed form:
\[
H(w,S)= (\mathrm{diag}(y)-yy^T)\otimes \phi\phi^T,
\]
and hence
\[
\mathrm{tr}(H)=\sum_{j=1}^k y_j(1-y_j)\sum_{i=1}^d \phi_i^2.
\]
This makes the measure computationally cheap for the selected layer and explains its dependence on prediction confidence, feature scale, and weight scale [2405.16918]. A related single-example penultimate-layer formula is
\[
H(W,\{(x,y)\}) = \big(\mathrm{diag}(\hat{y})-\hat{y}\hat{y}^{\top}\big)\otimes \phi(x)\phi(x)^{\top},
\]
with
\[
\kappa_{Tr}(W) = \|W\|_2 \sum_{j=1}^k \hat{y}_j(1-\hat{y}_j)\sum_{i=1}^d \phi_i(x)^2,
\]
which makes explicit that the measure can collapse in highly confident regions, including confidently wrong ones [2510.14231].

Later work generalizes the same idea to specialized architectures. For convolutional neural networks with a final convolutional layer, global average pooling, and softmax cross-entropy, the exact trace of the Hessian with respect to the convolutional kernels is
\[
\operatorname{Tr} \left( \nabla^2_K \mathcal{L} \right) = \left( \sum_{j=1}^{C_{\text{out}}} \hat{y}^{(j)} (1 - \hat{y}^{(j)}) \right) \cdot \left\| \bar{\phi} \right\|^2,
\]
and this is then inserted into a filter-aware relative-flatness functional specialized to convolutional layers [2603.09016].

## 3. Reparameterization invariance and relation to generalization

A central claim of the relative-flatness literature is that the quantity is intended to resolve the reparameterization pathology of classical sharpness measures. The 2020 formulation emphasizes that raw Hessian flatness can vary under layer-wise and neuron-wise rescalings that preserve the represented function, whereas the relative measure is invariant under layer-wise reparameterizations and neuron-wise reparameterizations, with a mild normalization step for the feature layer [2001.00939]. A later grokking study states the same point more tersely, emphasizing invariance to neuron-wise rescaling and orthogonal transformations [2509.17738].

The conceptual bridge to generalization is feature-space robustness. The identity
\[
\psi(w,\phi(x)+A\phi(x)) = \psi(w+wA,\phi(x))
\]
shows that multiplicative perturbations of the features correspond to structured perturbations of the selected layer’s parameters [2001.00939]. This allows loss variation under feature perturbations to be expressed in terms of curvature with respect to \(w\). Under locally constant labels in feature space and at a local minimum, the expected loss increase under orthogonally averaged feature perturbations is approximated by relative flatness:
\[
E_{\mathcal F^\phi}(f,S,\alpha A) \approx \frac{\delta^2}{2m}\,\kappa^\phi_{Tr}(\omega) + \mathcal O(\delta^3).
\]
This provides the paper’s formal mechanism linking flatness and generalization [2001.00939].

The same work situates flatness inside a broader decomposition of the generalization gap into a representativeness term and a feature-robustness term. If the training set is representative in feature space and labels are locally stable there, then flatness becomes the dominant local geometric contributor to generalization [2001.00939]. This suggests that relative flatness is not presented as a universal explanation, but as a conditional one.

Grokking offers a temporally separated test of that claim. In modular-arithmetic experiments with a 2-layer transformer, neural collapse and relative flatness both emerge near the onset of generalization, but only relative flatness aligns tightly with the actual transition: NCC starts decreasing already during memorization, while \(\kappa^\phi_{\mathrm{Tr}(w)}\) stays high until the point where validation accuracy begins to rise, at which moment it drops sharply [2509.17738]. In CIFAR-10 experiments, regularizing the network away from flat solutions using
\[
\mathcal{L}_{\text{NC\_RF}}=\mathcal{L}_{\text{CE}-\lambda\,\kappa^\phi_{\mathrm{Tr}(w)}}
\]
delays generalization, whereas suppressing neural collapse by
\[
\mathcal{L}_{\text{NC\_REG}}=\mathcal{L}_{\text{CE}-\lambda\cdot \mathrm{NCC}}
\]
does not prevent it [2509.17738]. Within the studied regimes, the conclusion is that relative flatness is argued to be necessary, while neural collapse is neither necessary nor sufficient for generalization [2509.17738].

## 4. Relative flatness as an optimization objective

Once relative flatness was formulated as a computable curvature quantity, it was used directly as a regularizer. FAM, or Relative Flatness Aware Minimization, optimizes
\[
\mathcal{L}(W) + \lambda\,\kappa(w^l),
\]
where \(\kappa(w^l)\) is the relative flatness of a selected layer, usually a penultimate or bottleneck layer [2307.02337]. The method is explicitly motivated by the claim that relative flatness corresponds to robustness of the representation produced by that layer and therefore need not be computed for the entire network [2307.02337].

The exact gradient involves both Hessian-trace terms and third derivatives. The paper gives a gradient decomposition of \(\nabla_W\kappa(w^l)\) into a term weighted by \(\operatorname{Tr}(H_{s,i})\) and a term involving third-order partial derivatives of the loss [2307.02337]. Because the exact pairwise version can be expensive, the paper also uses the simplified approximation
\[
\widehat{\kappa}(w^l)=\|w^l\|_2^2\,\operatorname{Tr}(H),
\]
which retains the scale-aware character of the measure while being easier to evaluate [2307.02337].

The empirical scope is broad. Reported results include CIFAR10, CIFAR100, SVHN, FashionMNIST, 3D skull-shape reconstruction, and BERT-base fine-tuning on RTE [2307.02337]. On CIFAR100, the reported values are Baseline \(84.48\), SAM \(85.72\), and FAM \(87.2\); on CIFAR10, Baseline \(95.53\), SAM \(95.61\), and FAM \(95.62\) [2307.02337]. On RTE, the reported validation accuracy improves from \(0.67364\) to \(0.6982\), while the standard deviation decreases from \(0.018\) to \(0.0154\) [2307.02337]. The same paper also records caveats: FAM is not uniformly better than SAM, its usefulness depends on the locally constant-label assumption, and current implementations can still be \(20\%\)–\(40\%\) slower than vanilla or SAM training [2307.02337].

This suggests that relative flatness functions both as an explanatory quantity and as an operational regularizer, but only under assumptions about feature geometry and label stability that remain explicit in the theory.

## 5. Adversarial robustness and the “flat uncanny valley”

Relative flatness became especially prominent in work on adversarial examples. A widely observed empirical pattern is that, during an iterative white-box PGD attack, the flatness of the loss surface around the attacked example first becomes sharper until the label flips, but if the attack continues it enters a flat region in which the label remains flipped [2405.16918]. The loss rises monotonically or saturates, while relative sharpness rises first and then falls. This is termed the “flat uncanny valley” [2405.16918].

The phenomenon was reported across CIFAR-10 and CIFAR-100 and across architectures including ResNet-18, WideResNet-28-4, VGG11, and DenseNet121, and it was still visible for adversarially trained models, although the valley was farther away and stronger attacks were needed to reach it [2405.16918]. The same overall pattern was also observed for jailbreak trajectories in Vicuna-7B, Llama-2-7B-Chat, and Guanaco-7B-HF, though it was weaker and less often truly flat because the input space is discrete and the attacks are comparatively weaker [2405.16918].

A subsequent theoretical treatment sharpened the interpretation. Relative flatness is shown to imply only local adversarial robustness, not global robustness [2510.14231]. Under an \(L\)-Lipschitz feature extractor with \(\|\phi(x)\|\ge r\), any perturbation \(\xi\) satisfying \(\|\xi-x\|\le\delta\) induces a controlled feature perturbation
\[
\phi(\xi)=\phi(x)+\Delta A\phi(x), \qquad \Delta \le \frac{L\delta}{r},
\]
for some orthogonal matrix \(A\) [2510.14231]. A Taylor expansion then yields
\[
\big|\ell(f(\xi),y)-\ell(f(x),y)\big| \le \Delta\|W\|_F\|\nabla_W \ell\|_F +\frac{\Delta^2}{2}\kappa^{\phi}_{Tr}(W) +R_2(W,\Delta),
\]
and after bounding the third-order remainder,
\[
\ell(f(\xi),y)-\ell(f(x),y) \le \frac{\delta^2}{2r^2}L^2\kappa^{\phi}_{Tr}(W) + \frac{\delta^3}{24r^3}kmL^6.
\]
Thus smaller relative sharpness implies slower local loss increase under small input perturbations, but the guarantee saturates and does not extend to the full attack trajectory [2510.14231].

The same paper stresses that robustness beyond a local neighborhood requires the loss to curve sharply away from the data manifold. If the loss stayed flat everywhere along an adversarial path, then the attack could move through large regions without penalty [2510.14231]. This is consistent with the empirical finding that adversarial examples often lie in large, flat regions where the model is confidently wrong [2510.14231]. A common misconception is therefore that flat minima imply global adversarial robustness; the cited results explicitly reject that implication [2405.16918] [2510.14231].

## 6. Specialized and adjacent formulations

Relative flatness has been specialized beyond dense classifiers. In CNNs with a \(1\times1\) convolution plus global average pooling classifier head, the exact symbolic Hessian trace makes it possible to compute a deterministic, architecture-aware flatness score rather than relying on stochastic trace estimation [2603.09016]. In the reported correlation study on 84 modified ResNet-18 models trained on CIFAR-10 for 30 epochs, the proposed score yields \(R^2 \approx 0.3367\), Pearson \(r=0.5803\), and Spearman \(\rho=0.7621\), with the authors emphasizing the stronger monotonic than linear trend [2603.09016]. The same study reports that SGD with momentum tends to find flatter minima and smaller generalization gaps than AdamW, and that flatness stabilization can be used as an early-stopping signal [2603.09016].

In representation learning for visual place recognition, FlatVPR uses “flatness” in a different but structurally analogous sense. The goal is to rectify a foundation model’s latent manifold so that any descriptor between two adjacent anchors \(\mathbf z_A\) and \(\mathbf z_B\) can be reconstructed by
\[
\hat{\mathbf z}_{pseudo}=(1-t)\mathbf z_A+t\mathbf z_B,\qquad t\in[0,1].
\]
The associated Pullback Flatness Loss is
\[
\mathcal{L}_{flat} = \sum_{t \in (A, B)} \left\| \mathbf{z}_t - \big((1-\alpha_t)\mathbf{z}_A + \alpha_t \mathbf{z}_B\big) \right\|^2,
\]
which penalizes deviation from the line segment connecting adjacent anchors [2606.01734]. The paper explicitly interprets this as relative flatness of the feature manifold with respect to anchor segments used for map reconstruction, not as a full Riemannian curvature estimate [2606.01734].

This usage is technically distinct from Hessian-based relative flatness, but the structural analogy is clear: in both cases, flatness is defined relative to a representation geometry and to perturbations or interpolations considered operationally meaningful. A plausible implication is that “relative flatness” functions as a general design pattern for geometry-aware stability measures rather than a single universal formula.

## 7. Other mathematical meanings of flatness relative to a base

In algebraic geometry, the phrase “relative flatness” takes its classical meaning: flatness of a coherent sheaf or morphism over a specified base. For an essentially finite type morphism \(f:X\to Y\) with \(Y\) smooth over a field and a coherent sheaf \(F\) on \(X\), flatness over \(Y\) is characterized by the existence of \(d\ge \dim Y\) such that
\[
f^d\big(\operatorname{Ass}(\,\textstyle\bigotimes_{i=1}^d \pi_i^*F\,)\big) \subseteq \{\text{generic points of } Y\}.
\]
Equivalently, in commutative algebra, if \(R\) is an essentially smooth \(K\)-algebra, \(A\) is essentially of finite type over \(R\), and \(M\) is a finite \(A\)-module, then
\[
M^{\otimes_R d} \text{ torsion-free for some } d>\dim R \;\Longrightarrow\; M \text{ flat over } R
\]
[1002.3652]. Here the relation is explicitly to the base ring or base scheme.

A different relative notion appears in \(\mathfrak a\)-adic flatness. An \(A\)-module \(M\) is \(\mathfrak a\)-adically flat if
\[
\operatorname{Tor}_i^A(N,M)=0 \quad\text{for all } i>0 \text{ and every \(\mathfrak a\)-torsion \(A\)-module } N.
\]
This is weaker than ordinary flatness because it tests only against \(\mathfrak a\)-torsion modules rather than all modules [1606.01832]. Under weak proregularity, completion preserves \(\mathfrak a\)-adic flatness, and in the noetherian complete setting the distinction collapses:
\[
\text{\(\mathfrak a\)-adically flat + \(\mathfrak a\)-adically complete} \quad \Longrightarrow \quad \text{flat}
\]
[1606.01832].

These mathematical uses are conceptually separate from neural-network loss geometry. Their commonality lies only in the modifier “relative”: flatness is always assessed with respect to a specified ambient object, such as a base scheme, an ideal, a feature layer, or anchor-induced representation segments.

## 8. Synthesis and open interpretive issues

Across domains, relative flatness is not a single invariant but a methodological principle: flatness should be measured relative to the geometry that matters for the task. In neural networks, that geometry is the penultimate or chosen feature layer and the perturbations that preserve the network’s functional semantics [2001.00939]. In adversarial analysis, the same measure is informative about local robustness basins but does not certify global correctness or robustness [2510.14231]. In grokking and standard classification, it is presented as more fundamental than neural collapse for predicting the onset of generalization [2509.17738]. In CNN-specific work, exact formulas make the notion architecturally faithful for GAP-based convolutional heads [2603.09016]. In VPR, the notion is reformulated as interpolation-consistency along anchor segments [2606.01734].

Several controversies or misconceptions recur. One is the belief that any flatness measure can explain generalization; the relative-flatness literature insists that the connection holds only under assumptions such as representativeness and local label constancy in feature space [2001.00939]. Another is the belief that flatness implies adversarial robustness simpliciter; the uncanny-valley results show that adversarial examples can occupy broad, flat plateaus on the wrong side of the decision boundary [2405.16918]. A third is the tendency to treat neural collapse as the geometric cause of generalization; the grokking results argue instead that neural collapse is best understood as one route toward relative flatness in some regimes [2509.17738].

Taken together, these works support a restrained interpretation. Relative flatness is a useful geometric lens because it links curvature to perturbations that are operationally meaningful and because it avoids the worst reparameterization pathologies of raw Hessian measures. At the same time, the cited results consistently show that flatness alone is insufficient: for generalization it must be coupled with representativeness and local label stability, and for robustness it must be coupled with smoothness or Lipschitz control of the feature extractor and with the behavior of the function away from the data manifold [2001.00939] [2405.16918].

Source: https://www.emergentmind.com/topics/relative-flatness