-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensing_matrices.py
272 lines (210 loc) · 10.2 KB
/
sensing_matrices.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
268
269
270
271
272
import torch
import torch.nn as nn
import numpy as np
from conf import device
class CompletelyLearned(nn.Module):
"""Just a linear neural network layer, initialized with random Gaussian entries"""
def __init__(self, m, n):
super(CompletelyLearned, self).__init__()
self.m = m
self.n = n
self.param = nn.Parameter(torch.normal(0, 1/ np.sqrt(n), size=(1, m, n)).to(device))
def forward(self, b, test=False):
return self.param.repeat(b, 1, 1)
def to_superpixel(phi):
if len(phi.shape) == 2:
m, n = phi.shape
b = 1
twod=True
if len(phi.shape) == 3:
b, m, n = phi.shape
twod=False
weight = torch.ones((3, 3)).to(device)
weight = weight.unsqueeze(0).unsqueeze(0)
phi = phi.reshape(b*m , 1, int(np.sqrt(n)), int(np.sqrt(n))).contiguous()
phi = torch.nn.functional.conv2d(phi, weight, bias=None, stride=1, padding=1, dilation=1, groups=1).clamp(0, 1)
if twod:
return phi.reshape(m, n)
return phi.reshape(b, m, n)
class Pooling(nn.Module):
"""Class for learning pooling masks (d ones per row)"""
def __init__(self, m, n, d, initial_scalar, random_seed):
super(Pooling, self).__init__()
self.m = m
self.n = n
self.d = d
torch.manual_seed(random_seed)
noise = - torch.log(- torch.log(torch.rand((1, m, n), device=device))) / 1000
self.temperature = 1
self.param = nn.Parameter(noise)
self.scalar = nn.Parameter(torch.tensor( initial_scalar,device=device))
def forward(self, b, test=False):
if not test:
logits = self.param.repeat(b, 1, 1)
noise = - torch.log(- torch.log(torch.rand((b, self.m, self.n), device=device)))
probs = torch.softmax((logits + noise / 1000) / self.temperature, dim=2)
values, index = torch.topk(probs, self.d, dim=2)
y_hard = torch.zeros_like(probs, memory_format=torch.legacy_contiguous_format).scatter_(2, index, 1.0)
phi = y_hard - probs.detach() + probs
if test:
phi = torch.zeros(1, self.m, self.n, device=device)
noise = 1 * - torch.log(- torch.log(torch.rand((1, self.m, self.n), device=device))) / 1000
val, index = torch.topk(self.param + noise, self.d, dim=2)
phi.scatter_(2, index, 1.0)
phi = phi.reshape(1, self.m, self.n)
phi = phi.repeat(b, 1, 1)
norm = (phi.norm(dim=2).unsqueeze(2) + 10e-3)
return phi / norm * torch.maximum(torch.tensor(0.01).to(device),self.scalar)
class Pixel(nn.Module):
"""Class for learning pixel masks (d ones per row)"""
def __init__(self, m, n, d, initial_scalar, random_seed, use_superpixel):
super(Pixel, self).__init__()
self.m = m
self.n = n
self.d = d
self.use_superpixel = use_superpixel
torch.manual_seed(random_seed)
noise = - torch.log(- torch.log(torch.rand((1, m, n), device=device))) / 1000
self.temperature = 1
self.param = nn.Parameter(noise)
self.scalar = nn.Parameter(torch.ones(1) * initial_scalar)
def forward(self, b, test=False):
if not test:
logits = self.param.repeat(b, 1, 1)
noise = - torch.log(- torch.log(torch.rand((b, self.m, self.n), device=device)))
probs = torch.softmax((logits + noise / 1000) / self.temperature, dim=2)
values, index = torch.topk(probs, self.d, dim=2)
y_hard = torch.zeros_like(probs, memory_format=torch.legacy_contiguous_format).scatter_(2, index, 1.0)
phi = y_hard - probs.detach() + probs
if test:
phi = torch.zeros(1, self.m, self.n, device=device)
noise = 1 * - torch.log(- torch.log(torch.rand((1, self.m, self.n), device=device))) / 1000
val, index = torch.topk(self.param + noise, self.d, dim=2)
phi.scatter_(2, index, 1.0)
phi = phi.reshape(1, self.m, self.n)
phi = phi.repeat(b, 1, 1)
if self.use_superpixel:
phi = to_superpixel(phi)
norm = (phi.norm(dim=2).unsqueeze(2) + 10e-3)
return phi / norm * torch.maximum(torch.tensor(0.01).to(device),self.scalar)
class LeftDRegularGraph(nn.Module):
def __init__(self, m, n, d, initial_scalar, random_seed):
super(LeftDRegularGraph, self).__init__()
self.m = m
self.n = n
self.d = d
torch.manual_seed(random_seed)
noise = - torch.log(- torch.log(torch.rand((1, m, n), device=device))) / 1000
self.param = nn.Parameter(noise)
self.temperature = 1
self.scalar =nn.Parameter(torch.ones(1) * initial_scalar)
def forward(self, b, test=False):
if not test:
logits = self.param.repeat(b, 1, 1)
noise = - torch.log(- torch.log(torch.rand((b, self.m, self.n), device=device)))
probs = torch.softmax((logits + noise / 1000) / 1, dim=1)
values, index = torch.topk(probs, self.d, dim=1)
y_hard = torch.zeros_like(probs, memory_format=torch.legacy_contiguous_format).scatter_(1, index, 1.0)
phi = y_hard - probs.detach() + probs
if test:
phi = torch.zeros(self.m, self.n, device=device)
val, index = torch.topk(self.param[0], self.d, dim=0)
phi.scatter_(0, index, 1.0)
phi = phi.reshape(1, self.m, self.n)
phi = phi.repeat(b, 1, 1)
return phi / np.sqrt(self.d) * torch.maximum(torch.tensor(0.01).to(device),self.scalar)
class LoadedFromNumpy(nn.Module):
"""Loads a Numpy Matrix"""
def __init__(self, constant, m, n, path, d):
super(LoadedFromNumpy, self).__init__()
self.phi = torch.tensor(np.load(path)).to(device)
self.param = torch.zeros(m, n)
self.d = d
self.constant = constant
def __call__(self, b, test=False):
return self.phi.unsqueeze(0).repeat(b, 1, 1) * self.constant
class ConstructedPooling:
"""Constructs the pooling matrix from:
Petersen, Hendrik Bernd, Bubacarr Bah, and Peter Jung.
'Practical high-throughput, non-adaptive and noise-robust SARS-CoV-2 testing' ."""
def __init__(self, scalar):
self.scalar = scalar
Q = 31 # pool size
N = Q**2 # population size
prevalence = 0.73 # infection rate [%]
S1 = np.int(np.round((prevalence*N/100))) # between 1 to N
#print('Infected/Population: {}/{}\n'.format(S1,N))
# Pooling strategy to mix viral loads
# Permutation matrix
P = np.zeros((Q,Q))
P[0,Q-1] = 1
for q in range(0,Q-1):
P[q+1,q] = 1
#print('Permutation matrix P.shape:', P.shape)
#print(P)
# Pooling matrix (A) to mix viral loads
M = (S1+1)*Q # number of qPCR tests
A = np.ones((M,N))
for s in range(1,S1+1 +1):
for q in range(1,Q +1):
A_sq = 1/(S1+1) * np.linalg.matrix_power(P,(s-1)*(q-1))
A[(s-1)*Q:s*Q,(q-1)*Q:q*Q] = A_sq
self.A = torch.tensor(A).float().to(device)
self.param = self.A
def __call__(self, b, test=False):
return self.A.unsqueeze(0).repeat(b, 1, 1) * self.scalar
def to(self, device):
return self
def circulant(tensor, dim):
"""From: https://stackoverflow.com/questions/69820726/is-there-a-way-to-compute-a-circulant-matrix-in-pytorch
get a circulant version of the tensor along the {dim} dimension.
The additional axis is appended as the last dimension.
E.g. tensor=[0,1,2], dim=0 --> [[0,1,2],[2,0,1],[1,2,0]]"""
S = tensor.shape[dim]
tmp = torch.cat([tensor.flip((dim,)), torch.narrow(tensor.flip((dim,)), dim=dim, start=0, length=S-1)], dim=dim)
return tmp.unfold(dim, S, 1).flip((-1,))
class CircularConv(nn.Module):
def __init__(self, m, n, kernel_width, d, initial_scalar, random_seed):
super(CircularConv, self).__init__()
self.m = m
self.n = n
self.d = d
self.kernel_width = kernel_width
self.scalar = nn.Parameter(torch.tensor([initial_scalar], device=device).float())
torch.manual_seed(random_seed)
noise = - torch.log(- torch.log(torch.rand((1, kernel_width), device=device))) / 1000
self.kernel_param = nn.Parameter(noise)
noise = - torch.log(- torch.log(torch.rand((1, n), device=device))) / 1000
self.mask_param = nn.Parameter(noise)
self.temperature = 1
def forward(self, b, test=False):
if not test:
logits = self.kernel_param.repeat(b, 1)
noise = - torch.log(- torch.log(torch.rand((b, self.kernel_width), device=device)))
probs = torch.softmax((logits + noise / 1000) / self.temperature, dim=1)
values, index = torch.topk(probs, self.d, dim=1)
y_hard = (0 * torch.ones_like(probs, memory_format=torch.legacy_contiguous_format)).scatter_(1, index, 1.0)
kernel = y_hard - probs.detach() + probs
logits = self.mask_param.repeat(b, 1)
noise = - torch.log(- torch.log(torch.rand((b, self.n), device=device)))
probs = torch.softmax((logits + noise / 1000) / self.temperature, dim=1)
values, index = torch.topk(probs, self.m, dim=1)
y_hard = (0 * torch.ones_like(probs, memory_format=torch.legacy_contiguous_format)).scatter_(1, index, 1.0)
mask = y_hard - probs.detach() + probs
mask = mask.reshape(b, self.n, 1)
if test:
kernel = ( 0 * torch.ones(1, self.kernel_width, device=device))
noise = 0.2 * - torch.log(- torch.log(torch.rand((1, self.kernel_width), device=device))) / 1000
val, index = torch.topk(self.kernel_param + noise, self.d, dim=1)
kernel.scatter_(1, index, 1.0)
kernel = kernel.reshape(1, self.kernel_width)
kernel = kernel.repeat(b, 1)
mask = ( 0 * torch.ones(1, self.n, device=device))
noise = 0.2 * - torch.log(- torch.log(torch.rand((1, self.n), device=device))) / 1000
val, index = torch.topk(self.mask_param + noise, self.m, dim=1)
mask.scatter_(1, index, 1.0)
mask = mask.reshape(1, self.n, 1)
mask = mask.repeat(b, 1, 1)
kernel = kernel / np.sqrt(self.n) * torch.maximum(torch.tensor(0.01).to(device),self.scalar)
phi = circulant(kernel, -1)
return phi[mask.bool().squeeze(2)].reshape(-1, self.m, 784)