From 16e1b56d5243f0ff6c68414407c5f9b94043b42f Mon Sep 17 00:00:00 2001 From: Astitva Aggarwal Date: Tue, 26 Mar 2024 20:50:40 +0530 Subject: [PATCH] merge conflict resolution --- src/NeuralPDE.jl | 2 ++ src/advancedHMC_MCMC.jl | 20 +++++++------------- src/collocated_estim.jl | 27 --------------------------- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/src/NeuralPDE.jl b/src/NeuralPDE.jl index 9bbdf5ce49..4799cf0bae 100644 --- a/src/NeuralPDE.jl +++ b/src/NeuralPDE.jl @@ -30,6 +30,7 @@ using DomainSets: Domain, ClosedInterval, AbstractInterval, leftendpoint, righte using SciMLBase: @add_kwonly, parameterless_type using UnPack: @unpack import ChainRulesCore, Lux, ComponentArrays +using Lux: FromFluxAdaptor using ChainRulesCore: @non_differentiable RuntimeGeneratedFunctions.init(@__MODULE__) @@ -51,6 +52,7 @@ include("neural_adapter.jl") include("advancedHMC_MCMC.jl") include("BPINN_ode.jl") include("PDE_BPINN.jl") +include("dgm.jl") include("collocated_estim.jl") export NNODE, NNDAE, diff --git a/src/advancedHMC_MCMC.jl b/src/advancedHMC_MCMC.jl index 9064ddd9fa..7ca18bd58b 100644 --- a/src/advancedHMC_MCMC.jl +++ b/src/advancedHMC_MCMC.jl @@ -86,14 +86,6 @@ end vector_to_parameters(ps_new::AbstractVector, ps::AbstractVector) = ps_new function LogDensityProblems.logdensity(Tar::LogTargetDensity, θ) - # if Tar.estim_collocate - # return physloglikelihood(Tar, θ) / length(Tar.dataset[1]) + priorweights(Tar, θ) + - # L2LossData(Tar, θ) / length(Tar.dataset[1]) + - # L2loss2(Tar, θ) / length(Tar.dataset[1]) - # else - # return physloglikelihood(Tar, θ) / length(Tar.dataset[1]) + priorweights(Tar, θ) + - # L2LossData(Tar, θ) / length(Tar.dataset[1]) - # end if Tar.estim_collocate return physloglikelihood(Tar, θ) + priorweights(Tar, θ) + L2LossData(Tar, θ) + L2loss2(Tar, θ) @@ -455,7 +447,9 @@ function ahmc_bayesian_pinn_ode(prob::DiffEqBase.ODEProblem, chain; MCMCkwargs = (n_leapfrog = 30,), progress = false, verbose = false, estim_collocate = false) - !(chain isa Lux.AbstractExplicitLayer) && (chain = Lux.transform(chain)) + + !(chain isa Lux.AbstractExplicitLayer) && + (chain = adapt(FromFluxAdaptor(false, false), chain)) # NN parameter prior mean and variance(PriorsNN must be a tuple) if isinplace(prob) throw(error("The BPINN ODE solver only supports out-of-place ODE definitions, i.e. du=f(u,p,t).")) @@ -464,7 +458,7 @@ function ahmc_bayesian_pinn_ode(prob::DiffEqBase.ODEProblem, chain; strategy = strategy == GridTraining ? strategy(physdt) : strategy if dataset != [nothing] && - (length(dataset) < 2 || !(typeof(dataset) <: Vector{<:Vector{<:AbstractFloat}})) + (length(dataset) < 2 || !(dataset isa Vector{<:Vector{<:AbstractFloat}})) throw(error("Invalid dataset. dataset would be timeseries (x̂,t) where type: Vector{Vector{AbstractFloat}")) end @@ -475,7 +469,7 @@ function ahmc_bayesian_pinn_ode(prob::DiffEqBase.ODEProblem, chain; end if chain isa Lux.AbstractExplicitLayer - # Flux-vector, Lux-Named Tuple + # Lux-Named Tuple initial_nnθ, recon, st = generate_Tar(chain, init_params) else error("Only Lux.AbstractExplicitLayer Neural networks are supported") @@ -553,7 +547,7 @@ function ahmc_bayesian_pinn_ode(prob::DiffEqBase.ODEProblem, chain; MCMC_alg = kernelchoice(Kernel, MCMCkwargs) Kernel = AdvancedHMC.make_kernel(MCMC_alg, integrator) samples, stats = sample(hamiltonian, Kernel, initial_θ, draw_samples, adaptor; - progress = progress, verbose = verbose, drop_warmup = true) + progress = progress, verbose = verbose) samplesc[i] = samples statsc[i] = stats @@ -571,7 +565,7 @@ function ahmc_bayesian_pinn_ode(prob::DiffEqBase.ODEProblem, chain; MCMC_alg = kernelchoice(Kernel, MCMCkwargs) Kernel = AdvancedHMC.make_kernel(MCMC_alg, integrator) samples, stats = sample(hamiltonian, Kernel, initial_θ, draw_samples, - adaptor; progress = progress, verbose = verbose, drop_warmup = true) + adaptor; progress = progress, verbose = verbose) @info("Sampling Complete.") @info("Current Physics Log-likelihood : ", physloglikelihood(ℓπ, samples[end])) diff --git a/src/collocated_estim.jl b/src/collocated_estim.jl index a2f81b3ed9..3902f74a27 100644 --- a/src/collocated_estim.jl +++ b/src/collocated_estim.jl @@ -162,31 +162,4 @@ function calderivatives(prob, dataset) gradients = (A2 .- A1) ./ sqrt(eps(eltype(dataset[end][1]))) return gradients -end - -function calculate_derivatives(dataset) - - # u = dataset[1] - # u1 = dataset[2] - # t = dataset[end] - # # control points - # n = Int(floor(length(t) / 10)) - # # spline for datasetvalues(solution) - # # interp = BSplineApprox(u, t, 4, 10, :Uniform, :Uniform) - # interp = CubicSpline(u, t) - # interp1 = CubicSpline(u1, t) - # # derivatives interpolation - # dx = t[2] - t[1] - # time = collect(t[1]:dx:t[end]) - # smoothu = [interp(i) for i in time] - # smoothu1 = [interp1(i) for i in time] - # # derivative of the spline (must match function derivative) - # û = tvdiff(smoothu, 20, 0.5, dx = dx, ε = 1) - # û1 = tvdiff(smoothu1, 20, 0.5, dx = dx, ε = 1) - # # tvdiff(smoothu, 100, 0.035, dx = dx, ε = 1) - # # FDM - # # û1 = diff(u) / dx - # # dataset[1] and smoothu are almost equal(rounding errors) - # return [û, û1] - end \ No newline at end of file