-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESN.m
213 lines (157 loc) · 7.62 KB
/
ESN.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
classdef ESN < handle
properties
nInputUnits
nReservoirUnits
nOutputUnits
W_in
W_hat
W_out
rho
leaky_parameter
ridge_parameter
type
methodWeightCompute
rls_delta
rls_lambda
trained
end
methods
function obj = ESN(nInputUnits, nInternalUnits, nOutputUnits, varargin)
% set the number of units
obj.nInputUnits = nInputUnits + 1; % +1 for the bias unit
obj.nReservoirUnits = nInternalUnits;
obj.nOutputUnits = nOutputUnits;
% esn parameters
obj.rho = 0.9;
obj.type = 'plain_esn';
obj.methodWeightCompute = 'pseudoinverse';
obj.leaky_parameter = NaN;
obj.ridge_parameter = NaN;
obj.rls_lambda = NaN;
obj.rls_delta = NaN;
args = varargin;
nargs= length(args);
for i=1:2:nargs
switch args{i}
case 'rho'
obj.rho = args{i+1} ;
case 'type'
obj.type = args{i+1} ;
case 'leaky_parameter'
obj.leaky_parameter = args{i+1};
case 'methodWeightCompute'
obj.methodWeightCompute = args{i+1};
case 'ridge_parameter'
obj.ridge_parameter = args{i+1};
case 'rls'
obj.methodWeightCompute = args{i+1};
case 'rls_lambda'
obj.rls_lambda = args{i+1};
case 'rls_delta'
obj.rls_delta = args{i+1};
otherwise
error('the option does not exist');
end
end
% checking configurations
if strcmp(obj.type, 'leaky_esn') && isnan(obj.leaky_parameter)
error('Incompatible option choosed: leaky_esn but not leaky_parameter set!');
end
if strcmp(obj.methodWeightCompute, 'ridge_regression') && isnan(obj.ridge_parameter)
error('Incompatible option choosed: ridge_regression but not ridge_parameter set!');
end
if strcmp(obj.methodWeightCompute, 'rls') && (isnan(obj.rls_lambda) || isnan(obj.rls_delta))
error('Incompatible option choosed: RLS but parameters not set correctly!');
end
%%%% generate weight matrices
obj.W_in = (0.4 - (-0.4)).*rand(obj.nReservoirUnits, obj.nInputUnits) - 0.4;
connectivity = min([10/obj.nReservoirUnits, 1]); % 10% of connectivity in the reservoir
obj.W_hat = obj.rho * generate_internal_weights(obj.nReservoirUnits, connectivity);
obj.W_out = (0.5 - (-0.5)).*rand(obj.nOutputUnits, obj.nInputUnits+obj.nReservoirUnits) - 0.5;
%%% trained flag
obj.trained = 0;
end
function X_i = compute_statematrix ( obj, inputSequence, curr_state, ~ )
nDataPoints = length(inputSequence(:,1));
%%% current inputsequence state matrix
X_i = zeros(obj.nInputUnits+obj.nReservoirUnits, nDataPoints);
for i = 1:nDataPoints
in = [1 inputSequence(i,:)]';
x = curr_state(obj.nInputUnits+1:end, :);
switch obj.type
case 'plain_esn', x = tanh (obj.W_in * in + obj.W_hat * x);
case 'leaky_esn', ...
x = (1 - obj.leaky_parameter) .* x + ...
obj.leaky_parameter .* tanh (obj.W_in * in + obj.W_hat * x);
otherwise
error('Unrecognised esn.type!');
end
X_i(:,i) = [in; x];
curr_state = X_i(:,i);
end
end
%% TRAIN
function last_state = train (obj, trainInputs, trainTargets, washout, type)
assert(size(trainInputs,1) == size(trainTargets,1));
nTimeSeries = size(trainInputs, 1);
% 'Is it online training?"
if strcmp(obj.methodWeightCompute, 'rls')
[obj.W_out, last_state] = RLS(obj, trainInputs, trainTargets, washout, type);
else % direct methods case
% Computing state matrix
X = compute_multiple_series_state_matrix(obj, trainInputs, washout, NaN, 'training');
switch type
case 'seq2seq'
% Computing targets taking into account intial transient
Y = compute_mutiple_series_targets(trainTargets, washout);
% Compressing all X_i into a BIG state matrix
X_tr = cat(2, X{:});
% Compressing targets into a unique sequence
y_tr = cat(1, Y{:});
case 'seq2elem'
% Root state mapping: selecting last state as
% representative for the entire sequence
foo = cellfun(@(x) x(:,end), X, 'UniformOutput', 0);
X_tr = cat(2,foo{:});
y_tr = trainTargets;
otherwise
error('Unrecognized type!');
end
last_state = X_tr(:,end);
assert(size(X_tr,2) == size(y_tr,1));
% Solve the linear system 'Y_tgt = W_out*X' in terms of W_out using
switch obj.methodWeightCompute
case 'pseudoinverse'
obj.W_out = y_tr' * pinv(X_tr);
case 'ridge_regression'
I = eye(size(X_tr,1));
obj.W_out = y_tr' * X_tr' * inv(X_tr*X_tr' + obj.ridge_parameter*I);
otherwise error('incorrect obj.methodWeightCompute!');
end
end
% set trained flag
obj.trained = 1;
end
%% TEST
function testPred = test (obj, testInputs, state, washout, type)
if obj.trained == 0
error('esn.test: TESTING A NON TRAINED NETWORK!');
end
X = compute_multiple_series_state_matrix(obj, testInputs, washout, state, 'test');
switch type
case 'seq2seq'
% Compressing all X_i into a BIG state matrix
X_ts = cat(2, X{:});
case 'seq2elem'
% Root state mapping: selecting last state as
% representative for the entire sequence
foo = cellfun(@(x) x(:,end), X, 'UniformOutput', 0);
X_ts = cat(2,foo{:});
otherwise
error('Unrecognized type!');
end
% produce predictions real value
testPred = (obj.W_out * X_ts)';
end
end
end