Comments (4)
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.
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.
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.
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)
- The MNIST Neural ODE example does not work with `ReverseDiffAdjoint` HOT 6
- Update Documentation to mention loading AD Packages for Training HOT 4
- `ComponentArrays` makes coupling layers type-unstable unexpectedly HOT 2
- ComponentArrays makes Custom Layers containing Chains type-unstable HOT 4
- Gradients of shared parameters do not behave as expected HOT 1
- inconsistent LSTM results in time series forecast between Flux.jl and Lux.jl
- Broadcast Layer
- Can't use freeze with ComponentArray. HOT 2
- `Lux.testmode` resorts to scalar indexing with frozen params HOT 2
- Custom Model for Neural ODE HOT 1
- Periodic Padding HOT 1
- Export trained model for Tensorflow/PyTorch/C++? HOT 2
- Bug in `ConvTranspose`? HOT 1
- Generating Parameters with CUDA HOT 2
- Zygote gradient fails for Custom Layer HOT 2
- Adaptors should **not** change the dtype HOT 1
- Any equivalency to torch.nn.Parameter? HOT 2
- Support for MultiRNNCell HOT 5
- GPU evaluation of `Recurrence()` broken on Metal HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lux.jl.