-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinearity_simulation.py
More file actions
183 lines (145 loc) · 6.97 KB
/
linearity_simulation.py
File metadata and controls
183 lines (145 loc) · 6.97 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
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from utils import (interfere_with_anchors, get_measurements,
quantise_measurements, do_MDS, ortho_procrustes, make_LXX)
def gradient_descent_X(D, X_0, W):
# does gradient descent
lr = 0.007
# if (np.isnan(lr)):
# lr = 0
# if (np.isinf(lr)):
# lr = 0
n_iter = 20
N = X_0.shape[1]
e = np.ones([N,1])
X = X_0.copy()
for i in range(n_iter):
L = make_LXX(X)
P = D - L
P = W*P
grad = (1/N**2) * (8 * X @ (P - np.diag(np.diag(P @ e)) ))
X -= lr*grad
return X, L
def make_D_ensembles(y, number_of_anchors):
# populates the distance matrices for all rows
num_elements = int((number_of_anchors+2)* (number_of_anchors+1) * 0.5)
trials = y.shape[1]
dim = number_of_anchors+2
all_D_oracles_x1 = np.zeros([trials, dim, dim])
all_D_oracles_x2 = np.zeros([trials, dim, dim])
all_D_oracles_x1_plus_x2 = np.zeros([trials, dim, dim])
ind = np.triu_indices(all_D_oracles_x1[0].shape[0], k=1)
for i in range(trials):
data = y[0:num_elements,i]
all_D_oracles_x1[i][ind] = data
all_D_oracles_x1[i] += all_D_oracles_x1[i].T
data = y[num_elements: 2*num_elements,i]
all_D_oracles_x2[i][ind] = data
all_D_oracles_x2[i] += all_D_oracles_x2[i].T
data = y[2*num_elements: 3*num_elements,i]
all_D_oracles_x1_plus_x2[i][ind] = data
all_D_oracles_x1_plus_x2[i] += all_D_oracles_x1_plus_x2[i].T
return all_D_oracles_x1, all_D_oracles_x2, all_D_oracles_x1_plus_x2
###############################################################################
if __name__ == "__main__":
np.random.seed(23) # 22, 21, 12 are interesting to look at
image_size = 64
number_of_anchors = 15
num_of_rows_in_A = 100
num_bits = 8
# makes signals
xs = np.random.normal(size=(3,image_size**2))
xs[2] = xs[0]+xs[1]
# random anchors
anchors = np.random.normal(size=(number_of_anchors,image_size**2))
# interefere with anchors
x1_inter = interfere_with_anchors(image_size, xs[0], anchors)
x2_inter = interfere_with_anchors(image_size, xs[1], anchors)
x1_plus_x2_inter = interfere_with_anchors(image_size, xs[2], anchors)
opu_input = np.vstack((x1_inter, x2_inter, x1_plus_x2_inter))
# get quantized measurements
y, A = get_measurements(opu_input, num_of_rows_in_A)
y, y_quant = quantise_measurements(y, num_bits)
floor = 0
y_quant[y_quant<floor] = 0
# put measurements into distance matrices
all_D_quant_x1, all_D_quant_x2, all_D_quant_x1_plus_x2 = make_D_ensembles(
y_quant, number_of_anchors)
# for storing results
manual = np.zeros([num_of_rows_in_A, number_of_anchors+1-2]).astype(
'complex128')
direct = np.zeros([num_of_rows_in_A, number_of_anchors+1-2]).astype(
'complex128')
manual_gd = np.zeros([num_of_rows_in_A, number_of_anchors+1-2]).astype(
'complex128')
direct_gd = np.zeros([num_of_rows_in_A, number_of_anchors+1-2]).astype(
'complex128')
for trial in tqdm(range(num_of_rows_in_A)):
for i in range(2,number_of_anchors+1):
ind = np.random.choice(np.arange(1, number_of_anchors+1), i,
replace=False)
ind = np.hstack((ind,0,number_of_anchors+1))
ind.sort()
D_quant_x1 = all_D_quant_x1[:,:,ind][trial][ind,:]
D_quant_x2 = all_D_quant_x2[:,:,ind][trial][ind,:]
D_quant_x1_plus_x2 = all_D_quant_x1_plus_x2[:,:,ind][trial][ind,:]
# normal MDS
recovered_points_x1 = do_MDS(D_quant_x1, i)
recovered_points_x2 = do_MDS(D_quant_x2, i)
recovered_points_x1_plus_x2 = do_MDS(D_quant_x1_plus_x2, i)
recovered_points_x2 = ortho_procrustes(
recovered_points_x1, recovered_points_x2)
recovered_points_x1_plus_x2 = ortho_procrustes(
recovered_points_x1, recovered_points_x1_plus_x2)
manual_sum = recovered_points_x1[0] + recovered_points_x2[0]
opu_sum = recovered_points_x1_plus_x2[0]
manual[trial, i-2] = manual_sum
direct[trial, i-2] = opu_sum
# grad descent
# for x1
X_0 = np.vstack((np.real(recovered_points_x1), np.imag(
recovered_points_x1)))
W = (D_quant_x1>0).astype('float') + np.eye(D_quant_x1.shape[0])
X, L = gradient_descent_X(D_quant_x1, X_0, W)
recovered_points_x1 = X[0] + 1j*X[1]
recovered_points_x1 -= recovered_points_x1[-1]
# same as above but for x2
X_0 = np.vstack((np.real(recovered_points_x2),
np.imag(recovered_points_x2)))
W = (D_quant_x2>0).astype('float') + np.eye(D_quant_x2.shape[0])
X, L = gradient_descent_X(D_quant_x2, X_0, W)
recovered_points_x2 = X[0] + 1j*X[1]
recovered_points_x2 -= recovered_points_x2[-1]
# same as above but for x1+x2
X_0 = np.vstack((np.real(recovered_points_x1_plus_x2),
np.imag(recovered_points_x1_plus_x2)))
W = (D_quant_x1_plus_x2>0).astype('float') + np.eye(
D_quant_x1_plus_x2.shape[0])
X, L = gradient_descent_X(D_quant_x1_plus_x2, X_0, W)
recovered_points_x1_plus_x2 = X[0] + 1j*X[1]
recovered_points_x1_plus_x2 -= recovered_points_x1_plus_x2[-1]
# align x2 anchors with x1 anchors
recovered_points_x2 = ortho_procrustes(
recovered_points_x1, recovered_points_x2)
# align x1+x2 anchors with x1 anchors
recovered_points_x1_plus_x2 = ortho_procrustes(
recovered_points_x1, recovered_points_x1_plus_x2)
manual_sum = recovered_points_x1[0] + recovered_points_x2[0]
opu_sum = recovered_points_x1_plus_x2[0]
manual_gd[trial, i-2] = manual_sum
direct_gd[trial, i-2] = opu_sum
anchors_axis = np.arange(2, number_of_anchors+1)
rel_errors = 100*(np.abs(manual - direct) / np.abs(direct))
rel_errors_sdr = 100*(np.abs(manual_gd - direct_gd) / np.abs(direct_gd))
plt.rcParams.update({'font.size': 14})
plt.figure(figsize=(5,4))
plt.plot(anchors_axis, np.mean(rel_errors, axis=0), label='MDS', linewidth=2.5)
plt.plot(anchors_axis, np.mean(rel_errors_sdr, axis=0), label='MDS-GD',
color='r', linestyle=(0, (5, 1)), linewidth=2.5)
plt.ylabel('Average relative error (%)')
plt.xlabel('Number of anchors')
plt.xticks(np.arange(min(anchors_axis), max(anchors_axis)+1, 2.0))
plt.grid(which='major')
plt.legend()
plt.tight_layout()