Skip to content

Commit 8092f24

Browse files
docs: add docstrings for models
1 parent eff01d5 commit 8092f24

File tree

7 files changed

+212
-23
lines changed

7 files changed

+212
-23
lines changed

docs/pages.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ pages = [
1919
"States"=>"api/states.md",
2020
"Train"=>"api/train.md",
2121
"Predict"=>"api/predict.md",
22-
"Initializers"=>"api/inits.md",
23-
"ReCA"=>"api/reca.md"] #"References" => "references.md"
22+
"Initializers"=>"api/inits.md"] #"References" => "references.md"
2423
]

docs/src/api/models.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
# Models
22

3+
## Echo State Networks
4+
35
```@docs
46
ESN
57
DeepESN
68
HybridESN
79
```
10+
11+
## Reservoir Computing with Cellular Automata
12+
13+
```@docs
14+
RECA
15+
```
16+
17+
The input encodings are the equivalent of the input matrices of the ESNs. These are the available encodings:
18+
19+
```@docs
20+
RandomMapping
21+
```
22+
23+
The training and prediction follow the same workflow as the ESN. It is important to note that currently we were unable to find any papers using these models with a `Generative` approach for the prediction, so full support is given only to the `Predictive` method.

docs/src/api/reca.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/layers/basic.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ vectors are concatenated with `vcat` in order of appearance.
128128
129129
## Parameters
130130
131-
- None (`Collect` has no trainable parameters).
131+
- None.
132132
133133
## States
134134
135-
- None (state is passed through unchanged).
135+
- None.
136136
137137
## Notes
138138

src/layers/esn_cell.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@doc raw"""
22
ESNCell(in_dims => out_dims, [activation];
33
use_bias=false, init_bias=rand32,
4-
init_reservoir=rand_sparse, init_input=weighted_init,
4+
init_reservoir=rand_sparse, init_input=scaled_rand,
55
init_state=randn32, leak_coefficient=1.0)
66
77
Echo State Network (ESN) recurrent cell with optional leaky integration.
@@ -29,6 +29,7 @@ Echo State Network (ESN) recurrent cell with optional leaky integration.
2929
- `init_reservoir`: Initializer for the reservoir matrix `W_res`.
3030
Default is [`rand_sparse`](@ref).
3131
- `init_input`: Initializer for the input matrix `W_in`.
32+
Default is [`scaled_rand`](@ref).
3233
- `init_state`: Initializer for the hidden state when an external
3334
state is not provided. Default is [`randn32`](@extref).
3435
- `leak_coefficient`: Leak rate `α ∈ (0,1]`. Default: `1.0`.

src/models/esn.jl

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,91 @@
1-
"""
1+
@doc raw"""
22
ESN(in_dims, res_dims, out_dims, activation=tanh;
33
leak_coefficient=1.0, init_reservoir=rand_sparse, init_input=scaled_rand,
44
init_bias=zeros32, init_state=randn32, use_bias=false,
55
state_modifiers=(), readout_activation=identity)
66
7-
Build a ESN.
7+
Echo State Network (ESN): a reservoir (recurrent) layer followed by an optional
8+
sequence of state-modifier layers and a linear readout.
9+
10+
`ESN` composes:
11+
1) a stateful [`ESNCell`](@ref) (reservoir),
12+
2) zero or more `state_modifiers` applied to the reservoir state, and
13+
3) a [`LinearReadout`](@ref) mapping reservoir features to outputs.
14+
15+
## Equations
16+
17+
For input `\mathbf{x}(t) ∈ \mathbb{R}^{in\_dims}`, reservoir state
18+
`\mathbf{h}(t) ∈ \mathbb{R}^{res\_dims}`, and output
19+
`\mathbf{y}(t) ∈ \mathbb{R}^{out\_dims}`:
20+
21+
```math
22+
\begin{aligned}
23+
\tilde{\mathbf{h}}(t) &= \phi\!\left(\mathbf{W}_{in}\,\mathbf{x}(t) +
24+
\mathbf{W}_{res}\,\mathbf{h}(t-1) + \mathbf{b}\right) \\
25+
\mathbf{h}(t) &= (1-\alpha)\,\mathbf{h}(t-1) + \alpha\,\tilde{\mathbf{h}}(t) \\
26+
\mathbf{z}(t) &= \psi\!\left(\mathrm{Mods}\big(\mathbf{h}(t)\big)\right) \\
27+
\mathbf{y}(t) &= \rho\!\left(\mathbf{W}_{out}\,\mathbf{z}(t) + \mathbf{b}_{out}\right)
28+
\end{aligned}
29+
```
30+
31+
## Arguments
32+
33+
- `in_dims`: Input dimension.
34+
- `res_dims`: Reservoir (hidden state) dimension.
35+
- `out_dims`: Output dimension.
36+
- `activation`: Reservoir activation (for [`ESNCell`](@ref)). Default: `tanh`.
37+
38+
## Keyword arguments
39+
40+
Reservoir (passed to [`ESNCell`](@ref)):
41+
42+
- `leak_coefficient`: Leak rate `α ∈ (0,1]`. Default: `1.0`.
43+
- `init_reservoir`: Initializer for `W_res`. Default: [`rand_sparse`](@ref).
44+
- `init_input`: Initializer for `W_in`. Default: [`scaled_rand`](@ref).
45+
- `init_bias`: Initializer for reservoir bias (used iff `use_bias=true`).
46+
Default: [`zeros32`](@extref).
47+
- `init_state`: Initializer used when an external state is not provided.
48+
Default: [`randn32`](@extref).
49+
- `use_bias`: Whether the reservoir uses a bias term. Default: `false`.
50+
51+
Composition:
52+
53+
- `state_modifiers`: A layer or collection of layers applied to the reservoir
54+
state before the readout. Accepts a single layer, an `AbstractVector`, or a
55+
`Tuple`. Default: empty `()`.
56+
- `readout_activation`: Activation for the linear readout. Default: `identity`.
57+
58+
## Inputs
59+
60+
- `x :: AbstractArray (in_dims, batch)`
61+
62+
## Returns
63+
64+
- Output `y :: (out_dims, batch)`.
65+
- Updated layer state (NamedTuple).
66+
67+
## Parameters
68+
69+
- `cell` — parameters of the internal [`ESNCell`](@ref), including:
70+
- `input_matrix :: (res_dims × in_dims)` — `W_in`
71+
- `reservoir_matrix :: (res_dims × res_dims)` — `W_res`
72+
- `bias :: (res_dims,)` — present only if `use_bias=true`
73+
- `states_modifiers` — a `Tuple` with parameters for each modifier layer (may be empty).
74+
- `readout` — parameters of [`LinearReadout`](@ref), typically:
75+
- `weight :: (out_dims × res_dims)` — `W_out`
76+
- `bias :: (out_dims,)` — `b_out` (if the readout uses bias)
77+
78+
> Exact field names for modifiers/readout follow their respective layer
79+
> definitions.
80+
81+
## States
82+
83+
Created by `initialstates(rng, esn)`:
84+
85+
- `cell` — states for the internal [`ESNCell`](@ref) (e.g. `rng` used to sample initial hidden states).
86+
- `states_modifiers` — a `Tuple` with states for each modifier layer.
87+
- `readout` — states for [`LinearReadout`](@ref).
88+
889
"""
990
@concrete struct ESN <: AbstractEchoStateNetwork{(:cell, :states_modifiers, :readout)}
1091
cell

src/models/esn_deep.jl

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""
1+
@doc raw"""
22
DeepESN(in_dims::Int,
33
res_dims::AbstractVector{<:Int},
44
out_dims,
@@ -12,8 +12,113 @@
1212
state_modifiers=(),
1313
readout_activation=identity)
1414
15-
Build a deep ESN: a stack of `StatefulLayer(ESNCell)` with optional per-layer
16-
state modifiers, followed by a final linear readout.
15+
Deep Echo State Network (DeepESN): a stack of stateful [`ESNCell`](@ref) layers
16+
(optionally with per-layer state modifiers) followed by a linear readout.
17+
18+
`DeepESN` composes, for `L = length(res_dims)` layers:
19+
1) a sequence of stateful [`ESNCell`](@ref) with widths `res_dims[ℓ]`,
20+
2) zero or more per-layer `state_modifiers[ℓ]` applied to the layer's state, and
21+
3) a final [`LinearReadout`](@ref) from the last layer's features to the output.
22+
23+
## Equations
24+
25+
For input `\mathbf{x}(t) ∈ \mathbb{R}^{in\_dims}`, per-layer reservoir states
26+
`\mathbf{h}^{(\ell)}(t) ∈ \mathbb{R}^{res\_dims[\ell]}` (`\ell = 1..L`), and output
27+
`\mathbf{y}(t) ∈ \mathbb{R}^{out\_dims}`:
28+
29+
```math
30+
\begin{aligned}
31+
\tilde{\mathbf{h}}^{(1)}(t) &= \phi_1\!\left(
32+
\mathbf{W}^{(1)}_{in}\,\mathbf{x}(t) + \mathbf{W}^{(1)}_{res}\,\mathbf{h}^{(1)}(t-1)
33+
+ \mathbf{b}^{(1)}\right) \\
34+
\mathbf{h}^{(1)}(t) &= (1-\alpha_1)\,\mathbf{h}^{(1)}(t-1) + \alpha_1\,\tilde{\mathbf{h}}^{(1)}(t) \\
35+
\mathbf{u}^{(1)}(t) &= \mathrm{Mods}_1\!\big(\mathbf{h}^{(1)}(t)\big) \\
36+
\tilde{\mathbf{h}}^{(\ell)}(t) &= \phi_\ell\!\left(
37+
\mathbf{W}^{(\ell)}_{in}\,\mathbf{u}^{(\ell-1)}(t) +
38+
\mathbf{W}^{(\ell)}_{res}\,\mathbf{h}^{(\ell)}(t-1) + \mathbf{b}^{(\ell)}\right),
39+
\quad \ell=2..L \\
40+
\mathbf{h}^{(\ell)}(t) &= (1-\alpha_\ell)\,\mathbf{h}^{(\ell)}(t-1) + \alpha_\ell\,\tilde{\mathbf{h}}^{(\ell)}(t),
41+
\quad \ell=2..L \\
42+
\mathbf{u}^{(\ell)}(t) &= \mathrm{Mods}_\ell\!\big(\mathbf{h}^{(\ell)}(t)\big), \quad \ell=2..L \\
43+
\mathbf{y}(t) &= \rho\!\left(\mathbf{W}_{out}\,\mathbf{u}^{(L)}(t) + \mathbf{b}_{out}\right)
44+
\end{aligned}
45+
46+
## Where
47+
48+
- `\mathbf{x}(t) ∈ ℝ^{in_dims × batch}` — input at time `t`.
49+
- `\mathbf{h}^{(\ell)}(t) ∈ ℝ^{res_dims[ℓ] × batch}` — hidden state of layer `ℓ`.
50+
- `\tilde{\mathbf{h}}^{(\ell)}(t)` — candidate state before leaky mixing.
51+
- `\mathbf{u}^{(\ell)}(t)` — features after applying the `ℓ`-th `state_modifiers` (identity if none).
52+
- `\mathbf{y}(t) ∈ ℝ^{out_dims × batch}` — network output.
53+
54+
- `\mathbf{W}^{(\ell)}_{in} ∈ ℝ^{res_dims[ℓ] × in\_size[ℓ]}` — input matrix at layer `ℓ`
55+
(`in_size[1]=in_dims`, `in_size[ℓ]=res_dims[ℓ-1]` for `ℓ>1`).
56+
- `\mathbf{W}^{(\ell)}_{res} ∈ ℝ^{res_dims[ℓ] × res_dims[ℓ]}` — reservoir matrix at layer `ℓ`.
57+
- `\mathbf{b}^{(\ell)} ∈ ℝ^{res_dims[ℓ] × 1}` — reservoir bias (broadcast over batch), present iff `use_bias[ℓ]=true`.
58+
- `\mathbf{W}_{out} ∈ ℝ^{out_dims × res_dims[L]}` — readout matrix.
59+
- `\mathbf{b}_{out} ∈ ℝ^{out_dims × 1}` — readout bias (if used by the readout).
60+
61+
- `\phi_\ell` — activation of layer `ℓ` (`activation[ℓ]`, default `tanh`).
62+
- `\alpha_\ell ∈ (0,1]` — leak coefficient of layer `ℓ` (`leak_coefficient[ℓ]`).
63+
- `\mathrm{Mods}_\ell(·)` — composition of modifiers for layer `ℓ` (may be empty).
64+
- `\rho` — readout activation (`readout_activation`, default `identity`).
65+
66+
## Arguments
67+
68+
- `in_dims`: Input dimension.
69+
- `res_dims`: Vector of reservoir (hidden) dimensions per layer; its length sets the depth `L`.
70+
- `out_dims`: Output dimension.
71+
- `activation`: Reservoir activation(s). Either a single function (broadcast to all layers)
72+
or a vector/tuple of length `L`. Default: `tanh`.
73+
74+
## Keyword arguments
75+
76+
Per-layer reservoir options (passed to each [`ESNCell`](@ref)):
77+
78+
- `leak_coefficient`: Leak rate(s) `α_ℓ ∈ (0,1]`. Scalar or length-`L` collection. Default: `1.0`.
79+
- `init_reservoir`: Initializer(s) for `W_res^{(ℓ)}`. Scalar or length-`L`. Default: [`rand_sparse`](@ref).
80+
- `init_input`: Initializer(s) for `W_in^{(ℓ)}`. Scalar or length-`L`. Default: [`scaled_rand`](@ref).
81+
- `init_bias`: Initializer(s) for reservoir bias (used iff `use_bias[ℓ]=true`).
82+
Scalar or length-`L`. Default: [`zeros32`](@extref).
83+
- `init_state`: Initializer(s) used when an external state is not provided.
84+
Scalar or length-`L`. Default: [`randn32`](@extref).
85+
- `use_bias`: Whether each reservoir uses a bias term. Boolean scalar or length-`L`. Default: `false`.
86+
87+
Composition:
88+
89+
- `state_modifiers`: Per-layer modifier(s) applied to each layer’s state before it
90+
feeds into the next layer (and the readout for the last layer). Accepts `nothing`,
91+
a single layer, a vector/tuple of length `L`, or per-layer collections. Defaults to no modifiers.
92+
- `readout_activation`: Activation for the final linear readout. Default: `identity`.
93+
94+
## Inputs
95+
96+
- `x :: AbstractArray (in_dims, batch)`
97+
98+
## Returns
99+
100+
- Output `y :: (out_dims, batch)`.
101+
- Updated layer state (NamedTuple) containing states for all cells, modifiers, and readout.
102+
103+
## Parameters
104+
105+
- `cells :: NTuple{L,NamedTuple}` — parameters for each [`ESNCell`](@ref), including:
106+
- `input_matrix :: (res_dims[ℓ] × in_size[ℓ])` — `W_in^{(ℓ)}`
107+
- `reservoir_matrix :: (res_dims[ℓ] × res_dims[ℓ])` — `W_res^{(ℓ)}`
108+
- `bias :: (res_dims[ℓ],)` — present only if `use_bias[ℓ]=true`
109+
- `states_modifiers :: NTuple{L,Tuple}` — per-layer tuples of modifier parameters (empty tuples if none).
110+
- `readout` — parameters of [`LinearReadout`](@ref), typically:
111+
- `weight :: (out_dims × res_dims[L])` — `W_out`
112+
- `bias :: (out_dims,)` — `b_out` (if the readout uses bias)
113+
114+
> Exact field names for modifiers/readout follow their respective layer definitions.
115+
116+
## States
117+
118+
- `cells :: NTuple{L,NamedTuple}` — states for each [`ESNCell`](@ref).
119+
- `states_modifiers :: NTuple{L,Tuple}` — per-layer tuples of modifier states.
120+
- `readout` — states for [`LinearReadout`](@ref).
121+
17122
"""
18123
@concrete struct DeepESN <: AbstractEchoStateNetwork{(:cells, :states_modifiers, :readout)}
19124
cells

0 commit comments

Comments
 (0)