-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdata_utils.py
242 lines (188 loc) · 7.46 KB
/
data_utils.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
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
import json
import numpy as np
import scipy.sparse as sparse
import defenses
import upper_bounds
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
assert len(np.shape(obj)) == 1 # Can only handle 1D ndarrays
return obj.tolist()
if isinstance(obj, np.floating):
return float(obj)
else:
return super(NumpyEncoder, self).default(obj)
def get_class_map():
return {-1: 0, 1: 1}
def get_centroids(X, Y, class_map):
num_classes = len(set(Y))
num_features = X.shape[1]
centroids = np.zeros((num_classes, num_features))
for y in set(Y):
centroids[class_map[y], :] = np.mean(X[Y == y, :], axis=0)
return centroids
def get_centroid_vec(centroids):
assert centroids.shape[0] == 2
centroid_vec = centroids[0, :] - centroids[1, :]
centroid_vec /= np.linalg.norm(centroid_vec)
centroid_vec = np.reshape(centroid_vec, (1, -1))
return centroid_vec
# Can speed this up if necessary
def get_sqrt_inv_cov(X, Y, class_map):
num_classes = len(set(Y))
num_features = X.shape[1]
sqrt_inv_covs = np.zeros((num_classes, num_features, num_features))
for y in set(Y):
cov = np.cov(X[Y == y, :], rowvar=False)
U_cov, S_cov, _ = np.linalg.svd(cov)
sqrt_inv_covs[class_map[y], ...] = U_cov.dot(np.diag(1 / np.sqrt(S_cov)).dot(U_cov.T))
return sqrt_inv_covs
# Can speed this up if necessary
def get_data_params(X, Y, percentile):
num_classes = len(set(Y))
num_features = X.shape[1]
centroids = np.zeros((num_classes, num_features))
class_map = get_class_map()
centroids = get_centroids(X, Y, class_map)
# Get radii for sphere
sphere_radii = np.zeros(2)
dists = defenses.compute_dists_under_Q(
X, Y,
Q=None,
centroids=centroids,
class_map=class_map,
norm=2)
for y in set(Y):
sphere_radii[class_map[y]] = np.percentile(dists[Y == y], percentile)
# Get vector between centroids
centroid_vec = get_centroid_vec(centroids)
# Get radii for slab
slab_radii = np.zeros(2)
for y in set(Y):
dists = np.abs(
(X[Y == y, :].dot(centroid_vec.T) - centroids[class_map[y], :].dot(centroid_vec.T)))
slab_radii[class_map[y]] = np.percentile(dists, percentile)
return class_map, centroids, centroid_vec, sphere_radii, slab_radii
def add_points(x, y, X, Y, num_copies=1):
if num_copies == 0:
return X, Y
if sparse.issparse(X):
X_modified = sparse.vstack((
X,
sparse.csr_matrix(
np.tile(x, num_copies).reshape(-1, len(x)))))
else:
X_modified = np.append(
X,
np.tile(x, num_copies).reshape(-1, len(x)),
axis=0)
Y_modified = np.append(Y, np.tile(y, num_copies))
return X_modified, Y_modified
def copy_random_points(X, Y, mask_to_choose_from=None, target_class=1, num_copies=1,
random_seed=18, replace=False):
# Only copy from points where mask_to_choose_from == True
np.random.seed(random_seed)
combined_mask = (np.array(Y, dtype=int) == target_class)
if mask_to_choose_from is not None:
combined_mask = combined_mask & mask_to_choose_from
idx_to_copy = np.random.choice(
np.where(combined_mask)[0],
size=num_copies,
replace=replace)
if sparse.issparse(X):
X_modified = sparse.vstack((X, X[idx_to_copy, :]))
else:
X_modified = np.append(X, X[idx_to_copy, :], axis=0)
Y_modified = np.append(Y, Y[idx_to_copy])
return X_modified, Y_modified
def threshold(X):
return np.clip(X, 0, np.max(X))
def rround(X, random_seed=3):
X_frac, X_int = np.modf(X)
X = X_int + (np.random.random_sample(X.shape) < X_frac)
return X
def project_onto_sphere(X, Y, radii, centroids, class_map):
for y in set(Y):
idx = class_map[y]
radius = radii[idx]
centroid = centroids[idx, :]
shifts_from_center = X[Y == y, :] - centroid
dists_from_center = np.linalg.norm(shifts_from_center, axis=1)
shifts_from_center[dists_from_center > radius, :] *= radius / np.reshape(dists_from_center[dists_from_center > radius], (-1, 1))
X[Y == y, :] = shifts_from_center + centroid
print("Number of (%s) points projected onto sphere: %s" % (y, np.sum(dists_from_center > radius)))
return X
def project_onto_slab(X, Y, v, radii, centroids, class_map):
"""
v^T x needs to be within radius of v^T centroid.
v is 1 x d and normalized.
"""
v = np.reshape(v / np.linalg.norm(v), (1, -1))
for y in set(Y):
idx = class_map[y]
radius = radii[idx]
centroid = centroids[idx, :]
# If v^T x is too large, then dists_along_v is positive
# If it's too small, then dists_along_v is negative
dists_along_v = (X[Y == y, :] - centroid).dot(v.T)
shifts_along_v = np.reshape(
dists_along_v - np.clip(dists_along_v, -radius, radius),
(1, -1))
X[Y == y, :] -= shifts_along_v.T.dot(v)
print("Number of (%s) points projected onto slab: %s" % (y, np.sum(np.abs(dists_along_v) > radius)))
return X
def get_projection_fn(
X_clean,
Y_clean,
sphere=True,
slab=True,
percentile=70):
class_map, centroids, centroid_vec, sphere_radii, slab_radii = get_data_params(X_clean, Y_clean, percentile)
if sphere and slab:
projector = upper_bounds.Projector()
def project_onto_feasible_set(X, Y):
num_examples = X.shape[0]
proj_X = np.zeros_like(X)
for idx in range(num_examples):
x = X[idx, :]
y = Y[idx]
class_idx = class_map[y]
centroid = centroids[class_idx, :]
sphere_radius = sphere_radii[class_idx]
slab_radius = slab_radii[class_idx]
proj_X[idx, :] = projector.project_onto_feasible_set(x, centroid, centroid_vec, sphere_radius, slab_radius)
return proj_X
else:
def project_onto_feasible_set(X, Y):
if sphere:
X = project_onto_sphere(X, Y, sphere_radii, centroids, class_map)
elif slab:
X = project_onto_slab(X, Y, centroid_vec, slab_radii, centroids, class_map)
return X
return project_onto_feasible_set
def filter_points_outside_feasible_set(X, Y,
centroids, centroid_vec,
sphere_radii, slab_radii,
class_map):
sphere_dists = defenses.compute_dists_under_Q(
X,
Y,
Q=None,
centroids=centroids,
class_map=class_map)
slab_dists = defenses.compute_dists_under_Q(
X,
Y,
Q=centroid_vec,
centroids=centroids,
class_map=class_map)
idx_to_keep = np.array([True] * X.shape[0])
for y in set(Y):
idx_to_keep[np.where(Y == y)[0][sphere_dists[Y == y] > sphere_radii[class_map[y]]]] = False
idx_to_keep[np.where(Y == y)[0][slab_dists[Y == y] > slab_radii[class_map[y]]]] = False
print(np.sum(idx_to_keep))
return X[idx_to_keep, :], Y[idx_to_keep]