Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build on LuxCore #206

Open
MartinuzziFrancesco opened this issue Mar 1, 2024 · 2 comments
Open

Build on LuxCore #206

MartinuzziFrancesco opened this issue Mar 1, 2024 · 2 comments

Comments

@MartinuzziFrancesco
Copy link
Collaborator

MartinuzziFrancesco commented Mar 1, 2024

A lot of recent issues can be tackled by an internal refactor of the drivers and generally of how the models are defined. Since this refactor is in the plans, it is worth exploring the possibility of building on LuxCore.jl, to provide a familiar interface to SciML users. I will update this issue with ideas on how to build the components

@MartinuzziFrancesco MartinuzziFrancesco changed the title Build on LuxLib Build on LuxCore Mar 1, 2024
@MartinuzziFrancesco
Copy link
Collaborator Author

MartinuzziFrancesco commented Mar 2, 2024

Some very raw initial ideas for the implementation:

struct ESNCell{use_bias, A, B, W, I, S} <: LuxCore.AbstractExplicitLayer
    activation::A
    in_dims::Int
    res_dims::Int
    init_bias::B
    init_reservoir::W
    init_input::I
    #init_feedback::F
    init_state::S
    leak_coefficient::Number
end

function ESNCell((in_dims, res_dims)::Pair{<:Int, <:Int}, activation=tanh;
    use_bias::Bool=false, init_bias=zeros32, init_reservoir=glorot_uniform,
    init_input=glorot_uniform, init_state=zeros32)
    return ESNCell{use_bias,typeof(activation),typeof(init_bias),
        typeof(init_reservoir),typeof(init_input),typeof(init_state)}(
        activation,in_dims,res_dims,init_bias,init_reservoir,init_input,init_state)
end

# none of the weights in a esn cell are trainable. should the be parameters or states?
function LuxCore.initialparameters(rng::AbstractRNG,
    esn::ESNCell{use_bias}) where {use_bias}

    ps = (input_matrix=esn.init_input(rng, esn.res_dims, esn.in_dims),
        reservoir_matrix=esn.init_reservoir(rng, esn.res_dims, esn.res_dims))
    use_bias && (ps = merge(ps, (bias=esn.init_bias(rng, esn.res_dims),)))
    return ps
end

function LuxCore.initialstates(rng::AbstractRNG,
    esn::ESNCell{use_bias}) where {use_bias}
    randn(rng, 1)
    st = (states = nothing, rng=LuxCore.replicate(rng))
    return st
end

const _ESNCellInputType = Tuple{<:AbstractMatrix, Tuple{<:AbstractMatrix}}

function (esn::ESNCell{true})((x, (hidden_state,))::_ESNCellInputType,
    ps, st::NamedTuple)
    h_new =(1 - esn.leaky_coefficient) * x + esn.activation.(st.input_matrix * x .+ 
        st.reservoir_matrix * hidden_state .+ st.bias)
    return (h_new, (h_new,)), st
end

function (esn::ESNCell{false})((x, (hidden_state,))::_ESNCellInputType,
    ps, st::NamedTuple)
    h_new = (1 - esn.leaky_coefficient) * x + esn.activation.(ps.input_matrix * x .+
        ps.reservoir_matrix * hidden_state)
    return (h_new, (h_new,)), st
end

# very WIP stuff here
# passes the data through the esn and obtains the states
function instantiate(esn::ESNCell, xs, ps, st)
    st_new = (:states)
    return NamedTuple(:last_state, :states)
end

struct ESN{T} <: LuxCore.AbstractExplicitContainerLayer{(:reservoir, :states, :readout)}
    reservoir::LuxCore.AbstractExplicitLayer
    states::Function # here go the nlas and state augmentations # this should be something like a chain
    readout::T # this has to be generic to accommodate multiple types of readouts # how do these return ps and st?
end

function ESN(in_size::Int, res_size::Int, out_size::Int;
    leaky_coefficient::Number = 0.0, init_reservoir = rand_sparse, init_input = scaled_rand,
    bias=false, init_bias=rand, readout = StandardRidge(0.0), states = States(), activation=tanh)

    esn = ESNCell(in_size, res_size, activation; bias=bias, init_reservoir=init_reservoir)
end

@MartinuzziFrancesco
Copy link
Collaborator Author

Actually I think all the weights in the ESN have to be states instead of parameters, in case someone needs to build a larger model with ESNs

function LuxCore.initialparameters(rng::AbstractRNG,
    esn::ESNCell{use_bias}) where {use_bias}

    return NamedTuple()
end

function LuxCore.initialstates(rng::AbstractRNG,
    esn::ESNCell{use_bias}) where {use_bias}
    randn(rng, 1)
    st = (input_matrix=esn.init_input(rng, esn.res_dims, esn.in_dims),
        reservoir_matrix=esn.init_reservoir(rng, esn.res_dims, esn.res_dims),
        states = nothing, rng=LuxCore.replicate(rng))
    use_bias && (st = merge(st, (bias=esn.init_bias(rng, esn.res_dims),)))
    return st
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant