-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathboolean_operations.py
267 lines (209 loc) · 6.27 KB
/
boolean_operations.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
# -*- coding: utf-8 -*-
#
# File : examples/conceptors/boolean_operations.py
# Description : Conceptor boolean operation
# Date : 16th 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 echotorch.nn.conceptors as ecnc
import echotorch.utils.matrix_generation as mg
import echotorch.utils.visualisation as ecvs
import argparse
import torch
import matplotlib.pyplot as plt
import numpy as np
import math
# region PARAMS
# Init random numb.
torch.random.manual_seed(1)
np.random.seed(1)
# Type params
dtype = torch.float64
# Reservoir params
reservoir_size = 2
# endregion PARAMS
# Argument parsing
parser = argparse.ArgumentParser(prog="Boolean operations", description="Boolean operation demo")
parser.add_argument("--x", type=str, default="", required=False)
parser.add_argument("--x-name", type=str, default="", required=False)
parser.add_argument("--y", type=str, default="", required=False)
parser.add_argument("--y-name", type=str, default="", required=False)
args = parser.parse_args()
# region MATRICES_INIT
# Load X state matrix from file or from random distrib ?
if args.x != "":
# Load state matrix X
x_generator = mg.matrix_factory.get_generator(
"matlab",
file_name=args.x,
entity_name=args.x_name
)
else:
# Generate internal weights
x_generator = mg.matrix_factory.get_generator(
"normal",
mean=0.0,
std=1.0
)
# end if
# Load Y state matrix from file or from random distrib ?
if args.y != "":
# Load state matrix X
y_generator = mg.matrix_factory.get_generator(
"matlab",
file_name=args.y,
entity_name=args.y_name
)
else:
# Generate internal weights
y_generator = mg.matrix_factory.get_generator(
"normal",
mean=0.0,
std=1.0
)
# end if
# Generate X and Y
X = x_generator.generate(size=(reservoir_size, reservoir_size), dtype=dtype)
Y = y_generator.generate(size=(reservoir_size, reservoir_size), dtype=dtype)
# Transpose on time dim / reservoir dim
X = X.t()
Y = Y.t()
# Add batch dimension
X = X.reshape(1, reservoir_size, reservoir_size)
Y = Y.reshape(1, reservoir_size, reservoir_size)
# endregion MATRICES_INIT
# region CONCEPTOR_A
# Create a conceptor
A = ecnc.Conceptor(input_dim=reservoir_size, aperture=1, dtype=dtype)
# Learn from state
A.filter_fit(X)
# Divide correlation matrix R by reservoir dimension
# and update C.
Ra = A.correlation_matrix()
A.set_R(Ra)
# Get conceptor matrix
Ua, Sa, Va = A.SVD
# Change singular values
Sa[0] = 0.95
Sa[1] = 0.2
# New C
Cnew = torch.mm(torch.mm(Ua, torch.diag(Sa)), Va)
# Recompute conceptor
A.set_C(Cnew, aperture=1)
# endregion CONCEPTOR_A
# region CONCEPTOR_B
# Create a conceptor
B = ecnc.Conceptor(input_dim=reservoir_size, aperture=1, dtype=dtype)
# Learn from state
B.filter_fit(Y)
# Divide correlation matrix R by reservoir dimension
# and update C.
Rb = B.correlation_matrix()
B.set_R(Rb)
# Get conceptor matrix
Ub, Sb, Vb = B.SVD
# Change singular values
Sb[0] = 0.8
Sb[1] = 0.3
# Recompute conceptor
B.set_C(torch.mm(torch.mm(Ub, torch.diag(Sb)), Vb), aperture=1)
# endregion CONCEPTOR_B
# region BOOLEAN_OPERATIONS
# AND, OR, NOT
AandB = ecnc.Conceptor.operator_AND(A, B)
AorB = ecnc.Conceptor.operator_OR(A, B)
notA = ecnc.Conceptor.operator_NOT(A)
print((A.conceptor_matrix()))
print((B.conceptor_matrix()))
print((AandB.conceptor_matrix()))
print((AorB.conceptor_matrix()))
print((notA.conceptor_matrix()))
# endregion BOOLEAN_OPERATIONS
# region PLOTS
# Figure
plt.figure(figsize=(12, 4))
# region PLOT_OR
# Select subplot
plt.subplot(1, 3, 1)
# Plot cross and circle
plt.plot([-1, 1], [0, 0], '--', color='black', linewidth=1)
plt.plot([0, 0], [-1, 1], '--', color='black', linewidth=1)
plt.plot(
np.cos(2.0 * math.pi * np.arange(200) / 200.0),
np.sin(2.0 * math.pi * np.arange(200) / 200),
'-',
color='black',
linewidth=1
)
# Plot ellipse
ecvs.plot_2D_ellipse(A.conceptor_matrix(), 'red', linewidth=3, resolution=200)
ecvs.plot_2D_ellipse(B.conceptor_matrix(), 'blue', linewidth=3, resolution=200)
ecvs.plot_2D_ellipse(AorB.conceptor_matrix(), 'magenta', linewidth=6, resolution=200)
# Title
plt.title("A OR B")
# Axis sticks
plt.xticks([-1, 0, 1])
plt.yticks([-1, 0, 1])
# endregion PLOT_OR
# region PLOT_AND
# Select subplot
plt.subplot(1, 3, 2)
# Plot cross and circle
plt.plot([-1, 1], [0, 0], '--', color='black', linewidth=1)
plt.plot([0, 0], [-1, 1], '--', color='black', linewidth=1)
plt.plot(
np.cos(2.0 * math.pi * np.arange(200) / 200),
np.sin(2.0 * math.pi * np.arange(200) / 200),
'-',
color='black',
linewidth=1
)
# Plot ellipse
ecvs.plot_2D_ellipse(A.conceptor_matrix(), 'red', linewidth=3, resolution=200)
ecvs.plot_2D_ellipse(B.conceptor_matrix(), 'blue', linewidth=3, resolution=200)
ecvs.plot_2D_ellipse(AandB.conceptor_matrix(), 'magenta', linewidth=6, resolution=200)
# Title
plt.title("A AND B")
# Axis sticks
plt.xticks([-1, 0, 1])
plt.yticks([-1, 0, 1])
# endregion PLOT_AND
# region PLOT_NOT
# Select subplot
plt.subplot(1, 3, 3)
# Plot cross and circle
plt.plot([-1, 1], [0, 0], '--', color='black', linewidth=1)
plt.plot([0, 0], [-1, 1], '--', color='black', linewidth=1)
plt.plot(
np.cos(2.0 * math.pi * np.arange(200) / 200),
np.sin(2.0 * math.pi * np.arange(200) / 200),
'-',
color='black',
linewidth=1
)
# Plot ellipses
ecvs.plot_2D_ellipse(A.conceptor_matrix(), 'red', linewidth=3, resolution=200)
ecvs.plot_2D_ellipse(notA.conceptor_matrix(), 'magenta', linewidth=6, resolution=200)
# Title
plt.title("NOT A")
# Axis sticks
plt.xticks([-1, 0, 1])
plt.yticks([-1, 0, 1])
# Show plot
plt.show()
# endregion PLOT_NOT
# endregion PLOTS