|
1 | | -abstract type AbstractOutputLayer end |
2 | | -abstract type AbstractPrediction end |
3 | | - |
4 | | -#general output layer struct |
5 | | -struct OutputLayer{T,I,S,L} <: AbstractOutputLayer |
6 | | - training_method::T |
7 | | - output_matrix::I |
8 | | - out_size::S |
9 | | - last_value::L |
10 | | -end |
11 | | - |
12 | | -function Base.show(io::IO, ol::OutputLayer) |
13 | | - print(io, "OutputLayer successfully trained with output size: ", ol.out_size) |
14 | | -end |
15 | | - |
16 | | -#prediction types |
17 | | -""" |
18 | | - Generative(prediction_len) |
19 | | -
|
20 | | -A prediction strategy that enables models to generate autonomous multi-step |
21 | | -forecasts by recursively feeding their own outputs back as inputs for |
22 | | -subsequent prediction steps. |
23 | | -
|
24 | | -# Parameters |
25 | | -
|
26 | | - - `prediction_len`: The number of future steps to predict. |
27 | | -
|
28 | | -# Description |
29 | | -
|
30 | | -The `Generative` prediction method allows a model to perform multi-step |
31 | | -forecasting by using its own previous predictions as inputs for future predictions. |
32 | | -
|
33 | | -At each step, the model takes the current input, generates a prediction, |
34 | | -and then incorporates that prediction into the input for the next step. |
35 | | -This recursive process continues until the specified |
36 | | -number of prediction steps (`prediction_len`) is reached. |
37 | | -""" |
38 | | -struct Generative{T} <: AbstractPrediction |
39 | | - prediction_len::T |
40 | | -end |
41 | | - |
42 | | -struct Predictive{I,T} <: AbstractPrediction |
43 | | - prediction_data::I |
44 | | - prediction_len::T |
45 | | -end |
46 | | - |
47 | | -""" |
48 | | - Predictive(prediction_data) |
49 | | -
|
50 | | -A prediction strategy for supervised learning tasks, |
51 | | -where a model predicts labels based on a provided set |
52 | | -of input features (`prediction_data`). |
53 | | -
|
54 | | -# Parameters |
55 | | -
|
56 | | - - `prediction_data`: The input data used for prediction, `feature` x `sample` |
57 | | -
|
58 | | -# Description |
59 | | -
|
60 | | -The `Predictive` prediction method uses the provided input data |
61 | | -(`prediction_data`) to produce corresponding labels or outputs based |
62 | | -on the learned relationships in the model. |
63 | | -""" |
64 | | -function Predictive(prediction_data::AbstractArray) |
65 | | - prediction_len = size(prediction_data, 2) |
66 | | - return Predictive(prediction_data, prediction_len) |
67 | | -end |
68 | | - |
69 | | -function obtain_prediction(rc::AbstractReservoirComputer, prediction::Generative, |
70 | | - x, output_layer::AbstractOutputLayer, args...; |
71 | | - initial_conditions=output_layer.last_value) |
72 | | - #x = last_state |
73 | | - prediction_len = prediction.prediction_len |
74 | | - train_method = output_layer.training_method |
75 | | - out_size = output_layer.out_size |
76 | | - output = output_storing(train_method, out_size, prediction_len, typeof(rc.states)) |
77 | | - out = initial_conditions |
78 | | - |
79 | | - for i in 1:prediction_len |
80 | | - x, x_new = next_state_prediction!(rc, x, out, i, args...) |
81 | | - out_tmp = get_prediction(train_method, output_layer, x_new) |
82 | | - out = store_results!(train_method, out_tmp, output, i) |
83 | | - end |
84 | | - |
85 | | - return output |
86 | | -end |
87 | | - |
88 | | -function obtain_prediction(rc::AbstractReservoirComputer, prediction::Predictive, |
89 | | - x, output_layer::AbstractOutputLayer, args...; kwargs...) |
90 | | - prediction_len = prediction.prediction_len |
91 | | - train_method = output_layer.training_method |
92 | | - out_size = output_layer.out_size |
93 | | - output = output_storing(train_method, out_size, prediction_len, typeof(rc.states)) |
94 | | - |
95 | | - for i in 1:prediction_len |
96 | | - y = @view prediction.prediction_data[:, i] |
97 | | - x, x_new = next_state_prediction!(rc, x, y, i, args...) |
98 | | - out_tmp = get_prediction(train_method, output_layer, x_new) |
99 | | - out = store_results!(output_layer.training_method, out_tmp, output, i) |
100 | | - end |
101 | | - |
102 | | - return output |
103 | | -end |
104 | | - |
105 | | -#linear models |
106 | | -function get_prediction(training_method, output_layer::AbstractOutputLayer, x) |
107 | | - return output_layer.output_matrix * x |
108 | | -end |
109 | | - |
110 | | -#single matrix for other training methods |
111 | | -function output_storing(training_method, out_size, prediction_len, storing_type) |
112 | | - return adapt(storing_type, zeros(out_size, prediction_len)) |
113 | | -end |
114 | | - |
115 | | -#general storing -> single matrix |
116 | | -function store_results!(training_method, out, output, i) |
117 | | - output[:, i] = out |
118 | | - return out |
119 | | -end |
120 | | - |
121 | 1 | function predict(rc, steps::Int, ps, st; initialdata=nothing) |
122 | 2 | if initialdata == nothing |
123 | 3 | initialdata = rand(Float32, 3) |
|
0 commit comments