← Back to Autonomy

Agentic AI Notes / 01 of 02 / Same vs new data

Knowledge
Generalization

Did the model learn the underlying pattern, or did it just memorize the answers it was shown?

R(h) ≤ R̂(h) + complexity / √M True error on new data is at most your measured error plus a penalty. The highlighted term is the one that bites, and M is the number of truly independent examples, not rows.

The idea in one line

Same task, new examples

Generalization is about performance on data the model has never seen, drawn from the same world it was trained on. It is the gap between how well a model does on its training set and how well it does in the wild.

A model that generalizes has captured the real structure of the problem. A model that fails to generalize has memorized its training examples, including their noise, and falls apart the moment something slightly new arrives.

Think of a student studying for an exam. If they memorize the answers to last year's paper, they ace the practice test and fail the real one. If they learn the concepts, they handle questions they have never seen. Generalization is the difference between those two students.

Why it happens

The tug of war: complexity vs evidence

Every model sits between two failure modes. Underfitting means the model is too simple to capture the pattern, so it does poorly everywhere. Overfitting means the model is flexible enough to trace every wiggle in the training data, including random noise, so it looks brilliant on what it has seen and stumbles on what it has not.

The classic balance is simple to state: a more complex model needs more data to earn its complexity. With too little evidence for too many parameters, the model fills the gaps with noise.

training error test error model complexity → sweet spot the gap error
As a model grows more flexible, training error keeps falling. Test error falls at first, then climbs again as the model starts fitting noise. The widening distance between the two curves is the generalization gap.

Theory, in more depth

How the gap is actually controlled

Formally, the object of study is the gap between true risk and empirical risk. For a hypothesis \(h\), a loss \(\ell\), and a sample \(S\) of size \(n\) drawn from distribution \(\mathcal{D}\):

true risk vs measured risk $$R(h)=\mathbb{E}_{(x,y)\sim\mathcal{D}}\big[\ell(h(x),y)\big],\qquad \hat{R}_S(h)=\tfrac{1}{n}\sum_{i=1}^{n}\ell(h(x_i),y_i)$$

Every theory below is a different strategy for bounding \(R(h)-\hat{R}_S(h)\). They differ in what they blame the gap on: the richness of the model class, the algorithm, or the information the model absorbed.

AUniform convergence: Rademacher and VC

The classical route bounds the worst-case gap across the whole hypothesis class \(\mathcal{H}\) at once. Rademacher complexity measures how well the class can fit pure random noise:

Rademacher generalization bound $$R(h)\le \hat{R}_S(h) + 2\,\mathfrak{R}_n(\mathcal{H}) + 3\sqrt{\tfrac{\log(2/\delta)}{2n}}\,,\qquad \mathfrak{R}_n(\mathcal{H})=\mathbb{E}_{S,\sigma}\Big[\sup_{h\in\mathcal{H}}\tfrac{1}{n}\textstyle\sum_i \sigma_i h(x_i)\Big]$$

VC dimension is the distribution-free combinatorial version, giving bounds that scale like \(\tilde{O}(\sqrt{d_{\mathrm{VC}}/n})\). The weakness is built in: a supremum over all of \(\mathcal{H}\) is driven by the most pathological hypothesis, so for heavily overparameterized models these bounds become vacuous.

BMargin bounds

Margin bounds partly rescue this by rewarding classifiers that separate the data with confidence. The effective complexity becomes the margin-normalized norm of the weights rather than the raw parameter count. For deep networks, spectrally-normalized margin bounds scale with the product of the layer spectral norms divided by the margin, which is why a very large network can still generalize if it separates cleanly with a wide margin.

CPAC-Bayes: the most useful modern lens

Instead of one hypothesis, bound the expected risk of a whole distribution \(Q\) over hypotheses, relative to a data-independent prior \(P\) chosen before seeing the data:

PAC-Bayes bound $$\mathbb{E}_{h\sim Q}\big[R(h)\big]\le \mathbb{E}_{h\sim Q}\big[\hat{R}_S(h)\big] + \sqrt{\tfrac{\mathrm{KL}(Q\,\|\,P)+\log(n/\delta)}{2(n-1)}}$$

This produces the only non-vacuous bounds known for deep networks. The \(\mathrm{KL}\) term measures how far training had to move from the prior, so it connects directly to flat minima: a solution that tolerates weight perturbation needs a smaller \(\mathrm{KL}\), hence a tighter bound. Practical reading: flatter solutions generalize better.

DAlgorithmic stability and information

Stability sidesteps the class entirely and asks about the algorithm. If swapping one training point barely changes the output hypothesis (uniform stability \(\beta\)), the gap is \(O(\beta + 1/\sqrt{n})\). This is how stochastic gradient descent earns its generalization: small steps for not-too-many iterations. The information-theoretic view is a cousin: the gap is controlled by the mutual information \(I(S;h)\) between the training set and the learned model, so memorization is penalized literally as information leakage.

The modern puzzle

When fitting everything still works

Classical theory predicts the tidy U-shaped curve above. Reality is stranger. Very large models can fit their training data perfectly, even fit random labels, and still generalize. The U-curve extends into a second descent.

Interactive · drag through the double descent
interpolation test train model capacity → error
train error
test error
gap

No single explanation covers it. The contributing pieces: the implicit regularization of gradient descent, which drifts toward minimum-norm or maximum-margin solutions; the inductive bias of the architecture itself; and benign overfitting, where high-dimensional structure lets a model absorb noise without being hurt by it. The honest summary is that we have several partial accounts and no unified theory.

The levers

What you can actually control

Worked example · Vehicle Health Management

Count vehicles, not snapshots

Predict remaining useful life from condition-monitoring features (vibration RMS, bearing temperature, current-signature harmonics). The regressor uses a clipped absolute-error loss \(\ell(f(x),y)=\min(|f(x)-y|,B)\), with horizon cap \(B=1000\) cycles. For a feature map with \(\|\phi(x)\|\le R_\phi\) and a norm-bounded head \(\|w\|\le W\), the per-unit complexity is \(\mathfrak{R}_M(\mathcal{F})\le R_\phi W/\sqrt{M}\), and the bound reads:

RUL generalization bound $$R(f)\le \hat{R}(f) + 2L\,\mathfrak{R}_M(\mathcal{F}) + 3B\sqrt{\tfrac{\log(2/\delta)}{2\,\color{#A8432B}{M}}}$$
The trap and the correctionA factor of 22 hiding in plain sight
The setup

200 vehicles run to failure, each logging about 500 sensor snapshots. That is \(N\approx 10^5\) training rows. The tempting move is to plug \(n=N=10^5\) into the bound.

Naive number

With \(\delta=0.05\), the confidence term scales like \(\sqrt{\log(2/\delta)/(2N)}\approx 0.004\). Times the cap that is a few cycles. It looks fantastic.

Why it is wrong

The 500 snapshots inside one vehicle's degradation trajectory are heavily correlated, and the label is a deterministic countdown along that trajectory. The exchangeable unit is the vehicle, not the row. The honest sample size is \(M=200\), not \(10^5\).

Honest number

With \(M=200\), the same term is \(\sqrt{\log(2/\delta)/(2M)}\approx 0.10\), on the order of 100 cycles. The ratio is exactly \(\sqrt{N/M}=\sqrt{500}\approx 22\). That is the entire price of pretending correlated rows are independent.

Censoring tightens it further

A live fleet is mostly still running, so RUL labels are right-censored. The evidence that constrains the failure tail is closer to the number of observed failure events. If only 60 of 200 vehicles have failed, the effective \(M\approx 60\) and the term climbs to \(\approx 0.16\). Survival-aware losses (a Cox partial likelihood, or a censoring-weighted pinball loss) keep censored vehicles contributing instead of being dropped.

The takeaway

This is why a model can look excellent offline and disappoint on a brand-new vehicle. The fix at evaluation time is the same lesson the bound teaches: cross-validate by leaving out whole vehicles, never individual rows. And by the PAC-Bayes reading, a flatter, weight-noise-tolerant RUL head carries across model years more gracefully than a sharply fit one.

Calculator · count vehicles, not snapshots
naive term (N rows)
honest term (M units)
optimism factor

Measure it in practice

Probe the gap the honest way

Theory says the gap is governed by independent evidence. The practical translation is one rule: split by the unit that varies, not by the row. For fleet models that unit is the vehicle. Grouped cross-validation produces an estimate whose spread reflects vehicle-to-vehicle variation, which is the variation deployment will actually see.

from sklearn.model_selection import GroupKFold
import numpy as np

# X: features   y: RUL labels   vehicle_id: one group label per row
gkf = GroupKFold(n_splits=5)
errs = []
for tr, te in gkf.split(X, y, groups=vehicle_id):
    model.fit(X[tr], y[tr])
    pred = model.predict(X[te])
    errs.append(np.mean(np.abs(pred - y[te])))

# spread across folds reflects vehicle-to-vehicle variation
print(np.mean(errs), np.std(errs))

The standard deviation across folds is the honest error bar. If it is large, the model has learned vehicle-specific quirks rather than the shared degradation physics.

Common ways the gap hides
  • Row-level cross-validation. Random row splits leak the same vehicle into both train and test, so the score measures memorization rather than generalization.
  • Target leakage. A feature that quietly uses future information, such as a smoothed signal that peeks past the cut point, inflates offline accuracy and then vanishes in deployment.
  • Drift hidden by the split. Training and testing inside one model year conceals the shift the next model year will bring.
In agent systems

The same idea governs an agent's reliability. A tool-use policy that scores well on the traces it was tuned on may have simply memorized those tasks. The honest probe is to hold out whole task families, not individual steps, the agentic analogue of leaving out whole vehicles. A policy that survives unseen task families has learned the skill rather than the script.

References

Where to read more

  1. Shalev-Shwartz & Ben-David (2014). Understanding Machine Learning: From Theory to Algorithms. PAC learning, VC dimension, Rademacher complexity.
  2. Bartlett, Foster & Telgarsky (2017). Spectrally-normalized margin bounds for neural networks. NeurIPS.
  3. Dziugaite & Roy (2017). Computing nonvacuous generalization bounds for deep networks. UAI. The first meaningful PAC-Bayes bounds for deep nets.
  4. Hardt, Recht & Singer (2016). Train faster, generalize better: stability of SGD. ICML.
  5. Xu & Raginsky (2017). Information-theoretic analysis of generalization capability of learning algorithms. NeurIPS.
  6. Zhang, Bengio, Hardt, Recht & Vinyals (2017). Understanding deep learning requires rethinking generalization. ICLR.
  7. Belkin, Hsu, Ma & Mandal (2019). Reconciling modern machine-learning practice and the bias-variance trade-off. PNAS. Double descent.
  8. Bartlett, Long, Lugosi & Tsigler (2020). Benign overfitting in linear regression. PNAS.
Companion page · 02 of 02 Knowledge Transfer The same inequality with one extra term: what happens when the new data comes from a different world.