Uncertainty, structured prediction, and causal learning · Uncertainty, structured prediction, and causal learning · UNC013 · Training method
Invariant risk minimization (IRM / IRMv1)
Learn representations for which a common predictor is simultaneously suitable across several training environments. The aim is to reduce reliance on environment-specific correlations.
In CodeSplash
No method yet, but the reference's example runs here as a notebook.
Do it in CodeSplash Open the reference's example as a Python or R notebook.
- Availability
- Runs as a notebook
- Code example
- Complete calculation
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Learn representations for which a common predictor is simultaneously suitable across several training environments. The aim is to reduce reliance on environment-specific correlations. The exact constrained principle and the practical IRMv1 gradient penalty are different mathematical objects; this entry does not promise automatic causal discovery or universal out-of-distribution generalization.
Question / hypotheses
Does a learned representation support a common optimal predictor across observed environments, and does that help on new environments? Test the latter empirically; satisfying an approximate training penalty alone is insufficient.
Architecture / computational structure
A shared representation network feeds a predictor evaluated separately in each labeled environment. The ideal formulation constrains one predictor to minimize every environment’s risk. IRMv1 commonly fixes a dummy scalar classifier at one and penalizes the squared derivative of each environment risk with respect to that scalar.
Assumptions
Training environments must expose variation useful for distinguishing stable from spurious associations. The target mechanism and loss need compatible invariance assumptions. Identical or uninformative environments cannot identify the desired representation, and the practical scalar relaxation has documented counterexamples.
Formula / computational definition
Ideal IRM minimizes subject to for every e. A common IRMv1 surrogate is . For binary logistic risk with logits z, the scalar derivative is .
Parameters and notation
Learned: representation and predictor parameters, depending on the implementation. Hyperparameters/design choices: environment definitions, risk weights, penalty λ and its annealing schedule, minibatch composition, architecture and optimization controls. Environment construction is a scientific modeling choice, not a harmless metadata setting.
Fitting, fine-tuning, and adaptation
Fine-tune with environment-stratified batches and the declared objective. Adapting only a target-domain head can sacrifice the invariance constraint; retaining the penalty requires multiple meaningful target/source environments. Validate after any change in labels, representation scale or penalty schedule.
How to use and calibrate
Compare empirical-risk minimization and other robustness baselines using held-out environments. Report worst-environment and average risk, calibration, sensitivity to λ and multiple seeds. Monitor risk and penalty separately; a tiny penalty with poor prediction may reflect a degenerate representation.
Example application
Train an image classifier across acquisition sites with different background–label correlations, then evaluate on a site where those correlations change. Plot accuracy by site and background condition.
Interpretation / pitfalls
Watch out
IRMv1 can fail even in simple settings covered by its motivating intuition. Gradient scale and representation scale can interact with the penalty. Do not equate invariant features with proven causal variables or treat low average training risk as evidence of distributional robustness.
Python and R code examples
Scope: Exact two-environment logistic IRMv1 risk/gradient-penalty calculation for fixed logits, with finite-difference verification of the dummy-classifier derivative. Representation learning is not run.
Dependencies / validation: use the shared NumPy/SciPy or base-R setup above. Python executed successfully with assertions; R reviewed but not executed.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Complete calculation — Exact two-environment logistic IRMv1 risk/gradient-penalty calculation for fixed logits, with finite-difference verification of the dummy-classifier derivative. Representation learning is not run.
Shared setup for this chapter
import numpy as np
from scipy.special import logsumexp, gammaln
from scipy.linalg import expm
from itertools import product
def rbf(a, b, ell=1.0):
a, b = np.atleast_2d(a), np.atleast_2d(b)
return np.exp(-np.sum((a[:, None, :] - b[None, :, :])**2, axis=2)/(2*ell**2))
logits = [np.array([.4,-.3,1.2,-.8]),np.array([.2,-.6,.8,-.4])]
labels = [np.array([1.,0.,1.,0.]),np.array([1.,0.,0.,0.])]
risks=[]; penalties=[]
for z,y in zip(logits,labels):
risk=lambda w: np.mean(np.logaddexp(0,w*z)-y*w*z)
g=np.mean((1/(1+np.exp(-z))-y)*z)
fd=(risk(1+1e-6)-risk(1-1e-6))/(2e-6)
assert np.isclose(g,fd,atol=1e-7)
risks.append(risk(1)); penalties.append(g*g)
print({'risks':risks,'penalties':penalties,'objective':float(sum(risks)+10*sum(penalties))})
Complete calculation — Exact two-environment logistic IRMv1 risk/gradient-penalty calculation for fixed logits, with finite-difference verification of the dummy-classifier derivative. Representation learning is not run.
logits <- list(c(.4,-.3,1.2,-.8),c(.2,-.6,.8,-.4))
labels <- list(c(1,0,1,0),c(1,0,0,0)); risks <- penalties <- numeric(2)
for(e in 1:2) {
z <- logits[[e]]; y <- labels[[e]]
risk <- function(w) mean(log1p(exp(w*z))-y*w*z)
g <- mean((plogis(z)-y)*z)
fd <- (risk(1+1e-6)-risk(1-1e-6))/(2e-6)
stopifnot(abs(g-fd)<1e-7)
risks[e] <- risk(1); penalties[e] <- g*g
}
print(list(risks=risks,penalties=penalties,objective=sum(risks)+10*sum(penalties)))
Sources
- Arjovsky et al. IRM formulationarxiv.org
- Primary counterexample analysisarxiv.org
- Authors’ experimentsgithub.com
From the CodeSplash reference, entry UNC013.