SciML / DiffEqFlux.jl

Pre-built implicit layer architectures with O(1) backprop, GPUs, and stiff+non-stiff DE solvers, demonstrating scientific machine learning (SciML) and physics-informed machine learning methods
https://docs.sciml.ai/DiffEqFlux/stable
MIT License
870 stars 157 forks source link

No method matching `finite_difference_jacobian` #142

Closed ranjanan closed 4 years ago

ranjanan commented 4 years ago

MWE:

using DiffEqFlux, Flux, Optim, OrdinaryDiffEq, Plots
using LinearAlgebra

function rober(du,u,p,t)
  y₁,y₂,y₃ = u
  k₁,k₂,k₃ = p
  du[1] = -k₁*y₁+k₃*y₂*y₃
  du[2] =  k₁*y₁-k₂*y₂^2-k₃*y₂*y₃
  du[3] =  k₂*y₂^2
  nothing
end

u0 = Float32[1.0,0.0,0.0]
prob = ODEProblem(rober,u0,(0f0,1f5),(4f-2,3f7,1f4))
t = 10f0 .^ (0:1f-1:5)
ode_data = Array(solve(prob,Rodas5(),
                       abstol=1e-6,
                       reltol=1e-6,
                       saveat=t))

dudt2 = FastChain(FastDense(3,16,tanh),
                  StiffLayer(FastDense(16, 16, tanh), 
                            ExponentialScaling(16), 
                            FastDense(16, 16, tanh)),
                  FastDense(16,3))

tend = 10f0
_tspan = (0f0,tend)
_t = t[t .<= tend]

n_ode = NeuralODE(dudt2,_tspan,Rodas5(autodiff=false),abstol=1e-6,reltol=1e-6,saveat=_t)

function predict_n_ode(p)
    n_ode(u0,p)
end

function loss_n_ode(p)
    pred = (predict_n_ode(p))
    loss = sum(abs2,ode_data[:,1:size(pred,2)] .- (pred))
    loss,pred
end

loss_n_ode(n_ode.p) # n_ode.p stores the initial parameters of the neural ODE

Status:

[aae7a2af] DiffEqFlux v1.3.0
  [587475ba] Flux v0.10.1
  [429524aa] Optim v0.20.1
  [1dea7af3] OrdinaryDiffEq v5.28.1
  [91a5bcdd] Plots v0.28.4
  [295af30f] Revise v2.5.0
  [90137ffa] StaticArrays v0.12.1
ranjanan commented 4 years ago

Sorry, wrong repository