-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyperdim.py
More file actions
284 lines (227 loc) · 8.29 KB
/
Copy pathhyperdim.py
File metadata and controls
284 lines (227 loc) · 8.29 KB
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
import numpy as np
import pandas as pd
from typing import Any, Dict, List, DefaultDict, Optional
from collections import defaultdict
import scipy.special
from hashlib import md5
import random
# TODO:
# ClassicalHopfield
def spinor_to_binary(v: np.array) -> np.array:
return (v + 1) / 2
def binary_to_spinor(v: np.array) -> np.array:
return (v - 1 / 2) * 2
def array_to_int(v: np.array) -> int:
r = 0
for idx, x in enumerate(v):
if x:
r |= 1 << idx
return r
def int_to_array(i: int, width: int) -> np.array:
v = []
for w in range(width):
if i & 2 ** w:
v.append(1)
else:
v.append(0)
return np.array(v[::-1], dtype=bool)
def hamming_dist(x: int, y: int) -> int:
return bin(x ^ y).count("1")
class RSP:
_plane: np.array
def __init__(self, n, seed: int = None) -> None:
prng = np.random.RandomState(seed)
self._plane = prng.choice([-1, 1], size=n)
def hash(self, v: np.array) -> int:
return np.sign(np.dot(v, self._plane))
class RSPHash:
_hashers: List[RSP]
def __init__(
self, dim: int = None, width: int = None, rsps: List[RSP] = None
) -> None:
if rsps:
self._hashers = np.array(rsps)
elif dim and width:
self._hashers = np.array([RSP(dim) for _ in range(width)])
else:
raise ValueError()
def hash(self, v: np.array) -> int:
res = np.vectorize(lambda x: x.hash(v))(self._hashers)
return arr_to_binary(spinor_to_binary(res))
class ClassicHopfield:
MAX_UPDATES = 2
W: np.array
def __init__(self, dim: int):
self.W = np.zeros((dim, dim))
def learn(self, v: np.array) -> None:
m = np.einsum("i,j->ij", v, v)
m -= np.diag(m.diagonal())
self.W += m
def recall(self, v: np.array) -> Optional[np.array]:
v_old = v
for n in range(self.MAX_UPDATES):
v_new = self.update(v_old)
diff = np.diff(np.vstack([v_old, v_new]), axis=0)
if np.all(diff == 0):
return v_new
v_old = v_new
print(f"Failed to converge ... {np.sum(diff != 0)} components")
return None
def update(self, v: np.array) -> np.array:
return np.sign(np.einsum("ij,i->j", self.W, v))
class ModernHopfield:
MAX_UPDATES = 2
patterns: np.array
def __init__(self, dim: int):
self.patterns = None
def learn(self, v: np.array) -> None:
if self.patterns is None:
self.patterns = v[None, :]
else:
self.patterns = np.concatenate([self.patterns, v[None, :]])
def update_kernel(self, x: np.array) -> np.array:
return np.exp(x)
def update(self, v) -> np.array:
d = len(v)
m = self.patterns * v
arg = np.einsum("ij,jk->ik", m, (np.ones((d, d)) - np.diag(np.ones(d))))
pos = arg + self.patterns
neg = arg - self.patterns
return np.sign(
np.sum(self.update_kernel(pos) - self.update_kernel(neg), axis=0)
)
def recall(self, v: np.array) -> Optional[np.array]:
v_old = v
for n in range(self.MAX_UPDATES):
v_new = self.update(v_old)
diff = np.diff(np.vstack([v_old, v_new]), axis=0)
if np.all(diff == 0):
return v_new
v_old = v_new
print(f"Failed to converge ... {np.sum(diff != 0)} components")
return None
class RSPMem:
"""
Use RSP LSH to dynamically build hopfield networks with only
nearest neighbors.
Can be used as an efficient associative memory.
"""
hasher: RSPHash
patterns: DefaultDict[int, List[np.array]]
dim: int
def __init__(
self,
dim: int,
width: int,
):
self.dim = dim
self.hasher = RSPHash(dim, width)
self.patterns = defaultdict(list)
def learn(self, v: np.array):
self.patterns[self.hasher.hash(v)].append(v)
def recall(self, v: np.array):
# find the buckets
h = self.hasher.hash(v)
buckets = {x for x in self.patterns if hamming_dist(h, x) <= 1}
patterns = []
for b in buckets:
patterns.extend(self.patterns[b])
print(f"NN patterns: {len(patterns)}")
assert patterns
mem = self._build_memory(patterns)
return mem.recall(v)
def _build_memory(self, patterns: List[np.array]) -> ModernHopfield:
hf = ModernHopfield(self.dim)
for pattern in patterns:
hf.learn(pattern)
return hf
def concat_ints(s: pd.Series) -> str:
res = 0
for x in s:
res = (res << x.bit_length()) | x
return str(res)
class ColumnHasher:
def __init__(self, width: int):
self.width = width
self._over = (self.width // 64) + 1
def hash(self, s: pd.Series) -> pd.Series:
res = []
for n in range(self._over):
hash_key = md5(bytes(str(n), "utf8")).hexdigest()[:16]
res.append(pd.util.hash_pandas_object(s, index=False, hash_key=hash_key))
h = pd.concat(res, axis=1).apply(concat_ints, axis=1)
return h.apply(lambda x: int(x) & int("1" * self.width, 2))
class ColumnBinder:
def __init__(self, width: int, prng: np.random.RandomState, name=""):
self.width = width
self.hasher = ColumnHasher(width)
# numpy truncates at 64 bits, need to stay in python
self.map_key = random.randint(0, 2 ** width)
def _map_hashes(self, s: pd.Series) -> pd.Series:
return s.apply(lambda x: int(x) ^ self.map_key)
def embed(self, s: pd.Series) -> pd.Series:
hashes = self.hasher.hash(s)
return self._map_hashes(hashes)
def get_query_key(self, val: Any) -> int:
return int(self.embed(pd.Series([val]))[0])
class RecordBinder:
def __init__(self, columns: List[str], width: int, prng: np.random.RandomState):
self.width = width
self.binders = {x: ColumnBinder(width, prng, name=x) for x in columns}
self._default_query_threshold = self._get_default_query_threshold(self.width)
@staticmethod
def _get_default_query_threshold(width, error_rate: float = 0.05) -> float:
# TODO: this is wrong
probs = np.array([scipy.special.binom(width, x) for x in range(width + 1)])
probs_cdf = np.cumsum(probs) / np.sum(probs)
return (width - np.max(np.where(probs_cdf < error_rate)[0])) / width
# TODO: Add a method for aggregating rows of embeddings from records in a series
def _agg_embeds(self, s: pd.Series, threshold=True) -> np.array:
res = []
for x in s.index:
# s could have duplicate indicies
if isinstance(s[x], int):
res.append(int_to_array(s[x], self.width))
else:
for y in s[x]:
res.append(int_to_array(int(y), self.width))
res = np.mean(np.array(res), axis=0)
if threshold:
res = res >= 0.5
return array_to_int(res)
def embed(self, df: pd.DataFrame) -> pd.Series:
hashes = pd.concat([self.binders[x].embed(df[x]) for x in df.columns], axis=1)
return hashes.apply(self._agg_embeds, axis=1)
def get_query_key(self, query: Dict) -> np.array:
tups = []
for name, vals in query.items():
if isinstance(vals, list):
for v in vals:
tups.append((name, self.binders[name].get_query_key(v)))
else:
tups.append((name, self.binders[name].get_query_key(vals)))
s = pd.DataFrame(tups).set_index(0)[1]
return self._agg_embeds(s)
def query(
self, df: pd.DataFrame, query: Dict, threshold: float = None
) -> pd.DataFrame:
threshold = (
threshold if threshold is not None else self._default_query_threshold
)
key = self.get_query_key(query)
embeds = self.embed(df)
similarities = embeds.apply(
lambda x: np.sum(
~np.logical_xor(
int_to_array(x, self.width), int_to_array(key, self.width)
)
)
/ self.width
)
similarities.name = "sim"
return pd.merge(
similarities.loc[similarities > threshold],
df,
left_index=True,
right_index=True,
)