|
| 1 | +# Building a model from scratch |
| 2 | + |
| 3 | +ReservoirComputing.jl provides utilities to build reservoir reservoir |
| 4 | +computing models from scratch. In this tutorial we are going to build |
| 5 | +an echo state network ([`ESN`](@ref)) and showcase how this custom |
| 6 | +implementation is equivalent to the provided model (minus some comfort |
| 7 | +utilities) |
| 8 | + |
| 9 | +## Using provided layers: ReservoirChain, ESNCell, and LinearReadout |
| 10 | + |
| 11 | +The library provides a [`ReservoirChain`](@ref), which is virtually |
| 12 | +equivivalent to Lux's [`Chain`](@extref). Passing layers, or functions, |
| 13 | +to the chain will concatenate them, and will allow the flow of the input |
| 14 | +data through the model. |
| 15 | + |
| 16 | +To build an ESN we also need a [`ESNCell`](@ref) to provide the ESN |
| 17 | +forward pass. However, the cell is stateless, so to keep the memoruy of |
| 18 | +the input we need to wrap it in a [`StatefulLayer`](@ref), which saves the |
| 19 | +internal state in the model states `st` and feeds it to the cell in the |
| 20 | +next step. |
| 21 | + |
| 22 | +Finally, we need the trainable readout for the reservoir computing. |
| 23 | +The library provides [`LinearReadout`](@ref), a dense layer the weights |
| 24 | +of which will be trained using linear regression. |
| 25 | + |
| 26 | +Putting it all together we get the following |
| 27 | + |
| 28 | +```@example scratch |
| 29 | +using ReservoirComputing |
| 30 | +
|
| 31 | +esn_scratch = ReservoirChain( |
| 32 | + StatefulLayer( |
| 33 | + ESNCell(3=>50) |
| 34 | + ), |
| 35 | + LinearReadout(50=>1) |
| 36 | +) |
| 37 | +``` |
| 38 | + |
| 39 | +Now, this implementation, elements naming aside, is completley equivalent to |
| 40 | +the following |
| 41 | + |
| 42 | +```@example scratch |
| 43 | +esn = ESN(3, 50, 1) |
| 44 | +``` |
| 45 | + |
| 46 | +and we can check it initializing the two models and comparing, for instance, |
| 47 | +the weights of the input layer: |
| 48 | + |
| 49 | +```@example scratch |
| 50 | +using Random |
| 51 | +Random.seed(43) |
| 52 | +
|
| 53 | +rng = MersenneTwister(17) |
| 54 | +ps_s, st_s = setup(rng, esn_scratch) |
| 55 | +
|
| 56 | +rng = MersenneTwister(17) |
| 57 | +ps, st = setup(rng, esn) |
| 58 | +
|
| 59 | +ps_s.layer_1.input_matrix == ps.cell.input_matrix |
| 60 | +``` |
| 61 | + |
| 62 | +Both the models can be trained using [`train!`](@ref), and predictions can be |
| 63 | +obtained with [`predict`](@ref). The internal states collected for linear |
| 64 | +regression are computed by traversing the [`ReservoirChain`](@ref), and |
| 65 | +stopping right before the [`LinearReadout`](@ref). |
| 66 | + |
| 67 | +## Manual state collection with Collect |
| 68 | + |
| 69 | +For more complicated models usually you would want to control when the state |
| 70 | +collection happens. In a [`ReservoirChain`](@ref), the collection of states is |
| 71 | +controlled by the layer [`Collect`](@ref). The role of this layer is to tell |
| 72 | +the [`collectstates`](@ref) function where to stop for state collection. All |
| 73 | +the readout layers have a `include_collect=true` keyword, which forces a |
| 74 | +[`Collect`](@ref) layer bvefore the readout. The model we wrote before can |
| 75 | +be written as |
| 76 | + |
| 77 | +```@example scratch |
| 78 | +esn_scratch = ReservoirChain( |
| 79 | + StatefulLayer( |
| 80 | + ESNCell(3=>50) |
| 81 | + ), |
| 82 | + Collect(), |
| 83 | + LinearReadout(50=>1; include_collect=false) |
| 84 | +) |
| 85 | +``` |
| 86 | + |
| 87 | +to make the collection explicit. This layer is useful in case one needs to build |
| 88 | +more complicated models such as a [`DeepESN`](@ref). We can build a deep model |
| 89 | +in multiple ways: |
| 90 | + |
| 91 | +```@example scratch |
| 92 | +deepesn_scratch = ReservoirChain( |
| 93 | + StatefulLayer( |
| 94 | + ESNCell(3=>50) |
| 95 | + ), |
| 96 | + StatefulLayer( |
| 97 | + ESNCell(50=>50) |
| 98 | + ), |
| 99 | + StatefulLayer( |
| 100 | + ESNCell(50=>50) |
| 101 | + ), |
| 102 | + Collect(), |
| 103 | + LinearReadout(50=>1; include_collect=false) |
| 104 | +) |
| 105 | +``` |
| 106 | + |
| 107 | +this first approach is the one provided by default in the library through |
| 108 | +[`DeepESN`](@ref). However, you could want the state collection to be after each |
| 109 | +cell |
| 110 | + |
| 111 | +```@example scratch |
| 112 | +deepesn_scratch = ReservoirChain( |
| 113 | + StatefulLayer( |
| 114 | + ESNCell(3=>50) |
| 115 | + ), |
| 116 | + Collect(), |
| 117 | + StatefulLayer( |
| 118 | + ESNCell(50=>50) |
| 119 | + ), |
| 120 | + Collect(), |
| 121 | + StatefulLayer( |
| 122 | + ESNCell(50=>50) |
| 123 | + ), |
| 124 | + Collect(), |
| 125 | + LinearReadout(50=>1; include_collect=false) |
| 126 | +) |
| 127 | +``` |
| 128 | + |
| 129 | +With this approach, the resulting state will be a concatenation of the states at each |
| 130 | +[`Collect`](@ref) point. So the resulting states for this architecture will be vector of |
| 131 | +size 150. |
| 132 | + |
| 133 | +```@example scratch |
| 134 | +ps, st = setup(rng, deepesn_scratch) |
| 135 | +states, st = collectstates(deepesn_scratch, rand(3, 300), ps, st) |
| 136 | +size(states[:,1]) |
| 137 | +``` |
| 138 | + |
| 139 | +This allows for even more complex constructions, where the |
| 140 | +state collection follows specific patterns |
| 141 | + |
| 142 | +```@example scratch |
| 143 | +deepesn_scratch = ReservoirChain( |
| 144 | + StatefulLayer( |
| 145 | + ESNCell(3=>50) |
| 146 | + ), |
| 147 | + StatefulLayer( |
| 148 | + ESNCell(50=>50) |
| 149 | + ), |
| 150 | + Collect(), |
| 151 | + StatefulLayer( |
| 152 | + ESNCell(50=>50) |
| 153 | + ), |
| 154 | + Collect(), |
| 155 | + LinearReadout(50=>1; include_collect=false) |
| 156 | +) |
| 157 | +``` |
| 158 | + |
| 159 | +Here, for instance, we have a [`Collect`](@ref) after the first two cells and then one |
| 160 | +at the very end. You can see how the size of the states is now 100: |
| 161 | + |
| 162 | +```@example scratch |
| 163 | +ps, st = setup(rng, deepesn_scratch) |
| 164 | +states, st = collectstates(deepesn_scratch, rand(3, 300), ps, st) |
| 165 | +size(states[:,1]) |
| 166 | +``` |
| 167 | + |
| 168 | +Similar approaches could be leveraged, for instance, when the data show |
| 169 | +multiscale dynamics that require specific modeling approaches. |
0 commit comments