-
Notifications
You must be signed in to change notification settings - Fork 0
/
stealth_sampling.py
144 lines (140 loc) · 5.92 KB
/
stealth_sampling.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
import os
import numpy as np
import subprocess
from sklearn.datasets import dump_svmlight_file
def stealth_sampling(X, K, path='./', prefix='tmp', timeout=10.0):
assert len(X)==len(K)
C = len(X)
while True:
Y = np.concatenate(X, axis=0)
Y += 1e-10 * np.random.randn(*Y.shape)
z = np.concatenate([i*np.ones(X[i].shape[0]) for i in range(C)])
com = ' '.join('%d' % (k,) for k in K)
dump_svmlight_file(Y, z, './%s_input.txt' % (prefix,), comment=com)
cmd = '%sstealth-sampling/main ./%s_input.txt > %s_output.txt' % (path, prefix, prefix)
proc = subprocess.Popen('exec ' + cmd, shell=True)
try:
proc.wait(timeout)
break
except:
proc.kill()
res = np.loadtxt('./%s_output.txt' % (prefix,))
p = np.split(res, np.cumsum([X[i].shape[0] for i in range(C)]))
os.remove('./%s_input.txt' % (prefix,))
os.remove('./%s_output.txt' % (prefix,))
return np.array(p[:-1]) / (Y.shape[0] * sum(K))
def stealth_sampling_bootstrap(X, K, path='./', prefix='tmp', ratio=0.3, num_sample=10, num_process=2, seed=0, timeout=10.0):
c = 0
c2 = 0
C = len(X)
q = [1e-10*np.ones(X[j].shape[0]) for j in range(C)]
for i in range(num_sample):
while True:
c2 += 1
commands = []
N = []
M = []
idx = []
for p in range(num_process):
prefix_p = '%s_p%05d' % (prefix, p)
np.random.seed(seed+c)
idx_p = []
Ni = []
Xi = []
Ki = []
for j in range(C):
nj = X[j].shape[0]
mj = int(np.round(ratio * nj))
idxj = np.random.permutation(nj)[:mj]
idx_p.append(idxj)
Ni.append(mj)
Xi.append(X[j][idxj, :])
Ki.append(int(np.round(ratio * K[j])))
N.append(Ni)
M.append(Ki)
idx.append(idx_p)
np.random.seed(seed+c2)
Yi = np.concatenate(Xi, axis=0)
Yi += 1e-10 * np.random.randn(*Yi.shape)
zi = np.concatenate([j*np.ones(Xi[j].shape[0]) for j in range(C)])
com = ' '.join('%d' % (k,) for k in Ki)
dump_svmlight_file(Yi, zi, './%s_input.txt' % (prefix_p,), comment=com)
cmd = '%sstealth-sampling/main ./%s_input.txt > %s_output.txt' % (path, prefix_p, prefix_p)
commands.append(cmd)
procs = [subprocess.Popen('exec ' + cmd, shell=True) for cmd in commands]
try:
for p in procs:
p.wait(timeout)
c += 1
break
except:
for p in procs:
p.kill()
print('killed')
for p in range(num_process):
prefix_p = '%s_p%05d' % (prefix, p)
res = np.loadtxt('./%s_output.txt' % (prefix_p,))
res = np.split(res, np.cumsum([N[p][i] for i in range(C)]))[:-1]
for j in range(C):
q[j][idx[p][j]] += res[j] / (sum(N[p]) * sum(M[p]) * num_sample * num_process)
os.remove('./%s_input.txt' % (prefix_p,))
os.remove('./%s_output.txt' % (prefix_p,))
qsum = np.sum(np.concatenate(q))
q = [qq/qsum for qq in q]
return q
def compute_wasserstein(X1, X2, path='./', prefix='tmp', timeout=10.0):
assert X1.shape[1] == X2.shape[1]
while True:
dump_svmlight_file(X1+1e-10*np.random.randn(*X1.shape), np.zeros(X1.shape[0]), './%s_input1.txt' % (prefix,))
dump_svmlight_file(X2+1e-10*np.random.randn(*X2.shape), np.zeros(X2.shape[0]), './%s_input2.txt' % (prefix,))
cmd = '%swasserstein/main ./%s_input1.txt ./%s_input2.txt > %s_output.txt' % (path, prefix, prefix, prefix)
proc = subprocess.Popen('exec ' + cmd, shell=True)
try:
proc.wait(timeout)
break
except:
proc.kill()
d = np.loadtxt('./%s_output.txt' % (prefix,))
os.remove('./%s_input1.txt' % (prefix,))
os.remove('./%s_input2.txt' % (prefix,))
os.remove('./%s_output.txt' % (prefix,))
return d
def compute_wasserstein_bootstrap(X1, X2, n, path='./', prefix='tmp', num_sample=10, num_process=2, seed=0, timeout=10.0):
n1 = X1.shape[0]
n2 = X2.shape[0]
n = min([n, n1, n2])
c = 0
c2 = 0
d = 0.0
for i in range(num_sample):
while True:
c2 += 1
commands = []
for p in range(num_process):
prefix_p = '%s_%05d' % (prefix, p)
np.random.seed(seed+c)
idx1 = np.random.permutation(n1)[:n]
idx2 = np.random.permutation(n2)[:n]
np.random.seed(seed+c2)
dump_svmlight_file(X1[idx1, :]+1e-10*np.random.randn(idx1.size, X1.shape[1]), np.zeros(idx1.size), './%s_input1.txt' % (prefix_p,))
dump_svmlight_file(X2[idx2, :]+1e-10*np.random.randn(idx2.size, X2.shape[1]), np.zeros(idx2.size), './%s_input2.txt' % (prefix_p,))
cmd = '%swasserstein/main ./%s_input1.txt ./%s_input2.txt > %s_output.txt' % (path, prefix_p, prefix_p, prefix_p)
commands.append(cmd)
procs = [subprocess.Popen('exec ' + cmd, shell=True) for cmd in commands]
try:
for p in procs:
p.wait(timeout)
c += 1
break
except:
for p in procs:
p.kill()
print('killed')
for p in range(num_process):
prefix_p = '%s_%05d' % (prefix, p)
dp = np.loadtxt('./%s_output.txt' % (prefix_p,))
d += dp / (num_sample * num_process)
os.remove('./%s_input1.txt' % (prefix_p,))
os.remove('./%s_input2.txt' % (prefix_p,))
os.remove('./%s_output.txt' % (prefix_p,))
return d