Skip to content
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

Deep Bayesian Model Discovery without using NeuralODE object #169

Open
gurtajbir opened this issue Aug 2, 2023 · 17 comments
Open

Deep Bayesian Model Discovery without using NeuralODE object #169

gurtajbir opened this issue Aug 2, 2023 · 17 comments

Comments

@gurtajbir
Copy link

gurtajbir commented Aug 2, 2023

Hi everyone. I am trying to implement the Deep Bayesian Model Discovery on the Lotka-Volterra model discussed in the Automated Discovery of Missing Physics example. 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. The main issue here is that the hamiltonian is fed a vector of parameters 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 will be very helpful. Thanks.

@ChrisRackauckas ChrisRackauckas transferred this issue from SciML/SciMLTutorials.jl Aug 2, 2023
@ChrisRackauckas
Copy link
Member

What have you tried so far? You'd do the exact same thing except change the optimization to the Bayesian fitting routine.

@gurtajbir
Copy link
Author

gurtajbir commented Aug 2, 2023

Hi Chris. I was trying to extract the parameters of the Flux model as a vector using a function like the following for a model U,

`p_model = []
for i in 1:length(U.layers)
weights = Float64.(Flux.params(U.layers[i])[1])
for row in weights
p_model = [p_model; row]
end
biases = Float64.(Flux.params(U.layers[i])[2])
p_model = [p_model; biases]
end'

This was now in the correct format to be fed into metric = DiagEuclideanMetric(length(p)) and integrator = Leapfrog(find_good_stepsize(h, p)). But the trouble was with converting the update to the parameter vector back to something that could be used by the neural network in the UDE dynamics.

@gurtajbir
Copy link
Author

gurtajbir commented Aug 2, 2023

I had posted the same on Julia discourse. I was advised to use θ, re = Flux.destructure(chain) which now enables me to make predictions in the UDE dynamics using the new set of parameters to re . This implementation on my part is inefficient but it able to get the job done as in it is able to take the new parameters and make a prediction based on that
function ude_dynamics!(du, u, p, t, p_true)
U = re(p) # Reconstruct with passed parameters p
û = U(u) # Network prediction
du[1] = p_true[1] * u[1] + û[1]
du[2] = -p_true[4] * u[2] + û[2]
end

@gurtajbir
Copy link
Author

gurtajbir commented Aug 2, 2023

What have you tried so far? You'd do the exact same thing except change the optimization to the Bayesian fitting routine.

What change are you referring to here? Do you mean a change to the loss l(θ) = -sum(abs2, ode_data .- predict_neuralode(θ)) - sum(θ .* θ) or somewhere here
integrator = Leapfrog(find_good_stepsize(h, p)) prop = AdvancedHMC.NUTS{MultinomialTS, GeneralisedNoUTurn}(integrator) adaptor = StanHMCAdaptor(MassMatrixAdaptor(metric), StepSizeAdaptor(0.45, integrator)) samples, stats = sample(h, prop, p, 500, adaptor, 500; progress = true)

I do not have a strong background in Bayesian Inference so most of this stuff is new to me. I did go on try exactly the same as in the tutorial and got the following result
Screenshot 2023-08-02 at 6 50 32 PM

@ChrisRackauckas
Copy link
Member

I highly recommend just changing to a Lux network instead of a Flux one. Is the tutorial using Flux? If so we should just update it

@Vaibhavdixit02
Copy link
Member

@gurtajbir it looks like you are on the right track, how many samples is that plot from? Are you dropping the warmup samples?

@gurtajbir
Copy link
Author

gurtajbir commented Aug 8, 2023

@gurtajbir it looks like you are on the right track, how many samples is that plot from? Are you dropping the warmup samples?

Hi @Vaibhavdixit02 . This plot was using the below
samples, stats = sample(h, prop, p, 500, adaptor, 2000; progress = true). These particular values were used according to the code provided here.

@gurtajbir
Copy link
Author

I highly recommend just changing to a Lux network instead of a Flux one. Is the tutorial using Flux? If so we should just update it

Hi @ChrisRackauckas. Just changing to Lux seemed to considerably improve how the plot looked. Also, with same network size and activation function, the code with Lux faster than that with Flux (almost 3 times as fast).
Screenshot 2023-08-10 at 3 49 32 PM

@ChrisRackauckas
Copy link
Member

Yeah that's expected. Could you make a PR to update that code?

@gurtajbir
Copy link
Author

Would you like me to update the existing deep bayesian discovery example in the PR or create a code file that resembles what I am trying out on Lotka-Volterra ?

@ChrisRackauckas
Copy link
Member

Updating the deep bayesian discovery example in a PR that changes it to Lux (and ultimately improves its stability) would be perfect.

@gurtajbir
Copy link
Author

Sounds good. I'll get started on it.

@Doublonmousse
Copy link

I've made a gist redoing https://docs.sciml.ai/Overview/stable/showcase/bayesian_neural_ode/#Step-5:-Plot-diagnostics with Lux and no NeuralODE object here : https://gist.github.com/Doublonmousse/64c5226edc8419dc6bf8e0594f2cb89f.

If this good enough ? I'll do a PR to update the example in this repo if it's okay

@ChrisRackauckas
Copy link
Member

Yeah that looks good, though I don't think you need the Metal import?

@Doublonmousse
Copy link

Yeah, I'm not using it (it's also imported twice ..).

Maybe vector_to_parameters is superfluous or can be improved as well?

I'll open a PR for it then soon so that we can discuss further things there.

@Doublonmousse
Copy link

Wait, the github version is already up to date with Lux

# ML Tools
using Lux, Zygote

With the change dating back from #217

but the live web version isn't up to date yet.

@ChrisRackauckas
Copy link
Member

There is currently an issue with the doc building due to a bug in CUDA #224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants