-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple.py
48 lines (36 loc) · 1.86 KB
/
multiple.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
import numpy as np
from ts_lsh.base import LSH
from ts_lsh.srp import SignedRandomProjectionLSH
class MultipleLSH(LSH):
def __init__(self, **kwargs):
super(MultipleLSH, self).__init__(**kwargs)
# It is important to keep the number of components and the output length as separated attributes
self.output_length = self.num_components = kwargs.get("num_components", 2)
self.scale = kwargs.get("scale", 1.0)
self.dist = kwargs.get('dist','normal')
if self.dist == 'normal':
self.weights = np.random.randn(self.num_components, self.input_length) * self.scale
elif self.dist == 'unif':
self.weights = (np.random.rand(self.num_components, self.input_length) * 2 * self.scale) - self.scale
def _hashfunction(self, input : np.array, **kwargs):
return np.dot(self.weights, input)
class MultipleRandomSampledLSH(LSH):
def __init__(self, **kwargs):
super(MultipleRandomSampledLSH, self).__init__(**kwargs)
self.output_length = self.num_components = kwargs.get("num_components", 2)
self.sample_size : int = kwargs.get("sample_size", self.input_length // 2) # Sample must be lower than length
self.scale = kwargs.get("scale", 1.0)
self.dist = kwargs.get('dist','normal')
if self.dist == 'normal':
self.weights = np.random.randn(self.num_components, self.sample_size) * self.scale
elif self.dist == 'unif':
self.weights = (np.random.rand(self.num_components, self.sample_size) * 2 * self.scale) - self.scale
self.sample_indexes = []
for k in range(self.output_length):
self.sample_indexes.append( np.random.choice(self.input_length, self.sample_size, replace = False) )
def _hashfunction(self, input : np.array, **kwargs):
ret = []
for k in range(self.num_components):
i = input[[self.sample_indexes[k]]]
ret.append(np.dot(self.weights[k,:], i))
return np.array(ret)