-
Notifications
You must be signed in to change notification settings - Fork 4
/
sketches.py
168 lines (129 loc) · 5.39 KB
/
sketches.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
import torch
import numpy as np
from scipy.linalg import hadamard as hadam_scipy
from time import time
torch.set_default_dtype(torch.float64)
def _hadamard(matrix):
n = matrix.shape[0]
if n == 1:
return matrix
_t1 = _hadamard(matrix[:n//2,::]+matrix[n//2:,::])
_t2 = _hadamard(matrix[:n//2,::]-matrix[n//2:,::])
return torch.cat((_t1, _t2), 0)
def hadamard(matrix):
if matrix.ndim == 1:
matrix = matrix.reshape((-1,1))
n = matrix.shape[0]
if n & (n-1) != 0:
new_dim = 2**(int(np.ceil(np.log(n)/np.log(2))))
pad_matrix = torch.zeros(new_dim-n, matrix.shape[1]).to(matrix.device)
matrix = torch.cat((matrix, pad_matrix))
n = matrix.shape[0]
diag = np.random.choice([-1,1], n, replace=True).reshape((-1,1))
matrix = torch.Tensor(diag).to(matrix.device) * matrix
return 1./np.sqrt(n) * _hadamard(matrix)
def rrs(matrix, sketch_size, nnz=None):
n = matrix.shape[0]
return matrix[np.random.choice(np.arange(n), sketch_size, replace=False),::]
def rrs_lev_scores(matrix, sketch_size, nnz=None):
n, d = matrix.shape
lev_scores = lev_approx(matrix, alpha=5)
prob = lev_scores / lev_scores.sum()
return matrix[np.random.choice(n, sketch_size, replace=False, p=prob),::]
def gaussian(matrix, sketch_size, nnz=None):
S = 1./np.sqrt(sketch_size) * torch.randn(sketch_size, matrix.shape[0]).to(matrix.device)
return S @ matrix
def sjlt(matrix, sketch_size, nnz=None):
n, d = matrix.shape
indices = np.vstack([np.random.choice(sketch_size, n).reshape((1,-1)), np.arange(n)])
values = np.random.choice(np.array([-1,1], dtype=np.float64), size=n)
S = torch.sparse_coo_tensor(indices, values, (sketch_size, n)).to(matrix.device)
sa = S @ matrix
return sa
def sparse_rademacher(matrix, sketch_size, nnz=None):
n, d = matrix.shape
if nnz is None:
nnz = d/n
d_tilde = int(nnz*n)
indices = np.vstack([np.repeat(np.arange(sketch_size), d_tilde).reshape((1,-1)),
np.random.choice(n,size=sketch_size*d_tilde).reshape((1,-1))])
values = np.random.choice(np.array([-1,1], dtype=np.float64), size=sketch_size*d_tilde)
S = torch.sparse_coo_tensor(indices, values, (sketch_size, n)).to(matrix.device)
return np.sqrt(n/(sketch_size*nnz*n)) * S @ matrix
def less(matrix, sketch_size, lev_scores=False, nnz=None):
if not lev_scores:
return sparse_rademacher(hadamard(matrix), sketch_size, nnz)
else:
n, d = matrix.shape
lev_scores = lev_approx(matrix, alpha=5)
prob = lev_scores / lev_scores.sum()
samples = torch.tensor(np.random.multinomial(d, pvals=prob, size=sketch_size)).to(matrix.device)
samples = samples / (d*prob.reshape((1,-1)))
S = torch.sqrt(samples/sketch_size) * torch.tensor(np.random.choice([-1,1], size=(sketch_size,n))).to(matrix.device)
return S @ matrix
def _srht(indices, v):
n = v.shape[0]
if n == 1:
return v
i1 = indices[indices < n//2]
i2 = indices[indices >= n//2]
if len(i1) == 0:
return _srht(i2-n//2, v[:n//2,::]-v[n//2:,::])
elif len(i2) == 0:
return _srht(i1, v[:n//2,::]+v[n//2:,::])
else:
return torch.cat([_srht(i1, v[:n//2,::]+v[n//2:,::]), _srht(i2-n//2, v[:n//2,::]-v[n//2:,::])], axis=0)
def srht(matrix, sketch_size, nnz=None):
#device = matrix.device
#matrix = matrix.cpu().numpy()
if matrix.ndim == 1:
matrix = matrix.reshape((-1,1))
# pad matrix with 0 if first dimension is not a power of 2
n = matrix.shape[0]
if n & (n-1) != 0:
new_dim = 2**(int(np.log(n) / np.log(2))+1)
matrix = torch.cat([matrix, torch.zeros(new_dim - n, matrix.shape[1]).to(matrix.device)], axis=0)
n = matrix.shape[0]
indices = np.sort(np.random.choice(np.arange(n), sketch_size, replace=False))
v = torch.tensor(np.random.choice([-1,1], n, replace=True)).reshape((-1,1)).to(matrix.device)
matrix = v * matrix
sa = _srht(indices, matrix)
return sa
def lev_approx(matrix, alpha=10):
n, d = matrix.shape
m = int(alpha*d)
sa = sjlt(matrix, m)
_, sig_vec, v_mat = torch.svd(sa)
y_mat = matrix @ v_mat.T / sig_vec.reshape((1,-1))
lev_vec = torch.sum(y_mat**2, axis=1)
return lev_vec.cpu().numpy()
'''
def _srht(indices, v):
n = v.shape[0]
if n == 1:
return v
i1 = indices[indices < n//2]
i2 = indices[indices >= n//2]
if len(i1) == 0:
return _srht(i2-n//2, v[:n//2,::]-v[n//2:,::])
elif len(i2) == 0:
return _srht(i1, v[:n//2,::]+v[n//2,::])
else:
_t1 = _srht(i1, v[:n//2,::]+v[n//2,::])
_t2 = _srht(i2-n//2, v[:n//2,::]-v[n//2,::])
return torch.cat((_t1, _t2), 0)
def srht(matrix, sketch_size, nnz=None):
if matrix.ndim == 1:
matrix = matrix.reshape((-1,1))
n = matrix.shape[0]
if n & (n-1) != 0:
new_dim = 2**(int(np.ceil(np.log(n)/np.log(2))))
pad_matrix = torch.zeros(new_dim-n, matrix.shape[1]).to(matrix.device)
matrix = torch.cat((matrix, pad_matrix))
n = matrix.shape[0]
diag = np.random.choice([-1,1], n, replace=True).reshape((-1,1))
matrix = torch.Tensor(diag).to(matrix.device) * matrix
indices = np.sort(np.random.choice(np.arange(n), sketch_size, replace=False))
return 1./np.sqrt(sketch_size) * _srht(indices, matrix)
#return _srht(indices, matrix)
'''