Optimising on a surrogate
The surrogate’s gradients lead to a better design, which is then re-simulated: optimisers tend to exploit errors.
Outcome: Optimise design parameters through a frozen surrogate, inside the training box; explain why optimisers tend to find surrogate errors, and how to guard against it; confirm the winner with the solver.
Builds on Choosing the next simulation, Loss, gradients and autograd
A neural surrogate is differentiable in its inputs, if they reach it through differentiable steps. The backpropagation that gives the gradient of the loss with respect to the weights also gives the gradient of the prediction with respect to the inputs: Example 4-3 printed it as design.grad. If the inputs are design parameters, that gradient says how to change the design to improve the predicted result, at the cost of one backward pass. Here it drives an optimiser.
Optimising the inputs
The recipe inverts the training loop: the network is frozen, and the design, a tensor with requires_grad=True, is handed to an optimiser. The loss is the objective to minimise, for example to maximise lift-to-drag. After each step, the design is clamped back into the training box.
The download stores no drag, so Example 8-3 solves a task it can check: the angle of attack that gives a target lift at a fixed sweep. The target is at , the value the solver gave in test run 46, so the right answer, , is known. The surrogate is the MLP that Example 4-5 trained on the 36 training runs and saved, loaded as in Example 4-8.
import numpy as np
import torch
from aircraft import MLP, load_runs
d = load_runs()
D = torch.tensor(np.c_[d["alpha"], d["sweep"]])
CL = torch.tensor(d["CL"])
# 1. The MLP of CL and its scaling, saved by the loop of 4.3.
ck = torch.load("cl_mlp.pt")
net = MLP()
net.load_state_dict(ck["model"])
mu, sd = torch.tensor(ck["mu"]), torch.tensor(ck["sd"])
def surrogate(design):
out = net((design - mu) / sd).squeeze(-1)
return out * ck["ys"] + ck["ym"] # back to CL
net.requires_grad_(False) # freeze the surrogate1
# 2. Optimise the design: which alpha gives the target CL
# at a sweep of 28 degrees?
target, sweep = 0.457, torch.tensor([28.0])
alpha = torch.tensor([2.0], requires_grad=True)
opt = torch.optim.Adam([alpha], lr=0.05)2
for _ in range(500):
cl = surrogate(torch.cat([alpha, sweep]))
loss = (cl - target) ** 2
opt.zero_grad()
loss.backward()
opt.step()
with torch.no_grad():
alpha.clamp_(-2.0, 8.0) # stay inside the box3
# 3. Check against the solver: test run 46, at alpha = 4
# and sweep = 28, gave CL = 0.457.
i = 46
print(f"alpha = {alpha.item():.2f}, surrogate CL = {cl.item():.3f}")
print(f"solver: alpha = {D[i, 0]:.2f}, CL = {CL[i]:.3f}")4
- Freezing: no gradients for the weights from here on, only for the design.
- The optimiser receives the design tensor, not the network’s weights.
- The clamp runs under
torch.no_grad()and in place, so it moves the design without becoming part of the graph that the next backward pass differentiates. - The solver at the surrogate’s answer. With a real solver it is a new run.
Example 8-3 prints alpha = 4.00, surrogate CL = 0.457 against the solver’s alpha = 4.00, CL = 0.457: inside the box, the surrogate’s answer is off by about 0.002° in .
The same recipe carries from one angle to a whole shape. In Baqué et al. 2018 a convolutional network on the surface mesh predicts pressure and drag, and its gradient with respect to the shape parameters drives the drag down under geometric constraints.
Optimisers tend to find the surrogate's mistakes
An optimiser searches for the input with the best predicted value.
Training usually fits values, not slopes: a model can match the solver's values well and still have wrong slopes between the runs. Its gradient at a few designs should be compared with the solver's, from finite differences (one extra run per variable) or an adjoint.
The finite-difference step comes from a step-size study (Martins and Ning 2021, chapter 6): too large a step misses the curvature, too small a step is lost in the solver's numerical noise.
Four guards, simplest first:
- Bounds and a trust region. The design stays inside the training box, and each optimisation round is limited to a small region around known runs (Alexandrov et al. 1998). An optimum on the edge of the box is less reliable: it has training runs on one side only.
- Penalise uncertainty. The objective is the predicted value plus k times the ensemble spread (for a minimisation), so uncertain regions look worse rather than better.
- Robust objectives. The objective is the worst case inside a conformal interval (uncertainty), so the design holds up if the error stays within the calibrated bound. An optimiser's pick may not resemble the calibration designs, so the bound is no guarantee there.
- Close the loop. The optimum is simulated and added to the data, the surrogate is retrained, and the optimisation repeats: active learning aimed at the optimum. With a Gaussian process and expected improvement this is the efficient global optimisation of Jones et al. 1998.
With or without gradients
Solvers can give gradients too, through adjoint methods: one adjoint solve, often at about the cost of one more simulation (Reuther et al. 1996), gives the gradient of one objective with respect to every design variable. A surrogate's gradient pays off mainly when no adjoint exists, or when many objectives, load cases or optimisation runs need gradients over and over.
Gradient-free optimisers such as differential evolution and genetic algorithms work with any surrogate, including non-differentiable ones, and can escape local optima more easily. Bayesian optimisation is also gradient-free, but it needs a surrogate that quantifies its uncertainty (active learning). A surrogate can make their many evaluations affordable, but their number grows quickly with the number of design variables; gradients scale better to tens or hundreds of variables (Martins and Ning 2021, chapter 1).
Either way, the surrogate has to be accurate wherever the optimiser goes. With many variables that takes a large design of experiments, or the trust regions and repeated runs of the guards above.
A surrogate-optimised design is a proposal. The winner has to be re-simulated with the solver, and tested where it matters.