-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexp_detection_mnist_all.py
239 lines (165 loc) · 6.42 KB
/
exp_detection_mnist_all.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
# This script runs experiments and generates plots for the hypothesis testing problem
from __future__ import division, print_function
from scipy import stats
import scipy as sp
from statsmodels.distributions.empirical_distribution import ECDF
#from kernel_two_sample_test.kernel_two_sample_test import *
#from mmd_test_with_Shogun import *
from twosample_tests import *
import mxnet as mx
import numpy as np
from mxnet import nd, autograd
from mxnet import gluon
import pickle
# two customized modules
from labelshift import *
from utils4gluon import *
deltalist = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
powerlist = []
powerlist_oracle = []
cdflist = []
cdflist_oracle = []
powerlist_x = []
cdflist_x = []
for delta in deltalist:
#-------------------- preparing data ----------------------------------------
ctx = mx.cpu()
mnist = mx.test_utils.get_mnist()
num_inputs = 784
num_outputs = 10
dfeat = 784
nclass = 10
batch_size = 64
dataset = mnist
X = dataset["train_data"]
y = dataset["train_label"]
# make the training data slightly unbalanced by knocking out the class distribution a little bit
# split it into train and validation
# The test set will have a uniform distribution over y
# the train will not.
n = X.shape[0]
# Random permutation of the data
idx = np.random.permutation(n)
X = X[idx,...]
y = y[idx]
# split the data into training and testing
num = 3
Xtest = X[(2*n // num):, :, :, :]
ytest = y[(2*n // num):]
X = X[:(n // num), :, :, :]
y = y[:(n // num)]
n = X.shape[0]
# Now adding perturbation to the train distribution
# the amount to perturb in one class is delta.
#delta = 0.5
ko_class = 5
idx = (y==ko_class).nonzero()
nn=len(idx[0])
nnko=np.round(delta*len(idx[0])).astype(int)
ko_idx = np.random.choice(nn,nnko)
mask = np.ones(n)>0
mask[idx[0][ko_idx]] = False
X = X[mask, ...]
y = y[mask]
# further splitting the training data into train and val
n = X.shape[0]
num = 2
Xtrain = X[:(n//num),:,:,:]
ytrain = y[:(n//num)]
Xval = X[(n//num):(2*n//num),:,:,:]
yval = y[(n//num):(2*n//num):]
# we will ignore the standard test data, which I believe has a different feature distribution.
#Xtest = dataset["test_data"]
#ytest = dataset["test_label"]
sz = 10000
#
# ------------------ Training a classifier -----------------
#
num_hidden = 256
net = gluon.nn.Sequential()
with net.name_scope():
net.add(gluon.nn.Dense(num_hidden, activation="relu"))
net.add(gluon.nn.Dense(num_hidden, activation="relu"))
net.add(gluon.nn.Dense(num_outputs))
net.collect_params().initialize(mx.init.Xavier(magnitude=2.24), ctx=ctx, force_reinit=True)
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .1})
# get the p-values for ground-truth tests
pvlist_gnd = []
for i in range(500):
yboot_s = np.random.choice(yval, size=sz, replace=True)
yboot_t = np.random.choice(ytest, size=sz, replace=True)
D, pv = stats.ks_2samp(yboot_s, yboot_t)
pvlist_gnd.append(pv.astype(float))
#D, tmp, pv = stats.anderson_ksamp([yboot_s, yboot_t])
#pvlist_gnd.append(pv)
cdfpval_gnd = ECDF(pvlist_gnd)
# get the p-values for p(x), q(x) test using kernel two-sample test
# reshape the data points first
Xval = Xval.reshape((-1, dfeat))
Xtest = Xtest.reshape((-1, dfeat))
r = 50
v = np.random.rand(dfeat, r)
DIM_REDUCTION = False
pvlist_x = []
for i in range(100):
idx1 = np.random.choice(range(len(Xval)), size=sz, replace=True)
idx2 = np.random.choice(range(len(Xtest)), size=sz, replace=True)
xboot_s = Xval[idx1,:]
xboot_t = Xtest[idx2,:]
if DIM_REDUCTION:
# dimension reduction
xboot_s = np.dot(xboot_s, v)
xboot_t = np.dot(xboot_t, v)
# how to choose kernel bandwidth?
# use the median trick!
# they are already randomized
sq_dist = np.sum((xboot_s[:sz,]-xboot_t[:sz,])**2,1)
h = np.sqrt(np.median(sq_dist)/2)
#print("start kernel test"+repr(i))
#pval = linear_time_rbf_mmd_test(xboot_s, xboot_t, bandwidth="median")
#print("kernel test returns pval = " + repr(pval))
#tmp1,tmp2, pval = kernel_two_sample_test(xboot_s, xboot_t, kernel_function='rbf', iterations=100,
# verbose=False, random_state=None, gamma=1/h)
# using btest-mmd
pval, tmp = btest_mmd_python(xboot_s, xboot_t, bandwidth="median")
# using t-test
#pval, tmp = linear_hotelling_test(xboot_s, xboot_t, reg=0.1)
print("testing p(x) returns stat = " + repr(tmp)+ "and pval = " + repr(pval))
pvlist_x.append(pval)
cdfpval_x = ECDF(pvlist_x)
temp = []
# get the p-values for better and better classifiers.
for i in range(1,10,2):
if i == 1:
epochs=1
else:
epochs=2
# Training
weighted_train(net, softmax_cross_entropy, trainer, Xtrain, ytrain, Xval, yval, ctx, dfeat, epoch=epochs, weightfunc=None)
# Prediction
ypred_s, ypred_s_soft = predict_all(Xval, net, ctx, dfeat)
ypred_t, ypred_t_soft = predict_all(Xtest, net, ctx, dfeat)
# Converting to numpy array for later convenience
ypred_s= ypred_s.asnumpy()
ypred_s_soft = ypred_s_soft.asnumpy()
ypred_t=ypred_t.asnumpy()
# use bootstrap and plot p-values
pvlist =[]
for i in range(500):
yboot_s = np.random.choice(ypred_s, size=sz, replace=True)
yboot_t = np.random.choice(ypred_t, size=sz, replace=True)
D, pv = stats.ks_2samp(yboot_s, yboot_t)
pvlist.append(pv.astype(float))
#D, tmp, pv = stats.anderson_ksamp([yboot_s, yboot_t])
#pvlist.append(pv)
cdfpval = ECDF(pvlist)
temp.append(cdfpval(0.05))
powerlist.append(temp)
powerlist_oracle.append(cdfpval_gnd(0.05))
powerlist_x.append(cdfpval_x(0.05))
cdflist.append(cdfpval)
cdflist_oracle.append(cdfpval_gnd)
cdflist_x.append(cdfpval_x)
results = [deltalist, powerlist, powerlist_oracle, cdflist, cdflist_oracle, powerlist_x, cdflist_x]
pickle.dump( results, open( "results_exp_mnist_full.p", "wb" ) )