--- library_name: pytorch tags: [refltron, x-ray-reflectivity, polarized-reflectivity, inverse-problem, mlp, variable-q, fixed-q] datasets: [carbon-lab/reflectivity-1m] --- # Refltron ![Refltron: reflectivity in, depth profile out](figures/hero.png) The dynamic-q perceptron: a dense MLP that inverts polarized (s, p) X-ray reflectivity curves into thin-film parameters (film thickness / roughness, oxide thickness, and the δ, β optical tensor components). *Dynamic q*: the q grid is an input channel, so curves need not share a fixed grid. This repo collects the trained versions; a fixed-q variant is the next step. Each version lives on its own **branch**, with weights, config, training log, diagnostics and figures. ## Which version to use **Use `v3-gelu-rq4`.** It is the best on every metric in the validation table below, and the only version without dead units. The two ReLU versions are effectively tied with each other; pick `v2-relu-log` over `v1` only if bare-oxide (no film) samples matter, where it predicts the smallest spurious film (see the second table). ```bash hf download carbon-lab/refltron --revision v3-gelu-rq4 --local-dir refltron-v3 ``` ### Validation metrics All versions scored identically with `analysis/eval_versions.py`: `best.pt` (lowest validation loss) on the full held-out split (100,000 curves, dataset rows 900k+). ↓ lower is better, ↑ higher is better; best in **bold**. | version | best epoch | hybrid loss ↓ | depth-profile L2 ↓ | label MSE ↓ | thickness MAE Å ↓ | roughness MAE Å ↓ | oxide MAE Å ↓ | thickness R² ↑ | train / val gap | dead units | |---|---|---|---|---|---|---|---|---|---|---| | [`v1-relu-rq4`](https://huggingface.co/carbon-lab/refltron/tree/v1-relu-rq4) | 431 | 0.0492 | 0.0463 | 2.92e-03 | 28.2 | 1.61 | 0.81 | 0.944 | 0.024 / 0.049 | 29% | | [`v2-relu-log`](https://huggingface.co/carbon-lab/refltron/tree/v2-relu-log) | 509 | 0.0486 | 0.0455 | 3.07e-03 | 28.2 | 1.80 | 0.81 | 0.944 | 0.024 / 0.049 | 33% | | [`v3-gelu-rq4`](https://huggingface.co/carbon-lab/refltron/tree/v3-gelu-rq4) | 469 | **0.0421** | **0.0396** | **2.49e-03** | **24.1** | **1.25** | **0.70** | **0.952** | 0.016 / 0.042 | 0% | - **hybrid loss**: the training objective = depth-profile L2 + label MSE. The single number to rank versions by. - **depth-profile L2**: distance between predicted and true electron-density depth profiles (Si-aligned). - **label MSE**: mean squared error of the 7 labels, each scaled to [0, 1]. - **train / val gap**: final-epoch hybrid loss on training vs validation rows; all versions overfit by a similar margin, so the ranking is not an artefact of fitting. ### Thickness error by film thickness, and out-of-distribution behaviour | version | MAE 20–100 Å films | MAE 100–400 Å | MAE 400–800 Å | 90th pct error Å | bare oxide (0 Å) → predicted | 2000 Å film → predicted | |---|---|---|---|---|---|---| | `v1-relu-rq4` | 3.4 | 16.8 | 41.8 | 74 | 400 | 828 | | `v2-relu-log` | 3.3 | 17.7 | 41.2 | 74 | 144 | 897 | | `v3-gelu-rq4` | 2.7 | 13.5 | 36.4 | 65 | 328 | 692 | Error grows with thickness for every version (thicker films have denser Kiessig fringes). The two OOD columns are the mean prediction over the last 50 epochs on curves outside the training range (films are 20–800 Å): no version extrapolates past ~900 Å, and all predict a spurious film (~145–400 Å) on a bare oxide. ![benchmark](figures/benchmark.png) ## Training ![training curves](figures/training.png) ## Takeaways - **GELU beats ReLU** at the same input regularizer: about 13% lower hybrid loss and ~4 Å lower thickness MAE, and it has no dead units (the ReLU versions lose roughly a third of a layer). - **log10 vs R·q⁴** makes little difference for ReLU (v1 ≈ v2 on validation). - Cosine LR decay removes the epoch-to-epoch jumps seen with a constant rate; validation plateaus near epoch 450–500 while train loss keeps falling, so longer training does not help this architecture. - Kiessig-fringe detail is lost in the mixer → L1 → L2 stack (L1 and L2 have no activation between them and collapse into one rank-≤400 linear map); fringe decoder R² is ≈0 from L2 on. Older branches kept for reference: - `v3-gelu-rq4-e146`: early partial upload, superseded by `v3-gelu-rq4`. ## Architecture (shared by all versions) | stage | op | shape | activation | |---|---|---|---| | input | regularize (s, p) → (S, P), stack with q, per-channel standardize | (B, 3, 256) | – | | mixer | Conv1d 3→8, kernel 1 (pointwise channel mix at every q) | (B, 8, 256) → flatten 2048 | act | | L1 | Linear 2048→400 | 400 | **none** | | L2 | Linear 400→800 | 800 | act | | L3 | Linear 800→400 | 400 | act | | L4 | Linear 400→300 | 300 | act | | L5 | Linear 300→200 | 200 | act | | L6 | Linear 200→100 | 100 | act | | L7 | Linear 100→7 | 7 normalized labels | – | Labels: film_thickness, film_roughness, oxide_thickness, delta_xx, delta_zz, beta_xx, beta_zz (normalized to [0, 1]: `physical = min + y·(max − min)`, bounds below and in `config.json`). Training: hybrid loss (Si-aligned depth-profile L2 + normalized label MSE), Adam 5e-4 with cosine decay, batch 1024, seed 42, on [`carbon-lab/reflectivity-1m`](https://huggingface.co/datasets/carbon-lab/reflectivity-1m) (rows 900k+ held out). ## Figures - `figures/hero.png` — what the model does: one held-out curve, its recovered depth profile, and predicted vs true thickness over all validation curves (v3). - `figures/benchmark.png` — the versions compared on the same 100k validation curves: losses, per-label error, error vs film thickness, out-of-distribution predictions. - `figures/training.png` — training curves of all versions (v3 was resumed at epoch 143). - Per-version figures are on each branch under `figures/` (training snapshot, curve-morph, layer health, saliency, evolution, per-layer cards). ## Single-channel fixed-q baselines Alongside Refltron, three small baselines reproduce the original fixed-q Keras network (Greco et al., `code-old`) in PyTorch (`OldNet`, see [`network.py` on any `single-fixed-q-*` branch](https://huggingface.co/carbon-lab/refltron/blob/single-fixed-q-anisotropy/network.py)). They are much simpler than Refltron and only work on **one fixed q grid**: - **Input:** one curve of 256 numbers. Every curve is sampled at the same 256 q values (0.0044–0.184 Å⁻¹, linear; θ 0.99–46.57° at 250 eV), so q itself is never passed in. A value's position in the vector says which q it belongs to. The models cannot read curves at any other energy or q grid. - **Network:** Linear 256→400→800→400→300→200→100→labels, ReLU after every layer, MSE on labels scaled to [0, 1], Adam 5e-4, batch 128, 60 epochs, last 20% of rows held out (200,000 curves). - **Data:** the `fixed-q` (uniaxial) and `fixed-q-isotropic` branches of [`carbon-lab/reflectivity-1m`](https://huggingface.co/datasets/carbon-lab/reflectivity-1m/tree/fixed-q). | input | what the network gets | |---|---| | `s` | ln R_s, on isotropic films (δ_xx = δ_zz, β_xx = β_zz), so 5 labels | | `p` | ln R_p, on uniaxial films, 7 labels | | `anisotropy` | A = (P − S·c)/(P + S·c) with c = cos²2θ, on uniaxial films, 7 labels. For n ≈ 1, r_p ≈ r_s·cos 2θ, so A removes the purely geometric part of P and keeps the anisotropy. It stays within ±1 and swings near θ = 45° (the Brewster angle). | **Use `single-fixed-q-anisotropy`.** On the 7-label uniaxial task it reaches about half the validation loss of `p` (2.99e-03 vs 5.51e-03) and has a higher R² for film thickness, film roughness, oxide thickness, δ_xx, β_xx. `p` is only slightly better on the remaining labels. `s` has a lower loss still, but it solves an easier problem (isotropic films, 5 labels), so its numbers are not directly comparable. ```bash hf download carbon-lab/refltron --revision single-fixed-q-anisotropy --local-dir refltron-single-anisotropy ``` Validation errors of the best checkpoint on all held-out curves, as MAE (R²). Lengths are in Å. | model | data | labels | best epoch | val MSE ↓ | film thickness MAE (R²) | film roughness MAE (R²) | oxide thickness MAE (R²) | δ_xx MAE (R²) | δ_zz MAE (R²) | β_xx MAE (R²) | β_zz MAE (R²) | |---|---|---|---|---|---|---|---|---|---|---|---| | [`single-fixed-q-s`](https://huggingface.co/carbon-lab/refltron/tree/single-fixed-q-s) | fixed-q-isotropic | 5 | 58 | 2.71e-03 | 12.58 Å (0.992) | 1.12 Å (0.975) | 0.97 Å (0.885) | 1.50e-04 (0.994) | – | 9.16e-05 (0.991) | – | | [`single-fixed-q-p`](https://huggingface.co/carbon-lab/refltron/tree/single-fixed-q-p) | fixed-q | 7 | 53 | 5.51e-03 | 18.45 Å (0.983) | 1.87 Å (0.943) | 1.39 Å (0.792) | 3.08e-04 (0.951) | 1.60e-04 (0.988) | 3.63e-04 (0.821) | 8.49e-05 (0.990) | | [`single-fixed-q-anisotropy`](https://huggingface.co/carbon-lab/refltron/tree/single-fixed-q-anisotropy) | fixed-q | 7 | 55 | **2.99e-03** | 12.54 Å (0.994) | 0.84 Å (0.990) | 1.30 Å (0.807) | 2.02e-04 (0.981) | 2.19e-04 (0.979) | 1.04e-04 (0.986) | 1.01e-04 (0.986) | ### How the models see the fixed q grid Each marker is one input neuron. It always holds the value at the same q, for every curve. ![single-channel inputs](figures/single_channel_inputs.png) ### Training ![single-channel training curves](figures/single_channel_training.png) ### Quality ![single-channel prediction quality](figures/single_channel_quality.png) Each branch also holds a training snapshot (`figures/snapshot_best.png`: one training and one validation curve, their depth profiles and labels), the weights (`model.safetensors`, `best.pt`), `config.json`, `metrics.json` and the training log.