Minibatch DRO against the full batch¶
Every other tutorial gives the ambiguity set the whole dataset. A training
loop at scale cannot do that, because each step sees only a minibatch. The
obvious approach is to run the ambiguity set on that minibatch alone, and
this changes the objective that is being minimized. This page measures how
large the change is and checks the two remedies in
optora.dro.minibatch against the full-batch
objective on the same problem.
Why the plug-in is biased¶
Write \(\hat P_m\) for the uniform distribution over a minibatch of \(m\) samples. The plug-in estimate is
The supremum is a nonlinear functional of the sample, so
\(\mathbb{E}[\hat V_m]\) is not the full-batch worst case, and
\(\nabla \hat V_m\) is not an unbiased stochastic gradient of it1. The
direction of the bias follows from the dual form of the worst case, the
one StochasticPhiDual uses below, which is an infimum of functionals
that are linear in the nominal distribution.
The worst case is therefore concave in the nominal, and since
\(\mathbb{E}[\hat P_m]\) is the full-batch distribution, Jensen's inequality
makes the plug-in biased low. For KL the plug-in is a log-sum-exp of a
subsample. The adversary can only upweight the high-loss points that the
minibatch happens to contain, and a small minibatch rarely contains the
rare ones.
The problem¶
The data come from the two-group regression of tutorial 07. There are \(N = 4096\) points, 90% of them with slope \((1, 0)\) and 10% with slope \((1, 1.5)\), and the noise level is \(0.3\). The model is linear with a bias, the loss is the squared loss, and the ambiguity set is a KL ball.
Minibatches are drawn with replacement from the dataset, so they are i.i.d. draws from its empirical distribution \(P_N\), and the full-batch objective
is exactly the population objective that every estimator targets. No
asymptotic argument enters the comparison. KLAmbiguitySet computes \(F\)
exactly on all \(N\) points, and every number below is measured against it.
Part 1, the bias of one estimate¶
The model is fixed at its least-squares fit. For each estimator the script draws 4000 independent minibatches, evaluates the estimate at every radius, and reports the bias as a fraction of the robust excess \(F - \bar\ell\), which is the amount the ambiguity set adds to the mean loss \(\bar\ell\).
| Radius | Plug-in, \(m = 16\) | \(m = 64\) | \(m = 256\) | Multilevel | Stochastic dual |
|---|---|---|---|---|---|
| \(4.7 \times 10^{-4}\) | \(-0.381\) | \(-0.188\) | \(-0.075\) | \(-0.074\) | \(+0.010\) |
| \(0.01\) | \(-0.441\) | \(-0.240\) | \(-0.106\) | \(-0.103\) | \(+0.009\) |
| \(0.05\) | \(-0.509\) | \(-0.302\) | \(-0.147\) | \(-0.140\) | \(+0.008\) |
| \(0.2\) | \(-0.584\) | \(-0.372\) | \(-0.197\) | \(-0.186\) | \(+0.007\) |
| \(1\) | \(-0.679\) | \(-0.464\) | \(-0.265\) | \(-0.250\) | \(+0.006\) |
The standard errors lie between \(0.005\) and \(0.015\), and the script prints them next to each bias. It asserts that the plug-in bias is negative at \(m = 16\) and increases with \(m\), that the multilevel bias matches the 256-sample plug-in within four combined standard errors, and that the bias of the stochastic dual is within four standard errors of zero.
- The plug-in recovers only part of the robust excess. At \(m = 64\) it reports 54% to 81% of the robust excess, and the shortfall grows with the radius because a larger ball puts more weight on the tail that a minibatch undersamples.
- The calibrated radius has the same bias. The first row is
kl_radius(N, 0.95), the radius under which the ambiguity set gives a 95% confidence interval for the risk (see Calibrating the ambiguity radius). A 16-sample plug-in reports only 62% of that correction. MultilevelMonteCarloEstimatorhas the bias of the 256-sample plug-in at an expected 68 samples per estimate, with base batch 16 and 4 levels21. Its variance is higher, and its standard error is about three times that of the 256-sample plug-in.StochasticPhiDualis unbiased on 64 samples at the optimal dual variables of the full batch. For fixed \((\eta, \lambda)\) the dual objective is an expectation, so its minibatch mean is an unbiased estimate of it, and at the optimal dual variables that expectation is \(F\).
A control variate, not a correction
Each estimate is reported minus the mean loss of its own minibatch, and for the multilevel estimator minus the mean loss of its base level. The subtracted term has the full-batch mean loss as its expectation, so the difference has the same bias as the estimate, while most of the sampling noise of the mean cancels. This keeps the standard errors near \(0.01\) with 4000 draws.
Part 2, training on minibatches¶
Each estimator now drives torch.optim.Adam on the model at radius
\(\rho = 0.2\), over 8 seeds, from a warm start fitted by least squares on a
64-sample pilot batch. Progress is measured on the full-batch objective as
the suboptimality \(F(\bar\theta) - \min F\), where \(\bar\theta\) is the
average of the second half of the iterates and \(\min F\) comes from a
full-batch L-BFGS reference.
| Estimator | Steps | Samples drawn | Mean suboptimality | Worst seed |
|---|---|---|---|---|
| Plug-in, \(m = 64\) | 800 | 51,200 | \(0.0616\) | \(0.0650\) |
| Multilevel | 500 | 36,448 | \(0.0213\) | \(0.0375\) |
| Stochastic dual, \(m = 64\) | 3000 | 192,000 | \(0.0028\) | \(0.0191\) |
For scale, the warm start is \(0.474\) above the optimum and the full-batch least-squares fit is \(0.415\) above it.
The plug-in descends quickly and then settles at a floor. The worst of the eight seeds is only 6% above their mean of \(0.06\). Independent minibatch noise would spread the seeds apart, so the floor comes from the bias. The plug-in gradient is an unbiased stochastic gradient of \(\mathbb{E}[\hat V_{64}]\), so training approaches the minimizer of that function, and that minimizer has a higher full-batch objective than the minimizer of \(F\).
The multilevel estimator goes below the plug-in floor while drawing fewer samples. Each of its steps runs three exact solves, namely the base level, the full batch, and its two halves as one batched solve, and the batches are larger on average. On a CPU a multilevel step takes more than twice as long as a plug-in step.
The stochastic dual has no inner solve and no bias, so each of its steps is the cheapest of the three, and with enough steps it reaches the lowest suboptimality. Its weakness is variance. KL's conjugate is exponential, so a minibatch that contains one high-loss point can produce a gradient far larger than the typical one. The worst seed is seven times the mean.
Start the dual variables on the scale of the loss
With the defaults \(\log \eta = 0\) and \(\lambda = 0\), the first
minibatches are far from the dual optimum, and the exponential
conjugate produces very large gradients. The StochasticPhiDual
docstring warns that this start can stall training under KL, and in
this script it makes training considerably slower. The script instead
starts \((\log \eta, \lambda)\) at the exact dual optimum of the pilot
batch, which uses only those 64 samples.
Which one to use¶
| Situation | Use |
|---|---|
| A fixed batch size, many cheap steps, and a chi-square or KL ball | StochasticPhiDual, with its dual variables started on the loss scale |
| A set without a finite conjugate (total variation), or an exact solve on every step | MultilevelMonteCarloEstimator |
| A small radius and large batches, where the measured bias is acceptable | The plug-in, which underestimates the full-batch worst case |
Reading the figure¶
- The left panel shows the bias as a fraction of the robust excess against the radius, with two-standard-error bars. The plug-in curves lie below zero and move further down as the radius grows. The multilevel curve lies on the 256-sample plug-in curve, and the stochastic dual lies on zero. The dotted vertical line marks the calibrated radius.
- The right panel shows the suboptimality of the averaged iterate on the full-batch objective against the number of samples drawn, on log axes, with the range over seeds shaded. The dotted horizontal line marks the plug-in floor. The plug-in curve settles on that floor after an early transient, and the other two continue below it.
Runtime
This tutorial takes around 20 to 30 seconds on a CPU. Every KL worst case is computed by a bisection with a fixed number of steps, so on 64-point minibatches the cost of a plug-in or multilevel step is the launch overhead of many small kernels rather than arithmetic.
The bias as the batch grows¶
The table of Part 1 measures the bias at three batch sizes. The animation sweeps the batch size from 8 to 2048 at the training radius \(0.2\) and draws two thousand minibatches at every size. The left panel shows the distribution of the plug-in estimate of the robust excess \(F - \bar\ell\) over those minibatches, together with the full-batch value. The middle panel shows the relative bias with its two-standard-error band, and it marks the multilevel estimator and the stochastic dual at their own batch sizes. The right panel shows the share of minibatches that contain at least one of the top 1% of losses. The bias shrinks as this share grows, because the adversary cannot upweight a point that the minibatch does not contain.
The figure¶
The figure below is rendered from this script when the site is built, so it always shows the output of the current code.
Source¶
"""Minibatch KL-DRO against the full-batch objective on the same problem.
Every earlier tutorial gives the ambiguity set the whole dataset. Training at
scale cannot do that, because each step sees a minibatch of `m` samples, and
the *plug-in* estimate, which runs the ambiguity set on that minibatch alone,
is biased. The worst case is a nonlinear functional of the sample, so its
expectation over minibatches is not the full-batch worst case, and its
gradient is not an unbiased stochastic gradient of the full-batch objective
(Levy et al. 2020). For KL-DRO the plug-in is the log-sum-exp of a subsample,
and it is biased *low*, because a small batch rarely contains the rare,
high-loss points that the adversary would upweight.
The problem. The dataset has `N = 4096` points from the two-group regression
of tutorial 07, with 90% of the points on slope `(1, 0)`, 10% on slope
`(1, 1.5)`, and noise level 0.3. The model is linear with a bias, the loss is
the squared loss, and the ambiguity set is a KL ball. Minibatches are drawn
with replacement from the dataset, so they are i.i.d. draws from its
empirical distribution and the full-batch objective
F(theta) = sup_{KL(q || uniform_N) <= radius} E_q[loss(theta)]
is *exactly* the population objective that the minibatch estimators target.
No asymptotic argument enters the comparison. Every number below is measured
against `F`, which `KLAmbiguitySet` computes exactly on all `N` points.
Part 1 fixes the model at its least-squares fit and measures the bias of each
estimator of `F`, as a fraction of the robust excess `F - mean(loss)`, over a
range of radii that includes the 95% calibrated radius `kl_radius(N, 0.95)`.
The estimators are the following.
* The plug-in on `m = 16, 64, 256` samples.
* `MultilevelMonteCarloEstimator` with base batch 16 and 4 levels, which has
the bias of the 256-sample plug-in at an expected batch of about 68
samples (Blanchet and Glynn 2015; Levy et al. 2020).
* `StochasticPhiDual` on 64 samples, at the dual variables that are optimal
for the full batch, where the expectation of its minibatch mean is
exactly `F`.
Each estimate is reported minus the mean loss of the same minibatch, and for
the multilevel estimator minus the mean loss of its base level. That control
variate has the full-batch mean loss as its expectation, so it leaves the
bias unchanged and removes most of the sampling noise of the mean itself.
Part 2 trains the model from a warm start (least squares on a pilot batch)
with `torch.optim.Adam` on each minibatch objective, over several seeds, and
tracks the suboptimality `F(theta_bar) - min F` of the averaged iterate
against a full-batch L-BFGS reference. The plug-in descends quickly and then
stalls at a floor that its bias sets, not its noise, so more steps do not
move it. The multilevel estimator goes below that floor on a comparable
number of samples, at three exact solves per step. The stochastic dual has
no inner solve and no bias, but KL's exponential conjugate makes its
gradients heavy-tailed. It therefore needs many more steps, each much
cheaper, and a start for its dual variables on the scale of the loss. Here
that start is the exact dual optimum of the pilot batch, which uses those
samples alone.
References:
Daniel Levy, Yair Carmon, John C. Duchi, and Aaron Sidford, "Large-Scale
Methods for Distributionally Robust Optimization", NeurIPS 2020.
Jose H. Blanchet and Peter W. Glynn, "Unbiased Monte Carlo for
Optimization and Functions of Expectations via Multi-Level
Randomization", Winter Simulation Conference 2015.
Jikai Jin, Bohang Zhang, Haiyang Wang, and Liwei Wang, "Non-convex
Distributionally Robust Optimization: Non-asymptotic Analysis",
NeurIPS 2021.
Run:
python tutorials/08_minibatch_dro_training.py
"""
import functools
import math
from collections.abc import Callable
import matplotlib.pyplot as plt
import torch
from _plotting import save_figure
from optora.dro import (
KLAmbiguitySet,
MultilevelMonteCarloEstimator,
StochasticPhiDual,
kl_conjugate,
kl_radius,
)
SEED = 20261026
NUM_POINTS = 4096
MINORITY_SHARE = 0.1
NOISE = 0.3
MAJORITY_SLOPE = (1.0, 0.0)
MINORITY_SLOPE = (1.0, 1.5)
BIAS_RADII = (kl_radius(NUM_POINTS, 0.95), 0.01, 0.05, 0.2, 1.0)
PLUG_IN_SIZES = (16, 64, 256)
BASE_BATCH, MAX_LEVEL = 16, 4
DUAL_BATCH = 64
REPLICATIONS = 4000
TRAIN_RADIUS = 0.2
NUM_SEEDS = 8
LEARNING_RATE = 0.03
STEPS = {"plug-in (m = 64)": 800, "multilevel": 500, "stochastic dual": 3000}
Estimator = Callable[[torch.Tensor], torch.Tensor]
def make_dataset(generator: torch.Generator) -> tuple[torch.Tensor, torch.Tensor]:
"""Draw the two-group regression dataset.
Args:
generator: Seeded generator.
Returns:
The `(N, 3)` design matrix (two features and a bias column) and the
`(N,)` targets, in float64.
"""
minority = torch.rand(NUM_POINTS, generator=generator) < MINORITY_SHARE
features = torch.randn(NUM_POINTS, 2, generator=generator, dtype=torch.float64)
slope = torch.where(
minority.unsqueeze(-1),
torch.tensor(MINORITY_SLOPE, dtype=torch.float64),
torch.tensor(MAJORITY_SLOPE, dtype=torch.float64),
)
noise = torch.randn(NUM_POINTS, generator=generator, dtype=torch.float64)
targets = torch.sum(slope * features, dim=-1) + NOISE * noise
design = torch.cat([features, torch.ones(NUM_POINTS, 1, dtype=torch.float64)], -1)
return design, targets
def squared_loss(
theta: torch.Tensor, design: torch.Tensor, targets: torch.Tensor
) -> torch.Tensor:
"""Per-sample squared loss of a batch of linear models.
Args:
theta: Parameters of shape `(..., 3)`.
design: Design rows of shape `(..., m, 3)` or `(m, 3)`.
targets: Targets of shape `(..., m)` or `(m,)`.
Returns:
Losses of shape `(..., m)`.
"""
prediction = torch.sum(design * theta.unsqueeze(-2), dim=-1)
return (prediction - targets) ** 2
def minimize(
parameters: list[torch.Tensor], objective: Callable[[], torch.Tensor]
) -> None:
"""Minimize `objective` over `parameters` to float64 precision with L-BFGS."""
optimizer = torch.optim.LBFGS(
parameters,
max_iter=500,
tolerance_grad=1e-12,
tolerance_change=1e-15,
line_search_fn="strong_wolfe",
)
def closure() -> torch.Tensor:
optimizer.zero_grad()
value = objective()
value.backward()
return value
optimizer.step(closure)
def relative_excess_bias(
estimates: torch.Tensor, batch_means: torch.Tensor, excess: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
"""Return the bias of an estimator as a fraction of the robust excess.
Args:
estimates: Estimates of shape `(radii, replications)`.
batch_means: The control variate, of shape `(replications,)`, whose
expectation is the full-batch mean loss.
excess: True robust excess `F - mean(loss)` per radius.
Returns:
The relative bias and its Monte Carlo standard error, per radius.
"""
excess_estimates = estimates - batch_means
replications = excess_estimates.shape[-1]
bias = (excess_estimates.mean(-1) - excess) / excess
standard_error = excess_estimates.std(-1) / math.sqrt(replications) / excess
return bias, standard_error
def estimator_bias(
design: torch.Tensor, targets: torch.Tensor, generator: torch.Generator
) -> dict[str, tuple[torch.Tensor, torch.Tensor]]:
"""Measure every estimator's bias against the full-batch worst case.
Args:
design: Full design matrix.
targets: Full targets.
generator: Seeded generator for the minibatch draws.
Returns:
Relative bias and standard error per radius, keyed by estimator.
"""
theta = torch.linalg.lstsq(design, targets.unsqueeze(-1)).solution.squeeze(-1)
loss = squared_loss(theta, design, targets)
radii = torch.tensor(BIAS_RADII, dtype=torch.float64).unsqueeze(-1)
uniform = torch.full((NUM_POINTS,), 1.0 / NUM_POINTS, dtype=torch.float64)
full_batch = KLAmbiguitySet(uniform, radius=radii.squeeze(-1))
excess = full_batch.worst_case_expectation(loss) - loss.mean()
def draw(size: int, count: int = REPLICATIONS) -> torch.Tensor:
return loss[torch.randint(NUM_POINTS, (count, size), generator=generator)]
results = {}
for size in PLUG_IN_SIZES:
batch = draw(size)
plug_in = KLAmbiguitySet(
torch.full((size,), 1.0 / size, dtype=torch.float64), radius=radii
)
results[f"plug-in (m = {size})"] = relative_excess_bias(
plug_in.worst_case_expectation(batch), batch.mean(-1), excess
)
multilevel = MultilevelMonteCarloEstimator(
functools.partial(KLAmbiguitySet, radius=radii),
BASE_BATCH,
MAX_LEVEL,
dtype=torch.float64,
)
sizes = torch.tensor(
[multilevel.sample_batch_size(generator) for _ in range(REPLICATIONS)]
)
estimates, base_means = [], []
for size in sizes.unique().tolist():
batch = draw(size, int((sizes == size).sum()))
estimates.append(multilevel(batch))
base_means.append(batch[..., :BASE_BATCH].mean(-1))
results["multilevel"] = relative_excess_bias(
torch.cat(estimates, dim=-1), torch.cat(base_means), excess
)
dual = StochasticPhiDual(
radii.squeeze(-1), kl_conjugate, initial_lam=float(loss.mean())
)
minimize(list(dual.parameters()), lambda: dual(loss).sum())
batch = draw(DUAL_BATCH)
with torch.no_grad():
dual_estimates = dual(batch.unsqueeze(-2)).transpose(0, 1)
results["stochastic dual"] = relative_excess_bias(
dual_estimates, batch.mean(-1), excess
)
return results
def pilot_dual_point(pilot_loss: torch.Tensor) -> tuple[float, float]:
"""Solve the KL dual exactly on the pilot batch.
The stochastic dual's variables carry the loss's scale, and a start far
from it makes KL's exponential conjugate produce rare, very large
gradients that slow Adam down or stall it. The minibatch dual optimum of
the pilot batch is computed from those samples alone, so it is a valid
start that uses no other data.
Args:
pilot_loss: Losses of the pilot batch at the warm-start model.
Returns:
The pilot batch's optimal `(log_eta, lam)`.
"""
dual = StochasticPhiDual(
torch.tensor(TRAIN_RADIUS, dtype=torch.float64),
kl_conjugate,
initial_lam=float(pilot_loss.mean()),
)
minimize(list(dual.parameters()), lambda: dual(pilot_loss))
return float(dual.log_eta.detach()), float(dual.lam.detach())
def train(
method: str,
design: torch.Tensor,
targets: torch.Tensor,
theta_start: torch.Tensor,
dual_start: tuple[float, float],
objective: Callable[[torch.Tensor], torch.Tensor],
) -> tuple[list[int], torch.Tensor]:
"""Train `NUM_SEEDS` models with Adam on one minibatch objective.
Training runs in float32, as a typical training loop would. The
full-batch objective is evaluated in float64.
Args:
method: Key of `STEPS` naming the estimator.
design: Full design matrix.
targets: Full targets.
theta_start: Warm start shared by every seed.
dual_start: Starting `(log_eta, lam)` of the stochastic dual.
objective: The full-batch objective `F`, on `(..., 3)` parameters.
Returns:
The number of samples drawn by each checkpoint, and `F` at the
average of the second half of the iterates up to each checkpoint,
of shape `(checkpoints, NUM_SEEDS)`.
"""
generator = torch.Generator().manual_seed(SEED + len(method))
design32, targets32 = design.float(), targets.float()
theta = theta_start.float().expand(NUM_SEEDS, 3).clone().requires_grad_()
parameters = [theta]
if method == "stochastic dual":
dual = StochasticPhiDual(
torch.full((NUM_SEEDS,), TRAIN_RADIUS),
kl_conjugate,
initial_log_eta=dual_start[0],
initial_lam=dual_start[1],
)
parameters += list(dual.parameters())
estimate: Estimator = dual
elif method == "multilevel":
estimate = MultilevelMonteCarloEstimator(
functools.partial(KLAmbiguitySet, radius=TRAIN_RADIUS),
BASE_BATCH,
MAX_LEVEL,
)
else:
estimate = KLAmbiguitySet(
torch.full((DUAL_BATCH,), 1.0 / DUAL_BATCH), radius=TRAIN_RADIUS
).worst_case_expectation
optimizer = torch.optim.Adam(parameters, lr=LEARNING_RATE)
schedule = torch.optim.lr_scheduler.LambdaLR(
optimizer, lambda step: 1.0 / math.sqrt(1.0 + step / 100.0)
)
steps = STEPS[method]
checkpoints = sorted({int(x) for x in torch.logspace(1, math.log10(steps), 25)})
history = torch.empty(steps, NUM_SEEDS, 3, dtype=torch.float64)
samples, drawn, gaps = [], 0, []
for step in range(steps):
if isinstance(estimate, MultilevelMonteCarloEstimator):
size = estimate.sample_batch_size(generator)
else:
size = DUAL_BATCH
index = torch.randint(NUM_POINTS, (NUM_SEEDS, size), generator=generator)
loss = squared_loss(theta, design32[index], targets32[index])
optimizer.zero_grad()
estimate(loss).sum().backward()
optimizer.step()
schedule.step()
history[step] = theta.detach().double()
drawn += size
if step + 1 in checkpoints:
averaged = history[(step + 1) // 2 : step + 1].mean(0)
samples.append(drawn)
gaps.append(objective(averaged))
return samples, torch.stack(gaps)
def main() -> None:
generator = torch.Generator().manual_seed(SEED)
design, targets = make_dataset(generator)
bias = estimator_bias(design, targets, generator)
print(f"bias of the robust excess, as a fraction of it ({REPLICATIONS} draws)")
print(" radius " + "".join(f"{name:>22}" for name in bias))
for index, radius in enumerate(BIAS_RADII):
cells = "".join(
f"{float(b[index]):>+13.3f} +- {float(s[index]):.3f}"
for b, s in bias.values()
)
print(f" {radius:<8.2g}{cells}")
plug_in = [bias[f"plug-in (m = {size})"][0] for size in PLUG_IN_SIZES]
largest_error = bias[f"plug-in (m = {PLUG_IN_SIZES[-1]})"][1]
assert bool(torch.all(plug_in[0] < 0.0))
assert bool(torch.all(plug_in[0] < plug_in[1])), "bias shrinks with m"
assert bool(torch.all(plug_in[1] < plug_in[2])), "bias shrinks with m"
multilevel_bias, multilevel_error = bias["multilevel"]
combined_error = torch.sqrt(multilevel_error**2 + largest_error**2)
assert bool(torch.all(torch.abs(multilevel_bias - plug_in[2]) < 4 * combined_error))
dual_bias, dual_error = bias["stochastic dual"]
assert bool(torch.all(torch.abs(dual_bias) < 4 * dual_error))
uniform = torch.full((NUM_POINTS,), 1.0 / NUM_POINTS, dtype=torch.float64)
full_batch = KLAmbiguitySet(uniform, radius=TRAIN_RADIUS)
def objective(theta: torch.Tensor) -> torch.Tensor:
return full_batch.worst_case_expectation(squared_loss(theta, design, targets))
reference = torch.zeros(3, dtype=torch.float64, requires_grad=True)
minimize([reference], lambda: objective(reference))
optimum = objective(reference.detach())
(gradient,) = torch.autograd.grad(objective(reference), reference)
assert float(gradient.norm()) < 1e-8
pilot = torch.randint(
NUM_POINTS, (DUAL_BATCH,), generator=torch.Generator().manual_seed(SEED)
)
theta_start = torch.linalg.lstsq(
design[pilot], targets[pilot].unsqueeze(-1)
).solution.squeeze(-1)
dual_start = pilot_dual_point(
squared_loss(theta_start, design[pilot], targets[pilot])
)
least_squares = torch.linalg.lstsq(design, targets.unsqueeze(-1)).solution
erm_gap = float(objective(least_squares.squeeze(-1)) - optimum)
start_gap = float(objective(theta_start) - optimum)
print(f"\nfull-batch optimum at radius {TRAIN_RADIUS}: F* = {float(optimum):.5f}")
print(f" F(least squares) - F* = {erm_gap:.4f}")
print(f" F(warm start) - F* = {start_gap:.4f}")
curves = {}
print("\nsuboptimality F(theta_bar) - F* of the averaged iterate, over seeds")
print(f" {'method':<20}{'steps':>7}{'samples':>10}{'mean':>10}{'max':>10}")
for method in STEPS:
samples, values = train(
method, design, targets, theta_start, dual_start, objective
)
gaps = values - optimum
assert bool(torch.all(gaps > -1e-9)), method
curves[method] = (samples, gaps)
final = gaps[-1]
print(
f" {method:<20}{STEPS[method]:>7}{samples[-1]:>10}"
f"{float(final.mean()):>10.4f}{float(final.max()):>10.4f}"
)
# Every seed stalls at nearly the same suboptimality. This is a floor set
# by the bias, since independent minibatch noise would spread the seeds.
plug_in_final = curves["plug-in (m = 64)"][1][-1]
floor = float(plug_in_final.mean())
assert float(plug_in_final.max() - plug_in_final.min()) < 0.5 * floor
assert float(curves["multilevel"][1][-1].mean()) < 0.5 * floor
assert float(curves["stochastic dual"][1][-1].mean()) < floor
colors = {name: f"C{index}" for index, name in enumerate(bias)}
fig, (ax_bias, ax_train) = plt.subplots(1, 2, figsize=(12, 4.5))
for name, (values, errors) in bias.items():
ax_bias.errorbar(
BIAS_RADII,
values,
yerr=2 * errors,
marker="o",
capsize=3,
color=colors[name],
label=name,
)
ax_bias.axhline(0.0, color=plt.rcParams["text.color"], linestyle="--")
ax_bias.axvline(
BIAS_RADII[0], color="gray", linestyle=":", label="kl_radius(N, 0.95)"
)
ax_bias.set_xscale("log")
ax_bias.set_xlabel("radius")
ax_bias.set_ylabel("bias / robust excess")
ax_bias.set_title("minibatch estimates of the full-batch worst case")
ax_bias.legend(fontsize="small")
for method, (samples, gaps) in curves.items():
mean = gaps.mean(-1)
color = colors[method]
ax_train.plot(samples, mean, color=color, label=method)
ax_train.fill_between(
samples, gaps.amin(-1), gaps.amax(-1), color=color, alpha=0.15
)
ax_train.axhline(floor, color="gray", linestyle=":", label="plug-in floor")
ax_train.set_xscale("log")
ax_train.set_yscale("log")
ax_train.set_xlabel("samples drawn")
ax_train.set_ylabel("F(averaged iterate) - min F")
ax_train.set_title(f"training at radius {TRAIN_RADIUS} (range over seeds)")
ax_train.legend(fontsize="small")
fig.tight_layout()
save_figure(fig, "08_minibatch_dro_training")
if __name__ == "__main__":
main()
-
Daniel Levy, Yair Carmon, John C. Duchi and Aaron Sidford, "Large-Scale Methods for Distributionally Robust Optimization", NeurIPS (2020). arXiv:2010.05893. ↩↩
-
Jose H. Blanchet and Peter W. Glynn, "Unbiased Monte Carlo for Optimization and Functions of Expectations via Multi-Level Randomization", Winter Simulation Conference (2015). ↩