-
-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EnsembleProblem with timepoint_meanvar errors due to internal mutation #446
Comments
Alternatively, I can try to parallelize the SDE paths by using a pmap or map loop, but that seems to run into similar issues: Example code:
The exact error is: MethodError: Cannot |
https://diffeqflux.sciml.ai/dev/examples/optimization_sde/#Example-1:-Fitting-Data-with-SDEs-1 That example uses EnsembleThreads without a problem, so we know it's not a problem. What your example points to is that |
I copied the example you gave and it works just fine, but when I apply it to my problem I still seem to get errors:
|
The error message was telling you that you didn't have enough arguments in your callback: using DifferentialEquations
using Flux
using DifferentialEquations.EnsembleAnalysis
using IterTools: ncycle
using Flux: @epochs
using DiffEqSensitivity
using Statistics
using DiffEqFlux
# Input parameters
T = 1.0f0
tspan = (0.0f0,T)
m=1.0
v=1.0
true_y0 = exp(T)*m + (exp(T)-1)*( sqrt( 2*v/(1-exp(-2*T)) )+1 )
true_Z = sqrt(2*v/(1-exp(-2*T)))
u0 = [true_y0 + 0.25] # initial guess
Z = true_Z + 0.25
p = [Z]
function drift_nn!(du,u,p,t)
du[1] = - u[1] - p[1] - 1.0f0
end
function stoch_nn!(du,u,p,t)
du[1] = p[1]
end
probSDE_nn = SDEProblem(drift_nn!,stoch_nn!,u0,tspan,p)
ensemble_prob = EnsembleProblem(probSDE_nn)
sol = solve(ensemble_prob,EM(),saveat = T,trajectories=1000, dt=0.005)
function loss_2(p)
tmp_prob = remake(probSDE_nn,p=p)
ensemble_prob = EnsembleProblem(tmp_prob)
sim = solve(ensemble_prob,EM(),saveat = T, trajectories=1000, dt=0.005)
arraysol = Array(sim)
loss = (m - mean(arraysol,dims=3)[2])^2 + (v - var(arraysol,dims=3)[2])^2
end
cb2 = function(l,p) #callback function to observe training
display(loss_2(p))
false
end
Z_pre = deepcopy(Z)
u0_pre = deepcopy(u0)
# train model on the same data num_cycle times
opt = ADAM(0.01)
@time res = DiffEqFlux.sciml_train(loss_2,p,opt, maxiters = 5, cb=cb2) |
Awesome, thank you so much! One last question: How do I get the schiml_train function to update the intial position of the SDE (u0 in the example). My first guess trains, but the u0/p[2] parameter doesn't update.
|
It's because you're not using function loss_2(p)
tmp_prob = remake(probSDE_nn,p=p,u0=[[p[2]]))
... |
Hi,
Is there a way to combine the ensembleproblem command to solve SDEs in combination with Flux Neural Nets. Right now, when I try to combine them, I get the error when I try to train the Neural Net. "Info: Epoch 1
└ @ Main /home/ec2-user/.julia/packages/Flux/Fj3bt/src/optimise/train.jl:121
Mutating arrays is not supported
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] (::Zygote.var"#459#460")(::Nothing) at /home/ec2-user/.julia/packages/Zygote/1GXzF/src/lib/array.jl:67
[3] (::Zygote.var"#1009#back#461"{Zygote.var"#459#460"})(::Nothing) at /home/ec2-user/.julia/packages/ZygoteRules/6nssF/src/adjoint.jl:49
[4] materialize! at ./broadcast.jl:826 [inlined]"
Here is the code:
Thank you!
The text was updated successfully, but these errors were encountered: