Giter Club home page Giter Club logo

Comments (4)

avik-pal avatar avik-pal commented on July 28, 2024 1

Closing this since it seems to have been resolved! For future reference, https://discourse.julialang.org/ is a better place for usage-related questions/bugs!

from lux.jl.

arthur-bizzi avatar arthur-bizzi commented on July 28, 2024

I'm not sure I fully understand what you're getting at, but here are some points regarding your code:

  • You should definitely not convert your parameters to a ComponentArray inside a layer. Lux layers in general should work the same given parameters in the form of tuples or CAs.
  • You're trying to use prob without defining it first. These should probably be a part of your layer struct.
  • It's unclear how you intend the data to flow into your layer. If it's in the usual form of a large matrix, then the time vector should probably be separated so it can be fed into the ODE solver. This is connected to the definition of time-steps, which you're also missing. I would suggest you think about what you're doing a bit more.
  • Lux.initialstates should be defined.
  • There's a few typos in your code.

Hope this helps. We might be able to help more if you reduce your problem to a minimal working example.

from lux.jl.

RohanRajagopal avatar RohanRajagopal commented on July 28, 2024

Hey, thank you very ,very much for your helpful comments. Let me work on it a bit more, before I respond with a set of code that I have adapted based on your suggestions. But, let me to try to be more clear in what I hope to do.

I am trying to implement a layer wherein the activation function of each node is a differential equation; that receives a time series (sequence input), and that is fed to a differential equation system as some sort of external input, (and so acts as the activation function), the ODE is then solved to generate another time series, (sequence output). This is what I did, at least I think I did in this set of code with a "single" node.

tspan = (0, 200)
time_steps = collect(1:1:10)

function hopf_oscillator(du, u, p, t)
	@unpack W = p
	ω_h = 1.
	μ = 1.
	ω_ext = 1
	I0 = 0.1
	w_rand = rand(5)
	signal = [cos.(i*t) for i in 1:5] 
	Iext = W' * signal
	du[1] = u[1] * (μ - u[1]^2) + I0 * Iext * cos(u[2])
	du[2] = ω_h - I0 * Iext * sin(u[2])/u[1]
end

u0_h = [.1, .1]
p_h = ComponentArray(W = rand(5))

prob_h = ODEProblem(hopf_oscillator, u0_h, dt = 0.1, tspan, p_h)

function gradients_hopf_with_loss_function(p)
	_prob = remake(prob_h, u0 = u0_h, tspan = (0.0, 20.0), p = p)
	sol = solve(_prob, Rosenbrock23(), saveat = time_steps)
	theta = sol[2,:]
        D =  cos.(time_steps)
	r = sol[1, end]
	x = r*cos.(theta) .- D
	sum(abs2, x)  #loss function
end

for i in 1:50
	dp_h = Zygote.gradient(gradients_hopf_with_loss_function, p_h)
	for j in 1:length(p_h)
		p_h[j] = p_h[j] - 0.1 * dp_h[1][j]
	end
end

I hope that makes sense, somewhat. Thank you very much.

from lux.jl.

RohanRajagopal avatar RohanRajagopal commented on July 28, 2024

Okay this seems to work,

using Lux, DifferentialEquations, Zygote, SciMLSensitivity, Random, ComponentArrays, Parameters, DataInterpolations, Plots


rng = Random.default_rng()
Random.seed!(rng, 0)

struct Modified_Dense{M <: Lux.AbstractExplicitLayer} <: Lux.AbstractExplicitContainerLayer{(:model,)}
    model::M
    u0
    time_steps 
    solver
    sensealg
    tspan
    kwargs    
end

function Modified_Dense(model::Lux.AbstractExplicitLayer; u0 = 0.1 .* ones(model.out_dims, 2), time_steps = (0.1:0.1:100), solver=Tsit5(), tspan=(0.0f0, 100.0f0), 
    sensealg=InterpolatingAdjoint(; autojacvec=ZygoteVJP()), kwargs...)
    return Modified_Dense(model, u0, time_steps, solver, sensealg, tspan, kwargs)
end

diffeqsol_to_array(x::ODESolution) = mapreduce(permutedims, vcat, x.u)

function (n::Modified_Dense)(x, ps, st)
    function ODE_Activation(du, u, p, t)
        @unpack ps = p
        for k in 1:n.model.out_dims
            du[k, 1] = u[k, 1] * (1 - u[k, 1]^2) + (ps.weight[k]*x(t) + ps.bias[k]) * cos(u[k, 2])
            du[k, 2] = k - (ps.weight[k]*x(t) + ps.bias[k]) * sin(u[k, 2]) / u[k, 1]
        end
    end

    prob = ODEProblem(ODE_Activation, n.u0, n.tspan, ps)
    sol = solve(prob, n.solver, saveat = n.time_steps)
    u1 = sol[:,1,:]
    u2 = sol[:,2,:]
    out = u1.*sin.(u2)
    println(size(out))
    return out, st
end

model = Modified_Dense(Dense(1,10, identity))

ps, st = Lux.setup(rng, model)
ps = ComponentArray(ps=ps)
dev = gpu_device()

x = CubicSpline(sin.(4 * π * model.time_steps), model.time_steps)

model(x, ps, st)

gradient(ps -> sum(first(model(x, ps, st))), ps)

from lux.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.