如何在 Julia 中使用 ImplicitEuler 求解器?
How to use ImplicitEuler solver in Julia?
在尝试调用隐式欧拉求解 ODE 时,我收到以下错误:
MethodError: no method matching OrdinaryDiffEq.NLNewtonConstantCache(::Float32, ::Array{Float64,2}, ::LinearAlgebra.LU{Float64,Array{Float64,2}}, ::Bool, ::Bool, ::Bool, ::Float32, ::DiffEqBase.UDerivativeWrapper{ODEFunction{false,DiffEqFlux.var"#dudt_#50"{NeuralODE{Chain{Tuple{Dense{typeof(tanh),Array{Float32,2},Array{Float32,1}},Dense{typeof(identity),Array{Float32,2},Array{Float32,1}}}},Array{Float32,1},Flux.var"#12#14"{Chain{Tuple{Dense{typeof(tanh),Array{Float32,2},Array{Float32,1}},Dense{typeof(identity),Array{Float32,2},Array{Float32,1}}}}},Tuple{Float32,Float32},ImplicitEuler{0,false,DefaultLinSolve,NLNewton{Rational{Int64},Rational{Int64},Rational{Int64}},DataType},Tuple{},Base.Iterators.Pairs{Symbol,Any,NTuple{4,Symbol},NamedTuple{(:dt, :saveat, :reltol, :abstol),Tuple{Float64,StepRangeLen{Float32,Float64,Float64},Float64,Float64}}}}},LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Float32,Array{Float32,1}}, ::Float64, ::Float32, ::Float32)
Closest candidates are:
OrdinaryDiffEq.NLNewtonConstantCache(::tType, ::J, ::W, ::Bool, ::Bool, ::Bool, ::tType, ::ufType, !Matched::tType, ::tType, ::tType) where {tType, J, W, ufType} at /Users/sdoneva/.julia/packages/OrdinaryDiffEq/nV9bA/src/nlsolve/type.jl:62
我就是这样调用求解器的:
using Flux, DiffEqFlux, DifferentialEquations, Plots
u0 = [1.0,1.0]
solver = ImplicitEuler(autodiff = false)
tspan_train = (0.0f0,4.00f0)
train_size = 32
t_train = range(tspan_train[1],tspan_train[2],length = train_size)
function create_neural_ode(solver, tspan, t_saveat)
dudt = Chain(
Dense(2,50,tanh),
Dense(50,2))
ps = Flux.params(dudt)
n_ode = NeuralODE(dudt, tspan, solver, dt=1/2^4, saveat = t_saveat, reltol=1e-7, abstol=1e-9)
n_ode
end
n_ode = create_neural_ode(solver, tspan_train, t_train)
plot(n_ode(u0))
ImplicitEuler 的正确调用方式是什么?
您的状态是 Float64 而时间是 Float32,它只是无法使用该组合初始化隐式工具。我将其标记为错误并希望尽快修复它,但与此同时我建议同时使用 Float32 或 Float64。示例:
using Flux, DiffEqFlux, DifferentialEquations, Plots
u0 = Float32[1.0,1.0]
solver = ImplicitEuler(autodiff = false)
tspan_train = (0.0f0,4.00f0)
train_size = 32
t_train = range(tspan_train[1],tspan_train[2],length = train_size)
function create_neural_ode(solver, tspan, t_saveat)
dudt = Chain(
Dense(2,50,tanh),
Dense(50,2))
ps = Flux.params(dudt)
n_ode = NeuralODE(dudt, tspan, solver, dt=1/2^4, saveat = t_saveat, reltol=1e-7, abstol=1e-9)
n_ode
end
n_ode = create_neural_ode(solver, tspan_train, t_train)
plot(n_ode(u0))
工作正常。
在尝试调用隐式欧拉求解 ODE 时,我收到以下错误:
MethodError: no method matching OrdinaryDiffEq.NLNewtonConstantCache(::Float32, ::Array{Float64,2}, ::LinearAlgebra.LU{Float64,Array{Float64,2}}, ::Bool, ::Bool, ::Bool, ::Float32, ::DiffEqBase.UDerivativeWrapper{ODEFunction{false,DiffEqFlux.var"#dudt_#50"{NeuralODE{Chain{Tuple{Dense{typeof(tanh),Array{Float32,2},Array{Float32,1}},Dense{typeof(identity),Array{Float32,2},Array{Float32,1}}}},Array{Float32,1},Flux.var"#12#14"{Chain{Tuple{Dense{typeof(tanh),Array{Float32,2},Array{Float32,1}},Dense{typeof(identity),Array{Float32,2},Array{Float32,1}}}}},Tuple{Float32,Float32},ImplicitEuler{0,false,DefaultLinSolve,NLNewton{Rational{Int64},Rational{Int64},Rational{Int64}},DataType},Tuple{},Base.Iterators.Pairs{Symbol,Any,NTuple{4,Symbol},NamedTuple{(:dt, :saveat, :reltol, :abstol),Tuple{Float64,StepRangeLen{Float32,Float64,Float64},Float64,Float64}}}}},LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Float32,Array{Float32,1}}, ::Float64, ::Float32, ::Float32)
Closest candidates are:
OrdinaryDiffEq.NLNewtonConstantCache(::tType, ::J, ::W, ::Bool, ::Bool, ::Bool, ::tType, ::ufType, !Matched::tType, ::tType, ::tType) where {tType, J, W, ufType} at /Users/sdoneva/.julia/packages/OrdinaryDiffEq/nV9bA/src/nlsolve/type.jl:62
我就是这样调用求解器的:
using Flux, DiffEqFlux, DifferentialEquations, Plots
u0 = [1.0,1.0]
solver = ImplicitEuler(autodiff = false)
tspan_train = (0.0f0,4.00f0)
train_size = 32
t_train = range(tspan_train[1],tspan_train[2],length = train_size)
function create_neural_ode(solver, tspan, t_saveat)
dudt = Chain(
Dense(2,50,tanh),
Dense(50,2))
ps = Flux.params(dudt)
n_ode = NeuralODE(dudt, tspan, solver, dt=1/2^4, saveat = t_saveat, reltol=1e-7, abstol=1e-9)
n_ode
end
n_ode = create_neural_ode(solver, tspan_train, t_train)
plot(n_ode(u0))
ImplicitEuler 的正确调用方式是什么?
您的状态是 Float64 而时间是 Float32,它只是无法使用该组合初始化隐式工具。我将其标记为错误并希望尽快修复它,但与此同时我建议同时使用 Float32 或 Float64。示例:
using Flux, DiffEqFlux, DifferentialEquations, Plots
u0 = Float32[1.0,1.0]
solver = ImplicitEuler(autodiff = false)
tspan_train = (0.0f0,4.00f0)
train_size = 32
t_train = range(tspan_train[1],tspan_train[2],length = train_size)
function create_neural_ode(solver, tspan, t_saveat)
dudt = Chain(
Dense(2,50,tanh),
Dense(50,2))
ps = Flux.params(dudt)
n_ode = NeuralODE(dudt, tspan, solver, dt=1/2^4, saveat = t_saveat, reltol=1e-7, abstol=1e-9)
n_ode
end
n_ode = create_neural_ode(solver, tspan_train, t_train)
plot(n_ode(u0))
工作正常。