# Deep Bayesian Model Discovery without using NeuralODE object

**URL:** https://discourse.julialang.org/t/deep-bayesian-model-discovery-without-using-neuralode-object/102403
**Category:** Modelling & Simulations
**Tags:** ode, bayesian-inference, sciml
**Created:** [August 2, 2023, 3:51pm UTC](https://discourse.julialang.org/t/deep-bayesian-model-discovery-without-using-neuralode-object/102403 "2023-08-02T15:51:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![gsh19](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gsh19/32/49350_2.png) [@gsh19](https://discourse.julialang.org/u/gsh19)
#### Post date: [August 2, 2023, 3:51pm UTC](https://discourse.julialang.org/t/deep-bayesian-model-discovery-without-using-neuralode-object/102403/1 "2023-08-02T15:51:55Z")

</div>

Hi everyone. I am trying to implement the [Deep Bayesian Model Discovery](https://docs.sciml.ai/Overview/stable/showcase/bayesian_neural_ode/)on the Lotka-Volterra model discussed in the [Automated Discovery of Missing Physics example](https://docs.sciml.ai/Overview/stable/showcase/missing_physics/). The problem I am facing is that I am not able to figure out a way to pass the parameters of the neural network embedded in the ODE of the Lotka-Volterra model to the Hamiltonian as done [here](https://github.com/SciML/SciMLDocs/blob/8660ddc193fea9777b8aa01d5bdf24c2d53b5004/docs/src/showcase/bayesian_neural_ode.md?plain=1#L101). The main issue here is that the hamiltonian is [fed a vector of parameters](https://github.com/TuringLang/AdvancedHMC.jl/blob/eb9b2e0d60ef3dd85768d6e6a9f19de15b8f7130/README.md?plain=1#L205) and they are updated naturally as the optimization is carried out. I am having trouble achieving the same with the missing physics example.  
Any pointers as to how this can be achieved or existing code will be very helpful. Thanks.

---

<div class="post-metadata">

### Author: ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)
#### Post date: [August 2, 2023, 4:36pm UTC](https://discourse.julialang.org/t/deep-bayesian-model-discovery-without-using-neuralode-object/102403/2 "2023-08-02T16:36:36Z")

</div>

Recently @Astitva_Aggarwal faced the same issue while working on Bayesian PINNs and worked around it with this function.

```julia

 function vector_to_parameters(ps_new::AbstractVector, ps::NamedTuple)
    @assert length(ps_new) == Lux.parameterlength(ps)
    i = 1
    function get_ps(x)
        z = reshape(view(ps_new, i:(i + length(x) - 1)), size(x))
        i += length(x)
        return z
    end
    return fmap(get_ps, ps)
end

```

Basically, you want to be able to recreate the ComponentArray (/NamedTuple) from the vector while passing it to Lux chains. Since this will probably come up frequently enough when working with optimizer libraries and ppls I wonder if Lux should be able to handle it automatically, until then it might be nice to put it somewhere in the docs. @Astitva_Aggarwal if you can do that it will be pretty helpful to people!

---

<div class="post-metadata">

### Author: ![Astitva\_Aggarwal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astitva_aggarwal/32/47067_2.png) [@Astitva\_Aggarwal](https://discourse.julialang.org/u/Astitva_Aggarwal)
#### Post date: [August 2, 2023, 5:49pm UTC](https://discourse.julialang.org/t/deep-bayesian-model-discovery-without-using-neuralode-object/102403/3 "2023-08-02T17:49:52Z")

</div>

Yeah will do!  
Also @gsh19 if you are using Lux chains then Lux.setup extracts the NN structure information as st and p (named tuple of initial parameter values). something like this:  
~~  
dudt2 = Lux.Chain(x → x.^3,  
Lux.Dense(2, 50, tanh),  
Lux.Dense(50, 2))  
p, st = Lux.setup(rng, dudt2)  
inititalparams=collect(Float64, vcat(ComponentArrays.ComponentArray(p)))

now initialparams is a vector of initial parameters of the lux chain used to create the NeuralODE.  
this can be directly used for the sampling in AdvancedHMC(pass into find\_good\_stepsize())  
~~  
In case you are using a flux chain then simply after the Flux chain creation adding:  
~~  
θ, re = Flux.destructure(chain)  
~~  
this returns θ(vector of initial parameters), re(recontruct funciton to recreate a NN with a diff set of parameters θi), now θ can be directly passed into the find\_good\_stepsize() again

---

<div class="post-metadata">

### Author: ![gsh19](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gsh19/32/49350_2.png) [@gsh19](https://discourse.julialang.org/u/gsh19)
#### Post date: [August 2, 2023, 6:22pm UTC](https://discourse.julialang.org/t/deep-bayesian-model-discovery-without-using-neuralode-object/102403/4 "2023-08-02T18:22:03Z")

</div>

Thanks @Astitva_Aggarwal and @Vaibhavdixit02 for your inputs. Will implement this soon and will let you know how it goes. Really appreciate the help.

---

<div class="post-metadata">

### Author: ![gsh19](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gsh19/32/49350_2.png) [@gsh19](https://discourse.julialang.org/u/gsh19)
#### Post date: [August 3, 2023, 12:45am UTC](https://discourse.julialang.org/t/deep-bayesian-model-discovery-without-using-neuralode-object/102403/5 "2023-08-03T00:45:11Z")

</div>

Hi @Astitva_Aggarwal @Vaibhavdixit02 . I was using a Flux chain in my code and using `θ, re = Flux.destructure(chain)` did the trick for me. @Vaibhavdixit02 to your point, I think these subtle differences of how handling the above is not very straightforward in Lux does need to be included in the tutorials.
