TorchSpin: Differentiable Electron Paramagnetic Resonance Simulation in PyTorch
Organizations: Department of Chemistry, University of California, Davis, Davis, California 95616, United States · Linac Coherent Light Source, SLAC National Accelerator Laboratory, Menlo Park, California 94025, United States
Abstract
Interpreting electron paramagnetic resonance (EPR) spectra commonly requires repeated forward simulation and optimization of spin Hamiltonian parameters. Established software provides mature simulation and fitting methods, but these calculations are not generally available as differentiable components within machine-learning workflows. We present TorchSpin, an open-source PyTorch framework that ports the organization and numerical conventions of EasySpin into a tensor-based computational environment. Representative calculations spanning continuous-wave EPR, ENDOR, pulse EPR, spin dynamics, trajectory simulation, and magnetometry closely reproduced corresponding EasySpin results. In matched benchmarks, TorchSpin CPU execution was faster than EasySpin for six of nine workloads although performance depended strongly on the calculation. We therefore focused on the capabilities enabled by automatic differentiation in PyTorch. In a Cu(II) powder-spectrum demonstration, feedback propagated through TorchSpin improved the quality of a supervised convolutional neural network (CNN) without using parameter labels during refinement. Models trained from randomly initialized CNNs using only noisy spectra and TorchSpin reconstruction loss achieved median similarities of 0.99. CNNs trained on TorchSpin-generated libraries also transferred without adaptation to independently generated EasySpin spectra with performance comparable to EasySpin-trained controls. Once trained, the CNN produced initial parameter estimates in approximately 1 ms per spectrum while a batch of 512 was processed in approximately 1.3 ms with GPU acceleration. TorchSpin therefore provides both a tested Python implementation of established EPR calculations and a differentiable physical layer for rapid learned inference, spectral refinement, and parameter-label-free training.
Figures & tables
| Workload | EasySpin (s) | TorchSpin CPU (s) | ES/TS CPU | TorchSpin CUDA mode (s) | ES/TS CUDA |
|---|---|---|---|---|---|
| pepper: Cu + 2 N, matrix | 0.292 [0.229–0.351] | 0.847 (32 t) | 0.344 | 0.674 (hybrid) | 0.433 |
| pepper: Mn(II), matrix | 3.03 [3.03–3.14] | 1.07 (32 t) | 2.83 | 0.954 (hybrid) | 3.18 |
| pepper: perturbative grid | 0.0818 [0.072–0.0825] | 0.0616 (1 t) | 1.33 | 0.162 (hybrid) | 0.506 |
| pepper: strain summation | 0.814 [0.808–0.816] | 0.994 (8 t) | 0.82 | 1.23 (hybrid) | 0.661 |
| pepper: 20-call fit pattern | 2.5 [2.5–2.5] | 1.29 (8 t) | 1.93 | 1.77 (hybrid) | 1.41 |
| chili: nitroxide + proton | 1.15 [1.14–1.16] | 0.157 (1 t) | 7.32 | N/A | N/A |
| Model coordinate | Sampled interval | Physical transformation |
|---|---|---|
| g center | 2.040–2.080 | g x = g g/2; g y = g + g/2 |
| g rhombicity, g | 0–0.030 | g y g x |
| g z | 2.18–2.30 | – |
| A center | 35–85 MHz | A x = A A/2; A y = A + A/2 |
| A rhombicity, A | 0–40 MHz | A y A x |
| A z | 450–650 MHz | – |
| Metric | Supervised CNN | Physics-refined CNN |
|---|---|---|
| Median clean-spectrum cosine | 0.9768 | 0.9849 |
| Minimum cosine | 0.5934 | 0.8178 |
| Fraction with cosine 0.950 | 76.6% | 90.2% |
| Spectra improved | — | 373/512 (72.9%) |
| Parameter | Supervised MAE | Refined MAE |
|---|---|---|
| g x | 0.00125 | 0.00116 |
| g y | 0.00111 | 0.00099 |
| g z | 0.00369 | 0.00396 |
| A x (MHz) | 5.72 | 5.89 |
| A y (MHz) | 4.25 | 4.44 |
| A z (MHz) | 8.26 | 8.42 |
| Model | Median cosine | Fraction 0.950 | g x MAE | g y MAE | g z MAE | A x MAE (MHz) | A y MAE (MHz) | A z MAE (MHz) | lw MAE (mT) |
|---|---|---|---|---|---|---|---|---|---|
| Supervised control | 0.9698 | 65.2% | 0.00144 | 0.00140 | 0.00432 | 6.45 | 4.41 | 10.0 | 0.0855 |
| Label-free seed 1 | 0.9923 | 98.6% | 0.00118 | 0.000821 | 0.00392 | 6.98 | 5.01 | 9.04 | 0.107 |
| Label-free seed 2 | 0.9921 | 98.6% | 0.00115 | 0.000879 | 0.00454 | 7.01 | 4.82 | 9.81 | 0.105 |
| Label-free seed 3 | 0.9913 | 98.6% | 0.00126 | 0.000945 | 0.00444 | 6.96 | 5.05 | 9.87 | 0.124 |
| Training library | Median cosine | Fraction 0.950 | Mean NRMSE |
|---|---|---|---|
| TorchSpin | 0.9656 ± 0.0008 | 61.59 ± 2.07% | 0.3075 ± 0.0067 |
| EasySpin | 0.9667 ± 0.0004 | 63.93 ± 1.59% | 0.3005 ± 0.0063 |
| Training library | g x | g y | g z | A x (MHz) | A y (MHz) | A z (MHz) | lw (mT) |
|---|---|---|---|---|---|---|---|
| TorchSpin | 0.00142 | 0.00138 | 0.00481 | 5.65 | 4.56 | 10.9 | 0.0799 |
| EasySpin | 0.00141 | 0.00133 | 0.00490 | 5.99 | 4.55 | 10.5 | 0.0807 |
| Operation | Device/configuration | Median [IQR] (ms) | Per-spectrum value (ms) |
|---|---|---|---|
| Single CNN inference, resident | CPU, 1 thread | 0.976 [0.974–0.985] | 0.976 |
| Host normalization + CNN prediction | CPU, 1 thread | 0.994 [0.992–0.998] | 0.994 |
| Single CNN inference, resident | RTX 4090 | 1.019 [1.016–1.024] | 1.019 |
| Host normalization + transfer + prediction | RTX 4090 | 1.047 [1.045–1.053] | 1.047 |
| Batch CNN inference, n = 32 | RTX 4090 | 1.029 [1.026–1.034] | 0.0321 |
| Batch CNN inference, n = 128 | RTX 4090 | 1.016 [1.014–1.021] | 0.00794 |