Hi
i am running the below code in 1.11.1
using ComponentArrays, Lux, DiffEqFlux, Optimization, Random, Plots, OptimizationOptimisers
rng = Random.default_rng()
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 = Lux.Chain(x -> x.^3,
Lux.Dense(2, 50, tanh),
Lux.Dense(50, 2))
p, st = Lux.setup(rng, dudt2)
prob_neuralode = NeuralODE(dudt2, tspan, Tsit5(), saveat = tsteps)
theta_init = ComponentArray(p=p,u0=u0)
function predict_neuralode(theta)
Array(prob_neuralode(theta.u0, theta.p, st)[1])
end
function loss_neuralode(theta)
pred = predict_neuralode(theta)
loss = sum(abs2, ode_data .- pred)
return loss, pred
end
loss_list = []
# Do not plot by default for the documentation
# Users should change doplot=true to see the plots callbacks
callback = function (theta, l, pred; doplot = false)
push!(loss_list, l)
if length(loss_list) % 10 == 0
println("Current loss after $(length(loss_list)) iterations: $(loss_list[end])")
end
# plot current prediction against data
if doplot
plt = scatter(tsteps, ode_data[1,:], label = "data")
scatter!(plt, tsteps, pred[1,:], label = "prediction")
display(plot(plt))
end
return false
end
callback(theta_init, loss_neuralode(theta_init)...; doplot=true)
# use Optimization.jl to solve the problem
adtype = Optimization.AutoZygote()
optf = Optimization.OptimizationFunction((theta,x) -> loss_neuralode(theta), adtype)
optprob = Optimization.OptimizationProblem(optf, theta_init)
result_neuralode = Optimization.solve(optprob,
OptimizationOptimisers.Adam(0.05),
callback = callback,
maxiters = 100)
I got the output and error as follows
Current loss after 10 iterations: 82.406425
Current loss after 20 iterations: 78.856865
Current loss after 30 iterations: 72.838615
Current loss after 40 iterations: 60.01752
Current loss after 50 iterations: 32.493797
Current loss after 60 iterations: 26.385363
Current loss after 70 iterations: 22.401882
Current loss after 80 iterations: 18.412098
Current loss after 90 iterations: 8.481232
Current loss after 100 iterations: 3.4045079
Output should be scalar; gradients are not defined for output (3.4045079f0, Float32[2.338852 2.2056694 1.8878452 1.4737391 0.9712124 0.3802982 -0.22470805 -0.81994236 -1.3281096 -1.593053 -1.6756915 -1.7236149 -1.7132694 -1.4907907 -1.1634691 -0.820791 -0.48301852 -0.14866462 0.18228889 0.51112044 0.8450304 1.1903967 1.5002295 1.660663 1.6566764 1.5508975 1.4023738 1.2287798 1.003169 0.6947515; 0.39235187 0.80841744 1.1559838 1.4060826 1.5416878 1.4959542 1.411158 1.3012307 1.029037 0.59033424 0.13585138 -0.32202947 -0.77851284 -1.2236539 -1.5217149 -1.6047399 -1.5676715 -1.518556 -1.480465 -1.4481436 -1.4021496 -1.3260632 -1.1587306 -0.8625075 -0.54747766 -0.25357255 0.0076087415 0.23902002 0.43310583 0.5404223])
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:35
[2] sensitivity(y::Tuple{Float32, Matrix{Float32}})
@ Zygote ~/.julia/packages/Zygote/TWpme/src/compiler/interface.jl:114
[3] gradient(::Function, ::ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(p = ViewAxis(1:252, Axis(layer_1 = ViewAxis(1:0, Shaped1DAxis((0,))), layer_2 = ViewAxis(1:150, Axis(weight = ViewAxis(1:100, ShapedAxis((50, 2))), bias = ViewAxis(101:150, Shaped1DAxis((50,))))), layer_3 = ViewAxis(151:252, Axis(weight = ViewAxis(1:100, ShapedAxis((2, 50))), bias = ViewAxis(101:102, Shaped1DAxis((2,))))))), u0 = ViewAxis(253:254, Shaped1DAxis((2,))))}}}, ::SciMLBase.NullParameters)
@ Zygote ~/.julia/packages/Zygote/TWpme/src/compiler/interface.jl:148
[4] gradient
@ ~/.julia/packages/DifferentiationInterface/srtnM/ext/DifferentiationInterfaceZygoteExt/DifferentiationInterfaceZygoteExt.jl:98 [inlined]
[5] gradient!(f::Function, grad::ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(p = ViewAxis(1:252, Axis(layer_1 = ViewAxis(1:0, Shaped1DAxis((0,))), layer_2 = ViewAxis(1:150, Axis(weight = ViewAxis(1:100, ShapedAxis((50, 2))), bias = ViewAxis(101:150, Shaped1DAxis((50,))))), layer_3 = ViewAxis(151:252, Axis(weight = ViewAxis(1:100, ShapedAxis((2, 50))), bias = ViewAxis(101:102, Shaped1DAxis((2,))))))), u0 = ViewAxis(253:254, Shaped1DAxis((2,))))}}}, prep::DifferentiationInterface.NoGradientPrep, backend::AutoZygote, x::ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(p = ViewAxis(1:252, Axis(layer_1 = ViewAxis(1:0, Shaped1DAxis((0,))), layer_2 = ViewAxis(1:150, Axis(weight = ViewAxis(1:100, ShapedAxis((50, 2))), bias = ViewAxis(101:150, Shaped1DAxis((50,))))), layer_3 = ViewAxis(151:252, Axis(weight = ViewAxis(1:100, ShapedAxis((2, 50))), bias = ViewAxis(101:102, Shaped1DAxis((2,))))))), u0 = ViewAxis(253:254, Shaped1DAxis((2,))))}}}, contexts::DifferentiationInterface.Constant{SciMLBase.NullParameters})
@ DifferentiationInterfaceZygoteExt ~/.julia/packages/DifferentiationInterface/srtnM/ext/DifferentiationInterfaceZygoteExt/DifferentiationInterfaceZygoteExt.jl:112
[6] (::OptimizationZygoteExt.var"#grad#14"{SciMLBase.NullParameters, OptimizationFunction{true, AutoZygote, var"#41#42", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, AutoZygote})(res::ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(p = ViewAxis(1:252, Axis(layer_1 = ViewAxis(1:0, Shaped1DAxis((0,))), layer_2 = ViewAxis(1:150, Axis(weight = ViewAxis(1:100, ShapedAxis((50, 2))), bias = ViewAxis(101:150, Shaped1DAxis((50,))))), layer_3 = ViewAxis(151:252, Axis(weight = ViewAxis(1:100, ShapedAxis((2, 50))), bias = ViewAxis(101:102, Shaped1DAxis((2,))))))), u0 = ViewAxis(253:254, Shaped1DAxis((2,))))}}}, θ::ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(p = ViewAxis(1:252, Axis(layer_1 = ViewAxis(1:0, Shaped1DAxis((0,))), layer_2 = ViewAxis(1:150, Axis(weight = ViewAxis(1:100, ShapedAxis((50, 2))), bias = ViewAxis(101:150, Shaped1DAxis((50,))))), layer_3 = ViewAxis(151:252, Axis(weight = ViewAxis(1:100, ShapedAxis((2, 50))), bias = ViewAxis(101:102, Shaped1DAxis((2,))))))), u0 = ViewAxis(253:254, Shaped1DAxis((2,))))}}}, p::SciMLBase.NullParameters)
@ OptimizationZygoteExt ~/.julia/packages/OptimizationBase/gvXsf/ext/OptimizationZygoteExt.jl:39
[7] macro expansion
@ ~/.julia/packages/OptimizationOptimisers/xC7Ic/src/OptimizationOptimisers.jl:137 [inlined]
[8] macro expansion
@ ~/.julia/packages/Optimization/qX4vR/src/utils.jl:32 [inlined]
[9] __solve(cache::OptimizationCache{OptimizationFunction{true, AutoZygote, var"#41#42", OptimizationZygoteExt.var"#grad#14"{SciMLBase.NullParameters, OptimizationFunction{true, AutoZygote, var"#41#42", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, AutoZygote}, OptimizationZygoteExt.var"#fg!#16"{SciMLBase.NullParameters, OptimizationFunction{true, AutoZygote, var"#41#42", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, AutoZygote}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, OptimizationBase.ReInitCache{ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(p = ViewAxis(1:252, Axis(layer_1 = ViewAxis(1:0, Shaped1DAxis((0,))), layer_2 = ViewAxis(1:150, Axis(weight = ViewAxis(1:100, ShapedAxis((50, 2))), bias = ViewAxis(101:150, Shaped1DAxis((50,))))), layer_3 = ViewAxis(151:252, Axis(weight = ViewAxis(1:100, ShapedAxis((2, 50))), bias = ViewAxis(101:102, Shaped1DAxis((2,))))))), u0 = ViewAxis(253:254, Shaped1DAxis((2,))))}}}, SciMLBase.NullParameters}, Nothing, Nothing, Nothing, Nothing, Nothing, Optimisers.Adam, Bool, var"#38#40", Nothing})
@ OptimizationOptimisers ~/.julia/packages/OptimizationOptimisers/xC7Ic/src/OptimizationOptimisers.jl:83
[10] solve!(cache::OptimizationCache{OptimizationFunction{true, AutoZygote, var"#41#42", OptimizationZygoteExt.var"#grad#14"{SciMLBase.NullParameters, OptimizationFunction{true, AutoZygote, var"#41#42", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, AutoZygote}, OptimizationZygoteExt.var"#fg!#16"{SciMLBase.NullParameters, OptimizationFunction{true, AutoZygote, var"#41#42", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, AutoZygote}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, OptimizationBase.ReInitCache{ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(p = ViewAxis(1:252, Axis(layer_1 = ViewAxis(1:0, Shaped1DAxis((0,))), layer_2 = ViewAxis(1:150, Axis(weight = ViewAxis(1:100, ShapedAxis((50, 2))), bias = ViewAxis(101:150, Shaped1DAxis((50,))))), layer_3 = ViewAxis(151:252, Axis(weight = ViewAxis(1:100, ShapedAxis((2, 50))), bias = ViewAxis(101:102, Shaped1DAxis((2,))))))), u0 = ViewAxis(253:254, Shaped1DAxis((2,))))}}}, SciMLBase.NullParameters}, Nothing, Nothing, Nothing, Nothing, Nothing, Optimisers.Adam, Bool, var"#38#40", Nothing})
@ SciMLBase ~/.julia/packages/SciMLBase/YTOjh/src/solve.jl:187
[11] solve(::OptimizationProblem{true, OptimizationFunction{true, AutoZygote, var"#41#42", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED_NO_TIME), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(p = ViewAxis(1:252, Axis(layer_1 = ViewAxis(1:0, Shaped1DAxis((0,))), layer_2 = ViewAxis(1:150, Axis(weight = ViewAxis(1:100, ShapedAxis((50, 2))), bias = ViewAxis(101:150, Shaped1DAxis((50,))))), layer_3 = ViewAxis(151:252, Axis(weight = ViewAxis(1:100, ShapedAxis((2, 50))), bias = ViewAxis(101:102, Shaped1DAxis((2,))))))), u0 = ViewAxis(253:254, Shaped1DAxis((2,))))}}}, SciMLBase.NullParameters, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, @Kwargs{}}, ::Optimisers.Adam; kwargs::@Kwargs{callback::var"#38#40", maxiters::Int64})
@ SciMLBase ~/.julia/packages/SciMLBase/YTOjh/src/solve.jl:95
[12] top-level scope
@ In[6]:63
if I change the outut of loss_neuralode to loss only, this code can be worked.
when the outputs of loss_neuralode are loss and pred
how can I run this code ?
best