
Fit a Neural ODE depth profile (Pillar 2, deep variant)
Source:R/piml_neural_ode.R
piml_neural_ode_fit.RdModels the vector field \(dy/dz = f_\theta(z, y)\) where
\(f_\theta\) is a small MLP (default 2 x 16 + tanh). The forward
model is a fixed-step RK4 integrator that runs end-to-end on torch,
so training drives the MLP weights — and, optionally, the surface
value \(y_0\) — by back-propagating through the whole integration
(a proper Neural ODE, Chen et al. 2018).
Usage
piml_neural_ode_fit(
depths,
values,
y_surface = NULL,
hidden = c(16L, 16L),
n_steps = 4L,
epochs = 500L,
lr = 0.01,
seed = NULL,
verbose = FALSE
)Arguments
- depths
Numeric vector of horizon mid-depths.
- values
Numeric vector of observed values, same length.
- y_surface
Optional numeric; if supplied, $y_0$ is fixed (no learnable parameter) — used when you trust a specific 0 cm reading.
Integer vector with MLP hidden-layer widths.
- n_steps
Integer, RK4 steps between successive observation depths. Higher = more accurate, slower.
- epochs, lr
Integer and numeric — training hyperparameters for Adam.
- seed
Optional integer — forwarded to
torch::torch_manual_seedfor reproducibility.- verbose
Logical; print training loss every 50 epochs.
Details
Compared to piml_profile_fit(), the Neural ODE variant can capture
non-monotone profiles (E horizons below an A, bulge in a Bt) that
the parametric exponential-asymptote ODE cannot represent.
Examples
# \donttest{
# torch is a Suggests dependency and libtorch is a separate binary
# download, so requireNamespace() alone is not a sufficient guard.
if (requireNamespace("torch", quietly = TRUE) &&
isTRUE(torch::torch_is_installed())) {
# An A-E-Bt sequence: SOC falls into a bleached E horizon at 15 cm
# and partly recovers in the clay-illuvial Bt below it. This is the
# non-monotone shape the parametric ODE cannot represent.
depths <- c(5, 15, 30, 50, 80)
soc <- c(22, 9, 16, 14, 11)
fit <- piml_neural_ode_fit(depths, soc, epochs = 80L, n_steps = 3L,
seed = 1L)
print(fit)
# Three numbers, never one. At the 80 epochs this example can
# afford, the Neural ODE is BEATEN by the 4-parameter ODE, and both
# beat the mean-only baseline.
par_fit <- piml_profile_fit(depths, soc)
print(round(c(neural = fit$rmse, # 3.5703
parametric = par_fit$rmse, # 2.4084
mean_only = sqrt(mean((soc - mean(soc))^2))), 4))
# Read the fitted vectors before trusting that ranking. The
# parametric fit wins on RMSE by degenerating into a step: below
# 15 cm its predictions are constant to 1.63e-06, i.e. it returns
# the mean of the four deep horizons (12.5) and models no shape.
print(round(par_fit$fitted, 4))
print(signif(max(abs(diff(par_fit$fitted[2:5]))), 3))
print(mean(soc[2:5]))
# The Neural ODE is smooth but, at this budget, still monotone ---
# it has not found the E-horizon dip either. Neither fit has earned
# the word "non-monotone" here.
print(round(fit$fitted, 4))
print(all(diff(fit$fitted) < 0))
# Why: the loss is still falling at the last epoch, so this is an
# under-trained fit rather than the model's ceiling (the default is
# epochs = 500L, six times this budget). Quote the trace, never the
# single final number.
print(round(fit$loss_history[c(1, 20, 40, 60, 80)], 5))
}
#> <edaphos_piml_neural_ode>
#> dy/dz = f_theta(z, y), MLP hidden = 16 -> 16
#> n obs = 5 rmse = 3.57 final loss = 0.5062
#> neural parametric mean_only
#> 3.5703 2.4355 4.4989
#> [1] 21.8458 12.2512 12.2062 12.2062 12.2062
#> [1] 0.045
#> [1] 12.5
#> [1] 18.4505 15.4342 13.4813 12.8103 12.4058
#> [1] TRUE
#> [1] 2.77732 0.85243 0.66257 0.56551 0.50617
# }