-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdata.py
396 lines (319 loc) · 16.9 KB
/
data.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
from jax import random
import jax
import jax.numpy as np
import numpy as onp
import torchvision
import torch
import os
class MixtureGaussian:
def __init__(self, n_train, n_test, dim, classes, key):
"""
Mixture of Gaussian dataset, create classes by assigning each class a different mean and then sample from
Gaussian to create inputs, one-hot encode targets.
:param n_train: int, number of training examples
:param n_test: int, number of test examples
:param dim: int, dimensionality of inputs
:param classes: int, number of classes, i.e. cluster centers
:param key: key, used to generate randomness
"""
self.n_train = n_train
self.n_test = n_test
self.dim = dim
self.classes = classes
self.key = key
self.x_train, self.y_train, self.x_test, self.y_test = self.get_data()
def get_data(self):
n_per_class_train = (self.n_train + 1) // self.classes
n_per_class_test = (self.n_test + 1) // self.classes
# Sample random centers
key, self.key = random.split(self.key, 2)
c = random.normal(key=key, shape=(self.classes, self.dim))
key, self.key = random.split(self.key, 2)
x_trains = []
y_trains = []
x_tests = []
y_tests = []
for i in range(self.classes):
# Create training input and targets with different means
x_train = random.normal(key, shape=(n_per_class_train, self.dim)) + np.reshape(c[i, :], (1, -1))
x_trains.append(x_train)
y_trains += [i for _ in range(n_per_class_train)]
# Create test input and targets with different means
x_test = random.normal(key, shape=(n_per_class_test, self.dim)) + np.reshape(c[i, :], (1, -1))
x_tests.append(x_test)
y_tests += [i for _ in range(n_per_class_test)]
x_train = np.concatenate(x_trains, axis=0)
y_train = jax.nn.one_hot(y_trains, self.classes)
x_test = np.concatenate(x_tests, axis=0)
y_test = jax.nn.one_hot(y_tests, self.classes)
x_train = jax.numpy.float64(x_train)
x_test = jax.numpy.float64(x_test)
return x_train, y_train, x_test, y_test
def get_emp_cov(self):
"""Calculates empirical covariance and cross-covariance"""
emp_cov = 1 / self.n_train * self.x_train.T @ self.x_train
cross_cov = 1 / self.n_train * self.y_train.T @ self.x_train
return emp_cov, cross_cov
class TinyMNIST:
def __init__(self, n_train, n_test, d, classes=10, flat=True, key=None, noise_level=None):
"""
Implements data structure for MNIST, allowing to resize the images
:param n_train: int, number of training examples
:param n_test: int, number of test examples
:param dim: int, dimensionality of inputs, images are rescaled according to d
:param classes: int, number of classes, one of '2' or '10'
:param flat: bool, flatten image to vector
:param key: key, used to generate randomness
:param noise_level: float, between 0 and 1, specifying noise level in targets
"""
self.n_train = n_train
self.n_test = n_test
self.d = d
self.flat = flat
self.key = key
self.noise_level = noise_level
self.classes = classes
self.get_data()
if noise_level is not None:
random_key, self.key = random.split(self.key)
self.randomize(random_key)
def get_data(self):
# Load and store data
dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)
trainset = torchvision.datasets.MNIST(root=dir_path + '/data', train=True, download=True)
testset = torchvision.datasets.MNIST(root=dir_path + '/data', train=False, download=True)
# Resize the data through interpolation
self.x_train = torch.unsqueeze(trainset.train_data[:, :, :], 1) / 255.0
self.x_train = torch.nn.functional.interpolate(self.x_train, (self.d, self.d)).squeeze().numpy()
self.x_test = torch.unsqueeze(testset.train_data[:, :, :], 1) / 255.0
self.x_test = torch.nn.functional.interpolate(self.x_test, (self.d, self.d)).squeeze().numpy()
self.y_train = onp.expand_dims(trainset.train_labels.numpy(), axis=1)
self.y_test = onp.expand_dims(testset.train_labels.numpy(), axis=1)
if self.classes == 2:
where = self.y_train < 2
indices = np.where(where > 0)
self.x_train = self.x_train[indices[0], :, :]
self.y_train = 2 * (self.y_train[indices[0]] - 1/2)
self.x_train = self.x_train[:self.n_train, :, :]
self.y_train = self.y_train[:self.n_train]
self.x_test = self.x_test[:self.n_test, :, :]
self.y_test = self.y_test[:self.n_test]
if self.classes != 2:
self.y_train = onp.expand_dims(self.y_train[:self.n_train], axis=1)
if self.classes != 2:
# If we use more than two classes, one-hot encode instead of -1, 1 targets
self.y_train = jax.nn.one_hot(self.y_train, self.classes).squeeze()
self.y_test = jax.nn.one_hot(self.y_test, self.classes).squeeze()
if self.flat:
# Flatten the data to vectors to use fully-connected architectures
self.x_train = onp.reshape(self.x_train, (self.n_train, -1))
self.x_test = onp.reshape(self.x_test, (self.n_test, -1))
# Use float64 to guarantee accurate rank calculations
self.x_train = jax.numpy.float64(self.x_train)
self.x_test = jax.numpy.float64(self.x_test)
def add_data(self, num_samples):
"""Add num_samples to dataset"""
trainset = torchvision.datasets.MNIST(root='./data', train=True, download=False)
x_train_new = torch.unsqueeze((trainset.train_data[self.n_train:(self.n_train + num_samples), :, :] / 255.0),
dim=1)
x_train_new = torch.nn.functional.interpolate(x_train_new, (self.d, self.d)).squeeze().numpy()
y_train_new = onp.expand_dims(trainset.train_labels[self.n_train:(self.n_train + num_samples)].numpy(),
axis=1)
y_train_new = jax.nn.one_hot(y_train_new, self.classes).squeeze()
self.n_train += num_samples
if self.flat:
x_train_new = onp.reshape(x_train_new, (num_samples, -1))
x_train_new = jax.numpy.float64(x_train_new)
self.x_train = np.concatenate([self.x_train, x_train_new], axis=0)
self.y_train = np.concatenate([self.y_train, y_train_new], axis=0)
def sub_classes(self):
"""Create subclasses in case of K < 10"""
use_classes = np.array([i for i in range(self.classes)])
where = self.y_train == use_classes
where = np.sum(where, axis=1)
indices = np.where(where > 0)
self.x_train = self.x_train[indices[0], :]
self.y_train = self.y_train[indices[0], ]
self.y_train = jax.nn.one_hot(self.y_train, self.classes).squeeze()
self.n_train = self.x_train.shape[0]
def randomize(self, key):
"""Randomize a portion of the labels, according to 'noise_level'"""
key, new_key = random.split(key, 2)
indices = random.bernoulli(key, shape=(self.n_train, 1), p=self.noise_level)
noise = random.bernoulli(key, shape=(self.n_train, 1), p=0.5)
y_train_noisy = self.y_train - 2 * noise * indices * self.y_train
self.correct_indices, _ = np.where(y_train_noisy == self.y_train)
self.wrong_indices, _ = np.where(y_train_noisy != self.y_train)
self.y_train = y_train_noisy
def get_emp_cov(self):
"""Calculate empirical covariance and cross covariance"""
emp_cov = self.x_train.T @ self.x_train
cross_cov = self.y_train.T @ self.x_train
return emp_cov, cross_cov
class TinyFashionMNIST:
def __init__(self, n_train, n_test, d, classes=10, flat=True, key=None, noise_level=None):
"""
Implements data structure for FashionMNIST, allowing to resize the images
:param n_train: int, number of training examples
:param n_test: int, number of test examples
:param dim: int, dimensionality of inputs, images are rescaled according to d
:param classes: int, number of classes, one of '2' or '10'
:param flat: bool, flatten image to vector
:param key: key, used to generate randomness
:param noise_level: float, between 0 and 1, specifying noise level in targets
"""
self.n_train = n_train
self.n_test = n_test
self.d = d
self.flat = flat
self.key = key
self.noise_level = noise_level
self.classes = classes
self.get_data()
if self.classes < 10 and self.classes > 1:
self.sub_classes()
if noise_level is not None:
random_key, self.key = random.split(self.key)
self.randomize(random_key)
def get_data(self):
# Load and store data
dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)
trainset = torchvision.datasets.FashionMNIST(root=dir_path + '/data', train=True, download=True)
testset = torchvision.datasets.FashionMNIST(root=dir_path + '/data', train=False, download=True)
# Resize the data through interpolation
self.x_train = torch.unsqueeze(trainset.train_data[:, :, :], 1) / 255.0
self.x_train = torch.nn.functional.interpolate(self.x_train, (self.d, self.d)).squeeze().numpy()
self.y_train = onp.expand_dims(trainset.train_labels.numpy(), axis=1)
if self.classes == 2:
where = self.y_train < 2
indices = np.where(where > 0)
self.x_train = self.x_train[indices[0], :, :]
self.y_train = 2 * (self.y_train[indices[0]] - 1 / 2)
self.x_train = self.x_train[:self.n_train, :, :]
self.y_train = self.y_train[:self.n_train]
if self.classes != 2:
self.y_train = onp.expand_dims(self.y_train[:self.n_train], axis=1)
self.x_test = onp.expand_dims(testset.test_data[:self.n_test, :, :].numpy(), 3) / 255.0
if self.classes != 2:
# If we use more than two classes, one-hot encode instead of -1, 1 targets
self.y_train = jax.nn.one_hot(self.y_train, self.classes).squeeze()
self.y_test = onp.expand_dims(testset.test_labels[:self.n_test].numpy(), axis=1)
self.y_test = jax.nn.one_hot(self.y_test, self.classes).squeeze()
if self.flat:
# Flatten the data to vectors to use fully-connected architectures
self.x_train = onp.reshape(self.x_train, (self.n_train, -1))
self.x_test = onp.reshape(self.x_test, (self.n_test, -1))
# Use float64 to guarantee accurate rank calculations
self.x_train = jax.numpy.float64(self.x_train)
self.x_test = jax.numpy.float64(self.x_test)
def add_data(self, num_samples):
"""Add num_samples to dataset"""
trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True)
x_train_new = torch.unsqueeze((trainset.train_data[self.n_train:(self.n_train + num_samples), :, :] / 255.0),
dim=1)
x_train_new = torch.nn.functional.interpolate(x_train_new, (self.d, self.d)).squeeze().numpy()
y_train_new = onp.expand_dims(trainset.train_labels[self.n_train:(self.n_train + num_samples)].numpy(),
axis=1)
y_train_new = jax.nn.one_hot(y_train_new, self.classes).squeeze()
self.n_train += num_samples
if self.flat:
x_train_new = onp.reshape(x_train_new, (num_samples, -1))
x_train_new = jax.numpy.float64(x_train_new)
self.x_train = np.concatenate([self.x_train, x_train_new], axis=0)
self.y_train = np.concatenate([self.y_train, y_train_new], axis=0)
def sub_classes(self):
"""Create subclasses in case of K < 10"""
use_classes = np.array([i for i in range(self.classes)])
where = self.y_train == use_classes
where = np.sum(where, axis=1)
indices = np.where(where > 0)
self.x_train = self.x_train[indices[0], :]
self.y_train = self.y_train[indices[0], ]
self.y_train = jax.nn.one_hot(self.y_train, self.classes).squeeze()
self.n_train = self.x_train.shape[0]
def randomize(self, key):
"""Randomize a portion of the labels, according to 'noise_level'"""
key, new_key = random.split(key, 2)
indices = random.bernoulli(key, shape=(self.n_train, 1), p=self.noise_level)
noise = random.bernoulli(key, shape=(self.n_train, 1), p=0.5)
y_train_noisy = self.y_train - 2 * noise * indices * self.y_train
self.correct_indices, _ = np.where(y_train_noisy == self.y_train)
self.wrong_indices, _ = np.where(y_train_noisy != self.y_train)
self.y_train = y_train_noisy
def get_emp_cov(self):
emp_cov = self.x_train.T @ self.x_train
cross_cov = self.y_train.T @ self.x_train
return emp_cov, cross_cov
class TinyCIFAR10:
def __init__(self, n_train, n_test, d, classes=10, flat=True, key=None):
"""
Implements data structure for CIFAR10, allowing to resize the images
:param n_train: int, number of training examples
:param n_test: int, number of test examples
:param dim: int, dimensionality of inputs, images are rescaled according to d
:param classes: int, number of classes, one of '2' or '10'
:param flat: bool, flatten image to vector
:param key: key, used to generate randomness
"""
self.n_train = n_train
self.n_test = n_test
self.flat = flat
self.d = d
self.key = key
self.classes = classes
self.get_data()
def get_data(self):
# Load and store data
dir_path = os.path.dirname(os.path.realpath(__file__))
trainset = torchvision.datasets.CIFAR10(root=dir_path + '/data', train=True, download=True)
testset = torchvision.datasets.CIFAR10(root=dir_path + '/data', train=False, download=True)
# Resize the data through interpolation
self.x_train = torch.tensor(trainset.data[:, :, :]) / 255.0
self.x_train = torch.transpose(self.x_train, 1, 3)
self.x_train = torch.nn.functional.interpolate(self.x_train, (self.d, self.d)).squeeze().numpy()
self.y_train = onp.expand_dims(trainset.targets, axis=1)
if self.classes == 2:
where = self.y_train < 2
indices = np.where(where > 0)
self.x_train = self.x_train[indices[0], :, :]
self.y_train = 2 * (self.y_train[indices[0]] - 1 / 2)
self.x_train = self.x_train[:self.n_train, :, :]
self.y_train = self.y_train[:self.n_train]
if self.classes != 2:
self.y_train = onp.expand_dims(self.y_train[:self.n_train], axis=1)
self.x_test = onp.expand_dims(testset.data[:self.n_test, :, :], 3) / 255.0
if self.classes != 2:
# If we use more than two classes, one-hot encode instead of -1, 1 targets
self.y_train = jax.nn.one_hot(self.y_train, self.classes).squeeze()
if self.flat:
# Flatten the data to vectors to use fully-connected architectures
self.x_train = self.x_train[:, 0, :, :]
self.x_train = onp.reshape(self.x_train, (self.n_train, -1))
self.x_test = self.x_test[:, 0, :, :]
self.x_test = onp.reshape(self.x_test, (self.n_test, -1))
# Use float64 to guarantee accurate rank calculations
self.x_train = jax.numpy.float64(self.x_train)
self.x_test = jax.numpy.float64(self.x_test)
def get_emp_cov(self):
"""Calculate empirical covariance and cross covariance"""
emp_cov = self.x_train.T @ self.x_train
cross_cov = self.y_train.T @ self.x_train
return emp_cov, cross_cov
def get_dataset(name, n_train, n_test, dim, classes):
"""
Helper function to load the desired dataset.
:param dataset: str, one 'MNIST', 'CIFAR', 'FashionMNIST'
:param n_train: int, number of training examples
:param n_test: int, number of test examples
:param dim: int, desired input dimensionality achieved through rescaling
:param classes: int, desired number of classes, one of '2' or '10'
:return: data, object of class dataset
"""
if name == 'MNIST':
return TinyMNIST(n_train=n_train, n_test=n_test, d=int(np.sqrt(dim)), classes=classes)
if name == 'CIFAR':
return TinyCIFAR10(n_train=n_train, n_test=n_test, d=int(np.sqrt(dim)), classes=classes)
if name == 'FashionMNIST':
return TinyFashionMNIST(n_train=n_train, n_test=n_test, d=int(np.sqrt(dim)), classes=classes)