Skip to content

Commit

Permalink
moving stuff out of ReservoirComputing.jl file
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinuzziFrancesco committed Jan 8, 2025
1 parent 7980d4b commit 230bbe8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 80 deletions.
80 changes: 1 addition & 79 deletions src/ReservoirComputing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,7 @@ using StatsBase: sample
using WeightInitializers: DeviceAgnostic, PartialFunction, Utils
@reexport using WeightInitializers

#define global types
abstract type AbstractReservoirComputer end
abstract type AbstractOutputLayer end
abstract type AbstractPrediction end
#should probably move some of these
abstract type AbstractGRUVariant end

#general output layer struct
struct OutputLayer{T, I, S, L} <: AbstractOutputLayer
training_method::T
output_matrix::I
out_size::S
last_value::L
end

#prediction types
"""
Generative(prediction_len)
A prediction strategy that enables models to generate autonomous multi-step
forecasts by recursively feeding their own outputs back as inputs for
subsequent prediction steps.
# Parameters
- `prediction_len::Int`: The number of future steps to predict.
# Description
The `Generative` prediction method allows a model to perform multi-step
forecasting by using its own previous predictions as inputs for future predictions.
This approach is especially useful in time series analysis, where each prediction
depends on the preceding data points.
At each step, the model takes the current input, generates a prediction,
and then incorporates that prediction into the input for the next step.
This recursive process continues until the specified
number of prediction steps (`prediction_len`) is reached.
"""
struct Generative{T} <: AbstractPrediction
prediction_len::T
end

struct Predictive{I, T} <: AbstractPrediction
prediction_data::I
prediction_len::T
end

"""
Predictive(prediction_data)
A prediction strategy for supervised learning tasks,
where a model predicts labels based on a provided set
of input features (`prediction_data`).
# Parameters
- `prediction_data`: The input data used for prediction, typically structured as a matrix
where each column represents a sample, and each row represents a feature.
# Description
The `Predictive` prediction method is a standard approach
in supervised machine learning tasks. It uses the provided input data
(`prediction_data`) to produce corresponding labels or outputs based
on the learned relationships in the model. Unlike generative prediction,
this method does not recursively feed predictions into the model;
instead, it operates on fixed input data to produce a single batch of predictions.
This method is suitable for tasks like classification,
regression, or other use cases where the input features
and the number of steps are predefined.
"""
function Predictive(prediction_data)
prediction_len = size(prediction_data, 2)
Predictive(prediction_data, prediction_len)
end

#fallbacks for initializers #eventually to remove once migrated to WeightInitializers.jl
for initializer in (:rand_sparse, :delay_line, :delay_line_backward, :cycle_jumps,
Expand Down Expand Up @@ -155,9 +79,7 @@ export scaled_rand, weighted_init, informed_init, minimal_init
export rand_sparse, delay_line, delay_line_backward, cycle_jumps, simple_cycle, pseudo_svd
export RNN, MRNN, GRU, GRUParams, FullyGated, Minimal
export train
export ESN
export HybridESN, KnowledgeModel
export DeepESN
export ESN, HybridESN, KnowledgeModel, DeepESN
export RECA, sample
export RandomMapping, RandomMaps
export Generative, Predictive, OutputLayer
Expand Down
6 changes: 5 additions & 1 deletion src/esn/esn_input_layers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end
informed_init([rng::AbstractRNG=Utils.default_rng()], [T=Float32], dims...;
scaling=0.1, model_in_size, gamma=0.5)
Create an input layer for informed echo state networks.
Create an input layer for informed echo state networks [^Pathak2018].
# Arguments
Expand All @@ -109,6 +109,10 @@ Create an input layer for informed echo state networks.
- `gamma`: The gamma value. Default is 0.5.
# Examples
[^Pathak2018]: Pathak, Jaideep, et al. "Hybrid forecasting of chaotic processes:
Using machine learning in conjunction with a knowledge-based model."
Chaos: An Interdisciplinary Journal of Nonlinear Science 28.4 (2018).
"""
function informed_init(rng::AbstractRNG, ::Type{T}, dims::Integer...;
scaling=T(0.1), model_in_size, gamma=T(0.5)) where {T <: Number}
Expand Down
1 change: 1 addition & 0 deletions src/esn/esn_reservoir_drivers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ function allocate_tmp(::MRNN, tmp_type, res_size)
return [Adapt.adapt(tmp_type, zeros(res_size, 1)) for i in 1:2]
end

abstract type AbstractGRUVariant end
#GRU-based driver
struct GRU{F, L, R, V, B} #not an abstractreservoirdriver
activation_function::F
Expand Down
75 changes: 75 additions & 0 deletions src/predict.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,78 @@
abstract type AbstractOutputLayer end
abstract type AbstractPrediction end

#general output layer struct
struct OutputLayer{T, I, S, L} <: AbstractOutputLayer
training_method::T
output_matrix::I
out_size::S
last_value::L
end

#prediction types
"""
Generative(prediction_len)
A prediction strategy that enables models to generate autonomous multi-step
forecasts by recursively feeding their own outputs back as inputs for
subsequent prediction steps.
# Parameters
- `prediction_len::Int`: The number of future steps to predict.
# Description
The `Generative` prediction method allows a model to perform multi-step
forecasting by using its own previous predictions as inputs for future predictions.
This approach is especially useful in time series analysis, where each prediction
depends on the preceding data points.
At each step, the model takes the current input, generates a prediction,
and then incorporates that prediction into the input for the next step.
This recursive process continues until the specified
number of prediction steps (`prediction_len`) is reached.
"""
struct Generative{T} <: AbstractPrediction
prediction_len::T
end

struct Predictive{I, T} <: AbstractPrediction
prediction_data::I
prediction_len::T
end

"""
Predictive(prediction_data)
A prediction strategy for supervised learning tasks,
where a model predicts labels based on a provided set
of input features (`prediction_data`).
# Parameters
- `prediction_data`: The input data used for prediction, typically structured as a matrix
where each column represents a sample, and each row represents a feature.
# Description
The `Predictive` prediction method is a standard approach
in supervised machine learning tasks. It uses the provided input data
(`prediction_data`) to produce corresponding labels or outputs based
on the learned relationships in the model. Unlike generative prediction,
this method does not recursively feed predictions into the model;
instead, it operates on fixed input data to produce a single batch of predictions.
This method is suitable for tasks like classification,
regression, or other use cases where the input features
and the number of steps are predefined.
"""
function Predictive(prediction_data)
prediction_len = size(prediction_data, 2)
Predictive(prediction_data, prediction_len)
end


function obtain_prediction(rc::AbstractReservoirComputer,
prediction::Generative,
x,
Expand Down

0 comments on commit 230bbe8

Please sign in to comment.