-
Notifications
You must be signed in to change notification settings - Fork 117
/
morphing_sines.py
452 lines (377 loc) · 15 KB
/
morphing_sines.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
442
443
444
445
446
447
448
449
450
451
452
# -*- coding: utf-8 -*-
#
# File : examples/conceptors/morphing_sines.py
# Description : Morphing sines with Conceptors
# Date : 6th 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.datasets as etds
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 scipy.interpolate import interp1d
from scipy import signal
# Random numb. init
torch.random.manual_seed(1)
np.random.seed(1)
# ESN params
reservoir_size = 100
spectral_radius = 1.5
input_scaling = 1.5
bias_scaling = 0.2
connectivity = 10.0 / reservoir_size
dtype=torch.float64
# Patterns
sine1_period_length = 8.8342522
sine2_period_length = 9.8342522
# Sequence lengths
washout_length = 500
learn_length = 1000
signal_plot_length = 20
conceptor_test_length = 200
singular_plot_length = 50
# Regularization
ridge_param_wstar = 0.01
ridge_param_wout = 0.01
# Plots
n_plots = 9
# Morphing
morphing_range = [-2, 3]
morphing_length = 200
morphing_washout = 500
morphing_pre_record_length = 50
morphing_delay_time = 500
morphing_delay_plot_points = 25
morphing_total_length = morphing_washout + morphing_pre_record_length * 2 + morphing_length
morphing_n_top_plots = 8
# Aperture
alpha = 10
# 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)
# 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)
# 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)
# end if
# Load x0 from matlab from or init randomly
if args.x0 != "":
print("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
# Four pattern (two sine, two periodic)
pattern1_training = etds.SinusoidalTimeseries(sample_len=washout_length + learn_length, n_samples=1, a=1,
period=sine1_period_length,
dtype=dtype
)
pattern2_training = etds.SinusoidalTimeseries(sample_len=washout_length + learn_length, n_samples=1, a=1,
period=sine2_period_length,
dtype=dtype
)
pattern3_training = etds.PeriodicSignalDataset(sample_len=washout_length + learn_length, n_samples=1,
period=[0.9000000000000002, -0.11507714997817164, 0.17591170369788622, -0.9, -0.021065045054201592],
dtype=dtype
)
pattern4_training = etds.PeriodicSignalDataset(sample_len=washout_length + learn_length, n_samples=1,
period=[0.9, -0.021439412841318672, 0.0379515995051003, -0.9, 0.06663989939293802],
dtype=dtype
)
# 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 self-predicting ESN
# which will be loaded with the
# four patterns.
spesn = ecnc.SPESN(
input_dim=1,
hidden_dim=reservoir_size,
output_dim=1,
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,
washout=washout_length,
dtype=dtype
)
# Create a set of conceptors
conceptors = ecnc.ConceptorSet(input_dim=reservoir_size, dtype=dtype)
# Create four conceptors, one for each pattern
conceptors.add(0, ecnc.Conceptor(input_dim=reservoir_size, aperture=alpha, dtype=dtype))
conceptors.add(1, ecnc.Conceptor(input_dim=reservoir_size, aperture=alpha, dtype=dtype))
conceptors.add(2, ecnc.Conceptor(input_dim=reservoir_size, aperture=alpha, dtype=dtype))
conceptors.add(3, ecnc.Conceptor(input_dim=reservoir_size, aperture=alpha, dtype=dtype))
# 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,
esn_cell=spesn.cell,
conceptor=conceptors,
dtype=dtype
)
# We create an outside observer to plot
# internal states and SVD afterwards
observer = ecvs.NodeObserver(spesn.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)
last_X = torch.empty(4, reservoir_size, 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]
last_X[i] = X[0, -1]
# 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
training_NRMSE = echotorch.utils.nrmse(predY, Y_collector)
print(("Training NRMSE : {}".format(training_NRMSE)))
# Train conceptors (Compute C from R)
conceptors.finalize()
# Morphing values
morphing_scales = np.linspace(morphing_range[0], morphing_range[1], morphing_length + 1)
# Morphing outputs
morphing_output = torch.zeros(morphing_total_length)
# Initialize morphing vectors
morphing_vectors = torch.zeros(1, morphing_total_length, 4)
# Morphing vectors for the washout and pre-morphing periods
for t in range(morphing_washout + morphing_pre_record_length):
morphing_vectors[0, t, 0] = 1.0 - morphing_scales[0]
morphing_vectors[0, t, 1] = morphing_scales[0]
# end for
# Morphing vectors for the morphing period
for t in range(morphing_length):
add_time = morphing_washout + morphing_pre_record_length
morphing_vectors[0, t + add_time, 0] = 1.0 - morphing_scales[t]
morphing_vectors[0, t + add_time, 1] = morphing_scales[t]
# end for
# Morphing vectors for the post-morphing period
for t in range(morphing_pre_record_length):
add_time = morphing_washout + morphing_pre_record_length + morphing_length
morphing_vectors[0, t + add_time, 0] = 1.0 - morphing_scales[-1]
morphing_vectors[0, t + add_time, 1] = morphing_scales[-1]
# end for
# Length to plot
morphing_plot_length = morphing_length + morphing_pre_record_length * 2
# Randomly generated initial state (x0)
x0 = x0_generator.generate(size=reservoir_size, dtype=dtype)
# Washout
conceptor_net.washout = morphing_washout
# Generate sample
morphed_outputs = conceptor_net(
torch.zeros(1, morphing_total_length, 1, dtype=dtype),
reset_state=False,
morphing_vectors=morphing_vectors
)
plt.plot(morphed_outputs[0, :, 0].numpy(), color='r')
plt.show()
# We compute a quadratic interpolation of the output
# morphed signal.
interpolation_increment = 0.1
interpolation_points = np.arange(1, morphing_plot_length, interpolation_increment) - 1.0
interpolation_length = interpolation_points.shape[0]
morphed_outputs_interpolated = interp1d(
np.arange(morphing_plot_length),
morphed_outputs[0, :, 0].numpy(),
kind='quadratic'
)(interpolation_points)
plt.plot(morphed_outputs_interpolated, color='r')
plt.show()
# We compute the length of each period (number of timesteps)
# by counting the gap between each point where the signal
# cross the x-axis from below.
x_crossing_discounts = np.zeros(interpolation_length)
old_val = 1
counter = 0
for i in range(interpolation_length-1):
# Cross x-axis from below
if morphed_outputs_interpolated[i] < 0 and morphed_outputs_interpolated[i+1] > 0:
counter += 1
x_crossing_discounts[i] = counter
old_val = counter
counter = 0
else:
x_crossing_discounts[i] = old_val
counter += 1
# end if
# end for
plt.plot(x_crossing_discounts, color='pink')
plt.show()
# We get periods length back to the sampling rate
# More remove first 20, and the last was which are
# not accurate enough.
interpolation_steps = int(1.0 / interpolation_increment)
x_crossing_discounts = x_crossing_discounts[list(range(interpolation_steps - 1, interpolation_length, interpolation_steps))]
x_crossing_discounts *= interpolation_increment
x_crossing_discounts[:20] = np.ones(20) * x_crossing_discounts[19]
x_crossing_discounts[-1] = x_crossing_discounts[-2]
plt.plot(x_crossing_discounts, color='pink')
plt.show()
# We compute expected period lengths at the start of the morphing
# and at the end
sines_period_diffs = sine2_period_length - sine1_period_length
period_length_start = sine1_period_length + morphing_range[0] * sines_period_diffs
period_length_end = sine1_period_length + morphing_range[1] * sines_period_diffs
# We compute a reference to display the expected period length on future plots
period_lengths_plotting = np.zeros(morphing_plot_length)
period_lengths_plotting[:morphing_pre_record_length] = period_length_start * np.ones(morphing_pre_record_length)
period_lengths_plotting[morphing_pre_record_length:morphing_pre_record_length+morphing_length] = \
np.linspace(
period_length_start,
period_length_end, morphing_length
)
period_lengths_plotting[morphing_pre_record_length+morphing_length:] = \
period_length_end * np.ones(morphing_pre_record_length)
plt.plot(period_lengths_plotting, color='b')
plt.show()
# Two points to show the period lengths of the two patterns used in the training phase
morphing_point_sine1 = morphing_pre_record_length + morphing_length * \
-morphing_range[0] / float(morphing_range[1] - morphing_range[0])
morphing_point_sine2 = morphing_pre_record_length + morphing_length * \
-(morphing_range[0] - 1) / float(morphing_range[1] - morphing_range[0])
# Morphing values for each plots
morphing_delay_ms = np.linspace(morphing_range[0], morphing_range[1], morphing_n_top_plots)
morphing_delay_data = torch.zeros(morphing_n_top_plots, morphing_delay_time)
# Random initial state
x0 = torch.randn(reservoir_size)
# Washout
conceptor_net.washout = morphing_washout
# For each morphing value
for i in range(morphing_n_top_plots):
# Morphing vector
morphing_vector = torch.DoubleTensor([1.0 - morphing_delay_ms[i], morphing_delay_ms[i], 0.0, 0.0])
morphing_vector = morphing_vector.reshape(1, 4)
# Generate delay output
morphed_outputs = conceptor_net(
torch.zeros(1, morphing_delay_time + morphing_washout, 1, dtype=dtype),
reset_state=False,
morphing_vectors=morphing_vector
)
morphing_delay_data[i] = morphed_outputs[0, :, 0]
# end for
# Top plots finger points
morphing_top_plots_points = morphing_pre_record_length + np.arange(0, morphing_n_top_plots) * \
morphing_length / (morphing_n_top_plots - 1)
# New figure
plt.figure(figsize=(16, 2))
# For each top plots
plot_index = 0
for i in range(morphing_n_top_plots):
# Subplot
plt.subplot(1, morphing_n_top_plots, plot_index + 1)
plot_index += 1
# Plot data
plt.plot(morphing_delay_data[i, :-2], morphing_delay_data[i, 1:-1], 'o-', color='red', markersize=1, linewidth=1)
# Plot points
plot_data = morphing_delay_data[i, np.arange(0, morphing_delay_plot_points + 1)]
plt.plot(plot_data[:-2], plot_data[1:-1], 'o', color='blue', markersize=8)
# Limits
plt.xlim([-1.4, 1.4])
plt.ylim([-1.4, 1.4])
plt.xticks([])
plt.yticks([])
# end for
# Show
plt.show()
# Second figure
plt.figure(figsize=(14, 6))
# Subplot 1
plt.subplot(2, 1, 1)
plt.plot(morphed_outputs[0, :, 0].numpy(), '-', color='red', linewidth=2)
plt.plot([morphing_point_sine1, morphing_point_sine2], [-1, -1], 'o', color='green', markersize=15)
plt.plot(morphing_top_plots_points, 1.1 * np.ones(morphing_n_top_plots), 'x', color='blue', markersize=10)
plt.grid(color='grey', linestyle='--', linewidth=1, axis='x')
plt.xlim([0, morphing_plot_length])
plt.ylim([-1.3, 1.3])
# Subplot 2
plt.subplot(2, 1, 2)
plt.plot(x_crossing_discounts, linewidth=6, color='magenta')
plt.plot(period_lengths_plotting, color='blue')
plt.plot([morphing_point_sine1, morphing_point_sine2], np.array([7, 7]), 'o', color='green', markersize=15)
plt.xlim([0, morphing_plot_length])
plt.ylim([6.5, 12.5])
plt.xticks([])
# Show
plt.show()