-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathmorphing_square_debug.py
437 lines (360 loc) · 13.9 KB
/
morphing_square_debug.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
# -*- 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 <[email protected]>
# 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 echotorch.nn.Node import Node
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(2)
np.random.seed(2)
# Debug ?
debug = True
if debug:
torch.set_printoptions(precision=16)
debug_mode = Node.DEBUG_OUTPUT
precision = 0.0001
else:
debug_mode = Node.NO_DEBUG
# end if
# 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
# Sequence lengths
washout_length = 500
learn_length = 1000
signal_plot_length = 20
conceptor_test_length = 200
singular_plot_length = 50
free_run_length = 100000
interpolation_rate = 20
# Regularization
ridge_param_wstar = 0.0001
ridge_param_wout = 0.01
# Plots
n_plots = 9
# Morphing
min_mu = -0.5
max_mu = 1.5
morphing_range = [0.0, 1.0]
morphing_length = 30
morphing_washout = 190
morphing_plot_length = 15
morphing_plot_points = 15
# 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)
# 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 != "":
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, dtype=dtype)
# Second sine periodic pattern
pattern2_training = pattern_library(pattern_id=1, washout_length=washout_length, learn_length=learn_length, dtype=dtype)
# First 5-periodic pattern
pattern3_training = pattern_library(pattern_id=4, washout_length=washout_length, learn_length=learn_length, dtype=dtype)
# Second 5-periodic pattern
pattern4_training = pattern_library(pattern_id=5, washout_length=washout_length, learn_length=learn_length, 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 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, debug=debug_mode, dtype=dtype))
conceptors.add(1, ecnc.Conceptor(input_dim=reservoir_size, aperture=alpha, debug=debug_mode, dtype=dtype))
conceptors.add(2, ecnc.Conceptor(input_dim=reservoir_size, aperture=alpha, debug=debug_mode, dtype=dtype))
conceptors.add(3, ecnc.Conceptor(input_dim=reservoir_size, aperture=alpha, debug=debug_mode, 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,
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,
washout=washout_length,
fill_left=True,
debug=debug_mode,
dtype=dtype
)
# We create an outside observer to plot
# internal states and SVD afterwards
observer = ecvs.NodeObserver(conceptor_net.cell, initial_state='init')
# If in debug mode
if debug_mode > Node.NO_DEBUG:
# Load sample matrices
for i in range(4):
# Input patterns
conceptor_net.cell.debug_point(
"u{}".format(i),
torch.reshape(torch.from_numpy(np.load("data/debug/morphing_square/u{}.npy".format(i))), shape=(-1, 1)),
precision
)
# States
conceptor_net.cell.debug_point(
"X{}".format(i),
torch.from_numpy(np.load("data/debug/morphing_square/X{}.npy".format(i))),
precision
)
# Targets
conceptor_net.cell.debug_point(
"Y{}".format(i),
torch.from_numpy(np.load("data/debug/morphing_square/Y{}.npy".format(i))),
precision
)
# Xold
conceptor_net.cell.debug_point(
"Xold{}".format(i),
torch.from_numpy(np.load("data/debug/morphing_square/Xold{}.npy".format(i))),
precision
)
# Conceptor
conceptors[i].debug_point(
"C",
torch.from_numpy(np.load("data/debug/morphing_square/C{}.npy".format(i))),
precision
)
# end for
# Load debug W, xTx, xTy
conceptor_net.cell.debug_point("Wstar", torch.from_numpy(np.load("data/debug/morphing_square/Wstar.npy", allow_pickle=True)), precision)
conceptor_net.cell.debug_point("Win", torch.from_numpy(np.load("data/debug/morphing_square/Win.npy")), precision)
conceptor_net.cell.debug_point("Wbias", torch.from_numpy(np.load("data/debug/morphing_square/Wbias.npy")), precision)
conceptor_net.cell.debug_point("xTx", torch.from_numpy(np.load("data/debug/morphing_square/xTx.npy")), precision)
conceptor_net.cell.debug_point("xTy", torch.from_numpy(np.load("data/debug/morphing_square/xTy.npy")), precision)
conceptor_net.cell.debug_point("w_ridge_param", 0.0001, precision)
conceptor_net.cell.debug_point("ridge_xTx", torch.from_numpy(np.load("data/debug/morphing_square/ridge_xTx.npy")), precision)
conceptor_net.cell.debug_point("inv_xTx", torch.from_numpy(np.load("data/debug/morphing_square/inv_xTx.npy")), precision)
conceptor_net.cell.debug_point("w", torch.from_numpy(np.load("data/debug/morphing_square/W.npy")), precision)
# end if
# 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)))
# 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()
# Run loaded reservoir to observe a messy output. Do this with starting
# from four states originally obtained in the four driving conditions
# initialize network state.
# Figure (square size)
plt.figure(figsize=(12, 8))
# For each pattern
for p in range(4):
# Set hidden state
conceptor_net.cell.set_hidden(last_X[p])
# Run trained ESN with empty inputs (no conceptor learning)
generated = conceptor_net(torch.zeros(1, conceptor_test_length, 1, dtype=dtype), reset_state=False)
# Select subplot and plot the messy output
plt.subplot(4, 1, p + 1)
plt.plot(generated[0].numpy(), color='b')
# end for
# Show figure
plt.show()
# Conceptors ON
conceptor_net.conceptor_active(True)
# Train conceptors (Compute C from R)
conceptors.finalize()
# Corresponding mixture vectors
mixture_vectors = torch.empty((n_plots, n_plots, 1, 4))
# Rows and columns
row_mus = torch.linspace(min_mu, max_mu, n_plots)
col_mus = torch.linspace(min_mu, max_mu, n_plots)
# Compute mixture vectors
for i in range(n_plots):
for j in range(n_plots):
# The first two entries in mixture_vectors relate to the first two patterns,
# the second two entries to the last two patterns.
mixture_vectors[i, j, 0, :2] = row_mus[i] * torch.Tensor([1.0 - col_mus[j], col_mus[j]])
mixture_vectors[i, j, 0, 2:] = (1.0 - row_mus[i]) * torch.Tensor([1.0 - col_mus[j], col_mus[j]])
# end for
# end for
# No washout this time
conceptor_net.washout = morphing_washout
# Output for each mixture
plots = torch.empty((n_plots, n_plots, morphing_length))
# Randomly generated initial state (x0)
x0 = x0_generator.generate(size=reservoir_size, dtype=dtype)
# For each morphing
for i in range(n_plots):
for j in range(n_plots):
# Mixture vector
mixture_vector = mixture_vectors[i, j]
# Randomly generated initial state (x0)
conceptor_net.cell.set_hidden(x0)
# Generate sample
generated_sample = conceptor_net(
torch.zeros(1, morphing_length + morphing_washout, 1, dtype=dtype),
reset_state=False,
morphing_vectors=mixture_vector
)
# Save outputs
plots[i, j] = generated_sample[0, :, 0]
# end for
# end for
# Figure (square size)
plt.figure(figsize=(18, 18))
# Panel index
panel_index = 1
# For each morphing
for i in range(n_plots):
for j in range(n_plots):
# Subplot
plt.subplot(n_plots, n_plots, panel_index)
# Morphing data
thisdata = plots[i, j, :morphing_plot_points]
# Title
plt.title(
"{},{},{},{}".format(
mixture_vectors[i, j, 0, 0],
mixture_vectors[i, j, 0, 1],
mixture_vectors[i, j, 0, 2],
mixture_vectors[i, j, 0, 3]
),
fontsize=8
)
# Y label
if j == 0:
plt.ylabel(row_mus[i].item())
# end if
# X label
if i == n_plots - 1:
plt.xlabel(col_mus[j].item())
# end if
# Limits and ticks
plt.xticks([])
plt.yticks([])
plt.ylim([-1, 1])
# Original timeseries
n = (n_plots - 5.0) / 4.0
if (i == n + 1 and j == n + 1) or (i == n + 1 and j == 3 * n + 3) or (i == 3 * n + 3 and j == n + 1) or (i == 3 * n + 3 and j == 3 * n + 3):
plt.plot(thisdata, 'r', linewidth=3)
else:
plt.plot(thisdata, 'g', linewidth=3)
# end
# Next panel
panel_index += 1
# end
# end
# Show
plt.show()