Newton and Hessian-Free Newton-Krylov with Second Order Adjoint Sensitivity Analysis
In many cases it may be more optimal or more stable to fit using second order Newton-based optimization techniques. Since SciMLSensitivity.jl provides second order sensitivity analysis for fast Hessians and Hessian-vector products (via forward-over-reverse), we can utilize these in our neural/universal differential equation training processes.
sciml_train is set up to automatically use second order sensitivity analysis methods if a second order optimizer is requested via Optim.jl. Thus Newton and NewtonTrustRegion optimizers will use a second order Hessian-based optimization, while KrylovTrustRegion will utilize a Krylov-based method with Hessian-vector products (never forming the Hessian) for large parameter optimizations.
using SciMLSensitivity
using Lux, ComponentArrays, DiffEqFlux, Optimization, OptimizationOptimisers,
OrdinaryDiffEq, Plots, Random, OptimizationOptimJL
u0 = Float32[2.0; 0.0]
datasize = 30
tspan = (0.0f0, 1.5f0)
tsteps = range(tspan[1], tspan[2], length = datasize)
function trueODEfunc(du, u, p, t)
true_A = [-0.1 2.0; -2.0 -0.1]
du .= ((u .^ 3)'true_A)'
end
prob_trueode = ODEProblem(trueODEfunc, u0, tspan)
ode_data = Array(solve(prob_trueode, Tsit5(), saveat = tsteps))
dudt2 = Chain(x -> x .^ 3, Dense(2, 50, tanh), Dense(50, 2))
prob_neuralode = NeuralODE(dudt2, tspan, Tsit5(), saveat = tsteps)
ps, st = Lux.setup(Random.default_rng(), prob_neuralode)
ps = ComponentArray(ps)
prob_neuralode = Lux.Experimental.StatefulLuxLayer(prob_neuralode, ps, st)
function predict_neuralode(p)
Array(prob_neuralode(u0, p))
end
function loss_neuralode(p)
pred = predict_neuralode(p)
loss = sum(abs2, ode_data .- pred)
return loss, pred
end
# Callback function to observe training
list_plots = []
iter = 0
callback = function (p, l, pred; doplot = false)
global list_plots, iter
if iter == 0
list_plots = []
end
iter += 1
display(l)
# plot current prediction against data
plt = scatter(tsteps, ode_data[1, :], label = "data")
scatter!(plt, tsteps, pred[1, :], label = "prediction")
push!(list_plots, plt)
if doplot
display(plot(plt))
end
return l < 0.01
end
adtype = Optimization.AutoZygote()
optf = Optimization.OptimizationFunction((x, p) -> loss_neuralode(x), adtype)
optprob1 = Optimization.OptimizationProblem(optf, prob_neuralode.ps)
pstart = Optimization.solve(optprob1, Optimisers.Adam(0.01), callback = callback, maxiters = 100).u
optprob2 = Optimization.OptimizationProblem(optf, pstart)
pmin = Optimization.solve(optprob2, NewtonTrustRegion(), callback = callback,
maxiters = 200)retcode: Failure
u: ComponentVector{Float32}(layer_1 = Float32[], layer_2 = (weight = Float32[-0.091428906 -0.4580148; 0.06010161 -0.011998319; … ; -0.25046286 0.5778018; -0.18339717 -0.08478768], bias = Float32[0.03868959; 0.56724346; … ; 0.13485973; 0.37431392;;]), layer_3 = (weight = Float32[0.08661308 -0.25581786 … -0.21189588 -0.20991422; -0.2930145 0.80373067 … -0.074231386 -0.80416566], bias = Float32[-0.59505254; 0.54617727;;]))Note that we do not demonstrate Newton() because we have not found a single case where it is competitive with the other two methods. KrylovTrustRegion() is generally the fastest due to its use of Hessian-vector products.