# ERROR: LoadError: Need an adjoint for constructor EnsembleSolution

**URL:** <https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611>\
**Category:** General Usage\
**Tags:** diffeq, flux\
**Created:** [July 6, 2020, 2:19pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611 "2020-07-06T14:19:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yanz4](https://avatars.discourse-cdn.com/v4/letter/y/b5e925/32.png) [@yanz4](https://discourse.julialang.org/u/yanz4)\
**Post date:** [July 6, 2020, 2:19pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/1 "2020-07-06T14:19:42Z")

</div>

I wish to use Flux.training to optimize a parameter in parallel ensemble simulation, but it shows adjoint error as below:

> ERROR: LoadError: Need an adjoint for constructor EnsembleSolution{Float64,2,Array{ODESolution{Float64,1,Array{Float64,1},Nothing,Nothing,Array{Float64,1},Array{Array{Float64,1},1},ODEProblem{Float64,Tuple{Float64,Float64},false,DiffEqBase.NullParameters,ODEFunction{false,var"#189#190",UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem},Tsit5,OrdinaryDiffEq.InterpolationData{ODEFunction{false,var"#189#190",UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Array{Float64,1},Array{Float64,1},Array{Array{Float64,1},1},OrdinaryDiffEq.Tsit5ConstantCache{Float64,Float64}},DiffEqBase.DEStats},1}}. Gradient is of type Array{Float64,2}

Does it mean Flux training is not compatible with EnsembleProblem? I’d be grateful of any insight into this problem solution.  
The code is as below:

```julia
using DifferentialEquations, Flux

pa = [1.0]

function model1(input) 
  prob = ODEProblem((u, p, t) -> 1.01u * pa[1], 0.5, (0.0, 1.0))
  
  function prob_func(prob, i, repeat)
    remake(prob, u0 = rand() * prob.u0)
  end
  
  ensemble_prob = EnsembleProblem(prob, prob_func = prob_func)
  sim = solve(ensemble_prob, Tsit5(), EnsembleThreads(), trajectories = 100)
end

Input_time_series = zeros(5, 100)
# loss function
loss(x, y) = Flux.mse(model1(x), y)

data = Iterators.repeated((Input_time_series, 0), 1)

cb = function () # callback function to observe training
  println("Tracked Parameters: ", params(pa))
end

opt = ADAM(0.1)
println("Starting to train")
Flux.@epochs 10 Flux.train!(loss, params(pa), data, opt; cb = cb)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 6, 2020, 2:57pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/2 "2020-07-06T14:57:05Z")

</div>

Is this just a repost of [Training on EnsembleProblem: ERROR: LoadError: Need an adjoint for constructor EnsembleSolution](https://discourse.julialang.org/t/training-on-ensembleproblem-error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42546)?

---

<div class="post-metadata">

**Author:** ![yanz4](https://avatars.discourse-cdn.com/v4/letter/y/b5e925/32.png) [@yanz4](https://discourse.julialang.org/u/yanz4)\
**Post date:** [July 6, 2020, 3:11pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/3 "2020-07-06T15:11:29Z")

</div>

Yes, with minor simplification in problem description and code.  
I have requested to deleted the previous post.  
Sorry about the inconvenience

---

<div class="post-metadata">

**Author:** ![yanz4](https://avatars.discourse-cdn.com/v4/letter/y/b5e925/32.png) [@yanz4](https://discourse.julialang.org/u/yanz4)\
**Post date:** [July 7, 2020, 3:52am UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/4 "2020-07-07T03:52:25Z")

</div>

Hi ChrisRackauckas,  
If I might, I wish to request for your kind help on this issue. This bug has baffled me for a while. I’m not sure whether it’s because my code is buggy, or there’s literally no adjoint method provided to EnsembleProblem?  
Thanks.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 7, 2020, 12:05pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/5 "2020-07-07T12:05:01Z")

</div>

It’s not you it’s me. It’s not a bug, just not implemented. I thought I’d get to it yesterday but didn’t quite yet. I’ll make sure to get something working here today though. I think only EnsembleSerial will work because Zygote won’t be able to differentiate multithreading or multiprocessing, but I want to figure out a way around that because it’s not too hard. So say, work-in-progress.

---

<div class="post-metadata">

**Author:** ![yanz4](https://avatars.discourse-cdn.com/v4/letter/y/b5e925/32.png) [@yanz4](https://discourse.julialang.org/u/yanz4)\
**Post date:** [July 7, 2020, 5:17pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/6 "2020-07-07T17:17:10Z")

</div>

Thanks so much for your offer to help. I’m looking forward to your insights.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 10, 2020, 1:36pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/7 "2020-07-10T13:36:48Z")

</div>

Big step forward: Zygote is now compatible with parallelism.

[https://github.com/FluxML/Zygote.jl/pull/728](https://github.com/FluxML/Zygote.jl/pull/728)

I won’t update here anymore, but you can track the issue here: [https://github.com/SciML/DiffEqFlux.jl/issues/279](https://github.com/SciML/DiffEqFlux.jl/issues/279)

---

<div class="post-metadata">

**Author:** ![yanz4](https://avatars.discourse-cdn.com/v4/letter/y/b5e925/32.png) [@yanz4](https://discourse.julialang.org/u/yanz4)\
**Post date:** [July 10, 2020, 3:06pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/9 "2020-07-10T15:06:01Z")

</div>

Thanks for the updates.  
I have updated Zygote version, but running my code still shows the same error. Should I revise my code somehow, besides using the newest version of Zygote?

Regards

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 10, 2020, 3:11pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/10 "2020-07-10T15:11:34Z")

</div>

That doesn’t fix the issue yet, and it requires the unreleased Zygote version.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 10, 2020, 3:11pm UTC](https://discourse.julialang.org/t/error-loaderror-need-an-adjoint-for-constructor-ensemblesolution/42611/11 "2020-07-10T15:11:49Z")

</div>

The issue will be closed when this is completed. Major progress, but not done yet Close!
