-
Notifications
You must be signed in to change notification settings - Fork 117
/
subspace_demo.py
441 lines (355 loc) · 12.4 KB
/
subspace_demo.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# -*- coding: utf-8 -*-
#
# File : examples/conceptors/subspace_demo.py
# Description : Conceptor first subspace demo
# Date : 5th of December, 2019
#
# This file is part of EchoTorch. EchoTorch is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Nils Schaetti <nils.schaetti@unine.ch>
# Imports
import numpy as np
import torch
import echotorch.nn.conceptors as ecnc
import echotorch.utils.matrix_generation as mg
import argparse
import echotorch.utils
import echotorch.utils.visualisation as ecvs
from echotorch.datasets import DatasetComposer
from torch.utils.data.dataloader import DataLoader
import matplotlib.pyplot as plt
from torch.autograd import Variable
from examples.conceptors.patterns.periodic_patterns import pattern_library
# Random numb. init
torch.random.manual_seed(1)
np.random.seed(1)
# Type parameter
dtype=torch.float64
# Reservoir parameters
reservoir_size = 100
spectral_radius = 1.5
bias_scaling = 0.2
connectivity = 10.0 / reservoir_size
# Inputs parameters
input_scaling = 1.5
# Sequence lengths
washout_length = 500
learn_length = 1000
# Training parameters
loading_method = ecnc.SPESNCell.INPUTS_SIMULATION
# Testing parameters
conceptor_test_length = 200
interpolation_rate = 20
# Plotting parameters
signal_plot_length = 20
singular_plot_length = 50
# Regularization
ridge_param_wstar = 0.0001
ridge_param_wout = 0.01
# Aperture
alpha = 10
gamma = 10.0
# Argument parsing
parser = argparse.ArgumentParser(prog="subspace_demo", description="Fig. 1 BC subspace first demo")
parser.add_argument("--w", type=str, default="", required=False)
parser.add_argument("--w-name", type=str, default="", required=False)
parser.add_argument("--win", type=str, default="", required=False)
parser.add_argument("--win-name", type=str, default="", required=False)
parser.add_argument("--wbias", type=str, default="", required=False)
parser.add_argument("--wbias-name", type=str, default="", required=False)
parser.add_argument("--x0", type=str, default="", required=False)
parser.add_argument("--x0-name", type=str, default="", required=False)
args = parser.parse_args()
# Load W from matlab file and random init ?
if args.w != "":
# Load internal weights
w_generator = mg.matrix_factory.get_generator("matlab", file_name=args.w, entity_name=args.w_name, scale=spectral_radius)
else:
# Generate internal weights
w_generator = mg.matrix_factory.get_generator("normal", mean=0.0, std=1.0, connectivity=connectivity, spectral_radius=spectral_radius)
# end if
# Load Win from matlab file or init randomly
if args.win != "":
# Load internal weights
win_generator = mg.matrix_factory.get_generator("matlab", file_name=args.win, entity_name=args.win_name, scale=input_scaling)
else:
# Generate Win
win_generator = mg.matrix_factory.get_generator("normal", mean=0.0, std=1.0, connectivity=1.0, scale=input_scaling)
# end if
# Load Wbias from matlab from or init randomly
if args.wbias != "":
wbias_generator = mg.matrix_factory.get_generator("matlab", file_name=args.wbias, entity_name=args.wbias_name, shape=reservoir_size, scale=bias_scaling)
else:
wbias_generator = mg.matrix_factory.get_generator("normal", mean=0.0, std=1.0, connectivity=1.0, scale=bias_scaling)
# end if
# Load x0 from matlab from or init randomly
if args.x0 != "":
x0_generator = mg.matrix_factory.get_generator("matlab", file_name=args.x0, entity_name=args.x0_name, shape=reservoir_size)
else:
x0_generator = mg.matrix_factory.get_generator("normal", mean=0.0, std=1.0, connectivity=1.0)
# end if
# First sine periodic pattern
pattern1_training = pattern_library(pattern_id=0, washout_length=washout_length, learn_length=learn_length)
# Second sine periodic pattern
pattern2_training = pattern_library(pattern_id=1, washout_length=washout_length, learn_length=learn_length)
# First 5-periodic pattern
pattern3_training = pattern_library(pattern_id=2, washout_length=washout_length, learn_length=learn_length)
# Second 5-periodic pattern
pattern4_training = pattern_library(pattern_id=3, washout_length=washout_length, learn_length=learn_length)
# Composer
dataset_training = DatasetComposer([pattern1_training, pattern2_training, pattern3_training, pattern4_training])
# Data loader
patterns_loader = DataLoader(dataset_training, batch_size=1, shuffle=False, num_workers=1)
# Create a set of conceptors
conceptors = ecnc.ConceptorSet(input_dim=reservoir_size)
# Create four conceptors, one for each pattern
# Create four conceptors, one for each pattern
for c_i in range(4):
conceptors.add(c_i, ecnc.Conceptor(
input_dim=reservoir_size,
aperture=alpha,
dtype=dtype
))
# end for
# Create a conceptor network using
# the self-predicting ESN which
# will learn four conceptors.
conceptor_net = ecnc.ConceptorNet(
input_dim=1,
hidden_dim=reservoir_size,
output_dim=1,
conceptor=conceptors,
learning_algo='inv',
w_generator=w_generator,
win_generator=win_generator,
wbias_generator=wbias_generator,
input_scaling=1.0,
ridge_param=ridge_param_wout,
w_ridge_param=ridge_param_wstar,
loading_method=loading_method,
washout=washout_length,
dtype=dtype
)
# We create an outside observer to plot
# internal states and SVD afterwards
observer = ecvs.NodeObserver(conceptor_net.cell, initial_state='init')
# Xold and Y collectors
Xold_collector = torch.empty(4 * learn_length, reservoir_size, dtype=dtype)
Y_collector = torch.empty(4 * learn_length, reservoir_size, dtype=dtype)
P_collector = torch.empty(4, signal_plot_length, dtype=dtype)
# Conceptors ON
conceptor_net.conceptor_active(True)
# Go through dataset
for i, data in enumerate(patterns_loader):
# Inputs and labels
inputs, outputs, labels = data
# To Variable
if dtype == torch.float64:
inputs, outputs = Variable(inputs.double()), Variable(outputs.double())
# end if
# Set conceptor to use
conceptors.set(i)
# Set state of the observer
observer.set_state("pattern{}".format(i))
# Feed SP-ESN
X = conceptor_net(inputs, inputs)
# Get targets
Y = conceptor_net.cell.targets(X[0])
# Get features
Xold = conceptor_net.cell.features(X[0])
# Save
Xold_collector[i*learn_length:i*learn_length+learn_length] = Xold
Y_collector[i*learn_length:i*learn_length+learn_length] = Y
P_collector[i] = inputs[0, washout_length:washout_length+signal_plot_length, 0]
# end for
# Observer set as inactive, it will stop observing
# reservoir states and inputs.
observer.set_active(False)
# Learn internal weights
conceptor_net.finalize()
# Predicted by W
predY = torch.mm(conceptor_net.cell.w, Xold_collector.t()).t()
# Compute NRMSE
if loading_method == ecnc.SPESNCell.W_LOADING:
training_NRMSE = echotorch.utils.nrmse(predY, Y_collector)
print(("Training NRMSE : {}".format(training_NRMSE)))
# end if
# Conceptors OFF
conceptor_net.conceptor_active(False)
# No washout this time
conceptor_net.washout = 0
# Run trained ESN with empty inputs (no conceptor learning)
generated = conceptor_net(torch.zeros(1, conceptor_test_length, 1, dtype=dtype))
# Plot the generated signal
plt.title("Messy output after loading W")
plt.plot(generated[0], color='r', linewidth=2)
plt.show()
# Conceptors ON
conceptor_net.conceptor_active(True)
# Save each generated pattern for display
generated_samples = torch.zeros(4, conceptor_test_length)
# NRMSE between original and aligned pattern
NRMSEs_aligned = torch.zeros(4)
# Train conceptors (Compute C from R)
conceptors.finalize()
# Figure (square size)
plt.figure(figsize=(12, 8))
# Set conceptors in evaluation mode and generate a sample
for i in range(4):
# Set it as current conceptor
conceptors.set(i)
# Randomly generated initial state (x0)
conceptor_net.cell.set_hidden(0.5 * torch.randn(reservoir_size, dtype=dtype))
# Generate sample
generated_sample = conceptor_net(torch.zeros(1, conceptor_test_length, 1, dtype=dtype), reset_state=False)
# Find best phase shift
generated_sample_aligned, _, NRMSE_aligned = echotorch.utils.pattern_interpolation(P_collector[i], generated_sample[0], interpolation_rate)
#
# Plot 1 : original pattern and recreated pattern
#
plt.subplot(4, 4, i * 4 + 1)
plt.plot(generated_sample_aligned, color='r', linewidth=5)
plt.plot(P_collector[i], color='b', linewidth=1.5)
# Title
if i == 0:
plt.title('p vs y')
# end if
# X labels
if i == 3:
plt.xticks([0, 10, 20])
else:
plt.xticks([])
# end if
# Y limits
plt.ylim([-1, 1])
plt.yticks([-1, 0, 1])
# We use StateVisualiser to plot neural activities of
# two reservoir units, the log10 of singular values
# of reservoir states, and their 10 leading SV.
state_visualiser = ecvs.StateVisualiser(observer=observer)
#
# Plot 2 : neurons
#
plt.subplot(4, 4, i * 4 + 2)
# Plot neurons
state_visualiser.plot_neurons(
point_name='X',
states="pattern{}".format(i),
idxs=None,
neuron_idxs=[0, 1, 2],
length=signal_plot_length,
colors=['b', 'orange', 'g'],
linewidth=1.5,
show_title=(i==0),
title="Two neurons",
xticks=[0, 10, 20] if i == 3 else None,
yticks=[-1, 0, 1],
ylim=[-1, 1]
)
#
# Plot 3 : Log10 of singular values (PC energy)
#
plt.subplot(4, 4, i * 4 + 3)
# Plot log10 of SV
state_visualiser.plot_singular_values(
point_name='X',
states="pattern{}".format(i),
idxs=None,
color='r',
linewidth=2,
show_title=(i == 0),
title="Log 10 PC Energy",
xticks=[0, 50, 100] if i == 3 else None,
ylim=[-20, 10],
log10=True
)
# Plot 4 : Learning PC energy
plt.subplot(4, 4, i * 4 + 4)
# Plot learning SV
state_visualiser.plot_singular_values(
point_name='X',
states="pattern{}".format(i),
idxs=None,
color='r',
length=10,
linewidth=2,
show_title=(i == 0),
title="Leading PC energy",
ylim=[0, 40.0]
)
# Save NRMSE
NRMSEs_aligned[i] = NRMSE_aligned
# end for
# Show
plt.show()
# Show NRMSE
print(("NRMSEs aligned : {}".format(torch.mean(NRMSEs_aligned))))
print((conceptors.similarity_matrix(based_on='R')))
# Plot R similarity matrix
ecvs.show_similarity_matrix(
sim_matrix=conceptors.similarity_matrix(based_on='R'),
title="R base similarities"
)
# Print the similarity matrix
print(("C-based similarity matrix, aperture = {}".format(alpha)))
print((conceptors.similarity_matrix()))
# Plot conceptors similarity matrix at aperture = 10.0
ecvs.show_similarity_matrix(
sim_matrix=conceptors.similarity_matrix(),
title="C based similarities, aperture = {}".format(alpha)
)
# Take conceptor for pattern 1 (sine) and pattern 3 (periodic)
Cs = conceptors[0]
Cp = conceptors[2]
# Divide aperture by 10 (to get aperture = 1.0)
Cs.PHI(1.0 / 10.0)
Cp.PHI(1.0 / 10.0)
# Figure with two plots
fig = plt.figure(figsize=(14, 6))
# Plots color
colors = ['b', 'orange', 'g', 'red', 'purple']
# Plot labels
plot_labels = ["a = 1.0", "a = 10.0", "a = 100.0", "a = 1000.0", "a = 10000.0"]
# Select first figure
plt.subplot(1, 2, 1)
# For each aperture (1.0, 10.0, 100.0, 1000.0, 10000.0)
for i in range(5):
# Plot conceptor's singular values.
plt.plot(Cs.SV.numpy(), color=colors[i], label=plot_labels[i])
# Title
if i == 0:
plt.title("Sine singular values")
# end if
# Legend
plt.legend(loc='best', ncol=1)
# Multiply aperture by 10.0
Cs.PHI(10.0)
# end for
# Select second figure
plt.subplot(1, 2, 2)
# For each aperture (1.0, 10.0, 100.0, 1000.0, 10000.0)
for i in range(5):
# Plot conceptor's singular values.
plt.plot(Cp.SV.numpy(), color=colors[i], label=plot_labels[i])
# Title
if i == 0:
plt.title("Periodic singular values")
# end if
# Legend
plt.legend(loc='best', ncol=1)
# Multiply aperture by 10.0
Cp.PHI(10.0)
# end for
# Show
plt.show()