Source-linked AI summary

TorchSpin: Differentiable Electron Paramagnetic Resonance Simulation in PyTorch

Alec H. Follmer

arXiv:2610.18924v1physics.chem-phcs.LG

TL;DR

EPR analysis relies on simulation and fitting, while established tools do not generally make those calculations differentiable within machine-learning workflows. TorchSpin ports EasySpin methods to PyTorch, reproduces representative reference calculations, and supports learned inference and spectral refinement, including transfer to independently generated EasySpin spectra.

  • Problem

    Established EPR simulators provide broad methods, but their conventional interfaces do not generally expose parameter-dependent signals to automatic differentiation within machine-learning workflows.

    Source: Introduction · p. 2
  • Method

    TorchSpin ports EasySpin’s organization and numerical conventions into a differentiable PyTorch framework and connects its forward calculations to learned parameter inference.

    Source: Introduction · p. 2, Discussion · p. 12
  • Results

    TorchSpin reproduced corresponding EasySpin spectra across representative EPR regimes, and TorchSpin-trained CNNs transferred without adaptation to independent EasySpin spectra with median reconstruction accuracy similar to EasySpin-trained controls.

    Source: Discussion · p. 12
  • Takeaways & Limitations

    Once trained, the CNN analyzed one spectrum in 0.976 ms on a single CPU thread, while a GPU-resident batch of 512 took 1.27 ms.

    Source: Discussion · p. 12

    Spectral reconstruction accuracy does not ensure recovery of every generating parameter when multiple parameter combinations produce similar spectra.

    Source: Discussion · p. 12

Abstract

from arXiv · show

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 $\geq$ 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.

Introduction

EPR parameter interpretation typically relies on forward simulation and fitting, and established packages cover many regimes. Their conventional interfaces do not generally expose parameter-dependent signals to automatic differentiation, motivating TorchSpin’s differentiable PyTorch framework and its tests across simulation and learning tasks.

Introduction

  • EPR parameter estimation commonly constructs a spin Hamiltonian, simulates its response over experimental coordinates, and compares the result with measured spectra.Except in the simplest cases, tensor principal values and orientations cannot be read directly from the experimental signal.Source: Introduction · p. 1
  • Established packages provide broad EPR simulation and fitting, but their conventional interfaces do not generally expose simulated-signal dependence on parameters through automatic differentiation.The passage identifies EasySpin, Spinach, XSophe, and specialized programs as established tools.Source: Introduction · pp. 1–2
  • Differentiable simulation lets spectral reconstruction error update predicted parameters or a network even when the spectrum’s generating Hamiltonian is unknown.Measured spectra, learned models, and physical simulation can participate in the same computation.Source: Introduction · p. 2
  • TorchSpin ports EasySpin’s organization and numerical conventions into an open-source, differentiable PyTorch framework, validated across representative EPR regimes and learning applications.The evaluation covers eight simulation pathways, nine benchmark workloads, and Cu(II) powder-spectrum applications.Source: Introduction · p. 2

Results

TorchSpin closely reproduced EasySpin across representative EPR calculations, while its speed varied by workload and execution mode. Differentiable spectral feedback improved CNN reconstructions, supported parameter-label-free training, and enabled transfer from TorchSpin-generated libraries to EasySpin spectra.

TorchSpin reproduces EasySpin across representative simulation regimes

  • Garlic, chili, pepper, and salt calculations closely matched EasySpin, with NRMSEs below 10^-15, 7.23 x 10^-5, 9.09 x 10^-5, and 1.92 x 10^-3, respectively.The ENDOR comparison required global amplitude scaling, so it validates transition frequencies and normalized lineshape rather than absolute intensity.Source: TorchSpin reproduces EasySpin across… · pp. 3–4, Figure 1 · p. 3
  • Agreement extended to spin-echo dynamics (mean-centered cosine >0.999), shaped-pulse trajectories (cosine 1.00), cardamom trajectories (NRMSE 0.0201), saffron ESEEM (0.0101), and curry magnetometry (1.48 x 10^-7).The cardamom comparison used independent finite trajectory ensembles, so its residuals also include sampling variation.Source: TorchSpin reproduces EasySpin across… · pp. 4–5

Performance is workload- and hardware-dependent

  • TorchSpin CPU execution was faster than EasySpin for six of nine workloads, including a 7.32-fold advantage for two-nucleus chili; EasySpin was faster for three workloads.Those three were Cu(II) matrix powder, strain summation, and CPU HYSCORE.Source: Performance is workload- and… · pp. 6–7, Figure 2 · p. 6
  • Hybrid CUDA reduced HYSCORE time from 0.752 s to 0.228 s, but increased perturbative-calculation time from 0.0616 s to 0.162 s.CUDA kernels accounted for no more than 16.6% of profiled wall time, and chili and cardamom had no CUDA execution path.Source: Figure 2 · p. 6, Performance is workload- and… · p. 7

Differentiable spectral feedback improves learned EPR inversion

  • Differentiable refinement improved 373 of 512 reconstructions (72.9%), raising median cosine similarity from 0.977 to 0.985.The increase in spectral agreement did not guarantee recovery of the original generating parameters.Source: Differentiable spectral feedback… · p. 8, Figure 4 · p. 11
  • Spectral refinement did not ensure unique parameter recovery: linewidth MAE rose from 0.0736 to 0.0921 mT, and equally good reconstructions could retain different Hamiltonian representations.A trained CNN produced parameter estimates in a median of about 1 ms per GPU-resident spectrum.Source: Differentiable spectral feedback… · pp. 8, 10
  • Parameter-label-free models achieved median cosine similarities of 0.992, 0.992, and 0.991 versus 0.970 for the supervised control; 98.6% of spectra exceeded 0.950 versus 65.2%.The models used TorchSpin reconstruction loss without parameter labels or clean spectral targets.Source: Differentiable spectral feedback… · pp. 8, 10, Figure 3 · p. 9
  • TorchSpin-trained and EasySpin-trained CNNs averaged median cosine similarities of 0.966 and 0.967, respectively, on held-out EasySpin spectra.The models were evaluated without retraining or adaptation, using TorchSpin as the common reconstruction model.Source: Differentiable spectral feedback… · p. 10

Discussion

TorchSpin makes established EPR calculations available in differentiable PyTorch workflows, supporting spectral refinement and learned inference beyond forward simulation speed alone. These models can rapidly screen spectra, but spectral agreement does not ensure unique or accurate recovery of generating parameters.

Discussion

  • TorchSpin brings representative EPR calculations into differentiable PyTorch workflows, where the forward model can generate training data, evaluate predictions, and propagate spectral disagreement through learning or optimization.The discussion frames this capability—not a universal reduction in simulation time—as the principal advantage.Source: Discussion · pp. 12–13
  • Spectral supervision can produce accurate reconstructions without consistently recovering generating parameters, so meaningful bounds, independent constraints, or uncertainty analysis remain important.Different parameter combinations can yield similar spectra, with linewidth less reliably constrained under the tested conditions.Source: Discussion · p. 12
  • 0.976 ms per spectrum on one CPU thread and 1.27 ms for a batch of 512 on GPU were required for CNN inference, supporting rapid screening or initial estimates before selective fitting.Generating the corresponding 512-point TorchSpin reconstruction required an additional 68.4 ms.Source: Discussion · p. 12
  • TorchSpin-trained CNNs transferred without adaptation to independent EasySpin spectra with median reconstruction accuracy similar to EasySpin-trained controls.The more demanding test is performance on experimental features absent from the libraries, including baseline distortions and field-calibration uncertainty.Source: Discussion · pp. 12–13
  • The Jupyter fitting interface combines parameter adjustment, fixed-parameter controls, optimizer and fitting-limit selection, and plots of the input, fit, residual, and RMSD across evaluations.Users can repeatedly adjust and refit within the notebook; execution may use CPU or hybrid CPU/CUDA according to the model and hardware.Source: Discussion · p. 13, Figure 5 · p. 13
  • TorchSpin's accompanying code, validation data, benchmarks, and notebooks provide a reproducible foundation for extending the approach to additional EPR methods and testing it against experimental spectra.Its EasySpin-derived organization preserves a familiar framework while exposing supported calculations to tensor batching and automatic differentiation.Source: Discussion · p. 13

Conclusions

TorchSpin combines representative EPR simulations with a differentiable PyTorch framework, and its Cu(II) demonstrations show applications in spectral refinement, label-free training, and transfer to EasySpin-generated spectra.

  • TorchSpin was competitive across diverse workloads, but neither CPU parallelism nor hybrid CUDA execution provided a universal speed advantage.Differentiable forward calculations supported refinement of a supervised inverse model, training without parameter labels or clean spectral targets, and transfer to independently generated EasySpin spectra within the tested domain.Source: Conclusions · p. 14

Methods

The methods describe TorchSpin’s implementation and evaluation, then detail supervised and parameter-label-free CNN training, cross-engine transfer, and timing procedures.

Software implementation

  • TorchSpin uses PyTorch tensor operations alongside NumPy and SciPy procedures not yet converted to tensor-native form, with parameter-dependent paths retained for automatic differentiation.SpinSystem, Experiment, and Options represent simulation inputs; field values use mT, energies and interaction parameters MHz, and angles radians.Source: Software implementation · p. 14

EasySpin reference comparisons

  • Reference calculations matched spin systems, experimental coordinates, and numerical settings, with output axes required to agree in length and within floating-point tolerance.A single coefficient scaled each TorchSpin trace before comparison; traces were normalized to the maximum absolute EasySpin signal, without smoothing, shifting, cropping, or point exclusion.Source: EasySpin reference comparisons · p. 14

Performance benchmarking

  • Nine workloads sampled distinct computational demands, and CUDA timing covered only selected cases using hybrid CPU/GPU execution where substantial work remained on the CPU.Chili and cardamom implementations were unavailable in the CUDA comparison; benchmark timing used sequential runs on the same workstation with controlled thread counts and repeated measurements.Source: Performance benchmarking · p. 15

Cu(II) spectral libraries; Neural inverse model and supervised training

  • The Cu(II) library used independent stratified parameter splits and noisy first-derivative spectra; the residual CNN was trained on normalized generating-parameter labels.The library contained 4,096 training, 512 validation, and 512 prospective test spectra, while the CNN had 1,297,127 trainable parameters and used fresh noise realizations during training.Source: Cu(II) spectral libraries · p. 15, Neural inverse model and supervised… · p. 16

Differentiable spectral refinement

  • Differentiable TorchSpin pepper refined the supervised checkpoint using spectral losses, with parameter labels excluded from refinement loss and checkpoint selection.The objective combined derivative cosine loss, integrated-spectrum cosine loss, and peak-weighted mean-squared error; validation used a fixed subset of 128 spectra.Source: Differentiable spectral refinement · p. 16

Parameter-label-free training

  • Randomly initialized CNNs were trained without parameter labels or clean spectral targets by reconstructing the fixed noisy training observations through TorchSpin.Three independently initialized models used noisy validation spectra for checkpoint selection, and all checkpoints were frozen before prospective testing.Source: Parameter-label-free training · pp. 16–17

Cross-engine library transfer; CNN evaluation

  • The cross-engine experiment trained matched-seed CNNs on separate TorchSpin- or EasySpin-generated libraries and evaluated them on a new EasySpin test set.The evaluation measured reconstruction similarity against noise-free EasySpin spectra and also assessed parameter errors, noise strata, boundary occupancy, output variance, and seed disagreement.Source: Cross-engine library transfer · p. 17, CNN evaluation · p. 17

Inference and reconstruction timing

  • Inference timing separated CNN parameter prediction from optional TorchSpin reconstruction and measured single-spectrum latency and batched GPU throughput.GPU tests included device-resident inputs and host-resident inputs with normalization and transfer; batch sizes were 1, 32, 128, and 512.Source: Inference and reconstruction timing · pp. 17–18

Code and data availability

TorchSpin source code, documentation, worked notebooks, and executable examples are available online.

  • TorchSpin source code, documentation, worked notebooks, and executable examples are available at the project GitHub repository.The reference-comparison and performance analyses used TorchSpin v0.2.2.Source: Code and data availability · p. 18

Performance benchmark design

The benchmark suite samples selected computational demands rather than exhaustively testing TorchSpin options, including two matrix-based pepper workloads.

Performance benchmark design

  • The benchmarks probe matrix diagonalization, perturbative resonance calculations, strain summation, repeated simulations, stochastic-Liouville calculations, multidimensional pulse EPR, and trajectory-based spin dynamics.CUDA timings cover only workloads with an implemented accelerator path, and CUDA mode combines GPU tensor operations with CPU-side orchestration.Source: Performance benchmark design · pp. 23–24

pepper: Cu(II) with two nitrogen nuclei

  • The Cu(II) matrix workload tests batched Hamiltonian construction and diagonalization, resonance-field determination, powder averaging, and interpolation across a dense transition set.Its spin-Hamiltonian matrix is 72 x 72, with two nitrogen nuclei coupled to the Cu(II) center.Source: pepper: Cu(II) with two nitrogen nuclei · p. 24

pepper: high-spin Mn(II)

  • The high-spin Mn(II) workload tests Hamiltonian construction and repeated diagonalization across a powder grid, with substantial transitions and orientation dependence despite a smaller matrix.The Hamiltonian is 36 x 36.Source: pepper: high-spin Mn(II) · p. 24

pepper: dense-grid perturbative calculation

The pepper workloads benchmark perturbative resonance evaluation, strain-dependent spectral summation, and repeated simulations representative of fitting workflows.

pepper: dense-grid perturbative calculation

  • The dense-grid perturbative test evaluates second-order resonances on a 4096-point field grid with a [91, 1] orientation grid.It uses an S = 1/2 system coupled to one 14N and two 1H nuclei.Source: pepper: dense-grid perturbative… · p. 24

pepper: strain summation

  • The strain workload tests orientation- and transition-dependent linewidths and accumulation of many broadened contributions.Its spectrum uses anisotropic g-strain and residual broadening with a [61, 4] orientation grid.Source: pepper: strain summation · pp. 24–25

pepper: repeated 20-call fitting pattern

  • A 20-simulation sequence varies gx from 2.000 to 2.010 to measure cumulative costs relevant to iterative fitting or parameter search.The benchmark includes repeated setup, Hamiltonian evaluation, and spectral accumulation.Source: pepper: repeated 20-call fitting pattern · p. 25

chili: nitroxide with two coupled nuclei

These workloads exercise distinct computational demands in slow-motion EPR and pulse-EPR simulation, from stochastic-Liouville operations to two-dimensional HYSCORE response construction.

chili: nitroxide with two coupled nuclei

  • The two-nucleus chili workload tests stochastic-Liouville construction, rotational basis truncation, and iterative linear algebra for a 30 ns nitroxide spectrum.The system couples the nitroxide to 14N and 1H.Source: chili: nitroxide with two coupled nuclei · p. 25

chili: orienting potential

  • The chili orienting-potential workload probes Liouvillian construction and orientational averaging for a partially ordered slow-motion system.The orienting potential couples rotational basis functions and requires a nonuniform molecular orientation distribution.Source: chili: orienting potential · p. 25

saffron: HYSCORE

  • The 512 x 512 HYSCORE workload tests pulse-sequence evolution and transformation of a two-dimensional time-domain response to the frequency domain.Its memory-access and tensor-operation patterns differ from field-swept continuous-wave simulations; CPU and hybrid CUDA execution were verified.Source: saffron: HYSCORE · p. 26

cardamom: stochastic rotational trajectories

The cardamom benchmark measures a stochastic trajectory-simulation workflow and reports output agreement and thread-scaling measurement conventions.

cardamom: stochastic rotational trajectories

  • The trajectory workload propagates 200 rotational trajectories per starting orientation for 1000 steps, then averages the ensemble into a field-domain spectrum.It tests rotational-trajectory generation, time-dependent spin-Hamiltonian construction, density-matrix propagation, and ensemble averaging.Source: cardamom: stochastic rotational… · p. 26
  • Independent finite trajectory ensembles cause small run-to-run timing variation because cardamom does not expose a public random seed.The benchmark measures the complete workflow while accepting this stochastic variation.Source: cardamom: stochastic rotational… · p. 26
  • Figure S5 compares median CPU times at 1, 8, and 32 threads, with cell colors encoding speedup on a log2 scale.Each condition used one warm-up and five timed replicates; the outlined configuration selects the smallest thread count whose timing IQR overlaps that of the lowest median.Source: Figure S5 · p. 27
  • CPU and CUDA outputs agreed to a maximum relative deviation of 3.3 x 10^-12 and an NRMSE of 2.2 x 10^-12 or better.Table S1 reports the matched-host campaign used for Figure 2 and Figure S5.Source: Table S1 · p. 27, cardamom: stochastic rotational… · p. 28

Machine-learning experiments

The experiments use bounded Cu(II) powder-spectrum libraries and a shared CNN, then assess differentiable refinement using spectral losses and validation-based checkpoint selection.

Cu(II) spectral libraries

  • The Cu(II) libraries span bounded axial-to-rhombic blue-copper-like parameters, with seven coordinates describing g, hyperfine, and linewidth values.The coordinate transformation preserves gx ≤ gy and Ax ≤ Ay while sampling axial and rhombic cases.Source: Cu(II) spectral libraries · p. 28, Table S2 · p. 28

CNN architecture and supervised training

  • All comparisons use the same 1,297,127-parameter residual one-dimensional CNN to predict seven bounded parameter coordinates.A sigmoid maps outputs to unit coordinates before conversion to physical parameters.Source: CNN architecture and supervised training · p. 28

Differentiable refinement after supervised initialization

  • Refinement combines derivative, integrated, and weighted spectral losses, with the parameter-error coefficient set to zero.Derivative and integrated losses are based on one minus cosine similarity; the integrated spectra are mean-corrected and normalized.Source: Differentiable refinement after… · pp. 28–29
  • Checkpoint selection uses the lowest spectral loss on 128 fixed validation spectra, including the supervised checkpoint as epoch zero.No test spectra were removed, and hyperparameters were not adjusted after test evaluation.Source: Differentiable refinement after… · p. 29
  • The supplementary analyses include noise-interval similarities, training histories, parameter recovery and correlations, and best-to-worst reconstruction examples.Tables S3–S5 are identified as test-set refinement effects, component-wise parameter MAE, and prospective performance for label-free training from random initialization; the supplied captions do not provide values.Source: Figure S6 · p. 29, Figure S7 · p. 30, Table S3 · p. 30

Cross-engine training-library transfer

The cross-engine comparison evaluates TorchSpin-trained CNNs on a shared prospective EasySpin test set across three paired initialization seeds. Its figures and tables describe reconstruction errors, ranked examples, parameter errors, and inference latency.

Cross-engine training-library transfer

  • Reconstruction NRMSE distributions compare TorchSpin- and EasySpin-library CNNs on the same prospective EasySpin test set across three paired seeds.All predictions were decoded with TorchSpin.Source: Figure S10 · p. 34
  • Ranked reconstructions show best, median-ranked, poor, and worst cases for each training library across three paired initialization seeds.Cases are ranked by cosine similarity to clean EasySpin test spectra; gray traces are noisy inputs, black traces clean references, and red traces TorchSpin reconstructions.Source: Figure S11 · p. 34, Figure S12 · p. 35, Figure S13 · p. 35
  • Table S6 reports mean ± sample standard deviation across three paired seeds on the shared 512-spectrum test set.The caption specifies that percentage uncertainties are percentage points, not confidence intervals.Source: Table S6 · p. 35
  • Table S7 reports mean component-wise parameter MAE across the three paired seeds in the training-library comparison.Source: Table S7 · p. 36
  • Table S8 reports CNN inference and TorchSpin reconstruction latency.Source: Table S8 · p. 37
Loading 2610.18924v1…