hdlib 2.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hdlib/__init__.py +16 -0
- hdlib/arithmetic/__init__.py +265 -0
- hdlib/arithmetic/quantum.py +1257 -0
- hdlib/model/__init__.py +12 -0
- hdlib/model/classification.py +1719 -0
- hdlib/model/clustering.py +172 -0
- hdlib/model/graph.py +764 -0
- hdlib/model/regression.py +306 -0
- hdlib/space.py +864 -0
- hdlib/vector.py +608 -0
- hdlib-2.1.0.data/scripts/chopin2.py +473 -0
- hdlib-2.1.0.dist-info/METADATA +137 -0
- hdlib-2.1.0.dist-info/RECORD +16 -0
- hdlib-2.1.0.dist-info/WHEEL +5 -0
- hdlib-2.1.0.dist-info/licenses/LICENSE +21 -0
- hdlib-2.1.0.dist-info/top_level.txt +1 -0
hdlib/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""__hdlib__ is a Python 3 library for building Vector-Symbolic Architectures (VSA, a.k.a. Hyperdimensional Computing).
|
|
2
|
+
|
|
3
|
+
VSA is an emergent computing paradigm that works by combining vectors in a high-dimensional space for representing and
|
|
4
|
+
processing information. This approach recently shown promise in various domains for dealing with different kind of
|
|
5
|
+
computational problems, including artificial intelligence, cognitive science, robotics, natural language processing,
|
|
6
|
+
bioinformatics, medical informatics, cheminformatics, and internet of things among other scientific disciplines.
|
|
7
|
+
|
|
8
|
+
The library is distributed under the MIT license as a Python package through PyPI and Conda on the _conda-forge_ channel.
|
|
9
|
+
|
|
10
|
+
Please refer to the official [Wiki](https://github.com/cumbof/hdlib/wiki) for any information about the implemented
|
|
11
|
+
modules and how to use the library."""
|
|
12
|
+
|
|
13
|
+
__author__ = ("Fabio Cumbo (fabio.cumbo@gmail.com)")
|
|
14
|
+
|
|
15
|
+
__version__ = "2.1.0"
|
|
16
|
+
__date__ = "Mar 10, 2026"
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""Implementation of the MAP arithmetic operators.
|
|
2
|
+
|
|
3
|
+
This library also provides the same set of arithmetic functions also accessible as _Vector_'s class methods.
|
|
4
|
+
However, while the result of calling these functions from a _Vector_ object woule be applied in place,
|
|
5
|
+
invoking the same functions from the _hdlib.arithmetic_ module would initialize new _Vector_ objects."""
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
from hdlib.space import Vector
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def bind(vector1: Vector, vector2: Vector) -> Vector:
|
|
13
|
+
"""Bind vectors.
|
|
14
|
+
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
vector1 : Vector
|
|
18
|
+
The first vector object.
|
|
19
|
+
vector2: Vector
|
|
20
|
+
The second vector object.
|
|
21
|
+
|
|
22
|
+
Returns
|
|
23
|
+
-------
|
|
24
|
+
Vector
|
|
25
|
+
A new vector object as the result of the bind operator on the two input vectors.
|
|
26
|
+
|
|
27
|
+
Raises
|
|
28
|
+
------
|
|
29
|
+
Exception
|
|
30
|
+
If vectors have different sizes or different vector types.
|
|
31
|
+
|
|
32
|
+
Notes
|
|
33
|
+
-----
|
|
34
|
+
The bind operator has the following properties:
|
|
35
|
+
(i) invertible (unbind);
|
|
36
|
+
(ii) it distributes over bundling;
|
|
37
|
+
(iii) it preserves the distance;
|
|
38
|
+
(iv) the resulting vector is dissimilar to the input vectors.
|
|
39
|
+
|
|
40
|
+
Examples
|
|
41
|
+
--------
|
|
42
|
+
>>> from hdlib.space import Vector
|
|
43
|
+
>>> from hdlib.arithmetic import bind
|
|
44
|
+
>>> vector1 = Vector(size=10000, vtype="binary")
|
|
45
|
+
>>> vector2 = Vector(size=10000, vtype="binary")
|
|
46
|
+
>>> vector3 = bind(vector1, vector2)
|
|
47
|
+
>>> type(vector3)
|
|
48
|
+
<class 'hdlib.space.Vector'>
|
|
49
|
+
|
|
50
|
+
The bind function returns a new Vector object whose content is computed as the element-wise
|
|
51
|
+
multiplication of the two input vectors.
|
|
52
|
+
|
|
53
|
+
>>> vector1 = Vector(size=10000, vtype="binary")
|
|
54
|
+
>>> vector2 = Vector(size=10000, vtype="bipolar")
|
|
55
|
+
>>> vector3 = bind(vector1, vector2)
|
|
56
|
+
Exception: Vector types are not compatible
|
|
57
|
+
|
|
58
|
+
The vector type of the two input vector is different and thus the binding cannot be performed.
|
|
59
|
+
|
|
60
|
+
>>> vector1 = Vector(size=10000, vtype="bipolar")
|
|
61
|
+
>>> vector2 = Vector(size=15000, vtype="bipolar")
|
|
62
|
+
>>> vector3 = bind(vector1, vector2)
|
|
63
|
+
Exception: Vectors must have the same size
|
|
64
|
+
|
|
65
|
+
It also throws an exception in case the size of the two input vectors is not the same.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
if vector1.size != vector2.size:
|
|
69
|
+
raise Exception("Vectors must have the same size")
|
|
70
|
+
|
|
71
|
+
if vector1.vtype != vector2.vtype:
|
|
72
|
+
raise Exception("Vector types are not compatible")
|
|
73
|
+
|
|
74
|
+
if vector1.vtype == "bipolar":
|
|
75
|
+
# Element-wise multiplication
|
|
76
|
+
vector = vector1.vector * vector2.vector
|
|
77
|
+
|
|
78
|
+
elif vector1.vtype == "binary":
|
|
79
|
+
# Element-wise XOR
|
|
80
|
+
vector = vector1.vector.astype(int) ^ vector2.vector.astype(int)
|
|
81
|
+
|
|
82
|
+
tags = set(vector1.tags).union(set(vector2.tags))
|
|
83
|
+
|
|
84
|
+
return Vector(size=vector1.size, vector=vector, tags=tags, vtype=vector1.vtype, seed=vector1.seed)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def bundle(vector1: Vector, vector2: Vector) -> Vector:
|
|
88
|
+
"""Bundle vectors.
|
|
89
|
+
|
|
90
|
+
Parameters
|
|
91
|
+
----------
|
|
92
|
+
vector1 : Vector
|
|
93
|
+
The first vector object.
|
|
94
|
+
vector2: Vector
|
|
95
|
+
The second vector object.
|
|
96
|
+
|
|
97
|
+
Returns
|
|
98
|
+
-------
|
|
99
|
+
Vector
|
|
100
|
+
A new vector object as the result of the bundle operator on the two input vectors.
|
|
101
|
+
|
|
102
|
+
Raises
|
|
103
|
+
------
|
|
104
|
+
Exception
|
|
105
|
+
If vectors have different sizes or different vector types.
|
|
106
|
+
|
|
107
|
+
Notes
|
|
108
|
+
-----
|
|
109
|
+
The bundle operator has the following properties:
|
|
110
|
+
(i) the resulting vector is similar to the input vectors;
|
|
111
|
+
(ii) the more vectors are involved in bundling, the harder it is to determine the component vectors;
|
|
112
|
+
(iii) if several copies of any vector are included in bundling, the resulting vector is closer to the
|
|
113
|
+
dominant vector than to the other components.
|
|
114
|
+
|
|
115
|
+
Examples
|
|
116
|
+
--------
|
|
117
|
+
>>> from hdlib.space import Vector
|
|
118
|
+
>>> from hdlib.arithmetic import bundle
|
|
119
|
+
>>> vector1 = Vector(size=10000, vtype="binary")
|
|
120
|
+
>>> vector2 = Vector(size=10000, vtype="binary")
|
|
121
|
+
>>> vector3 = budle(vector1, vector2)
|
|
122
|
+
>>> type(vector3)
|
|
123
|
+
<class 'hdlib.space.Vector'>
|
|
124
|
+
|
|
125
|
+
The bundle function returns a new Vector object whose content is computed as the element-wise sum
|
|
126
|
+
of the two input vectors.
|
|
127
|
+
|
|
128
|
+
>>> vector1 = Vector(size=10000, vtype="binary")
|
|
129
|
+
>>> vector2 = Vector(size=10000, vtype="bipolar")
|
|
130
|
+
>>> vector3 = budle(vector1, vector2)
|
|
131
|
+
Exception: Vector types are not compatible
|
|
132
|
+
|
|
133
|
+
The vector type of the two input vector is different and thus the bundling cannot be performed.
|
|
134
|
+
|
|
135
|
+
>>> vector1 = Vector(size=10000, vtype="bipolar")
|
|
136
|
+
>>> vector2 = Vector(size=15000, vtype="bipolar")
|
|
137
|
+
>>> vector3 = budle(vector1, vector2)
|
|
138
|
+
Exception: Vectors must have the same size
|
|
139
|
+
|
|
140
|
+
It also throws an exception in case the size of the two input vectors is not the same.
|
|
141
|
+
"""
|
|
142
|
+
|
|
143
|
+
if vector1.size != vector2.size:
|
|
144
|
+
raise Exception("Vectors must have the same size")
|
|
145
|
+
|
|
146
|
+
if vector1.vtype != vector2.vtype:
|
|
147
|
+
raise Exception("Vector types are not compatible")
|
|
148
|
+
|
|
149
|
+
if vector1.vtype == "bipolar":
|
|
150
|
+
# Element-wise addition
|
|
151
|
+
vector = vector1.vector + vector2.vector
|
|
152
|
+
|
|
153
|
+
elif vector1.vtype == "binary":
|
|
154
|
+
# Element-wise majority vote
|
|
155
|
+
vector = ((vector1.vector + vector2.vector) > 1).astype(int)
|
|
156
|
+
|
|
157
|
+
tags = set(vector1.tags).union(set(vector2.tags))
|
|
158
|
+
|
|
159
|
+
return Vector(size=vector1.size, vector=vector, tags=tags, vtype=vector1.vtype, seed=vector1.seed)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def subtraction(vector1: Vector, vector2: Vector) -> Vector:
|
|
163
|
+
"""Subtract two vectors.
|
|
164
|
+
|
|
165
|
+
Parameters
|
|
166
|
+
----------
|
|
167
|
+
vector1 : Vector
|
|
168
|
+
The first vector object.
|
|
169
|
+
vector2: Vector
|
|
170
|
+
The second vector object.
|
|
171
|
+
|
|
172
|
+
Returns
|
|
173
|
+
-------
|
|
174
|
+
Vector
|
|
175
|
+
A new vector object as the result of the subtraction operator on the two input vectors.
|
|
176
|
+
|
|
177
|
+
Raises
|
|
178
|
+
------
|
|
179
|
+
Exception
|
|
180
|
+
- If vectors have different sizes or different vector types;
|
|
181
|
+
- If vectors type is binary.
|
|
182
|
+
|
|
183
|
+
Examples
|
|
184
|
+
--------
|
|
185
|
+
>>> from hdlib.space import Vector
|
|
186
|
+
>>> from hdlib.arithmetic import subtraction
|
|
187
|
+
>>> vector1 = Vector(size=10000, vtype="binary")
|
|
188
|
+
>>> vector2 = Vector(size=10000, vtype="binary")
|
|
189
|
+
>>> vector3 = subtraction(vector1, vector2)
|
|
190
|
+
>>> type(vector3)
|
|
191
|
+
<class 'hdlib.space.Vector'>
|
|
192
|
+
|
|
193
|
+
The subtraction function returns a new Vector object whose content is computed as the element-wise
|
|
194
|
+
subtraction of the two input vectors.
|
|
195
|
+
|
|
196
|
+
>>> vector1 = Vector(size=10000, vtype="binary")
|
|
197
|
+
>>> vector2 = Vector(size=10000, vtype="bipolar")
|
|
198
|
+
>>> vector3 = subtraction(vector1, vector2)
|
|
199
|
+
Exception: Vector types are not compatible
|
|
200
|
+
|
|
201
|
+
The vector type of the two input vector is different and thus the subtraction cannot be performed.
|
|
202
|
+
|
|
203
|
+
>>> vector1 = Vector(size=10000, vtype="bipolar")
|
|
204
|
+
>>> vector2 = Vector(size=15000, vtype="bipolar")
|
|
205
|
+
>>> vector3 = subtraction(vector1, vector2)
|
|
206
|
+
Exception: Vectors must have the same size
|
|
207
|
+
|
|
208
|
+
It also throws an exception in case the size of the two input vectors is not the same.
|
|
209
|
+
"""
|
|
210
|
+
|
|
211
|
+
if vector1.size != vector2.size:
|
|
212
|
+
raise Exception("Vectors must have the same size")
|
|
213
|
+
|
|
214
|
+
if vector1.vtype != vector2.vtype:
|
|
215
|
+
raise Exception("Vector types are not compatible")
|
|
216
|
+
|
|
217
|
+
if vector1.vtype == "bipolar":
|
|
218
|
+
# Element-wise subtraction
|
|
219
|
+
vector = vector1.vector - vector2.vector
|
|
220
|
+
|
|
221
|
+
elif vector1.vtype == "binary":
|
|
222
|
+
raise Exception("Subtraction is not available for binary vectors")
|
|
223
|
+
|
|
224
|
+
return Vector(size=vector1.size, vector=vector, tags=vector1.tags, vtype=vector1.vtype, seed=vector1.seed)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def permute(vector: Vector, rotate_by: int=1) -> Vector:
|
|
228
|
+
"""Permute a vector
|
|
229
|
+
|
|
230
|
+
Parameters
|
|
231
|
+
----------
|
|
232
|
+
vector : Vector
|
|
233
|
+
The input vector object.
|
|
234
|
+
rotate_by: int
|
|
235
|
+
Rotate the input vector by `rotate_by` positions (the default is 1).
|
|
236
|
+
|
|
237
|
+
Returns
|
|
238
|
+
-------
|
|
239
|
+
Vector
|
|
240
|
+
A new vector object as the result of the permute operator on the input vector.
|
|
241
|
+
|
|
242
|
+
Notes
|
|
243
|
+
-----
|
|
244
|
+
The permute operator has the following properties:
|
|
245
|
+
(i) invertible;
|
|
246
|
+
(ii) it distributes over bundling and any elementwise operation;
|
|
247
|
+
(iii) it preserves the distance;
|
|
248
|
+
(iv) the resulting vector is dissimilar to the input vectors.
|
|
249
|
+
|
|
250
|
+
Examples
|
|
251
|
+
--------
|
|
252
|
+
>>> from hdlib.space import Vector
|
|
253
|
+
>>> from hdlib.arithmetic import permute
|
|
254
|
+
>>> vector1 = Vector(size=10000, vtype="binary")
|
|
255
|
+
>>> vector2 = permute(vector1, rotate_by=2)
|
|
256
|
+
>>> type(vector2)
|
|
257
|
+
<class 'hdlib.space.Vector'>
|
|
258
|
+
|
|
259
|
+
The permute function returns a new Vector object whose content is the same of the input
|
|
260
|
+
vector rotated by 2 positions.
|
|
261
|
+
"""
|
|
262
|
+
|
|
263
|
+
rolled = np.roll(vector.vector, rotate_by, axis=0)
|
|
264
|
+
|
|
265
|
+
return Vector(size=vector.size, vector=rolled, tags=vector.tags, vtype=vector.vtype, seed=vector.seed)
|