Giter VIP home page Giter VIP logo

Comments (1)

AlCap23 avatar AlCap23 commented on August 25, 2024

As a first draft inside sindy.jl

using Hyperopt

function SInDy_2(X::AbstractArray{S, 2}, DX::AbstractArray{S, 2}, Ψ::Basis, thresholds::AbstractArray ; f_target = (du, θ, ξ)->norm([norm(ξ, 0); norm(du .- θ'ξ, 2)]), p::AbstractArray = [], t::AbstractVector = [], maxiter::Int64 = 10, opt::T = Optimize.STRRidge(),denoise::Bool = false, normalize::Bool = true, convergence_error = eps()) where {T <: Optimize.AbstractOptimizer, S <: Number}
    @assert size(X)[end] == size(DX)[end]
    nx, nm = size(X)
    ny, nm = size(DX)

    θ = Ψ(X, p, t)

    scales = ones(eltype(X), length(Ψ))

    #ξ = zeros(eltype(X), length(Ψ))
    Ξ = zeros(eltype(X), length(Ψ), ny)


    iters = 0

    denoise ? optimal_shrinkage!') : nothing
    normalize ? DataDrivenDiffEq.normalize_theta!(scales, θ) : nothing

    for j in 1:ny
        ho = @hyperopt for i = length(thresholds), sampler = RandomSampler(), t = thresholds
            set_threshold!(opt, t)
            iters_ = sparse_regression!(view(Ξ, :, j), view(θ, :, :), DX[j, :]', maxiter, opt, false, false, convergence_error)
            f_target(view(DX, j, :), view(θ, :, :), view(Ξ, :, j))
        end
        t_, f_ = minimum(ho)
        set_threshold!(opt, t_[1])
        iters_ = sparse_regression!(view(Ξ, :, j), view(θ, :, :), DX[j, :]', maxiter, opt, false, false, convergence_error)
        iters_ > iters ? iters = iters_ : nothing
    end
    return SparseIdentificationResult(Ξ, Ψ, iters, opt, iters < maxiter, DX, X, p = p)
end

For comparision from the pendulum example

# Vary the sparsity threshold -> gives better results
# Use SR3 with high relaxation (allows the solution to diverge from LTSQ) and high iterations
using BenchmarkTools
using Hyperopt
λs = exp10.(-5:0.1:-1)
opt = STRRidge(1e-2)

# With ParetoFront
@btime SInDy(sol[:,:], DX[:, :], basis, λs, p = [1.0; 1.0], maxiter = 1500, opt = opt)

# Output
  12.790 s (8700924 allocations: 489.95 MiB)
Sparse Identification Result with 4 active terms.

# With Hyperopt
@btime SInDy_2(sol[:,:], DX[:, :], basis, λs, p = [1.0; 1.0], maxiter = 1500, opt = opt)

  16.494 s (8712305 allocations: 496.01 MiB)
Sparse Identification Result with 4 active terms.

from datadrivendiffeq.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.