Closed metanoid closed 4 years ago
I tried to put together a smaller MWE, but this code (adapted from the tutorials) does not show the same error. I don't understand why not.
using DiffEqFlux, Flux, Optim, OrdinaryDiffEq, Plots
u0 = [0.1f0, 1.1f0]
tspan = (0.0f0, 25.0f0)
tsteps = 0.0f0:1.0:25.0f0
model_univ = FastChain(FastDense(2, 16, tanh),
FastDense(16, 16, tanh),
FastDense(16, 1))
# The model weights are destructured into a vector of parameters
p_model = initial_params(model_univ)
n_weights = length(p_model)
# Parameters of the second equation (linear dynamics)
p_system = Float32[0.5, -0.5]
p_all = [p_model; p_system]
θ = Float32[u0; p_all]
function dudt_univ!(du, u, p, t)
# Destructure the parameters
model_weights = p[1:n_weights]
α = p[end - 1]
β = p[end]
# The neural network outputs a control taken by the system
# The system then produces an output
model_control, system_output = u
# Dynamics of the control and system
dmodel_control = model_univ(u, model_weights)[1]
dsystem_output = α*system_output + β*model_control
# Update in place
du[1] = dmodel_control
du[2] = dsystem_output
end
prob_univ = ODEProblem(dudt_univ!, u0, tspan, p_all)
sol_univ = solve(prob_univ, Tsit5(),abstol = 1e-8, reltol = 1e-6)
# let's say we want to estimate u0 and p from the fixed data of "[1]"
u0_est = FastChain(FastDense(1,2))
p_est = FastChain(FastDense(1,n_weights))
a_params = initial_params(u0_est)
b_params = initial_params(p_est)
θ = [a_params; b_params]
function predict_univ(θ)
a = θ[1:length(a_params)]
b = θ[(length(a_params) + 1):end]
u0 = u0_est([1.0f0], a)
p = p_est([1.0f0], b)
return Array(solve(prob_univ, Tsit5(), u0=u0, p=p,
saveat = tsteps))
end
loss_univ(θ) = sum(abs2, predict_univ(θ)[2,:] .- 1)
l = loss_univ(θ)
list_plots = []
iter = 0
callback = function (θ, l)
global list_plots, iter
if iter == 0
list_plots = []
end
iter += 1
println(l)
plt = plot(predict_univ(θ)', ylim = (0, 6))
push!(list_plots, plt)
display(plt)
return false
end
result_univ = DiffEqFlux.sciml_train(loss_univ, θ,
BFGS(initial_stepnorm = 0.01),
cb = callback)
It could just be upstream RNN issues like https://github.com/FluxML/Flux.jl/issues/1209
Correct, working correctly on Flux#stateful-map
branch, thanks
I'm trying to get
sciml_train
to build an estimator foru0
andp
, given a known functional form of the ODE.Inside the
predict
function, I index my parameter vector theta into two parts:Oddly enough, the first of these indexing operations works, the second does not - I get an error:
DimensionMismatch("array could not be broadcast to match destination")
Is there a better way to structure the code so that indexing into the parameter vector will not fail?
Reproducible example:
When running the final
sciml_train
line, the error appears on line 84 but not on line 73.Error message in full:
Versions: Julia 1.4.2