Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Does a neural network improve this surrogate?

Chapter 10 comparison notebook. Start with the kernel notebook, then use this experiment to compare retrieval, linear interpolation, kernel ridge regression (KRR), and a small multilayer perceptron (MLP). Read the chapter, lab, and pilot guide.

The data come from a synthetic analytical sheet, not a full-wave solver or a manufactured structure. All amplitudes and frequencies are dimensionless. A notebook download runs independently after installing requirements-labs.txt; execution needs no external dataset, GPU, or network connection.

1. Declare the experiment before fitting

  • Use the starter’s 80 designs, 101 frequencies, seed 24, and 40/20/20 whole-design split.

  • Select KRR from 12 candidates and the MLP from 6 candidates on validation complex RMSE. These are explicitly different search budgets, not an equal-cost benchmark.

  • For neural selection use initialization 24 only. Freeze the selected architecture and regularization, then repeat with initializations 42 and 73. Report all three; never pick an initialization using test error.

  • Fit the input scaler on the 40 training designs only. Targets are 101 real followed by 101 imaginary values; they need no relative rescaling because both describe the same bounded amplitude.

  • Keep the training set fixed after selection: neither model is refitted using validation or test targets.

  • Reveal test metrics only after every model is frozen. The random split mostly assesses interpolation in this one-dimensional family; coverage diagnostics below identify any boundary cases.

  • Search the same 901 candidate coordinates for each continuous surrogate; retrieval returns one of the 40 actual training designs. Use the seed-24 MLP for that comparison, regardless of the test ranking.

The MLP uses tanh hidden units, linear outputs, L2 regularization, and L-BFGS. It has no internal validation split or early stopping. Optimizer warnings and iterations are reported. See the MLPRegressor reference for the parameter definitions.

This is a reproducible teaching comparison on publicly available synthetic data, not a private blind benchmark. The code keeps test targets out of fitting and model selection. If you redesign the protocol after reading its published test results, use a new held-out experiment for any new accuracy claim.

Python 3.12.1; NumPy 2.4.6; Matplotlib 3.11.1; scikit-learn 1.9.1; SciPy 1.18.1
Synthetic analytical evidence only. One BLAS thread during training for a modest CPU workload.

2. Reproduce the physical generator and split

With e−iωte^{-i\omega t}, equal surrounding media, and power-normalized ports,

Δ=Ω−(0.9+0.2g),γ=0.025+0.02g,r=−γγ−iΔ,t=1+r.\Delta=\Omega-(0.9+0.2g),\quad\gamma=0.025+0.02g,\quad r=-\frac{\gamma}{\gamma-i\Delta},\quad t=1+r.

Check lossless power balance, resonance, and the complete two-port matrix. For S=(rttr)S=\begin{pmatrix}r&t\\t&r\end{pmatrix}, the off-diagonal condition r∗t+t∗r=0r^*t+t^*r=0 is needed in addition to ∣r∣2+∣t∣2=1|r|^2+|t|^2=1 to establish unitarity. This is a check of the analytical generator, not a constraint automatically satisfied by a regression model.

Maximum generator balance residual: 5.551e-16
Maximum off-diagonal unitarity residual: 3.628e-16
Train: IDs=[0, 6, 7, 8, 11, 17, 22, 23, 25, 29, 31, 32, 33, 35, 36, 37, 38, 41, 42, 45, 49, 52, 53, 54, 56, 57, 58, 59, 61, 62, 63, 65, 66, 67, 68, 73, 75, 76, 77, 78]; outside training-coordinate range=0
Validation: IDs=[1, 2, 4, 10, 13, 16, 20, 21, 30, 34, 40, 43, 44, 46, 48, 50, 51, 69, 74, 79]; outside training-coordinate range=1
Test: IDs=[3, 5, 9, 12, 14, 15, 18, 19, 24, 26, 27, 28, 39, 47, 55, 60, 64, 70, 71, 72]; outside training-coordinate range=0

3. Retrieval, interpolation, and kernel regression

Retrieval chooses the nearest training coordinate. Linear interpolation brackets the query between training coordinates and blends their complex amplitudes, not wrapped phases. Outside the training range it clips to the closest endpoint; the coverage count above makes that limitation visible. It has no fitted hyperparameters.

KRR solves the same complex linear system as the starter. All candidate validation scores are retained. No result in this cell accesses test targets.

Loading...
Selected KRR: length=0.12, alpha=1e-08; validation RMSE=0.000643005

4. Train the neural surrogate

For one hidden layer, h=tanh⁡(W1g~+b1)\mathbf h=\tanh(W_1\widetilde g+\mathbf b_1) and y^=W2h+b2\widehat{\mathbf y}=W_2\mathbf h+\mathbf b_2. The real output vector packs [Re⁡t1,…,Re⁡tM,Im⁡t1,…,Im⁡tM][\operatorname{Re}t_1,\ldots,\operatorname{Re}t_M,\operatorname{Im}t_1,\ldots,\operatorname{Im}t_M].

The mean squared error averaged over these 2M2M coordinates is half the mean complex squared error averaged over MM frequencies. This constant factor preserves unregularized model rankings but should be remembered when interpreting the optimizer’s loss and regularization. We select every model with the same complex RMSE, calculated after unpacking outputs.

Input scaling is fitted once using training inputs. The hidden weights change during training, but the physical design coordinate remains the input. L-BFGS is used because the dataset is small. Convergence warnings remain visible and a capped run must not be described as converged.

layers=(16,), alpha=0.01: ConvergenceWarning: lbfgs failed to converge after 35 iteration(s) (status=2):
ABNORMAL: 

You might also want to scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
layers=(32,), alpha=0.01: ConvergenceWarning: lbfgs failed to converge after 53 iteration(s) (status=2):
ABNORMAL: 

You might also want to scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
layers=(32, 32), alpha=0.01: ConvergenceWarning: lbfgs failed to converge after 79 iteration(s) (status=2):
ABNORMAL: 

You might also want to scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Loading...
Selected MLP: layers=(32,), alpha=0.0001; parameters=6730
Primary seed 24; repeats (42, 73). Models frozen before test evaluation.
Total MLP selection fits=6, repeat fits=2.

5. Reveal the held-out comparison

All model choices are fixed. Report complex RMSE, transmission-power MAE (an absolute power fraction), amplitude MAE, worst pointwise complex error, and circular phase MAE where both amplitudes exceed 0.1. Also report the excluded fraction. Thresholding phase never excuses poor amplitude prediction.

For this sheet only, reconstruct r^=t^−1\widehat r=\widehat t-1 and report max⁡∣ ∣r^∣2+∣t^∣2−1∣\max|\,|\widehat r|^2+|\widehat t|^2-1|. A low residual does not mean the right spectrum: retrieval can conserve power while retrieving the wrong resonance. An interpolated complex amplitude may violate losslessness even though the endpoint spectra are lossless.

Loading...
Loading...
Loading...
MLP test RMSE across initializations: mean=0.0231928; sample standard deviation=0.00732324
Three initializations describe training variability on ONE fixed dataset split, not a confidence interval or split-generalization study.

Inspect the complex-transmission locus

For this symmetric equal-air electric sheet only, reconstruct reflection as predicted transmission minus one. A lossless response lies on a circle of radius one half centered at one half in the complex transmission plane. Inside the disk the reconstructed model is passive with positive absorption; outside it the model would require gain.

Aimplied=1−∣t^−1∣2−∣t^∣2=12−2∣t^−12∣2.A_{\mathrm{implied}}=1-|\widehat t-1|^2-|\widehat t|^2 =\frac12-2\left|\widehat t-\frac12\right|^2.

Complex linear interpolation joins exact training responses by chords. A chord stays in this passive disk but generally leaves the lossless circle, introducing artificial loss relative to the generator. Conversely, nearest retrieval stays on the circle even when it returns the wrong resonance. Numerical response accuracy and physical consistency answer different questions.

The plot uses the middle sorted test ID, chosen independently of model errors, and the existing frozen predictions. Its signed residual is a diagnostic of reconstructed model outputs, not measured material absorption. The table also checks every test geometry. Projecting a prediction onto the circle would not prove spectral accuracy, broadband causality, or physical realizability; the disk test is restricted to this model and basis.

<Figure size 1150x450 with 2 Axes>
Loading...
Signed diagnostics use every held-out design; the plot uses the predeclared middle sorted test ID.
Positive implied A is artificial loss relative to this lossless generator; negative implied A indicates gain in the reconstructed shunt model.

6. Inspect where errors occur

The illustrated test geometry is fixed by its position in the sorted test IDs, not selected after seeing which model wins. The scatter plot shows every held-out geometry. The vertical axis uses a logarithmic scale; compare the numbers as well as the curves.

<Figure size 1200x460 with 2 Axes>

7. Compare inverse proposals with the exact generator

Create one feasible target at g=0.623g=0.623. Each method sees its complex spectrum and searches the same bounded grid. The target coordinate is not passed to the search. For every selected coordinate, evaluate the analytical generator and compare predicted and verified target RMSE.

Retrieval has flat objective regions because it returns the same training spectrum for a range of queries. To avoid silently reporting a fictitious retrieved geometry, its candidate is the actual training coordinate with the smallest target mismatch. Its 40 distinct library candidates are a subset of the information used by the 901-grid search for the continuous surrogates. The grid resolution and candidate counts are printed explicitly.

The target happens to lie on the grid; a zero verified error is possible. This does not establish a continuous global optimum or performance on an unknown target family.

Loading...
Target g=0.623; continuous-surrogate grid=901; spacing=0.001; retrieval candidates=40
<Figure size 1200x450 with 2 Axes>

8. Interpret, reproduce, and extend

Use the output tables to answer these questions before changing anything:

  1. Which method has the smallest held-out complex RMSE? Does the answer change if you compare worst-case error or power balance instead?

  2. Does the MLP improve on nearest retrieval and linear interpolation? Does it improve on KRR? A negative answer is a valid scientific result.

  3. How large is initialization variability relative to the gap between methods? Would three initializations justify a general ranking across different datasets?

  4. Which inverse proposal has the smallest verified target error? Why can this ranking differ from the ranking of predicted errors?

  5. Explain why a retrieved spectrum can have negligible balance error and still match the target poorly.

For the report, retain split IDs, every validation candidate, selected settings, optimizer status, all three neural runs, both test-metric tables, and predicted-versus-verified inverse results. Report any warning without silently loosening the criterion.

Extensions are new experiments. Declare a new protocol before inspecting their tests: sweep the training count, withhold an entire design interval, vary linewidth, or add documented complex noise. Keep them separate from this reference comparison. Do not tune on the published test results and call the resulting test set untouched.

The next step toward a device study is Stage B of the lab: a converged full-wave library with documented normalization and a geometry-level split. These analytical results do not demonstrate fabricated performance, full-wave accuracy, computational speedup, or universal superiority of any model.