SciML / SciMLSensitivity.jl

A component of the DiffEq ecosystem for enabling sensitivity analysis for scientific machine learning (SciML). Optimize-then-discretize, discretize-then-optimize, adjoint methods, and more for ODEs, SDEs, DDEs, DAEs, etc.
https://docs.sciml.ai/SciMLSensitivity/stable/
Other
333 stars 71 forks source link

Neural SDE example fails on convert(::Type{Float64}, x::Tracker.TrackedReal{Float64}) #1109

Closed John-Boik closed 2 months ago

John-Boik commented 2 months ago

Note: I have deleted my original post and replaced it with the note here. My post concerned the tutorial Neural Stochastic Differential Equations. But I realize now that it is outdated, and the new tutorial to follow is Neural Stochastic Differential Equations With Method of Moments.

When I tried to run the new tutorial, I received the same error as before, the first few lines of which are:

ERROR: LoadError: MethodError: no method matching Float64(::Tracker.TrackedReal{Float64})

Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat
   @ Base rounding.jl:207
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:792
  Float64(::IrrationalConstants.Log4π)
   @ IrrationalConstants ~/.julia/packages/IrrationalConstants/vp5v4/src/macro.jl:112
  ...

Stacktrace:
  [1] convert(::Type{Float64}, x::Tracker.TrackedReal{Float64})
    @ Base ./number.jl:7
  [2] setindex!(A::Vector{Float64}, x::Tracker.TrackedReal{Float64}, i1::Int64)
    @ Base ./array.jl:1021
  [3] copyto_unaliased!
    @ ./abstractarray.jl:1094 [inlined]
  [4] copyto!(dest::Vector{Float64}, src::Tracker.TrackedVector{Float64, Vector{Float64}})
    @ Base ./abstractarray.jl:1068
  [5] copyto!
    @ ~/.julia/packages/ComponentArrays/xO4hy/src/similar_convert_copy.jl:42 [inlined]
  [6] copyto!
    @ ./broadcast.jl:997 [inlined]
  [7] copyto!
    @ ./broadcast.jl:956 [inlined]
  [8] materialize!
    @ ./broadcast.jl:914 [inlined]
  [9] materialize!
    @ ./broadcast.jl:911 [inlined]
 [10] restructure(x::ComponentArrays.ComponentVector{…}, y::ComponentArrays.ComponentVector{…})
    @ ArrayInterface ~/.julia/packages/ArrayInterface/TFWPW/src/ArrayInterface.jl:637

However, the error was apparently caused a package that is used in the tutorial, or an extra one that I imported, but which has been updated and fixed in the last few days. In my original example, and in running the new tutorial, I imported those used in the new tutorial, and also the following:

import Base, DiffEqBase, OptimizationFlux
using SciMLSensitivity

I'm not sure which package caused the problem, but when I generated a new project today and imported only the packages used in the new tutorial (and also Lux and Zygote), the error disappeared. If someone else receives the same error as above, it is likely due an outdated package or some package conflict.

John-Boik commented 2 months ago

I mentioned in my main post that the neural SDE tutorial example does not run as given. Here are the errors I get when I run the code.

The first is UndefVarError: ActivationFunction not defined, which occurs when defining the drift neural network. That can be fixed simply by using x -> x.^3 instead.

The next is ComponentArray not defined which occurs at p1 = Lux.ComponentArray(p1). That can be fixed by using p1 = ComponentArrays.ComponentArray(p1).

The next is:

MethodError: no method matching (::DiffEqFlux.NeuralDSDE{…})(::Vector{…}, ::Vector{…}, ::@NamedTuple{…}, ::@NamedTuple{})

Closest candidates are:
  (::DiffEqFlux.NeuralDSDE)(::Any, ::Any, ::Any)
   @ DiffEqFlux ~/.julia/packages/DiffEqFlux/TglmB/src/neural_de.jl:95

which occurs at prediction0, st1, st2 = neuralsde(u0,p,st1,st2). This can be fixed by calling Lux.setup on neuralsde, as I mention in my main post:

p, st = Lux.setup(rng, neuralsde)
prediction0, st = neuralsde(u0,p,st)
ChrisRackauckas commented 2 months ago

Okay yeah, I'd first say that the v6 tutorials are more than 2 years old and won't necessarily be a good guide for v7 usage. There's a major version between them because there are breaking differences, some of which you see and highlighted here. I'd highly recommend just using the updated version and tutorial.